Improve parsing CLI arguments

pull/522/head
Arijit Basu 2 years ago committed by Arijit Basu
parent 88fe71779b
commit fbe6b2be10

@ -159,7 +159,7 @@ pub struct InputBuffer {
pub prompt: String, pub prompt: String,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct App { pub struct App {
pub version: String, pub version: String,
@ -610,7 +610,7 @@ impl App {
dir.focus = 0; dir.focus = 0;
if save_history { 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()) self.history = history.push(n.absolute_path.clone())
} }
} }
@ -758,29 +758,31 @@ impl App {
} }
fn enter(self) -> Result<Self> { fn enter(self) -> Result<Self> {
self.focused_node() if let Some(path) = self.focused_node().map(|n| n.absolute_path.clone()) {
.map(|n| n.absolute_path.clone()) self.change_directory(&path, true)
.map(|p| self.clone().change_directory(&p, true)) } else {
.unwrap_or(Ok(self)) Ok(self)
}
} }
fn back(self) -> Result<Self> { fn back(self) -> Result<Self> {
PathBuf::from(self.pwd.clone()) if let Some(p) = PathBuf::from(self.pwd.clone())
.parent() .parent()
.map(|p| self.clone().change_directory(&p.to_string_lossy(), true)) .and_then(|p| p.to_str())
.unwrap_or(Ok(self)) {
self.change_directory(p, true)
} else {
Ok(self)
}
} }
fn last_visited_path(mut self) -> Result<Self> { fn last_visited_path(mut self) -> Result<Self> {
self.history = self.history.visit_last(); self.history = self.history.visit_last();
if let Some(target) = self.history.peek() { if let Some(target) = self.history.peek().cloned() {
if target.ends_with('/') { if let Some(path) = target.strip_suffix('/') {
target self.change_directory(path, false)
.strip_suffix('/')
.map(|s| self.clone().change_directory(s, false))
.unwrap_or(Ok(self))
} else { } else {
self.clone().focus_path(target, false) self.focus_path(&target, false)
} }
} else { } else {
Ok(self) Ok(self)
@ -789,14 +791,11 @@ impl App {
fn next_visited_path(mut self) -> Result<Self> { fn next_visited_path(mut self) -> Result<Self> {
self.history = self.history.visit_next(); self.history = self.history.visit_next();
if let Some(target) = self.history.peek() { if let Some(target) = self.history.peek().cloned() {
if target.ends_with('/') { if let Some(path) = target.strip_suffix('/') {
target self.change_directory(path, false)
.strip_suffix('/')
.map(|s| self.clone().change_directory(s, false))
.unwrap_or(Ok(self))
} else { } else {
self.clone().focus_path(target, false) self.focus_path(&target, false)
} }
} else { } else {
Ok(self) Ok(self)

@ -54,19 +54,8 @@ fn main() {
} else if cli.version { } else if cli.version {
println!("xplr {}", xplr::app::VERSION); println!("xplr {}", xplr::app::VERSION);
} else { } else {
let write0 = cli.write0;
match runner::from_cli(cli).and_then(|a| a.run()) { match runner::from_cli(cli).and_then(|a| a.run()) {
Ok(Some(mut out)) => { Ok(Some(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();
}
print!("{}", out); print!("{}", out);
} }
Ok(None) => {} Ok(None) => {}

@ -1,5 +1,4 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use std::collections::VecDeque;
use std::env; use std::env;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf; use std::path::PathBuf;
@ -39,12 +38,12 @@ impl Cli {
/// Parse arguments from the command-line /// Parse arguments from the command-line
pub fn parse(args: env::Args) -> Result<Self> { pub fn parse(args: env::Args) -> Result<Self> {
let mut args: VecDeque<String> = args.skip(1).collect(); let mut args = args.skip(1).peekable();
let mut cli = Self::default(); let mut cli = Self::default();
let mut flag_ends = false; let mut flag_ends = false;
while let Some(arg) = args.pop_front() { while let Some(arg) = args.next() {
if flag_ends { if flag_ends {
cli.read_path(&arg)?; cli.read_path(&arg)?;
} else { } else {
@ -89,31 +88,21 @@ impl Cli {
} }
// Options // Options
"-c" | "--config" => { "-c" | "--config" => cli.config = args.next().map(PathBuf::from),
cli.config = args.pop_front().map(PathBuf::from)
}
"-C" | "--extra-config" => { "-C" | "--extra-config" => {
while let Some(path) = args.pop_front() { while let Some(path) =
if path.starts_with('-') { args.next_if(|path| !path.starts_with('-'))
args.push_front(path); {
break; cli.extra_config.push(PathBuf::from(path));
} else {
cli.extra_config.push(PathBuf::from(path));
}
} }
} }
"--read-only" => cli.read_only = true, "--read-only" => cli.read_only = true,
"--on-load" => { "--on-load" => {
while let Some(msg) = args.pop_front() { while let Some(msg) = args.next_if(|msg| !msg.starts_with('-')) {
if msg.starts_with('-') { cli.on_load.push(msg.trim().try_into()?);
args.push_front(msg);
break;
} else {
cli.on_load.push(msg.trim().try_into()?);
}
} }
} }

Loading…
Cancel
Save