Canop / broot

A new way to see and navigate directory trees : https://dystroy.org/broot
MIT License
10.73k stars 236 forks source link

Suggestion: alternative zsh `br` function #569

Open baodrate opened 2 years ago

baodrate commented 2 years ago

Since there's already functionality to handle different shells, I'm suggesting an alternative br launcher function to be installed for zsh. ATM, zsh shares the br shell function defined for bash:

https://github.com/Canop/broot/blob/f9fe6efebf3ce4e32d93705db8decc2019f7112f/src/shell_install/bash.rs#L41-L53

an zsh-specific alternative could be:

function br {
    emulate -L zsh
    (){
        broot --outcmd $@ && source $1
    } =() $@
}

alternate formatting:

function br {
    emulate -L zsh
    (){ broot --outcmd $1 ${@:2} && source $1 } =() $@
}

which is (IMO) quite a bit cleaner. instead of relying on the system's mktemp (which technically isn't standardized, although that'll never likely be an issue) and manually managing the file, this uses zsh's temp-file process substitution, and the file will be removed by zsh at the end of the anonymous function.

Canop commented 2 years ago

Any other opinion on that ?

AndydeCleyre commented 2 years ago

Wow, that's short. This is what I've been using:

br () {  # [<broot-opt>...]
  emulate -L zsh

  local cmdfile=$(mktemp)
  trap "rm ${(q-)cmdfile}" EXIT INT QUIT
  if { broot --outcmd "$cmdfile" $@ } {
    if [[ -r $cmdfile ]]  eval "$(<$cmdfile)"
  } else {
    return
  }
}

EDIT: It's worth noting that I do experience #469

baodrate commented 2 years ago

@AndydeCleyre I can reproduce that issue with the rewritten function but I don't know how to solve it yet. At least my suggestion isn't any worse than the status quo 😅

AndydeCleyre commented 7 months ago

FWIW I'm now using the function in this comment.