Use POSIX implementation for ksh shells

Only ksh93 supports DEBUG traps, and the rest don't have any features
that can be used for setting up hooks. May as well use the POSIX
implementation for all ksh shells.
This commit is contained in:
Ajeet D'Souza 2025-01-09 02:19:20 +05:30
parent d99d82f141
commit 678bbdefbc
9 changed files with 4 additions and 183 deletions

View File

@ -242,21 +242,6 @@ zoxide can be installed in 4 easy steps:
</details>
<details>
<summary>Ksh</summary>
> Add this to the <ins>**end**</ins> of your config file (usually `~/.kshrc`):
>
> ```sh
> # ksh93
> eval "$(zoxide init ksh)"
>
> # mksh, oksh, etc.
> eval "$(zoxide init ksh --hook=prompt)"
> ```
</details>
<details>
<summary>Nushell</summary>

View File

@ -112,7 +112,7 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':shell:(bash elvish fish ksh nushell posix powershell xonsh zsh)' \
':shell:(bash elvish fish nushell posix powershell xonsh zsh)' \
&& ret=0
;;
(query)

View File

@ -165,7 +165,7 @@ _zoxide() {
return 0
;;
zoxide__init)
opts="-h -V --no-cmd --cmd --hook --help --version bash elvish fish ksh nushell posix powershell xonsh zsh"
opts="-h -V --no-cmd --cmd --hook --help --version bash elvish fish nushell posix powershell xonsh zsh"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0

View File

