This commit is contained in:
parent
20a62f7be3
commit
9f7fea0cfa
126
src/main.rs
126
src/main.rs
@ -6,7 +6,6 @@ mod command;
|
||||
mod event;
|
||||
mod ui;
|
||||
mod util;
|
||||
use anyhow::anyhow;
|
||||
use anyhow::bail;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
@ -23,7 +22,6 @@ use crossterm::event::KeyModifiers;
|
||||
use event::EventMessage;
|
||||
use parking_lot::RwLock;
|
||||
use ropey::Rope;
|
||||
use std::str::FromStr;
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
sync::Arc,
|
||||
@ -81,6 +79,9 @@ pub struct CommandOptions {
|
||||
|
||||
/// The position of the cursor
|
||||
cmdline_position: u16,
|
||||
|
||||
/// Command combined
|
||||
combined_command: String,
|
||||
}
|
||||
|
||||
/// The state of the application
|
||||
@ -105,52 +106,31 @@ pub struct App {
|
||||
render_states: RwLock<RenderStates>,
|
||||
}
|
||||
|
||||
impl CommandOptions {
|
||||
#[must_use]
|
||||
pub fn from_template(template: &Template) -> Self {
|
||||
let defaults = Self {
|
||||
command: template.command(),
|
||||
impl Default for CommandOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
command: String::from("sh"),
|
||||
cmdline_position: 0_u16,
|
||||
hidden_options: Vec::new(),
|
||||
hidden_options: vec!["-c"],
|
||||
cmdline: String::new(),
|
||||
command_result: None,
|
||||
autorun: true,
|
||||
wordsplit: true,
|
||||
};
|
||||
|
||||
match template {
|
||||
Template::Awk => Self { ..defaults },
|
||||
Template::Sh(_) => Self {
|
||||
hidden_options: vec!["-c"],
|
||||
wordsplit: false,
|
||||
..defaults
|
||||
},
|
||||
Template::Jq => Self {
|
||||
cmdline_position: 2,
|
||||
cmdline: String::from("'.'"),
|
||||
hidden_options: vec!["-C"],
|
||||
..defaults
|
||||
},
|
||||
Template::Grep | Template::Rg => Self {
|
||||
cmdline_position: 1,
|
||||
cmdline: String::from("''"),
|
||||
hidden_options: vec!["--color=always"],
|
||||
..defaults
|
||||
},
|
||||
Template::Sed => Self {
|
||||
cmdline_position: 3_u16,
|
||||
cmdline: String::from("'s///g'"),
|
||||
..defaults
|
||||
},
|
||||
Template::Perl => Self {
|
||||
cmdline_position: 9_u16,
|
||||
cmdline: String::from("-p -e 's///'"),
|
||||
..defaults
|
||||
},
|
||||
wordsplit: false,
|
||||
combined_command: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandOptions {
|
||||
pub fn append_command(&mut self) {
|
||||
if !self.combined_command.is_empty() {
|
||||
self.combined_command += " | ";
|
||||
}
|
||||
|
||||
self.combined_command += self.cmdline.as_ref();
|
||||
}
|
||||
}
|
||||
|
||||
// impl App<'_> {
|
||||
impl App {
|
||||
/// Constructs a new instance of `App`
|
||||
@ -159,20 +139,18 @@ impl App {
|
||||
///
|
||||
/// * `command_request_tx` - The sender for the command request worker
|
||||
/// * `input` - The stdin to be passed in to each invocation
|
||||
/// * `template` - The template to use
|
||||
#[must_use]
|
||||
pub fn from_template(
|
||||
pub fn new(
|
||||
message_rx: Receiver<EventMessage>,
|
||||
command_request_tx: Sender<CommandRequest>,
|
||||
input: Option<String>,
|
||||
template: &Template,
|
||||
) -> Self {
|
||||
// let text_orig_rope = Rope::from_str(&input);
|
||||
let text_orig_rope = input.as_ref().map(|s| Rope::from_str(s));
|
||||
Self {
|
||||
text_orig: Arc::new(input),
|
||||
text_orig_rope,
|
||||
command_options: Arc::new(RwLock::new(CommandOptions::from_template(template))),
|
||||
command_options: Arc::new(RwLock::new(CommandOptions::default())),
|
||||
message_rx,
|
||||
command_request_tx,
|
||||
render_states: RwLock::new(RenderStates::default()),
|
||||
@ -191,47 +169,6 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Template {
|
||||
Sh(String),
|
||||
Jq,
|
||||
Grep,
|
||||
Rg,
|
||||
Sed,
|
||||
Awk,
|
||||
Perl,
|
||||
}
|
||||
|
||||
impl FromStr for Template {
|
||||
type Err = anyhow::Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().trim() {
|
||||
"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" | "bash" | "zsh" | "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::Sh(s) => s.to_string(),
|
||||
Self::Jq => String::from("jq"),
|
||||
Self::Grep => String::from("grep"),
|
||||
Self::Rg => String::from("rg"),
|
||||
Self::Sed => String::from("sed"),
|
||||
Self::Awk => String::from("awk"),
|
||||
Self::Perl => String::from("perl"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
#[cfg(debug_assertions)]
|
||||
enable_tracing();
|
||||
@ -254,12 +191,7 @@ fn main() -> Result<()> {
|
||||
let (message_rx, command_request_tx) = init_message_passing();
|
||||
|
||||
// Run the actual application
|
||||
let app = App::from_template(
|
||||
message_rx,
|
||||
command_request_tx,
|
||||
text_orig,
|
||||
&Template::from_str(&args.command)?,
|
||||
);
|
||||
let app = App::new(message_rx, command_request_tx, text_orig);
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = io::stdout();
|
||||
execute!(
|
||||
@ -350,6 +282,10 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<Optio
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..
|
||||
}) => {
|
||||
if command_options.command_result.is_some() {
|
||||
command_options.append_command();
|
||||
}
|
||||
|
||||
// Move stdout to stdin
|
||||
if let Some(result) = &command_options.command_result {
|
||||
app.text_orig = Arc::new(Some(result.stdout.to_string()));
|
||||
@ -405,10 +341,10 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<Optio
|
||||
command_options.cmdline_position = 0_u16;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
return Ok(Some(format!(
|
||||
"{} {}",
|
||||
command_options.command, command_options.cmdline
|
||||
)));
|
||||
if command_options.command_result.is_some() {
|
||||
command_options.append_command();
|
||||
}
|
||||
return Ok(Some(command_options.combined_command.to_string()));
|
||||
}
|
||||
KeyCode::Left => {
|
||||
command_options.cmdline_position =
|
||||
|
Loading…
Reference in New Issue
Block a user