From 638d804613e36462229a245830d3eae47a630a28 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 5 Apr 2021 21:58:06 +0530 Subject: [PATCH] Add support for Elvish --- CHANGELOG.md | 5 +++ shell.nix | 1 + src/cmd/init.rs | 2 ++ src/shell.rs | 32 ++++++++++++++--- templates/bash.txt | 20 +++++------ templates/elvish.txt | 78 ++++++++++++++++++++++++++++++++++++++++ templates/fish.txt | 16 ++++----- templates/nushell.txt | 14 ++++---- templates/posix.txt | 22 ++++++------ templates/powershell.txt | 18 +++++----- templates/xonsh.txt | 18 +++++----- templates/zsh.txt | 18 +++++----- 12 files changed, 177 insertions(+), 67 deletions(-) create mode 100644 templates/elvish.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index c6b60ec..88c6dbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Support for [Nushell](https://www.nushell.sh/). +- Support for [Elvish](https://elv.sh/). + +### Changed + +- `z` now excludes the current directory from search results. ### Fixed diff --git a/shell.nix b/shell.nix index 0536653..0d6e86a 100644 --- a/shell.nix +++ b/shell.nix @@ -6,6 +6,7 @@ in pkgs.mkShell { buildInputs = [ pkgs-master.cargo-audit + pkgs-master.elvish pkgs-master.nushell pkgs-python pkgs.bash diff --git a/src/cmd/init.rs b/src/cmd/init.rs index d425a7c..23f3406 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -50,6 +50,7 @@ impl Cmd 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(), @@ -65,6 +66,7 @@ impl Cmd for Init { #[derive(ArgEnum, Debug)] enum Shell { Bash, + Elvish, Fish, Nushell, Posix, diff --git a/src/shell.rs b/src/shell.rs index 7efe609..3e8660c 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -24,6 +24,7 @@ macro_rules! make_template { } make_template!(Bash, "bash.txt"); +make_template!(Elvish, "elvish.txt"); make_template!(Fish, "fish.txt"); make_template!(Nushell, "nushell.txt"); make_template!(Posix, "posix.txt"); @@ -123,8 +124,7 @@ mod tests { let opts = dbg!(&opts()[i]); let mut source = Bash(opts).render().unwrap(); source.push('\n'); - // FIXME: caused by - let source = source.as_str().trim_start(); + Command::new("shfmt") .args(&["-d", "-s", "-ln", "bash", "-i", "4", "-ci", "-"]) .write_stdin(source) @@ -134,6 +134,31 @@ mod tests { .stderr(""); } + #[test] + fn elvish_elvish_#i() { + let opts = dbg!(&opts()[i]); + let mut source = String::new(); + + // Filter out lines using edit:add-var, since that function + // is only available in the interactive editor. + for line in Elvish(opts) + .render() + .unwrap() + .split('\n') + .filter(|line| !line.starts_with("edit:add-var")) + { + source.push_str(line); + source.push('\n'); + } + + Command::new("elvish") + .args(&["-c", &source, "-norc"]) + .assert() + .success() + .stdout("") + .stderr(""); + } + #[test] fn fish_fish_#i() { let opts = dbg!(&opts()[i]); @@ -238,8 +263,7 @@ mod tests { let opts = dbg!(&opts()[i]); let mut source = Posix(opts).render().unwrap(); source.push('\n'); - // FIXME: caused by - let source = source.as_str().trim_start(); + Command::new("shfmt") .args(&["-d", "-s", "-ln", "posix", "-i", "4", "-ci", "-"]) .write_stdin(source) diff --git a/templates/bash.txt b/templates/bash.txt index 791f401..151fe9a 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -20,14 +20,14 @@ function __zoxide_cd() { \builtin cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %} } -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # # Hook to add new entries to the database. {%- match hook %} {%- when Hook::None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- when Hook::Prompt %} function __zoxide_hook() { @@ -50,13 +50,13 @@ function __zoxide_hook() { if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' {%- if hook == Hook::None %} - {{ NOT_CONFIGURED }} + {{ not_configured }} {%- else %} PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}" {%- endif %} fi -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -87,7 +87,7 @@ function __zoxide_zi() { __zoxide_result="$(zoxide query -i -- "$@")" && __zoxide_cd "${__zoxide_result}" } -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -113,11 +113,11 @@ function {{cmd}}i() { } {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with bash, add the following line to your bash # configuration file (usually ~/.bashrc): # diff --git a/templates/elvish.txt b/templates/elvish.txt new file mode 100644 index 0000000..5cda349 --- /dev/null +++ b/templates/elvish.txt @@ -0,0 +1,78 @@ +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} + +use builtin +use path + +{{ section }} +# Utility functions for zoxide. +# + +# cd + custom logic based on the value of _ZO_ECHO. +fn __zoxide_cd [path]{ + builtin:cd $path +{%- if echo %} + builtin:echo $pwd +{%- endif %} +} + +{{ section }} +# Hook configuration for zoxide. +# + +if (not (and (builtin:has-env __zoxide_hooked) (builtin:eq (builtin:get-env __zoxide_hooked) 1))) { + builtin:set-env __zoxide_hooked 1 + + # Initialize hook to track previous directory. + builtin:set-env __zoxide_oldpwd $pwd + before-chdir = [$@before-chdir [_]{ builtin:set-env __zoxide_oldpwd $pwd }] + + # edit:before-readline = [{ echo 'going to read' }] + # after-chdir = [[dir]{ zoxide add $pwd }] +} + +{{ section }} +# When using zoxide with --no-aliases, alias these internal functions as +# desired. +# + +# Jump to a directory using only keywords. +fn __zoxide_z [@rest]{ + if (builtin:eq [] $rest) { + __zoxide_cd ~ + } elif (builtin:eq [-] $rest) { + __zoxide_cd (builtin:get-env __zoxide_oldpwd) + } elif (and (builtin:eq (builtin:count $rest) 1) (path:is-dir $rest[0])) { + __zoxide_cd $rest[0] + } else { + __zoxide_cd (zoxide query --exclude $pwd -- $@rest) + } +} +edit:add-var __zoxide_z~ $__zoxide_z~ + +# Jump to a directory using interactive search. +fn __zoxide_zi [@rest]{ + __zoxide_cd (zoxide query -i -- $@rest) +} +edit:add-var __zoxide_zi~ $__zoxide_zi~ + +{{ section }} +# Convenient aliases for zoxide. Disable these using --no-aliases. +# + +{%- match cmd %} +{%- when Some with (cmd) %} + +edit:add-var z~ $__zoxide_z~ +edit:add-var zi~ $__zoxide_zi~ + +{%- when None %} +{{ not_configured }} + +{%- endmatch %} + +{{ section }} +# To initialize zoxide with xonsh, add the following line to your elvish +# configuration file (usually ~/.elvish/rc.elv): +# +# eval $(zoxide init elvish | slurp) diff --git a/templates/fish.txt b/templates/fish.txt index 3d0e8d3..388f089 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -25,7 +25,7 @@ function __zoxide_cd and builtin commandline -f repaint end -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # @@ -44,7 +44,7 @@ if test "$__zoxide_hooked" != 1 end end -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -74,7 +74,7 @@ function __zoxide_zi and __zoxide_cd $__zoxide_result end -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -99,11 +99,11 @@ function {{cmd}}i end {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with fish, add the following line to your fish # configuration file (usually ~/.config/fish/config.fish): # diff --git a/templates/nushell.txt b/templates/nushell.txt index 5309c71..36d59de 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # @@ -22,7 +22,7 @@ printf "zoxide: PWD hooks are not supported on Nushell.\n Use 'zoxide ini {%- endmatch %} -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -62,7 +62,7 @@ def __zoxide_zi [...rest:string] { {%- endif %} } -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -73,11 +73,11 @@ alias {{cmd}} = __zoxide_z '' alias {{cmd}}i = __zoxide_zi '' {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with Nushell: # # Initialize zoxide's Nushell script: diff --git a/templates/posix.txt b/templates/posix.txt index f91b8f7..288c85b 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -20,14 +20,14 @@ __zoxide_cd() { \cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %} } -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # # Hook to add new entries to the database. {%- match hook %} {%- when Hook::None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- when Hook::Prompt %} __zoxide_hook() { @@ -35,7 +35,7 @@ __zoxide_hook() { } {%- when Hook::Pwd %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} @@ -44,7 +44,7 @@ if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' {%- match hook %} {%- when Hook::None %} - {{ NOT_CONFIGURED }} + {{ not_configured }} {%- when Hook::Prompt %} PS1="${PS1}\$(__zoxide_hook)" {%- when Hook::Pwd %} @@ -54,7 +54,7 @@ if [ "${__zoxide_hooked}" != '1' ]; then {%- endmatch %} fi -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -83,7 +83,7 @@ __zoxide_zi() { __zoxide_result="$(zoxide query -i -- "$@")" && __zoxide_cd "${__zoxide_result}" } -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -109,11 +109,11 @@ __zoxide_unset '{{cmd}}i' } {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with your POSIX shell, add the following line to your # shell configuration file: # diff --git a/templates/powershell.txt b/templates/powershell.txt index 6d31fce..c89e36a 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -18,7 +18,7 @@ function __zoxide_cd($dir) { {%- endif %} } -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # @@ -32,7 +32,7 @@ if ($__zoxide_hooked -ne '1') { $__zoxide_hooked = '1' {%- match hook %} {%- when Hook::None %} - {{ NOT_CONFIGURED }} + {{ not_configured }} {%- when Hook::Prompt %} $__zoxide_prompt_old = $function:prompt function prompt { @@ -52,7 +52,7 @@ if ($__zoxide_hooked -ne '1') { {%- endmatch %} } -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -84,7 +84,7 @@ function __zoxide_zi { } } -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -95,11 +95,11 @@ Set-Alias {{cmd}} __zoxide_z Set-Alias {{cmd}}i __zoxide_zi {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with powershell, add the following line to your # powershell configuration file (the location is stored in $profile): # diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 7e44dd4..762f28d 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -1,5 +1,5 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} """Initialize zoxide on Xonsh.""" @@ -19,7 +19,7 @@ from typing import AnyStr, List, Optional import xonsh.dirstack # type: ignore # pylint: disable=import-error import xonsh.environ # type: ignore # pylint: disable=import-error -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -80,7 +80,7 @@ def __zoxide_errhandler(func): return wrapper -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # @@ -90,7 +90,7 @@ if globals().get("__zoxide_hooked") is not True: {% match hook -%} {%- when Hook::None -%} - {{ NOT_CONFIGURED }} + {{ not_configured }} {%- when Hook::Prompt -%} @events.on_post_prompt # type: ignore # pylint:disable=undefined-variable {%- when Hook::Pwd -%} @@ -103,7 +103,7 @@ if globals().get("__zoxide_hooked") is not True: subprocess.run([zoxide, "add", "--", pwd], check=False) -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -147,7 +147,7 @@ def __zoxide_zi(args: List[str]): __zoxide_cd(__zoxide_result) -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -158,11 +158,11 @@ aliases["{{cmd}}"] = __zoxide_z aliases["{{cmd}}i"] = __zoxide_zi {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with xonsh, add the following line to your xonsh # configuration file (usually ~/.xonshrc): # diff --git a/templates/zsh.txt b/templates/zsh.txt index aed3ce2..0efea68 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -1,7 +1,7 @@ -{%- let SECTION = "# =============================================================================\n#" -%} -{%- let NOT_CONFIGURED = "# -- not configured --" -%} +{%- let section = "# =============================================================================\n#" -%} +{%- let not_configured = "# -- not configured --" -%} -{{ SECTION }} +{{ section }} # Utility functions for zoxide. # @@ -20,7 +20,7 @@ function __zoxide_cd() { \builtin cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %} } -{{ SECTION }} +{{ section }} # Hook configuration for zoxide. # @@ -34,7 +34,7 @@ if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' {%- match hook %} {%- when Hook::None %} - {{ NOT_CONFIGURED }} + {{ not_configured }} {%- when Hook::Prompt %} precmd_functions+=(__zoxide_hook) {%- when Hook::Pwd %} @@ -42,7 +42,7 @@ if [ "${__zoxide_hooked}" != '1' ]; then {%- endmatch %} fi -{{ SECTION }} +{{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. # @@ -74,7 +74,7 @@ function __zoxide_zi() { __zoxide_result="$(zoxide query -i -- "$@")" && __zoxide_cd "${__zoxide_result}" } -{{ SECTION }} +{{ section }} # Convenient aliases for zoxide. Disable these using --no-aliases. # @@ -99,11 +99,11 @@ function {{cmd}}i() { } {%- when None %} -{{ NOT_CONFIGURED }} +{{ not_configured }} {%- endmatch %} -{{ SECTION }} +{{ section }} # To initialize zoxide with zsh, add the following line to your zsh # configuration file (usually ~/.zshrc): #