diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-07-30 20:14:42 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-07-30 20:14:42 +0200 |
commit | 2ebe8c10922c440b803016eaca3ea2dc209a7209 (patch) | |
tree | 826bc3363936e8f2220867313fe3260c0293477a | |
parent | 8a82d098e357ef29875fb5b37960cab46e72745f (diff) |
Reformat.
-rw-r--r-- | src/main.rs | 141 |
1 files changed, 100 insertions, 41 deletions
diff --git a/src/main.rs b/src/main.rs index 77ad9a5..c7dd48e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,14 @@ -use std::collections::{LinkedList, VecDeque}; use crossterm::{cursor, execute, terminal}; -use eyre::{bail, Result}; +use eyre::{Result, bail}; use regex::Regex; use rustyline::DefaultEditor; +use rustyline::error::ReadlineError; +use std::collections::{LinkedList, VecDeque}; use std::io; use std::io::Write; use std::path::{Component, Path, PathBuf}; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; +use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; use std::thread::spawn; -use rustyline::error::ReadlineError; const STDOUT_BUFFER_SIZE: usize = 1024; const STDERR_BUFFER_SIZE: usize = 1024; @@ -27,7 +27,7 @@ fn main() -> Result<()> { let line = match line_editor.readline(&interpolated_prompt) { Ok(line) => line, Err(ReadlineError::Eof) => break, - Err(err) => return Err(err.into()) + Err(err) => return Err(err.into()), }; let Ok(command) = Command::parse(&line) else { @@ -36,7 +36,12 @@ fn main() -> Result<()> { }; line_editor.add_history_entry(&line)?; - let Ok(CommandReceivers { stdout, stderr, exit_status }) = command.run() else { + let Ok(CommandReceivers { + stdout, + stderr, + exit_status, + }) = command.run() + else { eprintln!("unimplemented command: {}", line); continue; }; @@ -48,7 +53,8 @@ fn main() -> Result<()> { break; }; let mut out = io::stderr(); - out.write_all(bytes.as_slice()).expect("stdout write failed"); + out.write_all(bytes.as_slice()) + .expect("stdout write failed"); out.flush().expect("stdout flush failed"); } }); @@ -59,7 +65,8 @@ fn main() -> Result<()> { break; }; let mut out = io::stderr(); - out.write_all(bytes.as_slice()).expect("stderr write failed"); + out.write_all(bytes.as_slice()) + .expect("stderr write failed"); out.flush().expect("stdout flush failed"); } }); @@ -67,7 +74,9 @@ fn main() -> Result<()> { stdout_writer.join().expect("stdout writer thread failed"); stderr_writer.join().expect("stderr writer thread failed"); - let _exit_status = exit_status.recv().expect("failed to receive exit status from command"); + let _exit_status = exit_status + .recv() + .expect("failed to receive exit status from command"); } Ok(()) @@ -150,9 +159,18 @@ enum FileSpec { #[derive(Debug)] enum Command { - Pipe { left: Box<Command>, right: Box<Command> }, - Redirect { command: Box<Command>, target: FileSpec }, - External { program: String, args: Vec<String> }, + Pipe { + left: Box<Command>, + right: Box<Command>, + }, + Redirect { + command: Box<Command>, + target: FileSpec, + }, + External { + program: String, + args: Vec<String>, + }, Builtin(BuiltinCommand), Empty, } @@ -190,7 +208,7 @@ impl CommandContext { stdout: rout, stderr: rerr, exit_status: rexit, - } + }, } } @@ -201,11 +219,15 @@ impl CommandContext { impl Command { pub(crate) fn run(&self) -> Result<CommandReceivers> { - use crate::Command::*; use crate::BuiltinCommand::*; + use crate::Command::*; let (senders, receivers) = CommandContext::new().split(); - let CommandSenders { stdout, stderr, exit_status } = senders; + let CommandSenders { + stdout, + stderr, + exit_status, + } = senders; match self { Empty => { @@ -235,35 +257,70 @@ impl Command { #[derive(Debug)] enum BuiltinCommand { // File-oriented - Copy { from: FileSpec, to: FileSpec }, - Deltree { path: PathBuf }, - Dir { path: PathBuf }, + Copy { + from: FileSpec, + to: FileSpec, + }, + Deltree { + path: PathBuf, + }, + Dir { + path: PathBuf, + }, Fc, Find, - Mkdir { path: PathBuf }, + Mkdir { + path: PathBuf, + }, Move, - Remove { path: PathBuf }, - Rename { from: FileSpec, to: FileSpec }, + Remove { + path: PathBuf, + }, + Rename { + from: FileSpec, + to: FileSpec, + }, Replace, - Rmdir { path: PathBuf }, + Rmdir { + path: PathBuf, + }, Sort, - Tree { path: PathBuf }, - Type { file: FileSpec }, - Xcopy { from: FileSpec, to: FileSpec, recursive: bool }, + Tree { + path: PathBuf, + }, + Type { + file: FileSpec, + }, + Xcopy { + from: FileSpec, + to: FileSpec, + recursive: bool, + }, // Shell-oriented Append, - Chdir { path: PathBuf }, + Chdir { + path: PathBuf, + }, EchoOff, EchoOn, EchoPlain, - EchoText { message: String }, + EchoText { + message: String, + }, Exit, PathGet, - PathSet { value: String }, + PathSet { + value: String, + }, PromptGet, - PromptSet { message: String }, - Set { name: String, value: String }, + PromptSet { + message: String, + }, + Set { + name: String, + value: String, + }, Setver, Ver, @@ -333,14 +390,16 @@ enum BuiltinCommand { If, Pause, Prompt, - Rem { message: String }, + Rem { + message: String, + }, Shift, } impl Command { fn parse(input: &str) -> Result<Command> { - use Command::*; use BuiltinCommand::*; + use Command::*; let whitespace = Regex::new(r"\s+")?; let mut split_input = whitespace.splitn(input, 2); @@ -352,18 +411,18 @@ impl Command { match name.to_uppercase().as_str() { "" => Ok(Empty), - "ECHO" => - Ok(Builtin(match args.to_uppercase().as_str() { - "ON" => EchoOn, - "OFF" => EchoOff, - "" => EchoPlain, - _ => EchoText { message: args.to_string() }, - })), + "ECHO" => Ok(Builtin(match args.to_uppercase().as_str() { + "ON" => EchoOn, + "OFF" => EchoOff, + "" => EchoPlain, + _ => EchoText { + message: args.to_string(), + }, + })), "CLS" => Ok(Builtin(Cls)), - _ => - Err(eyre::eyre!("parse not implemented for {:?}", input)) + _ => Err(eyre::eyre!("parse not implemented for {:?}", input)), } } } |