mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-11-22 04:45:20 +00:00
Rename variables
This commit is contained in:
parent
650732c82c
commit
6d3cb4bc99
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -343,9 +343,9 @@ checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
|
||||
|
||||
[[package]]
|
||||
name = "ordered-float"
|
||||
version = "2.1.1"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "766f840da25490628d8e63e529cd21c014f6600c6b8517add12a6fa6167a6218"
|
||||
checksum = "b50b8919aecb97e5ee9aceef27e24f39c46b11831130f4a6b7b091ec5de0de12"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
40
build.rs
40
build.rs
@ -1,23 +1,6 @@
|
||||
use clap::IntoApp;
|
||||
use clap_generate::{generate_to, generators::*};
|
||||
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
||||
include!("src/app.rs");
|
||||
|
||||
fn completions() {
|
||||
let mut app = Cli::into_app();
|
||||
let bin_name = env!("CARGO_PKG_NAME");
|
||||
let out_dir = "contrib/completions";
|
||||
|
||||
generate_to::<Bash, _, _>(&mut app, bin_name, out_dir);
|
||||
generate_to::<Elvish, _, _>(&mut app, bin_name, out_dir);
|
||||
generate_to::<Fish, _, _>(&mut app, bin_name, out_dir);
|
||||
generate_to::<PowerShell, _, _>(&mut app, bin_name, out_dir);
|
||||
generate_to::<Zsh, _, _>(&mut app, bin_name, out_dir);
|
||||
}
|
||||
|
||||
fn git_version() -> Option<String> {
|
||||
// Packages releases of zoxide almost always use the source tarball
|
||||
// provided by GitHub, which does not include the `.git` folder. Since this
|
||||
@ -35,8 +18,29 @@ fn crate_version() -> String {
|
||||
format!("v{}", env::var("CARGO_PKG_VERSION").unwrap())
|
||||
}
|
||||
|
||||
fn generate_completions() {
|
||||
mod app {
|
||||
include!("src/app.rs");
|
||||
}
|
||||
|
||||
use app::App;
|
||||
use clap::IntoApp;
|
||||
use clap_generate::generate_to;
|
||||
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
|
||||
|
||||
let app = &mut App::into_app();
|
||||
let bin_name = &env::var("CARGO_PKG_NAME").unwrap();
|
||||
let out_dir = "contrib/completions";
|
||||
|
||||
generate_to::<Bash, _, _>(app, bin_name, out_dir);
|
||||
generate_to::<Elvish, _, _>(app, bin_name, out_dir);
|
||||
generate_to::<Fish, _, _>(app, bin_name, out_dir);
|
||||
generate_to::<PowerShell, _, _>(app, bin_name, out_dir);
|
||||
generate_to::<Zsh, _, _>(app, bin_name, out_dir);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let version = git_version().unwrap_or_else(crate_version);
|
||||
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version);
|
||||
completions();
|
||||
generate_completions();
|
||||
}
|
||||
|
8
contrib/completions/README.md
Normal file
8
contrib/completions/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# completions
|
||||
|
||||
Shell completions for zoxide, auto-generated by [`clap`][clap].
|
||||
|
||||
Since `clap` itself is in beta at the moment, these completions should not be
|
||||
treated as stable either.
|
||||
|
||||
[clap]: https://github.com/clap-rs/clap
|
28
src/app.rs
28
src/app.rs
@ -20,9 +20,9 @@ const ENV_HELP: &str = "ENVIRONMENT VARIABLES:
|
||||
global_setting(AppSettings::DisableHelpSubcommand),
|
||||
global_setting(AppSettings::GlobalVersion),
|
||||
global_setting(AppSettings::VersionlessSubcommands),
|
||||
version = option_env!("ZOXIDE_VERSION").unwrap_or("")
|
||||
version = option_env!("ZOXIDE_VERSION").unwrap_or_default()
|
||||
)]
|
||||
pub enum Cli {
|
||||
pub enum App {
|
||||
Add(Add),
|
||||
Import(Import),
|
||||
Init(Init),
|
||||
@ -43,7 +43,7 @@ pub struct Import {
|
||||
|
||||
/// Application to import from
|
||||
#[clap(arg_enum, long)]
|
||||
pub from: From,
|
||||
pub from: ImportFrom,
|
||||
|
||||
/// Merge into existing database
|
||||
#[clap(long)]
|
||||
@ -51,7 +51,7 @@ pub struct Import {
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Debug)]
|
||||
pub enum From {
|
||||
pub enum ImportFrom {
|
||||
Autojump,
|
||||
Z,
|
||||
}
|
||||
@ -60,7 +60,7 @@ pub enum From {
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Init {
|
||||
#[clap(arg_enum)]
|
||||
pub shell: Shell,
|
||||
pub shell: InitShell,
|
||||
|
||||
/// Prevents zoxide from defining any commands
|
||||
#[clap(long)]
|
||||
@ -72,11 +72,18 @@ pub struct Init {
|
||||
|
||||
/// Chooses event upon which an entry is added to the database
|
||||
#[clap(arg_enum, long, default_value = "pwd")]
|
||||
pub hook: Hook,
|
||||
pub hook: InitHook,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum InitHook {
|
||||
None,
|
||||
Prompt,
|
||||
Pwd,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Debug)]
|
||||
pub enum Shell {
|
||||
pub enum InitShell {
|
||||
Bash,
|
||||
Elvish,
|
||||
Fish,
|
||||
@ -87,13 +94,6 @@ pub enum Shell {
|
||||
Zsh,
|
||||
}
|
||||
|
||||
#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Hook {
|
||||
None,
|
||||
Prompt,
|
||||
Pwd,
|
||||
}
|
||||
|
||||
/// Search for a directory in the database
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Query {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use super::Run;
|
||||
use crate::app::{From, Import};
|
||||
use crate::app::{Import, ImportFrom};
|
||||
use crate::config;
|
||||
use crate::import::{Autojump, Import as _, Z};
|
||||
use crate::util;
|
||||
@ -19,12 +19,12 @@ impl Run for Import {
|
||||
|
||||
let resolve_symlinks = config::zo_resolve_symlinks();
|
||||
match self.from {
|
||||
From::Autojump => Autojump {
|
||||
ImportFrom::Autojump => Autojump {
|
||||
resolve_symlinks,
|
||||
now: util::current_time()?,
|
||||
}
|
||||
.import(&mut db, &self.path),
|
||||
From::Z => Z { resolve_symlinks }.import(&mut db, &self.path),
|
||||
ImportFrom::Z => Z { resolve_symlinks }.import(&mut db, &self.path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use super::Run;
|
||||
use crate::app::{Init, Shell};
|
||||
use crate::app::{Init, InitShell};
|
||||
use crate::config;
|
||||
use crate::error::WriteErrorHandler;
|
||||
use crate::shell::{self, Opts};
|
||||
@ -28,14 +28,14 @@ impl Run for Init {
|
||||
};
|
||||
|
||||
let source = match self.shell {
|
||||
Shell::Bash => shell::Bash(opts).render(),
|
||||
Shell::Elvish => shell::Elvish(opts).render(),
|
||||
Shell::Fish => shell::Fish(opts).render(),
|
||||
Shell::Nushell => shell::Nushell(opts).render(),
|
||||
Shell::Posix => shell::Posix(opts).render(),
|
||||
Shell::Powershell => shell::Powershell(opts).render(),
|
||||
Shell::Xonsh => shell::Xonsh(opts).render(),
|
||||
Shell::Zsh => shell::Zsh(opts).render(),
|
||||
InitShell::Bash => shell::Bash(opts).render(),
|
||||
InitShell::Elvish => shell::Elvish(opts).render(),
|
||||
InitShell::Fish => shell::Fish(opts).render(),
|
||||
InitShell::Nushell => shell::Nushell(opts).render(),
|
||||
InitShell::Posix => shell::Posix(opts).render(),
|
||||
InitShell::Powershell => shell::Powershell(opts).render(),
|
||||
InitShell::Xonsh => shell::Xonsh(opts).render(),
|
||||
InitShell::Zsh => shell::Zsh(opts).render(),
|
||||
}
|
||||
.context("could not render template")?;
|
||||
writeln!(io::stdout(), "{}", source).wrap_write("stdout")
|
||||
|
@ -4,7 +4,7 @@ mod init;
|
||||
mod query;
|
||||
mod remove;
|
||||
|
||||
use crate::app::Cli;
|
||||
use crate::app::App;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
@ -12,14 +12,14 @@ pub trait Run {
|
||||
fn run(&self) -> Result<()>;
|
||||
}
|
||||
|
||||
impl Run for Cli {
|
||||
impl Run for App {
|
||||
fn run(&self) -> Result<()> {
|
||||
match self {
|
||||
Cli::Add(cmd) => cmd.run(),
|
||||
Cli::Import(cmd) => cmd.run(),
|
||||
Cli::Init(cmd) => cmd.run(),
|
||||
Cli::Query(cmd) => cmd.run(),
|
||||
Cli::Remove(cmd) => cmd.run(),
|
||||
App::Add(cmd) => cmd.run(),
|
||||
App::Import(cmd) => cmd.run(),
|
||||
App::Init(cmd) => cmd.run(),
|
||||
App::Query(cmd) => cmd.run(),
|
||||
App::Remove(cmd) => cmd.run(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ mod import;
|
||||
mod shell;
|
||||
mod util;
|
||||
|
||||
use crate::app::Cli;
|
||||
use crate::app::App;
|
||||
use crate::cmd::Run;
|
||||
use crate::error::SilentExit;
|
||||
|
||||
@ -23,7 +23,7 @@ pub fn main() {
|
||||
env::remove_var("RUST_LIB_BACKTRACE");
|
||||
env::remove_var("RUST_BACKTRACE");
|
||||
|
||||
if let Err(e) = Cli::parse().run() {
|
||||
if let Err(e) = App::parse().run() {
|
||||
match e.downcast::<SilentExit>() {
|
||||
Ok(SilentExit { code }) => process::exit(code),
|
||||
Err(e) => {
|
||||
|
12
src/shell.rs
12
src/shell.rs
@ -1,9 +1,9 @@
|
||||
use crate::app::Hook;
|
||||
use crate::app::InitHook;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Opts<'a> {
|
||||
pub cmd: Option<&'a str>,
|
||||
pub(crate) hook: Hook,
|
||||
pub hook: InitHook,
|
||||
pub echo: bool,
|
||||
pub resolve_symlinks: bool,
|
||||
}
|
||||
@ -51,7 +51,7 @@ mod tests {
|
||||
fn opts() -> &'static [Opts<'static>] {
|
||||
static OPTS: OnceCell<Vec<Opts>> = OnceCell::new();
|
||||
const BOOLS: &[bool] = &[false, true];
|
||||
const HOOKS: &[Hook] = &[Hook::None, Hook::Prompt, Hook::Pwd];
|
||||
const HOOKS: &[InitHook] = &[InitHook::None, InitHook::Prompt, InitHook::Pwd];
|
||||
const CMDS: &[Option<&str>] = &[None, Some("z")];
|
||||
|
||||
OPTS.get_or_init(|| {
|
||||
@ -203,7 +203,7 @@ mod tests {
|
||||
.success()
|
||||
.stderr("");
|
||||
|
||||
if opts.hook != Hook::Pwd {
|
||||
if opts.hook != InitHook::Pwd {
|
||||
assert.stdout("");
|
||||
}
|
||||
}
|
||||
@ -218,7 +218,7 @@ mod tests {
|
||||
.success()
|
||||
.stderr("");
|
||||
|
||||
if opts.hook != Hook::Pwd {
|
||||
if opts.hook != InitHook::Pwd {
|
||||
assert.stdout("");
|
||||
}
|
||||
}
|
||||
@ -233,7 +233,7 @@ mod tests {
|
||||
.success()
|
||||
.stderr("");
|
||||
|
||||
if opts.hook != Hook::Pwd {
|
||||
if opts.hook != InitHook::Pwd {
|
||||
assert.stdout("");
|
||||
}
|
||||
}
|
||||
|
@ -30,17 +30,17 @@ function __zoxide_cd() {
|
||||
|
||||
# Hook to add new entries to the database.
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
function __zoxide_hook() {
|
||||
\builtin local -r __zoxide_retval="$?"
|
||||
zoxide add -- "$(__zoxide_pwd)"
|
||||
return "${__zoxide_retval}"
|
||||
}
|
||||
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
function __zoxide_hook() {
|
||||
\builtin local -r __zoxide_retval="$?"
|
||||
\builtin local -r __zoxide_pwd_tmp="$(__zoxide_pwd)"
|
||||
@ -62,7 +62,7 @@ function __zoxide_hook() {
|
||||
# Initialize hook.
|
||||
if [ "${__zoxide_hooked}" != '1' ]; then
|
||||
__zoxide_hooked='1'
|
||||
{%- if hook == Hook::None %}
|
||||
{%- if hook == InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- else %}
|
||||
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}"
|
||||
|
@ -29,11 +29,11 @@ if (not (and (builtin:has-env __zoxide_hooked) (builtin:eq (builtin:get-env __zo
|
||||
|
||||
# Initialize hook to add directories to zoxide.
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
edit:before-readline = [$@edit:before-readline []{ zoxide add -- $pwd }]
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
after-chdir = [$@after-chdir [_]{ zoxide add -- $pwd }]
|
||||
{%- endmatch %}
|
||||
}
|
||||
|
@ -34,11 +34,11 @@ end
|
||||
if test "$__zoxide_hooked" != 1
|
||||
set __zoxide_hooked 1
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
function __zoxide_hook
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
function __zoxide_hook --on-event fish_prompt
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
function __zoxide_hook --on-variable PWD
|
||||
{%- endmatch %}
|
||||
command zoxide add -- (__zoxide_pwd)
|
||||
|
@ -17,15 +17,15 @@ def __zoxide_prompt [] {
|
||||
|
||||
# Hook to add new entries to the database.
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
def __zoxide_hook [] {}
|
||||
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
def __zoxide_hook [] {
|
||||
shells | where active == $true && name == filesystem | get path | each { zoxide add -- $it }
|
||||
}
|
||||
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
def __zoxide_hook [] {}
|
||||
|
||||
printf "zoxide: PWD hooks are not supported on Nushell.\n Use 'zoxide init nushell --hook prompt' instead.\n"
|
||||
|
@ -26,15 +26,15 @@ __zoxide_cd() {
|
||||
|
||||
# Hook to add new entries to the database.
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
__zoxide_hook() {
|
||||
zoxide add -- "$(__zoxide_pwd)"
|
||||
}
|
||||
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
{{ not_configured }}
|
||||
|
||||
{%- endmatch %}
|
||||
@ -43,11 +43,11 @@ __zoxide_hook() {
|
||||
if [ "${__zoxide_hooked}" != '1' ]; then
|
||||
__zoxide_hooked='1'
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
PS1="${PS1}\$(__zoxide_hook)"
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
\printf "%s\n%s\n" \
|
||||
"zoxide: PWD hooks are not supported on POSIX shells." \
|
||||
" Use 'zoxide init posix --hook prompt' instead."
|
||||
|
@ -31,15 +31,15 @@ function __zoxide_hook {
|
||||
if ($__zoxide_hooked -ne '1') {
|
||||
$__zoxide_hooked = '1'
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
$__zoxide_prompt_old = $function:prompt
|
||||
function prompt {
|
||||
$null = __zoxide_hook
|
||||
& $__zoxide_prompt_old
|
||||
}
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
||||
$ExecutionContext.InvokeCommand.LocationChangedAction = {
|
||||
$null = __zoxide_hook
|
||||
|
@ -10,7 +10,7 @@ import sys
|
||||
{%- if cmd.is_some() %}
|
||||
from builtins import aliases # type: ignore # pylint: disable=no-name-in-module
|
||||
{%- endif %}
|
||||
{%- if hook != Hook::None %}
|
||||
{%- if hook != InitHook::None %}
|
||||
from builtins import events # type: ignore # pylint: disable=no-name-in-module
|
||||
{%- endif %}
|
||||
from subprocess import CalledProcessError
|
||||
@ -88,11 +88,11 @@ def __zoxide_errhandler(func):
|
||||
if globals().get("__zoxide_hooked") is not True:
|
||||
globals()["__zoxide_hooked"] = True
|
||||
{% match hook -%}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
@events.on_post_prompt # type: ignore # pylint:disable=undefined-variable
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
@events.on_chdir # type: ignore # pylint:disable=undefined-variable
|
||||
{%- endmatch %}
|
||||
def __zoxide_hook(**_kwargs):
|
||||
|
@ -33,11 +33,11 @@ function __zoxide_hook() {
|
||||
if [ "${__zoxide_hooked}" != '1' ]; then
|
||||
__zoxide_hooked='1'
|
||||
{%- match hook %}
|
||||
{%- when Hook::None %}
|
||||
{%- when InitHook::None %}
|
||||
{{ not_configured }}
|
||||
{%- when Hook::Prompt %}
|
||||
{%- when InitHook::Prompt %}
|
||||
precmd_functions+=(__zoxide_hook)
|
||||
{%- when Hook::Pwd %}
|
||||
{%- when InitHook::Pwd %}
|
||||
chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook")
|
||||
{%- endmatch %}
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user