mirror of
https://github.com/sayanarijit/xplr
synced 2024-11-04 18:00:14 +00:00
Improve parsing CLI arguments
This commit is contained in:
parent
88fe71779b
commit
fbe6b2be10
45
src/app.rs
45
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> {
|
||||
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<Self> {
|
||||
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> {
|
||||
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> {
|
||||
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)
|
||||
|
@ -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) => {}
|
||||
|
29
src/cli.rs
29
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<Self> {
|
||||
let mut args: VecDeque<String> = 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()?);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user