Browse Source

implement on all big shells woohoo

remotes/origin/clean-multishell-on-shell-exit
Gal Schlezinger 2 years ago
parent
commit
04fe8bde15
  1. 24
      src/shell/bash.rs
  2. 10
      src/shell/fish.rs
  3. 1
      src/shell/mod.rs
  4. 10
      src/shell/powershell.rs
  5. 31
      src/shell/trap_add_script.rs
  6. 10
      src/shell/zsh.rs

24
src/shell/bash.rs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
use crate::version_file_strategy::VersionFileStrategy;
use super::{shell::Shell, trap_add_script::TRAP_ADD};
use super::shell::Shell;
use indoc::{formatdoc, indoc};
use std::path::Path;
@ -64,3 +64,25 @@ impl Shell for Bash { @@ -64,3 +64,25 @@ impl Shell for Bash {
))
}
}
/// This code is based on Richard Hansen's answer on StackOverflow:
/// https://stackoverflow.com/a/7287873/1176984
///
/// Usage:
/// __fnm_trap_add__ 'echo "hello"' EXIT
pub const TRAP_ADD: &'static str = indoc::indoc!(
r#"
__fnm_trap_add__() {
__fnm_trap_add___cmd=$1; shift || fatal "${FUNCNAME} usage error"
for __fnm_trap_add___name in "$@"; do
trap -- "$(
extract_trap_cmd() { printf '%s\n' "$3"; }
eval "extract_trap_cmd $(trap -p "${__fnm_trap_add___name}")"
printf '%s\n' "${__fnm_trap_add___cmd}"
)" "${__fnm_trap_add___name}" \
|| fatal "unable to add to trap ${__fnm_trap_add___name}"
done
}
declare -f -t __fnm_trap_add__
"#
);

10
src/shell/fish.rs

@ -46,4 +46,14 @@ impl Shell for Fish { @@ -46,4 +46,14 @@ impl Shell for Fish {
autoload_hook = autoload_hook
))
}
fn delete_on_exit(&self, fnm_multishell: &Path) -> Option<String> {
Some(indoc::formatdoc! {r#"
function _fnm_delete_multishell --on-event fish_exit
rm -f "{fnm_multishell}"
end
_fnm_delete_multishell
"#, fnm_multishell = fnm_multishell.display()})
}
}

1
src/shell/mod.rs

@ -7,7 +7,6 @@ mod zsh; @@ -7,7 +7,6 @@ mod zsh;
#[allow(clippy::module_inception)]
mod shell;
mod trap_add_script;
pub use bash::Bash;
pub use fish::Fish;

10
src/shell/powershell.rs

@ -45,6 +45,16 @@ impl Shell for PowerShell { @@ -45,6 +45,16 @@ impl Shell for PowerShell {
autoload_hook = autoload_hook
))
}
fn delete_on_exit(&self, fnm_multishell: &Path) -> Option<String> {
Some(indoc::formatdoc!(
r#"
Register-EngineEvent PowerShell.Exiting -Action {{ rm "{fnm_multishell}" > $null }} > $null
"#,
fnm_multishell = fnm_multishell.display()
))
}
fn to_clap_shell(&self) -> clap_complete::Shell {
clap_complete::Shell::PowerShell
}

31
src/shell/trap_add_script.rs

@ -1,31 +0,0 @@ @@ -1,31 +0,0 @@
// This code is based on Richard Hansen's answer on StackOverflow:
// https://stackoverflow.com/a/7287873/1176984
pub const TRAP_ADD: &'static str = indoc::indoc!(
r#"
# appends a command to a trap
#
# - 1st arg: code to add
# - remaining args: names of traps to modify
#
__fnm_trap_add__() {
__fnm_trap_add___cmd=$1; shift || fatal "${FUNCNAME} usage error"
for __fnm_trap_add___name in "$@"; do
trap -- "$(
# helper fn to get existing trap command from output
# of trap -p
extract_trap_cmd() { printf '%s\n' "$3"; }
# print existing trap command with newline
eval "extract_trap_cmd $(trap -p "${__fnm_trap_add___name}")"
# print the new trap command
printf '%s\n' "${__fnm_trap_add___cmd}"
)" "${__fnm_trap_add___name}" \
|| fatal "unable to add to trap ${__fnm_trap_add___name}"
done
}
# set the trace attribute for the above function. this is
# required to modify DEBUG or RETURN traps because functions don't
# inherit them unless the trace attribute is set
declare -f -t __fnm_trap_add__
"#
);

10
src/shell/zsh.rs

@ -55,11 +55,15 @@ impl Shell for Zsh { @@ -55,11 +55,15 @@ impl Shell for Zsh {
fn delete_on_exit(&self, fnm_multishell: &Path) -> Option<String> {
Some(indoc::formatdoc!(
r#"
{TRAP_ADD}
__fnm_trap_add__ 'rm "{fnm_multishell}"' EXIT
autoload -U add-zsh-hook
_fnm_on_exit () {{
rm -f "{fnm_multishell}"
}}
add-zsh-hook zshexit _fnm_on_exit \
&& _fnm_on_exit
"#,
fnm_multishell = fnm_multishell.display(),
TRAP_ADD = super::trap_add_script::TRAP_ADD
))
}
}

Loading…
Cancel
Save