From 536086c4a00eeb1ee81a83f4a369e8631ffb2814 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 17 Dec 2022 11:29:34 -0500 Subject: [PATCH] Remove generic commands --- src/main.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba922e2..97e7da6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,13 @@ mod command; use ansi4tui::bytes_to_text; +use anyhow::anyhow; use anyhow::bail; use anyhow::Result; use command::CommandResult; use crossterm::event::DisableBracketedPaste; use crossterm::event::EnableBracketedPaste; +use std::str::FromStr; use std::{ io::{self, Write}, process::{Command, Stdio}, @@ -67,7 +69,7 @@ impl App { let wordsplit = true; match template { - Template::Awk | Template::Generic(_) => Self { + Template::Awk => Self { cmdline: String::new(), cmdline_position, text_orig, @@ -132,7 +134,6 @@ impl App { } pub enum Template { - Generic(String), Sh(String), Jq, Grep, @@ -142,25 +143,26 @@ pub enum Template { Perl, } -impl Template { - #[must_use] - pub fn from(s: &str) -> Self { +impl FromStr for Template { + type Err = anyhow::Error; + fn from_str(s: &str) -> Result { match s.to_lowercase().trim() { - "jq" => Self::Jq, - "grep" => Self::Grep, - "rg" => Self::Rg, - "sed" => Self::Sed, - "awk" => Self::Awk, - "perl" => Self::Perl, - s @ "sh" | s @ "bash" | s @ "zsh" | s @ "dash" => Self::Sh(s.to_string()), - c => Self::Generic(c.to_string()), + "jq" => Ok(Self::Jq), + "grep" => Ok(Self::Grep), + "rg" => Ok(Self::Rg), + "sed" => Ok(Self::Sed), + "awk" => Ok(Self::Awk), + "perl" => Ok(Self::Perl), + s @ "sh" | s @ "bash" | s @ "zsh" | s @ "dash" => Ok(Self::Sh(s.to_string())), + e => Err(anyhow!("{e} is not a supported command")), } } +} +impl Template { #[must_use] pub fn command(&self) -> String { match self { - Self::Generic(c) => c.to_string(), Self::Sh(s) => s.to_string(), Self::Jq => String::from("jq"), Self::Grep => String::from("grep"), @@ -184,6 +186,11 @@ fn main() -> Result<()> { None => bail!("You must pass a command"), }; + // Slurp all input + let text_orig = io::read_to_string(io::stdin())?; + + // Run the actual application + let app = App::from_template(text_orig, &Template::from_str(&arg)?); enable_raw_mode()?; let mut stdout = io::stdout(); // execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; @@ -196,11 +203,6 @@ fn main() -> Result<()> { let backend = CrosstermBackend::new(stdout); let mut terminal = Terminal::new(backend)?; - // Slurp all input - let text_orig = io::read_to_string(io::stdin())?; - - // Run the actual application - let app = App::from_template(text_orig, &Template::from(&arg)); let res = run_app(&mut terminal, app); // Restore terminal