zoxide/src/cmd/cmd.rs

187 lines
4.1 KiB
Rust
Raw Normal View History

2022-09-16 16:54:46 +00:00
#![allow(clippy::module_inception)]
2022-09-06 22:49:45 +00:00
use std::path::PathBuf;
2021-09-13 08:01:58 +00:00
2023-01-07 17:28:10 +00:00
use clap::{Parser, Subcommand, ValueEnum, ValueHint};
2022-09-16 16:54:46 +00:00
2023-06-10 19:37:07 +00:00
const HELP_TEMPLATE: &str = color_print::cstr!("\
{before-help}<bold><underline>{name} {version}</underline></bold>
{author}
https://github.com/ajeetdsouza/zoxide
{about}
{usage-heading}
{tab}{usage}
{all-args}{after-help}
<bold><underline>Environment variables:</underline></bold>
2023-06-10 19:37:07 +00:00
{tab}<bold>_ZO_DATA_DIR</bold> {tab}Path for zoxide data files
{tab}<bold>_ZO_ECHO</bold> {tab}Print the matched directory before navigating to it when set to 1
{tab}<bold>_ZO_EXCLUDE_DIRS</bold> {tab}List of directory globs to be excluded
{tab}<bold>_ZO_FZF_OPTS</bold> {tab}Custom flags to pass to fzf
{tab}<bold>_ZO_MAXAGE</bold> {tab}Maximum total age after which entries start getting deleted
{tab}<bold>_ZO_RESOLVE_SYMLINKS</bold>{tab}Resolve symlinks when storing paths");
2021-05-03 21:03:23 +00:00
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
2021-05-03 21:03:23 +00:00
#[clap(
about,
author,
help_template = HELP_TEMPLATE,
2022-02-16 22:46:45 +00:00
disable_help_subcommand = true,
propagate_version = true,
2023-06-10 19:37:07 +00:00
version,
2021-05-03 21:03:23 +00:00
)]
2022-02-16 22:46:45 +00:00
pub enum Cmd {
2021-05-03 21:03:23 +00:00
Add(Add),
2023-01-07 17:28:10 +00:00
Edit(Edit),
2021-05-03 21:03:23 +00:00
Import(Import),
Init(Init),
Query(Query),
Remove(Remove),
}
/// Add a new directory or increment its rank
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2021-05-03 21:03:23 +00:00
pub struct Add {
#[clap(num_args = 1.., required = true, value_hint = ValueHint::DirPath)]
pub paths: Vec<PathBuf>,
2021-05-03 21:03:23 +00:00
}
2023-01-07 17:28:10 +00:00
/// Edit the database
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2023-01-07 17:28:10 +00:00
pub struct Edit {
#[clap(subcommand)]
pub cmd: Option<EditCommand>,
}
#[derive(Clone, Debug, Subcommand)]
pub enum EditCommand {
#[clap(hide = true)]
Decrement { path: String },
#[clap(hide = true)]
Delete { path: String },
#[clap(hide = true)]
Increment { path: String },
#[clap(hide = true)]
Reload,
}
2021-05-03 21:03:23 +00:00
/// Import entries from another application
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2021-05-03 21:03:23 +00:00
pub struct Import {
2021-05-19 21:24:06 +00:00
#[clap(value_hint = ValueHint::FilePath)]
2021-05-03 21:03:23 +00:00
pub path: PathBuf,
/// Application to import from
#[clap(value_enum, long)]
2021-05-03 21:12:43 +00:00
pub from: ImportFrom,
2021-05-03 21:03:23 +00:00
/// Merge into existing database
#[clap(long)]
pub merge: bool,
}
#[derive(ValueEnum, Clone, Debug)]
2021-05-03 21:12:43 +00:00
pub enum ImportFrom {
2021-05-03 21:03:23 +00:00
Autojump,
2023-02-18 05:06:19 +00:00
#[clap(alias = "fasd")]
2021-05-03 21:03:23 +00:00
Z,
}
/// Generate shell configuration
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2021-05-03 21:03:23 +00:00
pub struct Init {
#[clap(value_enum)]
2021-05-03 21:12:43 +00:00
pub shell: InitShell,
2021-05-03 21:03:23 +00:00
2022-04-10 22:11:51 +00:00
/// Prevents zoxide from defining the `z` and `zi` commands
#[clap(long, alias = "no-aliases")]
pub no_cmd: bool,
2021-05-03 21:03:23 +00:00
2022-04-10 22:11:51 +00:00
/// Changes the prefix of the `z` and `zi` commands
2021-05-03 21:03:23 +00:00
#[clap(long, default_value = "z")]
pub cmd: String,
2022-04-10 22:11:51 +00:00
/// Changes how often zoxide increments a directory's score
#[clap(value_enum, long, default_value = "pwd")]
2021-05-03 21:12:43 +00:00
pub hook: InitHook,
}
#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
2021-05-03 21:12:43 +00:00
pub enum InitHook {
None,
Prompt,
Pwd,
2021-05-03 21:03:23 +00:00
}
#[derive(ValueEnum, Clone, Debug)]
2021-05-03 21:12:43 +00:00
pub enum InitShell {
2021-05-03 21:03:23 +00:00
Bash,
Elvish,
Fish,
Nushell,
Posix,
Powershell,
Xonsh,
Zsh,
}
/// Search for a directory in the database
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2021-05-03 21:03:23 +00:00
pub struct Query {
pub keywords: Vec<String>,
2023-05-08 19:21:26 +00:00
/// Show unavailable directories
#[clap(long, short)]
2021-05-08 03:05:34 +00:00
pub all: bool,
2021-05-03 21:03:23 +00:00
/// Use interactive selection
#[clap(long, short, conflicts_with = "list")]
pub interactive: bool,
/// List all matching directories
#[clap(long, short, conflicts_with = "interactive")]
pub list: bool,
/// Print score with results
#[clap(long, short)]
2021-05-03 21:03:23 +00:00
pub score: bool,
/// Exclude the current directory
2021-05-19 21:24:06 +00:00
#[clap(long, value_hint = ValueHint::DirPath, value_name = "path")]
2021-05-03 21:03:23 +00:00
pub exclude: Option<String>,
}
/// Remove a directory from the database
2021-10-20 18:31:06 +00:00
#[derive(Debug, Parser)]
#[clap(
author,
help_template = HELP_TEMPLATE,
)]
2021-05-03 21:03:23 +00:00
pub struct Remove {
2021-12-24 12:19:36 +00:00
#[clap(value_hint = ValueHint::DirPath)]
pub paths: Vec<String>,
2021-05-03 21:03:23 +00:00
}