Skip to main content
rework code
Source Link
thrig
  • 35.9k
  • 4
  • 70
  • 88

The eval line is doing several things. Maybe too many things. One approach would be to separate out the zoxide call, and to check whether it has returned something (or. Another need would be to check its exit status word; I'm not sure what the output ofwhether zoxide~/.rangerdir will be on a good versushas a bad rundirectory (or something) in it, and to so assume hereempty that an empty string is bad)file after use. These new checks create multiple points the function must cleanup and exit from:

zoxide_to_rangerfunction ()zoxide_to_ranger { local zout  zz; zout=$zz=$(zoxide query -i) #[[ no-z result?$zz no]] ranger&& call{ zle reset-prompt; return }  [[# -znot "$zout"sure ]]if &&the return'eval' 1is necessary; does ranger ever exit with eval# 'rangershell "$zout"commands ...to be run? 

Another approach would involve checking that ~/.rangerdir has a directory (or something) in it, and to empty the file after use. This will better avoid a chdir into some random old directory from a previous run, or an edge case where ranger fails and has never put anything into the file:

 localranger ldir=$(<$zz ~--choosedir=~/.rangerdir)  #< no$TTY  directory? nolocal chdirdd; dd=$(nor LASTDIR< clobber~/.rangerdir) [[ -z "$ldir"$dd ]] && { zle reset-prompt; return 1} LASTDIR=$ldir:> ~/.rangerdir  cd# "$LASTDIR"this ...could be "local" unless you use it elsewhere ...LASTDIR=$dd  cd $LASTDIR || { zle reset-prompt prompt; return #}  cleanup afterlocal thisprecmd; runfor (emptyprecmd thein file)$precmd_functions; $precmd :>zle ~/.rangerdirreset-prompt } 

I would probably use both approaches to better cleanup and better check that zoxide was not aborted from.

The eval line is doing several things. Maybe too many things. One approach would be to separate out the zoxide call, and to check whether it has returned something (or to check its exit status word; I'm not sure what the output of zoxide will be on a good versus a bad run, so assume here that an empty string is bad):

zoxide_to_ranger () { local zout  zout=$(zoxide query -i) # no result? no ranger call [[ -z "$zout" ]] && return 1 eval 'ranger "$zout" ... 

Another approach would involve checking that ~/.rangerdir has a directory (or something) in it, and to empty the file after use. This will better avoid a chdir into some random old directory from a previous run, or an edge case where ranger fails and has never put anything into the file:

 local ldir=$(< ~/.rangerdir)  # no directory? no chdir (nor LASTDIR clobber) [[ -z "$ldir" ]] && return 1 LASTDIR=$ldir cd "$LASTDIR" ... ... zle reset-prompt  # cleanup after this run (empty the file) :> ~/.rangerdir } 

I would probably use both approaches to better cleanup and better check that zoxide was not aborted from.

The eval line is doing several things. Maybe too many things. One approach would be to separate out the zoxide call, and to check whether it has returned something. Another need would be to check whether ~/.rangerdir has a directory (or something) in it, and to empty that file after use. These new checks create multiple points the function must cleanup and exit from:

function zoxide_to_ranger { local zz; zz=$(zoxide query -i) [[ -z $zz ]] && { zle reset-prompt; return }  # not sure if the 'eval' is necessary; does ranger ever exit with # shell commands to be run? ranger $zz --choosedir=~/.rangerdir < $TTY  local dd; dd=$(< ~/.rangerdir) [[ -z $dd ]] && { zle reset-prompt; return } :> ~/.rangerdir  # this could be "local" unless you use it elsewhere LASTDIR=$dd  cd $LASTDIR || { zle reset-prompt; return }  local precmd; for precmd in $precmd_functions; $precmd zle reset-prompt } 
Source Link
thrig
  • 35.9k
  • 4
  • 70
  • 88

The eval line is doing several things. Maybe too many things. One approach would be to separate out the zoxide call, and to check whether it has returned something (or to check its exit status word; I'm not sure what the output of zoxide will be on a good versus a bad run, so assume here that an empty string is bad):

zoxide_to_ranger () { local zout zout=$(zoxide query -i) # no result? no ranger call [[ -z "$zout" ]] && return 1 eval 'ranger "$zout" ... 

Another approach would involve checking that ~/.rangerdir has a directory (or something) in it, and to empty the file after use. This will better avoid a chdir into some random old directory from a previous run, or an edge case where ranger fails and has never put anything into the file:

 local ldir=$(< ~/.rangerdir) # no directory? no chdir (nor LASTDIR clobber) [[ -z "$ldir" ]] && return 1 LASTDIR=$ldir cd "$LASTDIR" ... ... zle reset-prompt # cleanup after this run (empty the file) :> ~/.rangerdir } 

I would probably use both approaches to better cleanup and better check that zoxide was not aborted from.