Skip to content

Commit

Permalink
Upgrade clap to 4.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-Sky committed Apr 4, 2023
1 parent ecaeacf commit f0f8e83
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 198 deletions.
332 changes: 244 additions & 88 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -20,7 +20,7 @@ travis-ci = { repository = "denisidoro/navi", branch = "master" }

[dependencies]
regex = { version = "1.6.0", default-features = false, features = ["std", "unicode-perl"] }
clap = { version = "3.2.14", features = ["derive", "cargo"] }
clap = { version = "4.2.1", features = ["derive", "cargo"] }
crossterm = "0.24.0"
lazy_static = "1.4.0"
directories-next = "2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain.toml
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.62.0"
components = [ "rustfmt", "clippy" ]
channel = "1.68.2"
components = [ "rustfmt", "clippy" ]
41 changes: 8 additions & 33 deletions src/commands/func/mod.rs
Expand Up @@ -6,44 +6,26 @@ use super::temp;
use crate::common::url;
use crate::prelude::*;
use clap::Args;
use clap::Parser;
use clap::ValueEnum;

const POSSIBLE_VALUES: &[&str] = &[
"url::open",
"welcome",
"widget::last_command",
"map::expand",
"temp",
];

impl FromStr for Func {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"url::open" => Ok(Func::UrlOpen),
"welcome" => Ok(Func::Welcome),
"widget::last_command" => Ok(Func::WidgetLastCommand),
"map::expand" => Ok(Func::MapExpand),
"temp" => Ok(Func::Temp),
_ => Err("no match"),
}
}
}

#[derive(Debug, Clone, Parser)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Func {
#[value(name = "url::open")]
UrlOpen,
#[value(name = "welcome")]
Welcome,
#[value(name = "widget::last_command")]
WidgetLastCommand,
#[value(name = "map::expand")]
MapExpand,
#[value(name = "temp")]
Temp,
}

#[derive(Debug, Clone, Args)]
pub struct Input {
/// Function name (example: "url::open")
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true)]
#[arg(ignore_case = true)]
pub func: Func,
/// List of arguments (example: "https://google.com")
pub args: Vec<String>,
Expand All @@ -63,10 +45,3 @@ impl Runnable for Input {
}
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Func::from_str(v).is_ok())
}
}
28 changes: 3 additions & 25 deletions src/commands/info.rs
@@ -1,31 +1,16 @@
use clap::Args;
use clap::ValueEnum;

use crate::filesystem;
use crate::prelude::*;

const POSSIBLE_VALUES: &[&str] = &["cheats-example", "cheats-path", "config-path", "config-example"];

impl FromStr for Info {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"cheats-example" => Ok(Info::CheatsExample),
"cheats-path" => Ok(Info::CheatsPath),
"config-example" => Ok(Info::ConfigExample),
"config-path" => Ok(Info::ConfigPath),
_ => Err("no match"),
}
}
}

#[derive(Debug, Clone, Args)]
pub struct Input {
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true)]
#[arg(ignore_case = true)]
pub info: Info,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Info {
CheatsExample,
CheatsPath,
Expand All @@ -46,10 +31,3 @@ impl Runnable for Input {
Ok(())
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Info::from_str(v).is_ok())
}
}
33 changes: 13 additions & 20 deletions src/commands/shell.rs
@@ -1,27 +1,27 @@
use std::fmt;
use std::fmt::Display;

use clap::Args;

use crate::common::shell::Shell;
use crate::prelude::*;

const POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish", "elvish"];

impl FromStr for Shell {
type Err = &'static str;
impl Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Bash => "bash",
Self::Zsh => "zsh",
Self::Fish => "fish",
Self::Elvish => "elvish",
};

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bash" => Ok(Shell::Bash),
"zsh" => Ok(Shell::Zsh),
"fish" => Ok(Shell::Fish),
"elvish" => Ok(Shell::Elvish),
_ => Err("no match"),
}
write!(f, "{s}")
}
}

#[derive(Debug, Clone, Args)]
pub struct Input {
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true, default_value = "bash")]
#[clap(ignore_case = true, default_value_t = Shell::Bash)]
pub shell: Shell,
}

Expand All @@ -41,10 +41,3 @@ impl Runnable for Input {
Ok(())
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Shell::from_str(v).is_ok())
}
}
3 changes: 2 additions & 1 deletion src/common/shell.rs
@@ -1,10 +1,11 @@
use crate::prelude::*;
use clap::ValueEnum;
use std::process::Command;
use thiserror::Error;

pub const EOF: &str = "NAVIEOF";

