From e1a15cae9afd5eba73ce5116833b4adc65d10547 Mon Sep 17 00:00:00 2001 From: gram Date: Fri, 16 Jan 2026 19:25:04 +0100 Subject: [PATCH 1/2] env --- src/cli.rs | 9 +++---- src/commands/repl.rs | 6 ++--- src/commands/vfs.rs | 15 ++++++++---- src/env.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 ++++++---- 5 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 src/env.rs diff --git a/src/cli.rs b/src/cli.rs index d587707..5a589bb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,10 +1,11 @@ use crate::args::*; use crate::commands::*; +use crate::env::Env; use std::fmt::Display; -use std::path::PathBuf; -pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> { +pub fn run_command(env: &mut E, command: &Commands) -> anyhow::Result<()> { use Commands::*; + let vfs = env.vfs_path(); match command { Postinstall => cmd_postinstall(&vfs), Build(args) => cmd_build(vfs, args), @@ -16,7 +17,7 @@ pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> { Badges(args) => cmd_badges(&vfs, args), Boards(args) => cmd_boards(&vfs, args), Inspect(args) => cmd_inspect(&vfs, args), - Repl(args) => cmd_repl(&vfs, args), + Repl(args) => cmd_repl(env, args), Shots(ShotsCommands::Download(args)) => cmd_shots_download(&vfs, args), Catalog(command) => match command { CatalogCommands::List(args) => cmd_catalog_list(args), @@ -37,7 +38,7 @@ pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> { RuntimeCommands::Monitor => cmd_monitor(root_args), RuntimeCommands::Logs => cmd_logs(root_args), }, - Vfs => cmd_vfs(), + Vfs => cmd_vfs(env), } } diff --git a/src/commands/repl.rs b/src/commands/repl.rs index 47d5369..7c6c448 100644 --- a/src/commands/repl.rs +++ b/src/commands/repl.rs @@ -1,5 +1,6 @@ use crate::args::{Cli, ReplArgs}; use crate::cli::{Error, run_command}; +use crate::env::Env; use crate::repl_helper::Helper; use anyhow::Result; use clap::Parser; @@ -7,10 +8,9 @@ use crossterm::style::Stylize; use rustyline::Editor; use rustyline::error::ReadlineError; use rustyline::history::FileHistory; -use std::path::Path; #[expect(clippy::unnecessary_wraps)] -pub fn cmd_repl(vfs: &Path, _args: &ReplArgs) -> Result<()> { +pub fn cmd_repl(env: &mut E, _args: &ReplArgs) -> Result<()> { let mut rl: Editor = Editor::new().unwrap(); rl.set_helper(Some(Helper::new())); // if rl.load_history(".history.txt").is_err() { @@ -33,7 +33,7 @@ pub fn cmd_repl(vfs: &Path, _args: &ReplArgs) -> Result<()> { } }; _ = rl.add_history_entry(&input); - let res = run_command(vfs.to_owned(), &cli.command); + let res = run_command(env, &cli.command); if let Err(err) = res { eprintln!("{} {}", "đŸ’Ĩ Error:".red(), Error(err)); was_ok = false; diff --git a/src/commands/vfs.rs b/src/commands/vfs.rs index d73b05d..13e42f0 100644 --- a/src/commands/vfs.rs +++ b/src/commands/vfs.rs @@ -1,19 +1,24 @@ -use crate::vfs::get_vfs_path; +use crate::env::{Env, MsgKind}; #[expect(clippy::unnecessary_wraps)] -pub fn cmd_vfs() -> anyhow::Result<()> { - let path = get_vfs_path(); +pub fn cmd_vfs(env: &mut E) -> anyhow::Result<()> { + let path = env.vfs_path(); let path = path.to_str().unwrap(); - println!("{path}"); + env.emit_msg(MsgKind::Success, path); Ok(()) } #[cfg(test)] mod tests { + use std::path::PathBuf; + use super::*; + use crate::env::StdEnv; #[test] fn test_smoke_cmd_vfs() { - cmd_vfs().unwrap(); + let vfs = PathBuf::new().join("hello"); + let mut env = StdEnv::new(vfs); + cmd_vfs(&mut env).unwrap(); } } diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..4f49ea8 --- /dev/null +++ b/src/env.rs @@ -0,0 +1,56 @@ +use std::path::PathBuf; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum MsgKind { + Info, + Progress1, + Progress2, + Warning, + Success, +} + +impl MsgKind { + pub const fn emoji(self) -> &'static str { + match self { + Self::Info => "â„šī¸", + Self::Progress1 => "âŗī¸", + Self::Progress2 => "⌛", + Self::Warning => "âš ī¸", + Self::Success => "✅", + } + } +} + +pub trait Env { + fn emit_msg(&mut self, kind: MsgKind, msg: &str); + fn vfs_path(&mut self) -> PathBuf; +} + +pub struct StdEnv { + pub vfs: PathBuf, + had_msg: bool, +} + +impl StdEnv { + pub const fn new(vfs: PathBuf) -> Self { + Self { + vfs, + had_msg: false, + } + } +} + +impl Env for StdEnv { + fn emit_msg(&mut self, kind: MsgKind, msg: &str) { + if kind == MsgKind::Success && !self.had_msg { + println!("{msg}"); + self.had_msg = true; + } + println!("{} {msg}", kind.emoji()); + self.had_msg = true; + } + + fn vfs_path(&mut self) -> PathBuf { + self.vfs.clone() + } +} diff --git a/src/main.rs b/src/main.rs index 1fd5971..420e1bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,21 +21,22 @@ mod cli; mod commands; mod config; mod crypto; +mod env; mod file_names; mod fs; mod images; mod langs; mod net; -mod repl_helper; -mod vfs; -mod wasm; - mod palettes; +mod repl_helper; #[cfg(test)] mod test_helpers; +mod vfs; +mod wasm; use crate::args::Cli; use crate::cli::{Error, run_command}; +use crate::env::StdEnv; use crate::vfs::get_vfs_path; use clap::Parser; use crossterm::style::Stylize; @@ -46,7 +47,8 @@ fn main() { Some(vfs) => vfs, None => get_vfs_path(), }; - let res = run_command(vfs, &cli.command); + let mut env = StdEnv::new(vfs); + let res = run_command(&mut env, &cli.command); if let Err(err) = res { eprintln!("{} {}", "đŸ’Ĩ Error:".red(), Error(err)); std::process::exit(1); From 8e250afdfa0740e3fa4035d79bd0d9983c6c8600 Mon Sep 17 00:00:00 2001 From: gram Date: Sat, 17 Jan 2026 08:33:54 +0100 Subject: [PATCH 2/2] simplify --- src/commands/vfs.rs | 2 +- src/env.rs | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/commands/vfs.rs b/src/commands/vfs.rs index 13e42f0..fc23a01 100644 --- a/src/commands/vfs.rs +++ b/src/commands/vfs.rs @@ -4,7 +4,7 @@ use crate::env::{Env, MsgKind}; pub fn cmd_vfs(env: &mut E) -> anyhow::Result<()> { let path = env.vfs_path(); let path = path.to_str().unwrap(); - env.emit_msg(MsgKind::Success, path); + env.emit_msg(MsgKind::Plain, path); Ok(()) } diff --git a/src/env.rs b/src/env.rs index 4f49ea8..2ce671c 100644 --- a/src/env.rs +++ b/src/env.rs @@ -7,6 +7,7 @@ pub enum MsgKind { Progress2, Warning, Success, + Plain, } impl MsgKind { @@ -17,6 +18,7 @@ impl MsgKind { Self::Progress2 => "⌛", Self::Warning => "âš ī¸", Self::Success => "✅", + Self::Plain => "", } } } @@ -28,26 +30,21 @@ pub trait Env { pub struct StdEnv { pub vfs: PathBuf, - had_msg: bool, } impl StdEnv { pub const fn new(vfs: PathBuf) -> Self { - Self { - vfs, - had_msg: false, - } + Self { vfs } } } impl Env for StdEnv { fn emit_msg(&mut self, kind: MsgKind, msg: &str) { - if kind == MsgKind::Success && !self.had_msg { + if kind == MsgKind::Plain { println!("{msg}"); - self.had_msg = true; + } else { + println!("{} {msg}", kind.emoji()); } - println!("{} {msg}", kind.emoji()); - self.had_msg = true; } fn vfs_path(&mut self) -> PathBuf {