Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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<E: Env>(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),
Expand All @@ -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),
Expand All @@ -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),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/repl.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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;
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<E: Env>(env: &mut E, _args: &ReplArgs) -> Result<()> {
let mut rl: Editor<Helper, FileHistory> = Editor::new().unwrap();
rl.set_helper(Some(Helper::new()));
// if rl.load_history(".history.txt").is_err() {
Expand All @@ -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;
Expand Down
15 changes: 10 additions & 5 deletions src/commands/vfs.rs
Original file line number Diff line number Diff line change
@@ -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<E: Env>(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();
}
}
53 changes: 53 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down