Skip to content

Commit

Permalink
Fix print and warn globals yielding
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Dec 30, 2023
1 parent 0e54d7f commit cd78fea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed the `print` and `warn` global functions yielding the thread, preventing them from being used in places such as the callback to `table.sort`.
- Fixed the `overwrite` option for `fs.move` not correctly removing existing files / directories. ([#133])

[#133]: https://github.com/filiptibell/lune/pull/133
Expand Down
23 changes: 16 additions & 7 deletions src/lune/globals/print.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt};
use tokio::{
io::{self, AsyncWriteExt},
task,
};

use crate::lune::{scheduler::LuaSchedulerExt, util::formatting::pretty_format_multi_value};
use crate::lune::util::formatting::pretty_format_multi_value;

pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_async_function(|_, args: LuaMultiValue| async move {
pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_function(|_, args: LuaMultiValue| {
let formatted = format!("{}\n", pretty_format_multi_value(&args)?);
let mut stdout = io::stdout();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
task::spawn(async move {
let _res = async move {
let mut stdout = io::stdout();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
Ok::<_, LuaError>(())
};
// FUTURE: Send any error back to scheduler and emit it properly
});
Ok(())
})
}
26 changes: 16 additions & 10 deletions src/lune/globals/warn.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt};

use crate::lune::{
scheduler::LuaSchedulerExt,
util::formatting::{format_label, pretty_format_multi_value},
use tokio::{
io::{self, AsyncWriteExt},
task,
};

pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_async_function(|_, args: LuaMultiValue| async move {
use crate::lune::util::formatting::{format_label, pretty_format_multi_value};

pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_function(|_, args: LuaMultiValue| {
let formatted = format!(
"{}\n{}\n",
format_label("warn"),
pretty_format_multi_value(&args)?
);
let mut stdout = io::stderr();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
task::spawn(async move {
let _res = async move {
let mut stdout = io::stderr();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
Ok::<_, LuaError>(())
};
// FUTURE: Send any error back to scheduler and emit it properly
});
Ok(())
})
}

0 comments on commit cd78fea

Please sign in to comment.