#[derive(Debug, Clone)]
#[derive(Debug, Clone, ValueEnum)]
pub enum Shell {
Bash,
Zsh,
Expand Down
35 changes: 17 additions & 18 deletions src/config/cli.rs
@@ -1,10 +1,10 @@
use crate::commands;
use crate::finder::{self, FinderChoice};
use crate::finder::FinderChoice;
use crate::prelude::*;
use clap::{crate_version, AppSettings, Parser, Subcommand};
use clap::{crate_version, Parser, Subcommand};

#[derive(Debug, Parser)]
#[clap(after_help = "\x1b[0;33mMORE INFO:\x1b[0;0m
#[command(after_help = "\x1b[0;33mMORE INFO:\x1b[0;0m
Please refer to \x1b[0;32mhttps://github.com/denisidoro/navi\x1b[0;0m
\x1b[0;33mENVIRONMENT VARIABLES:\x1b[0m
Expand Down Expand Up @@ -37,51 +37,50 @@ use clap::{crate_version, AppSettings, Parser, Subcommand};
navi --fzf-overrides '--nth 1,2' # only consider the first two columns for search
navi --fzf-overrides '--no-exact' # use looser search algorithm
navi --tag-rules='git,!checkout' # show non-checkout git snippets only")]
#[clap(setting = AppSettings::AllowHyphenValues)]
#[clap(version = crate_version!())]
pub(super) struct ClapConfig {
/// Colon-separated list of paths containing .cheat files
#[clap(short, long)]
#[arg(short, long)]
pub path: Option<String>,

/// Instead of executing a snippet, prints it to stdout
#[clap(long)]
#[arg(long)]
#[cfg(not(feature = "disable-command-execution"))]
pub print: bool,

/// Returns the best match
#[clap(long)]
#[arg(long)]
pub best_match: bool,

/// Searches for cheatsheets using the tldr-pages repository
#[clap(long)]
#[arg(long)]
pub tldr: Option<String>,

/// [Experimental] Comma-separated list that acts as filter for tags. Parts starting with ! represent negation
#[clap(long)]
#[arg(long)]
pub tag_rules: Option<String>,

/// Searches for cheatsheets using the cheat.sh repository
#[clap(long)]
#[arg(long)]
pub cheatsh: Option<String>,

/// Prepopulates the search field
#[clap(short, long)]
#[arg(short, long, allow_hyphen_values = true)]
pub query: Option<String>,

/// Finder overrides for snippet selection
#[clap(long)]
#[arg(long, allow_hyphen_values = true)]
pub fzf_overrides: Option<String>,

/// Finder overrides for variable selection
#[clap(long)]
#[arg(long, allow_hyphen_values = true)]
pub fzf_overrides_var: Option<String>,

/// Finder application to use
#[clap(long, possible_values = finder::POSSIBLE_VALUES, ignore_case = true)]
#[arg(long, ignore_case = true)]
pub finder: Option<FinderChoice>,

#[clap(subcommand)]
#[command(subcommand)]
pub cmd: Option<Command>,
}

Expand All @@ -100,13 +99,13 @@ pub enum Command {
#[cfg(not(feature = "disable-repo-management"))]
Repo(commands::repo::Input),
/// Used for fzf's preview window when selecting snippets
#[clap(setting = AppSettings::Hidden)]
#[command(hide = true)]
Preview(commands::preview::Input),
/// Used for fzf's preview window when selecting variable suggestions
#[clap(setting = AppSettings::Hidden)]
#[command(hide = true)]
PreviewVar(commands::preview::var::Input),
/// Used for fzf's preview window when selecting variable suggestions
#[clap(setting = AppSettings::Hidden)]
#[command(hide = true)]
PreviewVarStdin(commands::preview::var_stdin::Input),
/// Outputs shell widget source code
Widget(commands::shell::Input),
Expand Down
12 changes: 2 additions & 10 deletions src/finder/mod.rs
Expand Up @@ -4,20 +4,19 @@ use std::io::Write;
use std::process::{self, Output};
use std::process::{Command, Stdio};
pub mod structures;
use clap::ValueEnum;
pub use post::process;
use structures::Opts;
use structures::SuggestionType;

mod post;

#[derive(Debug, Clone, Copy, Deserialize)]
#[derive(Debug, Clone, Copy, Deserialize, ValueEnum)]
pub enum FinderChoice {
Fzf,
Skim,
}

pub const POSSIBLE_VALUES: &[&str] = &["fzf", "skim"];

impl FromStr for FinderChoice {
type Err = &'static str;

Expand Down Expand Up @@ -190,10 +189,3 @@ impl FinderChoice {
Ok((output, return_value))
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(FinderChoice::from_str(v).is_ok())
}
}

0 comments on commit f0f8e83

Please sign in to comment.