Add support for writing arbitrary bytes to stdout/stderr

This commit is contained in:
Filip Tibell 2023-05-19 13:49:54 +02:00
parent 2931aa690c
commit 2b94dbabe0
No known key found for this signature in database

View file

@ -1,7 +1,7 @@
use blocking::unblock; use blocking::unblock;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select}; use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
use mlua::prelude::*; use mlua::prelude::*;
use std::io::Write; use tokio::io::{self, AsyncWriteExt};
use crate::lua::{ use crate::lua::{
stdio::{ stdio::{
@ -26,14 +26,16 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.with_function("format", |_, args: LuaMultiValue| { .with_function("format", |_, args: LuaMultiValue| {
pretty_format_multi_value(&args) pretty_format_multi_value(&args)
})? })?
.with_function("write", |_, s: String| { .with_async_function("write", |_, s: LuaString| async move {
print!("{s}"); let mut stdout = io::stdout();
std::io::stdout().flush().expect("Could not flush stdout"); stdout.write_all(s.as_bytes()).await?;
stdout.flush().await?;
Ok(()) Ok(())
})? })?
.with_function("ewrite", |_, s: String| { .with_async_function("ewrite", |_, s: LuaString| async move {
eprint!("{s}"); let mut stderr = io::stderr();
std::io::stderr().flush().expect("Could not flush stderr"); stderr.write_all(s.as_bytes()).await?;
stderr.flush().await?;
Ok(()) Ok(())
})? })?
.with_async_function("prompt", |_, options: PromptOptions| { .with_async_function("prompt", |_, options: PromptOptions| {