manen

Fancy Lua REPL
Log | Files | Refs | README | LICENSE

commit 822b0447811447928a985d4026454b26f800b2a8
parent c4f012b2dcd46d330c65e5552a857098c14af29a
Author: Sylvia Ivory <git@sivory.net>
Date:   Mon, 23 Jun 2025 16:53:26 -0700

Remove tokio dependency

Diffstat:
MCargo.lock | 75+++++++++++++++++++++++++++++----------------------------------------------
MCargo.toml | 2+-
Msrc/editor.rs | 36++++++++++++------------------------
Msrc/main.rs | 17+++++++----------
4 files changed, 49 insertions(+), 81 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -144,12 +144,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db76d6187cd04dff33004d8e6c9cc4e05cd330500379d2394209271b4aeee" [[package]] -name = "bytes" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" - -[[package]] name = "cc" version = "1.2.27" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -165,6 +159,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" [[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] name = "chrono" version = "0.4.41" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -294,6 +294,16 @@ dependencies = [ ] [[package]] +name = "ctrlc" +version = "3.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f93780a459b7d656ef7f071fe699c4d3d2cb201c4b24d085b6ddc505276e73" +dependencies = [ + "nix", + "windows-sys 0.59.0", +] + +[[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -542,11 +552,11 @@ dependencies = [ "clap", "color-eyre", "comfy-table", + "ctrlc", "lazy_static", "mlua", "nu-ansi-term", "reedline", - "tokio", "tree-sitter-highlight", "tree-sitter-lua", ] @@ -611,6 +621,18 @@ dependencies = [ ] [[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] name = "nu-ansi-term" version = "0.50.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -919,16 +941,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] -name = "socket2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] name = "streaming-iterator" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1009,35 +1021,6 @@ dependencies = [ ] [[package]] -name = "tokio" -version = "1.45.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "parking_lot", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", - "windows-sys 0.52.0", -] - -[[package]] -name = "tokio-macros" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "tracing" version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -21,10 +21,10 @@ aho-corasick = "1.1.3" clap = { version = "4.5.40", features = ["derive"] } color-eyre = "0.6.5" comfy-table = "7.1.4" +ctrlc = "3.4.7" lazy_static = "1.5.0" mlua = { version = "0.10.5", features = ["anyhow", "send", "async"] } nu-ansi-term = "0.50.1" reedline = "0.40.0" -tokio = { version = "1.45.1", features = ["full"] } tree-sitter-highlight = "0.25.6" tree-sitter-lua = "0.2.0" diff --git a/src/editor.rs b/src/editor.rs @@ -11,7 +11,6 @@ use reedline::{ DefaultPrompt, DefaultPromptSegment, EditCommand, Emacs, KeyCode, KeyModifiers, Reedline, ReedlineEvent, Signal, default_emacs_keybindings, }; -use tokio::{signal, task}; use crate::{ format::TableFormat, highlight::LuaHighlighter, inspect::display_basic, validator::LuaValidator, @@ -75,20 +74,17 @@ impl Editor { fn register_ctrl_c(&self, is_running_lua: Arc<AtomicBool>) { let inner_cancel = self.cancel_lua.clone(); - tokio::spawn(async move { - loop { - signal::ctrl_c().await.unwrap(); - - if is_running_lua.load(Ordering::Relaxed) { - inner_cancel.store(true, Ordering::Relaxed); - } else { - process::exit(0) - } + ctrlc::set_handler(move || { + if is_running_lua.load(Ordering::Relaxed) { + inner_cancel.store(true, Ordering::Relaxed); + } else { + process::exit(0) } - }); + }) + .unwrap(); } - pub async fn run(mut self) { + pub fn run(mut self) { let is_running_lua = Arc::new(AtomicBool::new(false)); self.register_ctrl_c(is_running_lua.clone()); @@ -108,15 +104,7 @@ impl Editor { is_running_lua.store(true, Ordering::Relaxed); - let line = line.clone(); - let lua = self.lua.clone(); - let table_format = self.table_format; - - if let Err(e) = - task::spawn_blocking(move || Self::eval(lua, table_format, &line)) - .await - .unwrap() - { + if let Err(e) = self.eval(&line) { eprintln!("{e}") } @@ -167,11 +155,11 @@ impl Editor { Ok(()) } - fn eval(lua: Lua, table_format: TableFormat, line: &str) -> LuaResult<()> { - let value: LuaValue = lua.load(line).set_name("=stdin").eval()?; + fn eval(&self, line: &str) -> LuaResult<()> { + let value: LuaValue = self.lua.load(line).set_name("=stdin").eval()?; let stringify = match value { - LuaValue::Table(tbl) => table_format.format(&tbl, true)?, + LuaValue::Table(tbl) => self.table_format.format(&tbl, true)?, value => display_basic(&value, true), }; diff --git a/src/main.rs b/src/main.rs @@ -1,4 +1,6 @@ use std::{ + fs, + io::{Read, stdin}, path::{Path, PathBuf}, process, }; @@ -8,10 +10,6 @@ use editor::Editor; use highlight::LuaHighlighter; use mlua::prelude::*; use reedline::Highlighter; -use tokio::{ - fs, - io::{AsyncReadExt, stdin}, -}; use crate::{format::comfy_table, inspect::inspect}; @@ -86,23 +84,22 @@ fn eval_lua(file: String, path: &Path) -> LuaResult<()> { } } -#[tokio::main] -async fn main() -> color_eyre::Result<()> { +fn main() -> color_eyre::Result<()> { color_eyre::install()?; let cli = Cli::parse(); match &cli.command { - None | Some(Command::Repl) => Editor::new()?.run().await, + None | Some(Command::Repl) => Editor::new()?.run(), Some(Command::Run { path }) => { - eval_lua(fs::read_to_string(path).await?, path)?; + eval_lua(fs::read_to_string(path)?, path)?; } Some(Command::Highlight { path }) => { let file = if let Some(path) = path { - fs::read_to_string(path).await? + fs::read_to_string(path)? } else { let mut buffer = String::new(); - stdin().read_to_string(&mut buffer).await?; + stdin().read_to_string(&mut buffer)?; buffer };