From 31733ce425b095fe833676666831a18d5e43e209 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 10 Jan 2026 01:36:12 -0500 Subject: [PATCH] bin: update for repline 0.0.12 --- Cargo.lock | 8 +-- src/bin/doughlang.rs | 135 ++++++++++++++++++++++++++----------------- 2 files changed, 85 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 392f5c6..34d94a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,9 +62,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.179" +version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5a2d376baa530d1238d133232d15e239abad80d05838b4b59354e5268af431f" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" [[package]] name = "linux-raw-sys" @@ -121,9 +121,9 @@ dependencies = [ [[package]] name = "repline" -version = "0.0.10" +version = "0.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e60dec4cc94ec6dd0cdb58428304d471cf50588357e20f30f449c2c1a8504ee" +checksum = "6fc8bb58aab0cf230c95d2649cd54d23b5520e44abdc3f1d420ae844a619d9ef" dependencies = [ "crossterm", ] diff --git a/src/bin/doughlang.rs b/src/bin/doughlang.rs index 40503bb..1c4559c 100644 --- a/src/bin/doughlang.rs +++ b/src/bin/doughlang.rs @@ -22,65 +22,14 @@ fn clear() { print!("\x1b[H\x1b[2J\x1b[3J"); } -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -enum Verbosity { - #[default] - Pretty, - Debug, - Frob, - Quiet, -} -impl From<&str> for Verbosity { - fn from(value: &str) -> Self { - match value { - "quiet" | "false" | "0" | "no" => Verbosity::Quiet, - "debug" | "d" => Verbosity::Debug, - "frob" => Verbosity::Frob, - "pretty" => Verbosity::Pretty, - _ => Default::default(), - } - } -} - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -enum ParseMode { - #[default] - Expr, - Pat, - Bind, - Use, - Tokens, -} -impl From<&str> for ParseMode { - fn from(value: &str) -> Self { - match value { - "expr" => Self::Expr, - "pat" => Self::Pat, - "bind" => Self::Bind, - "use" => Self::Use, - "tokens" => Self::Tokens, - _ => Default::default(), - } - } -} -impl ParseMode { - fn with<'a>(&self) -> fn(&'a str, Verbosity) { - match self { - Self::Expr => parse::<'a, Expr>, - Self::Pat => parse::<'a, Pat>, - Self::Bind => parse::<'a, Bind>, - Self::Use => parse::<'a, Use>, - Self::Tokens => tokens::<'a, dyn Parse<'a, Prec = ()>>, - } - } -} - fn main() -> Result<(), Box> { let mut verbose = Verbosity::from(std::env::var("DO_VERBOSE").as_deref().unwrap_or_default()); let mut parsing = ParseMode::from(std::env::var("DO_PARSING").as_deref().unwrap_or_default()); + let color = parsing.color(); + let begin = verbose.begin(); if stdin().is_terminal() { - read_and("\x1b[32m", ".>", " >", |line| match line.trim_end() { + read_and_mut(color, begin, " > ", |rl, line| match line.trim_end() { "" => Ok(Response::Continue), "exit" => Ok(Response::Break), "help" => { @@ -101,11 +50,13 @@ fn main() -> Result<(), Box> { line @ ("tokens" | "expr" | "pat" | "bind" | "use") => { parsing = ParseMode::from(line); println!("Parse mode set to '{parsing:?}'"); + rl.set_color(parsing.color()); Ok(Response::Accept) } line @ ("quiet" | "debug" | "frob" | "pretty") => { verbose = Verbosity::from(line); println!("Verbosity set to '{verbose:?}'"); + rl.set_begin(verbose.begin()); Ok(Response::Accept) } _ if line.ends_with("\n\n") => { @@ -241,3 +192,79 @@ fn parse<'t, T: Parse<'t> + Annotation + for<'a> Walk<'a, DefaultTypes>>( } } } + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +enum Verbosity { + #[default] + Pretty, + Debug, + Frob, + Quiet, +} + +impl From<&str> for Verbosity { + fn from(value: &str) -> Self { + match value { + "quiet" | "false" | "0" | "no" => Verbosity::Quiet, + "debug" | "d" => Verbosity::Debug, + "frob" => Verbosity::Frob, + "pretty" => Verbosity::Pretty, + _ => Default::default(), + } + } +} + +impl Verbosity { + fn begin(self) -> &'static str { + match self { + Self::Pretty => ".> ", + Self::Debug => "?> ", + Self::Frob => "#> ", + Self::Quiet => "_> ", + } + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +enum ParseMode { + #[default] + Expr, + Pat, + Bind, + Use, + Tokens, +} + +impl From<&str> for ParseMode { + fn from(value: &str) -> Self { + match value { + "expr" => Self::Expr, + "pat" => Self::Pat, + "bind" => Self::Bind, + "use" => Self::Use, + "tokens" => Self::Tokens, + _ => Default::default(), + } + } +} +impl ParseMode { + fn with<'a>(&self) -> fn(&'a str, Verbosity) { + match self { + Self::Expr => parse::<'a, Expr>, + Self::Pat => parse::<'a, Pat>, + Self::Bind => parse::<'a, Bind>, + Self::Use => parse::<'a, Use>, + Self::Tokens => tokens::<'a, dyn Parse<'a, Prec = ()>>, + } + } + + fn color(&self) -> &'static str { + match self { + Self::Expr => "\x1b[36m", + Self::Pat => "\x1b[35m", + Self::Bind => "\x1b[34m", + Self::Use => "\x1b[33m", + Self::Tokens => "\x1b[32m", + } + } +}