From d5bfe6630fe955052e51bd8d0c938149251cbc96 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Sat, 13 Mar 2021 15:31:25 +0530 Subject: [PATCH] Use app.task instead of separate variables --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app.rs | 42 +++++++++++++++------ src/main.rs | 104 ++++++++++++++++++++++++---------------------------- 4 files changed, 80 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e71904..8155998 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1133,7 +1133,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.1.11" +version = "0.1.12" dependencies = [ "criterion", "crossterm", diff --git a/Cargo.toml b/Cargo.toml index 57196a0..81dceb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.1.11" # Update app.rs +version = "0.1.12" # Update app.rs authors = ["Arijit Basu "] edition = "2018" description = "An experimental, minimal, configurable TUI file explorer, stealing ideas from nnn and fzf." diff --git a/src/app.rs b/src/app.rs index f0d6d51..9ad1184 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,7 @@ use std::io::BufReader; use std::path::Path; use std::path::PathBuf; -pub const VERSION: &str = "v0.1.11"; // Update Cargo.toml +pub const VERSION: &str = "v0.1.12"; // Update Cargo.toml pub const UNSUPPORTED_STR: &str = "???"; pub const TOTAL_ROWS: usize = 50; @@ -274,6 +274,20 @@ pub fn parse_help_menu<'a>( m } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum Task { + NoOp, + Quit, + PrintAndQuit(String), + Call(CommandConfig), +} + +impl Default for Task { + fn default() -> Self { + Self::NoOp + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct App { pub version: String, @@ -285,8 +299,7 @@ pub struct App { pub parsed_key_bindings: HashMap)>, pub parsed_help_menu: Vec<(String, String)>, pub show_hidden: bool, - pub call: Option, - pub result: Option, + pub task: Task, } impl App { @@ -322,8 +335,7 @@ impl App { parsed_key_bindings, parsed_help_menu, show_hidden, - result: None, - call: None, + task: Task::NoOp, }) } @@ -411,7 +423,7 @@ impl App { } pub fn call(mut self, cmd: &CommandConfig) -> Result { - self.call = Some(cmd.clone()); + self.task = Task::Call(cmd.clone()); Ok(self) } @@ -631,22 +643,28 @@ impl App { pub fn print_focused(self) -> Result { let mut app = self; - app.result = app + app.task = app .directory_buffer .focused() - .and_then(|(p, _)| p.to_str().map(|s| s.to_string())); + .and_then(|(p, _)| p.to_str().map(|s| Task::PrintAndQuit(s.to_string()))) + .unwrap_or_default(); Ok(app) } pub fn print_pwd(self) -> Result { let mut app = self; - app.result = app.directory_buffer.pwd.to_str().map(|s| s.to_string()); + app.task = app + .directory_buffer + .pwd + .to_str() + .map(|s| Task::PrintAndQuit(s.to_string())) + .unwrap_or_default(); Ok(app) } pub fn print_selected(self) -> Result { let mut app = self; - app.result = Some( + app.task = Task::PrintAndQuit( app.selected_paths .clone() .iter() @@ -661,12 +679,12 @@ impl App { pub fn print_app_state(self) -> Result { let state = serde_yaml::to_string(&self)?; let mut app = self; - app.result = Some(state); + app.task = Task::PrintAndQuit(state); Ok(app) } pub fn quit(mut self) -> Result { - self.result = Some("".into()); + self.task = Task::Quit; Ok(self) } diff --git a/src/main.rs b/src/main.rs index bd8a139..e094ba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,17 +8,21 @@ use tui::backend::CrosstermBackend; use tui::widgets::{ListState, TableState}; use tui::Terminal; use xplr::app; +use xplr::app::Task; use xplr::error::Error; use xplr::input::Key; use xplr::ui; +use std::fs; handlebars_helper!(shellescape: |v: str| format!("{}", shellwords::escape(v))); +handlebars_helper!(readfile: |v: str| fs::read_to_string(v).unwrap()); fn main() -> Result<(), Error> { let mut app = app::create()?; let mut hb = Handlebars::new(); hb.register_helper("shellescape", Box::new(shellescape)); + hb.register_helper("readfile", Box::new(readfile)); hb.register_template_string( app::TEMPLATE_TABLE_ROW, &app.config @@ -48,15 +52,7 @@ fn main() -> Result<(), Error> { let mut list_state = ListState::default(); term::enable_raw_mode().unwrap(); - terminal.draw(|f| { - ui::draw( - &app, - &hb, - f, - &mut table_state, - &mut list_state, - ) - })?; + terminal.draw(|f| ui::draw(&app, &hb, f, &mut table_state, &mut list_state))?; let mut result = Ok(()); 'outer: for key in keys { @@ -64,58 +60,54 @@ fn main() -> Result<(), Error> { for action in actions.iter() { app = match app.handle(action) { Ok(mut a) => { - terminal.draw(|f| { - ui::draw( - &a, - &hb, - f, - &mut table_state, - &mut list_state, - ) - })?; + terminal + .draw(|f| ui::draw(&a, &hb, f, &mut table_state, &mut list_state))?; - if let Some(result) = a.result.clone() { - term::disable_raw_mode().unwrap(); - std::mem::drop(terminal); - if !result.is_empty() { - println!("{}", &result); - }; - break 'outer; - }; + match a.task.clone() { + Task::NoOp => {} + + Task::Quit => { + term::disable_raw_mode().unwrap(); + std::mem::drop(terminal); + break 'outer; + } + + Task::PrintAndQuit(txt) => { + term::disable_raw_mode().unwrap(); + std::mem::drop(terminal); + if !txt.is_empty() { + println!("{}", &txt); + }; + break 'outer; + } - if let Some(cmd) = a.call.clone() { - term::disable_raw_mode().unwrap(); - std::mem::drop(terminal); - if let Some((_, meta)) = a.directory_buffer.focused() { - let _ = std::process::Command::new(cmd.command.clone()) - .current_dir(&a.directory_buffer.pwd) - .args( - cmd.args - .iter() - .map(|arg| hb.render_template(arg, &meta).unwrap()), - ) - .status(); - }; + Task::Call(cmd) => { + term::disable_raw_mode().unwrap(); + std::mem::drop(terminal); + if let Some((_, meta)) = a.directory_buffer.focused() { + let _ = std::process::Command::new(cmd.command.clone()) + .current_dir(&a.directory_buffer.pwd) + .args( + cmd.args + .iter() + .map(|arg| hb.render_template(arg, &meta).unwrap()), + ) + .status(); + }; - term::enable_raw_mode().unwrap(); - let stdout = get_tty()?; - let stdout = AlternateScreen::from(stdout); - let backend = CrosstermBackend::new(stdout); - terminal = Terminal::new(backend)?; - a = a.refresh()?; - terminal.draw(|f| { - ui::draw( - &a, - &hb, - f, - &mut table_state, - &mut list_state, - ) - })?; + term::enable_raw_mode().unwrap(); + let stdout = get_tty()?; + let stdout = AlternateScreen::from(stdout); + let backend = CrosstermBackend::new(stdout); + terminal = Terminal::new(backend)?; + a = a.refresh()?; + terminal.draw(|f| { + ui::draw(&a, &hb, f, &mut table_state, &mut list_state) + })?; + } }; - a.call = None; - a.result = None; + a.task = Task::NoOp; a } Err(e) => {