Remove generic commands

This commit is contained in:
Austen Adler 2022-12-17 11:29:34 -05:00
parent 2b11951166
commit 536086c4a0

View File

@ -4,11 +4,13 @@
mod command; mod command;
use ansi4tui::bytes_to_text; use ansi4tui::bytes_to_text;
use anyhow::anyhow;
use anyhow::bail; use anyhow::bail;
use anyhow::Result; use anyhow::Result;
use command::CommandResult; use command::CommandResult;
use crossterm::event::DisableBracketedPaste; use crossterm::event::DisableBracketedPaste;
use crossterm::event::EnableBracketedPaste; use crossterm::event::EnableBracketedPaste;
use std::str::FromStr;
use std::{ use std::{
io::{self, Write}, io::{self, Write},
process::{Command, Stdio}, process::{Command, Stdio},
@ -67,7 +69,7 @@ impl App {
let wordsplit = true; let wordsplit = true;
match template { match template {
Template::Awk | Template::Generic(_) => Self { Template::Awk => Self {
cmdline: String::new(), cmdline: String::new(),
cmdline_position, cmdline_position,
text_orig, text_orig,
@ -132,7 +134,6 @@ impl App {
} }
pub enum Template { pub enum Template {
Generic(String),
Sh(String), Sh(String),
Jq, Jq,
Grep, Grep,
@ -142,25 +143,26 @@ pub enum Template {
Perl, Perl,
} }
impl Template { impl FromStr for Template {
#[must_use] type Err = anyhow::Error;
pub fn from(s: &str) -> Self { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().trim() { match s.to_lowercase().trim() {
"jq" => Self::Jq, "jq" => Ok(Self::Jq),
"grep" => Self::Grep, "grep" => Ok(Self::Grep),
"rg" => Self::Rg, "rg" => Ok(Self::Rg),
"sed" => Self::Sed, "sed" => Ok(Self::Sed),
"awk" => Self::Awk, "awk" => Ok(Self::Awk),
"perl" => Self::Perl, "perl" => Ok(Self::Perl),
s @ "sh" | s @ "bash" | s @ "zsh" | s @ "dash" => Self::Sh(s.to_string()), s @ "sh" | s @ "bash" | s @ "zsh" | s @ "dash" => Ok(Self::Sh(s.to_string())),
c => Self::Generic(c.to_string()), e => Err(anyhow!("{e} is not a supported command")),
}
} }
} }
impl Template {
#[must_use] #[must_use]
pub fn command(&self) -> String { pub fn command(&self) -> String {
match self { match self {
Self::Generic(c) => c.to_string(),
Self::Sh(s) => s.to_string(), Self::Sh(s) => s.to_string(),
Self::Jq => String::from("jq"), Self::Jq => String::from("jq"),
Self::Grep => String::from("grep"), Self::Grep => String::from("grep"),
@ -184,6 +186,11 @@ fn main() -> Result<()> {
None => bail!("You must pass a command"), 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()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
// execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; // execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
@ -196,11 +203,6 @@ fn main() -> Result<()> {
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?; 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); let res = run_app(&mut terminal, app);
// Restore terminal // Restore terminal