@ -182,7 +182,6 @@ const completion: Fig.Spec = {
"bash",
"elvish",
"fish",
"ksh",
"nushell",
"posix",
"powershell",

View File

@ -30,13 +30,6 @@ Add this to the \fBend\fR of your config file (usually
\fBzoxide init fish | source\fR
.fi
.TP
.B ksh
Add this to the \fBend\fR of your config file (usually \fB~/.kshrc\fR):
.sp
.nf
\fBeval $(zoxide init ksh)\fR
.fi
.TP
.B nushell
Add this to the \fBend\fR of your env file (find it by running
\fB$nu.env-path\fR in Nushell):

View File

@ -137,8 +137,8 @@ pub enum InitShell {
Bash,
Elvish,
Fish,
Ksh,
Nushell,
#[clap(alias = "ksh")]
Posix,
Powershell,
Xonsh,

View File

@ -6,7 +6,7 @@ use rinja::Template;
use crate::cmd::{Init, InitShell, Run};
use crate::config;
use crate::error::BrokenPipeHandler;
use crate::shell::{Bash, Elvish, Fish, Ksh, Nushell, Opts, Posix, Powershell, Xonsh, Zsh};
use crate::shell::{Bash, Elvish, Fish, Nushell, Opts, Posix, Powershell, Xonsh, Zsh};
impl Run for Init {
fn run(&self) -> Result<()> {
@ -19,7 +19,6 @@ impl Run for Init {
InitShell::Bash => Bash(opts).render(),
InitShell::Elvish => Elvish(opts).render(),
InitShell::Fish => Fish(opts).render(),
InitShell::Ksh => Ksh(opts).render(),
InitShell::Nushell => Nushell(opts).render(),
InitShell::Posix => Posix(opts).render(),
InitShell::Powershell => Powershell(opts).render(),

View File

@ -26,7 +26,6 @@ macro_rules! make_template {
make_template!(Bash, "bash.txt");
make_template!(Elvish, "elvish.txt");
make_template!(Fish, "fish.txt");
make_template!(Ksh, "ksh.txt");
make_template!(Nushell, "nushell.txt");
make_template!(Posix, "posix.txt");
make_template!(Powershell, "powershell.txt");
@ -159,42 +158,6 @@ mod tests {
.stderr("");
}
#[apply(opts)]
fn ksh_ksh(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Ksh(&opts).render().unwrap();
Command::new("ksh").args(["-n", "-c", &source]).assert().success().stdout("").stderr("");
}
#[apply(opts)]
fn ksh_shellcheck(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Ksh(&opts).render().unwrap();
Command::new("shellcheck")
.args(["--enable=all", "-"])
.write_stdin(source)
.assert()
.success()
.stdout("")
.stderr("");
}
#[apply(opts)]
fn ksh_shfmt(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let mut source = Ksh(&opts).render().unwrap();
source.push('\n');
Command::new("shfmt")
.args(["--diff", "--indent=4", "--language-dialect=mksh", "--simplify", "-"])
.write_stdin(source)
.assert()
.success()
.stdout("")
.stderr("");
}
#[apply(opts)]
fn nushell_nushell(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };

View File

@ -1,118 +0,0 @@
{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
# shellcheck shell=ksh
{{ section }}
# Utility functions for zoxide.
#
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
__zoxide_pwd() {
{%- if cfg!(windows) %}
\command cygpath -w "$(\builtin pwd -P)"
{%- else if resolve_symlinks %}
\command pwd -P
{%- else %}
\command pwd -L
{%- endif %}
}
# cd + custom logic based on the value of _ZO_ECHO.
__zoxide_cd() {
# shellcheck disable=SC2164
\command cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %}
}
{{ section }}
# Hook configuration for zoxide.
#
{% match hook %}
{%- when InitHook::None -%}
{{ not_configured }}
{%- when InitHook::Prompt -%}
# Hook to add new entries to the database.
__zoxide_hook() {
\command zoxide add -- "$(__zoxide_pwd || \builtin true)"
}
# Initialize hook.
if [[ ${PS1:=} == "${PS1#*\$(__zoxide_hook)}" ]]; then
PS1="${PS1}\$(__zoxide_hook)"
fi
{%- when InitHook::Pwd -%}
# Hook to add new entries to the database.
__zoxide_hook() {
__zoxide_retval="$?"
__zoxide_newpwd="$(__zoxide_pwd)"
if [[ ${__zoxide_oldpwd:-__zoxide_newpwd} != "${__zoxide_newpwd}" ]]; then
\command zoxide add -- "${__zoxide_newpwd}"
__zoxide_oldpwd="${__zoxide_newpwd}"
fi
return "${__zoxide_retval}"
}
# Initialize hook.
__zoxide_trap="$(\command trap -p DEBUG)"
if [[ ${__zoxide_trap} != *'__zoxide_hook'* ]]; then
\command trap "__zoxide_hook;${__zoxide_trap#';'}" DEBUG
fi
{%- endmatch %}
{{ section }}
# When using zoxide with --no-cmd, alias these internal functions as desired.
#
# Jump to a directory using only keywords.
__zoxide_z() {
# shellcheck disable=SC2199
if (($# == 0)); then
__zoxide_cd ~
elif [[ ($# == 1) && $1 == '-' ]]; then
__zoxide_cd "${OLDPWD}"
elif [[ ($# == 1) && -d $1 ]]; then
__zoxide_cd "$1"
elif [[ ($# == 2) && $1 == '--' ]]; then
__zoxide_cd "$2"
else
# shellcheck disable=SC2312
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${__zoxide_result}"
fi
}
# Jump to a directory using interactive search.
__zoxide_zi() {
__zoxide_result="$(\command zoxide query --interactive -- "$@")" && __zoxide_cd "${__zoxide_result}"
}
{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#
{%- match cmd %}
{%- when Some with (cmd) %}
\command unalias {{cmd}} >/dev/null 2>&1 || \true
{{cmd}}() {
__zoxide_z "$@"
}
\command unalias {{cmd}}i >/dev/null 2>&1 || \true
{{cmd}}i() {
__zoxide_zi "$@"
}
{%- when None %}
{{ not_configured }}
{%- endmatch %}
{{ section }}
# To initialize zoxide, add this to your configuration (usually ~/.kshrc):
#
# eval "$(zoxide init ksh)"