Skip to content

Commit

Permalink
feat(parser): Introduce clap based parser
Browse files Browse the repository at this point in the history
Still a WIP, but it's a start.
Will keep the old parser around for now, but will remove it once the
clap based parser is fully functional.

refactor(Parser): ♻️ removed unuseful files and adapted errors

feat(Parser): ✨ reimplemented strictness

fix(Parser): 🐛 fixed issue on parsing

issues were related to multiple args not working and paths not parsed

fix(Parser): 🔥 overwrited help to be able to do a -h

feat(Parser): added more tests and started added the different descriptions

feat(Parser): finished help argument and added a default to opts

Remove parenthesis

Co-authored-by: Mélanie Chauvel <perso@hack-libre.org>
Signed-off-by: MartinFillon <114775771+MartinFillon@users.noreply.github.com>

Typo fix

Co-authored-by: Mélanie Chauvel <perso@hack-libre.org>
Signed-off-by: MartinFillon <114775771+MartinFillon@users.noreply.github.com>

Typo fix

Co-authored-by: Mélanie Chauvel <perso@hack-libre.org>
Signed-off-by: MartinFillon <114775771+MartinFillon@users.noreply.github.com>
  • Loading branch information
MartinFillon and ariasuni committed Sep 9, 2023
1 parent 721eddf commit 6db6637
Show file tree
Hide file tree
Showing 15 changed files with 1,012 additions and 1,951 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ stage
# VHS testing stuff
out.gif
tests/tmp

#vscode
.vscode
113 changes: 113 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ name = "eza"

[dependencies]
ansi_term = "0.12"
clap = { version = "4.4.2", features = ["derive", "string"] }
glob = "0.3"
lazy_static = "1.3"
libc = "0.2"
Expand Down Expand Up @@ -75,8 +76,8 @@ version = "0.5.2"
default-features = false

[features]
default = [ "git" ]
git = [ "git2" ]
default = ["git"]
git = ["git2"]
vendored-openssl = ["git2/vendored-openssl"]


Expand Down
73 changes: 30 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::env;
use std::ffi::{OsStr, OsString};
use std::io::{self, Write, ErrorKind};
use std::path::{Component, PathBuf};
use clap::Parser;

use ansi_term::{ANSIStrings, Style};

Expand All @@ -34,9 +35,10 @@ use log::*;
use crate::fs::{Dir, File};
use crate::fs::feature::git::GitCache;
use crate::fs::filter::GitIgnore;
use crate::options::{Options, Vars, vars, OptionsResult};
use crate::options::{Options, Vars, vars};
use crate::output::{escape, lines, grid, grid_details, details, View, Mode};
use crate::theme::Theme;
use crate::options::parser::Opts;

mod fs;
mod info;
Expand All @@ -46,6 +48,7 @@ mod output;
mod theme;



fn main() {
use std::process::exit;

Expand All @@ -61,56 +64,40 @@ fn main() {
warn!("Failed to enable ANSI support: {}", e);
}

let args: Vec<_> = env::args_os().skip(1).collect();
match Options::parse(args.iter().map(std::convert::AsRef::as_ref), &LiveVars) {
OptionsResult::Ok(options, mut input_paths) => {

// List the current directory by default.
// (This has to be done here, otherwise git_options won’t see it.)
if input_paths.is_empty() {
input_paths = vec![ OsStr::new(".") ];
}

let git = git_options(&options, &input_paths);
let writer = io::stdout();

let console_width = options.view.width.actual_terminal_width();
let theme = options.theme.to_theme(terminal_size::terminal_size().is_some());
let exa = Exa { options, writer, input_paths, theme, console_width, git };
let cli = Opts::parse();

match exa.run() {
Ok(exit_status) => {
exit(exit_status);
}
let mut input_paths: Vec<&OsStr> = cli.paths.iter().map(OsString::as_os_str).collect();
if input_paths.is_empty() {
input_paths.push(OsStr::new("."));
}
let options = match Options::deduce(&cli, &LiveVars) {
Ok(o) => {o},
Err(e) => {
eprintln!("{e}");
exit(exits::OPTIONS_ERROR);
}
};

Err(e) if e.kind() == ErrorKind::BrokenPipe => {
warn!("Broken pipe error: {e}");
exit(exits::SUCCESS);
}
let git = git_options(&options, &input_paths);
let writer = io::stdout();

Err(e) => {
eprintln!("{e}");
exit(exits::RUNTIME_ERROR);
}
}
}
let console_width = options.view.width.actual_terminal_width();
let theme = options.theme.to_theme(terminal_size::terminal_size().is_some());
let exa = Exa { options, writer, input_paths, theme, console_width, git };

OptionsResult::Help(help_text) => {
print!("{help_text}");
match exa.run() {
Ok(exit_status) => {
exit(exit_status);
}

OptionsResult::Version(version_str) => {
print!("{version_str}");
Err(e) if e.kind() == ErrorKind::BrokenPipe => {
warn!("Broken pipe error: {e}");
exit(exits::SUCCESS);
}

OptionsResult::InvalidOptions(error) => {
eprintln!("eza: {error}");

if let Some(s) = error.suggestion() {
eprintln!("{s}");
}

exit(exits::OPTIONS_ERROR);
Err(e) => {
eprintln!("{e}");
exit(exits::RUNTIME_ERROR);
}
}
}
Expand Down
Loading

0 comments on commit 6db6637

Please sign in to comment.