Add support for Elvish

This commit is contained in:
Ajeet D'Souza 2021-04-05 21:58:06 +05:30
parent 3398cc721d
commit 638d804613
12 changed files with 177 additions and 67 deletions

View File

@ -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

View File

@ -6,6 +6,7 @@ in
pkgs.mkShell {
buildInputs = [
pkgs-master.cargo-audit
pkgs-master.elvish
pkgs-master.nushell
pkgs-python
pkgs.bash

View File

@ -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,

View File

@ -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 <https://github.com/djc/askama/issues/377>
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 <https://github.com/djc/askama/issues/377>
let source = source.as_str().trim_start();
Command::new("shfmt")
.args(&["-d", "-s", "-ln", "posix", "-i", "4", "-ci", "-"])
.write_stdin(source)

View File

@ -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):
#

78
templates/elvish.txt Normal file
View File

@ -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)

View File

@ -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):
#

View File

@ -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:

View File

@ -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:
#

View File

@ -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):
#

View File

@ -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):
#

View File

@ -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):
#