fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
867946468
master
1# localization: tier1 2# Provide a menu of the directories recently navigated to and ask the user to 3# choose one to make the new current working directory (cwd). 4 5function cdh --description"Menu based cd command" 6# See if we've been invoked with an argument. Presumably from the `cdh` completion script. 7# If we have just treat it as `cd` to the specified directory. 8if set -q argv[ 1 ] 9cd $argv 10return 11end 12 13if set -q argv[ 2 ] 14echo ( _ "cdh: Expected zero or one arguments" ) >&2 15return 1 16end 17 18set -l all_dirs$dirprev $dirnext 19if not set -q all_dirs[ 1 ] 20echo ( _ 'No previous directories to select. You have to cd at least once.' ) >&2 21return 0 22end 23 24# Reverse the directories so the most recently visited is first in the list. 25# Also, eliminate duplicates; i.e., we only want the most recent visit to a 26# given directory in the selection list. 27set -l uniq_dirs 28for dirin $all_dirs [ -1 .. 1 ] 29if not contains $dir $uniq_dirs 30set -a uniq_dirs$dir 31end 32end 33 34set -l letters a b c d e f g h i j k l m n o p q r s t u v w x y z 35set -l dirc( count $uniq_dirs ) 36if test $dirc -gt ( count $letters ) 37set -l msg( _ 'This should not happen. Have you changed the cd function?' ) 38printf " $msg \n" >&2 39set -l msg( _ 'There are %s unique dirs in your history but I can only handle %s' ) 40printf " $msg \n" $dirc ( count $letters ) >&2 41return 1 42end 43 44# Print the recent directories, oldest to newest. Since we previously 45# reversed the list, making the newest entry the first item in the array, 46# we count down rather than up. 47for iin ( seq $dirc -1 1 ) 48set -l dir$uniq_dirs [ $i ] 49set -l label_color --reset 50set -q fish_color_cwd 51and set label_color$fish_color_cwd 52set -l dir_color_reset( set_color --reset) 53set -l dir_color 54if test " $dir " = " $PWD " 55set dir_color( set_color $fish_color_history_current ) 56end 57 58set -l home_dir( string match -r"^ $HOME (/.*|\$)" " $dir " ) 59if set -q home_dir[ 2 ] 60set dir"~ $home_dir [ 2 ] " 61end 62printf '%s %s %2d) %s %s%s%s\n' ( set_color $label_color ) $letters [ $i ] $i ( set_color --reset) $dir_color $dir $dir_color_reset 63end 64 65# Ask the user which directory from their history they want to cd to. 66set -l msg( _ 'Select directory by letter or number: ' ) 67read -l -p"echo ' $msg '" choice 68if test -z " $choice " 69return 0 70else if string match -q -r'^[a-z]$' $choice 71# Convert the letter to an index number. 72set choice( contains -i$choice $letters ) 73end 74 75set -l msg( _ 'Error: expected a number between 1 and %d or letter in that range, got "%s"' ) 76if string match -q -r'^\d+$' $choice 77if test $choice -ge 1 -a $choice -le $dirc 78cd $uniq_dirs [ $choice ] 79return 80else 81printf " $msg \n" $dirc $choice >&2 82return 1 83end 84else 85printf " $msg \n" $dirc $choice >&2 86return 1 87end 88end