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..fc23a01 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::Plain, 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..2ce671c --- /dev/null +++ b/src/env.rs @@ -0,0 +1,53 @@ +use std::path::PathBuf; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum MsgKind { + Info, + Progress1, + Progress2, + Warning, + Success, + Plain, +} + +impl MsgKind { + pub const fn emoji(self) -> &'static str { + match self { + Self::Info => "ℹ️", + Self::Progress1 => "⏳️", + Self::Progress2 => "⌛", + Self::Warning => "⚠️", + Self::Success => "✅", + Self::Plain => "", + } + } +} + +pub trait Env { + fn emit_msg(&mut self, kind: MsgKind, msg: &str); + fn vfs_path(&mut self) -> PathBuf; +} + +pub struct StdEnv { + pub vfs: PathBuf, +} + +impl StdEnv { + pub const fn new(vfs: PathBuf) -> Self { + Self { vfs } + } +} + +impl Env for StdEnv { + fn emit_msg(&mut self, kind: MsgKind, msg: &str) { + if kind == MsgKind::Plain { + println!("{msg}"); + } else { + println!("{} {msg}", kind.emoji()); + } + } + + 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);