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