From fbe6b2be104af3c592367124148228ac01281fa9 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Mon, 24 Oct 2022 19:36:28 +0530 Subject: [PATCH] Improve parsing CLI arguments --- src/app.rs | 45 ++++++++++++++++++++++----------------------- src/bin/xplr.rs | 13 +------------ src/cli.rs | 29 +++++++++-------------------- 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/src/app.rs b/src/app.rs index e810fba..673a7d6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -159,7 +159,7 @@ pub struct InputBuffer { pub prompt: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct App { pub version: String, @@ -610,7 +610,7 @@ impl App { dir.focus = 0; if save_history { - if let Some(n) = self.clone().focused_node() { + if let Some(n) = self.focused_node() { self.history = history.push(n.absolute_path.clone()) } } @@ -758,29 +758,31 @@ impl App { } fn enter(self) -> Result { - self.focused_node() - .map(|n| n.absolute_path.clone()) - .map(|p| self.clone().change_directory(&p, true)) - .unwrap_or(Ok(self)) + if let Some(path) = self.focused_node().map(|n| n.absolute_path.clone()) { + self.change_directory(&path, true) + } else { + Ok(self) + } } fn back(self) -> Result { - PathBuf::from(self.pwd.clone()) + if let Some(p) = PathBuf::from(self.pwd.clone()) .parent() - .map(|p| self.clone().change_directory(&p.to_string_lossy(), true)) - .unwrap_or(Ok(self)) + .and_then(|p| p.to_str()) + { + self.change_directory(p, true) + } else { + Ok(self) + } } fn last_visited_path(mut self) -> Result { self.history = self.history.visit_last(); - if let Some(target) = self.history.peek() { - if target.ends_with('/') { - target - .strip_suffix('/') - .map(|s| self.clone().change_directory(s, false)) - .unwrap_or(Ok(self)) + if let Some(target) = self.history.peek().cloned() { + if let Some(path) = target.strip_suffix('/') { + self.change_directory(path, false) } else { - self.clone().focus_path(target, false) + self.focus_path(&target, false) } } else { Ok(self) @@ -789,14 +791,11 @@ impl App { fn next_visited_path(mut self) -> Result { self.history = self.history.visit_next(); - if let Some(target) = self.history.peek() { - if target.ends_with('/') { - target - .strip_suffix('/') - .map(|s| self.clone().change_directory(s, false)) - .unwrap_or(Ok(self)) + if let Some(target) = self.history.peek().cloned() { + if let Some(path) = target.strip_suffix('/') { + self.change_directory(path, false) } else { - self.clone().focus_path(target, false) + self.focus_path(&target, false) } } else { Ok(self) diff --git a/src/bin/xplr.rs b/src/bin/xplr.rs index ffc91b5..6cec1a3 100644 --- a/src/bin/xplr.rs +++ b/src/bin/xplr.rs @@ -54,19 +54,8 @@ fn main() { } else if cli.version { println!("xplr {}", xplr::app::VERSION); } else { - let write0 = cli.write0; match runner::from_cli(cli).and_then(|a| a.run()) { - Ok(Some(mut out)) => { - if write0 { - if out.ends_with('\n') { - out = out.chars().take(out.chars().count() - 1).collect() - }; - out = out - .trim_end() - .chars() - .map(|c| if c == '\n' { '\0' } else { c }) - .collect(); - } + Ok(Some(out)) => { print!("{}", out); } Ok(None) => {} diff --git a/src/cli.rs b/src/cli.rs index bb2be1a..3f078f4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,4 @@ use anyhow::{bail, Result}; -use std::collections::VecDeque; use std::env; use std::io::{BufRead, BufReader}; use std::path::PathBuf; @@ -39,12 +38,12 @@ impl Cli { /// Parse arguments from the command-line pub fn parse(args: env::Args) -> Result { - let mut args: VecDeque = args.skip(1).collect(); + let mut args = args.skip(1).peekable(); let mut cli = Self::default(); let mut flag_ends = false; - while let Some(arg) = args.pop_front() { + while let Some(arg) = args.next() { if flag_ends { cli.read_path(&arg)?; } else { @@ -89,31 +88,21 @@ impl Cli { } // Options - "-c" | "--config" => { - cli.config = args.pop_front().map(PathBuf::from) - } + "-c" | "--config" => cli.config = args.next().map(PathBuf::from), "-C" | "--extra-config" => { - while let Some(path) = args.pop_front() { - if path.starts_with('-') { - args.push_front(path); - break; - } else { - cli.extra_config.push(PathBuf::from(path)); - } + while let Some(path) = + args.next_if(|path| !path.starts_with('-')) + { + cli.extra_config.push(PathBuf::from(path)); } } "--read-only" => cli.read_only = true, "--on-load" => { - while let Some(msg) = args.pop_front() { - if msg.starts_with('-') { - args.push_front(msg); - break; - } else { - cli.on_load.push(msg.trim().try_into()?); - } + while let Some(msg) = args.next_if(|msg| !msg.starts_with('-')) { + cli.on_load.push(msg.trim().try_into()?); } }