broot icon indicating copy to clipboard operation
broot copied to clipboard

Suggestion: alternative zsh `br` function

Open baodrate opened this issue 3 years ago • 9 comments

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.

baodrate avatar Jun 29 '22 17:06 baodrate

Any other opinion on that ?

Canop avatar Jun 29 '22 17:06 Canop

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

AndydeCleyre avatar Jul 15 '22 15:07 AndydeCleyre

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

baodrate avatar Jul 15 '22 17:07 baodrate

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

AndydeCleyre avatar Mar 28 '24 19:03 AndydeCleyre