fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Nahorcolor: prefer `set_color --reset` over `set_color normal`867946468

master
3.1 KiB88 linesraw
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.
8    if set -q argv[1]
9        cd $argv
10        return
11    end
12
13    if set -q argv[2]
14        echo (_ "cdh: Expected zero or one arguments") >&2
15        return 1
16    end
17
18    set -l all_dirs $dirprev $dirnext
19    if not set -q all_dirs[1]
20        echo (_ 'No previous directories to select. You have to cd at least once.') >&2
21        return 0
22    end
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.
27    set -l uniq_dirs
28    for dir in $all_dirs[-1..1]
29        if not contains $dir $uniq_dirs
30            set -a uniq_dirs $dir
31        end
32    end
33
34    set -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
35    set -l dirc (count $uniq_dirs)
36    if test $dirc -gt (count $letters)
37        set -l msg (_ 'This should not happen. Have you changed the cd function?')
38        printf "$msg\n" >&2
39        set -l msg (_ 'There are %s unique dirs in your history but I can only handle %s')
40        printf "$msg\n" $dirc (count $letters) >&2
41        return 1
42    end
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.
47    for i in (seq $dirc -1 1)
48        set -l dir $uniq_dirs[$i]
49        set -l label_color --reset
50        set -q fish_color_cwd
51        and set label_color $fish_color_cwd
52        set -l dir_color_reset (set_color --reset)
53        set -l dir_color
54        if test "$dir" = "$PWD"
55            set dir_color (set_color $fish_color_history_current)
56        end
57
58        set -l home_dir (string match -r "^$HOME(/.*|\$)" "$dir")
59        if set -q home_dir[2]
60            set dir "~$home_dir[2]"
61        end
62        printf '%s %s %2d) %s %s%s%s\n' (set_color $label_color) $letters[$i] $i (set_color --reset) $dir_color $dir $dir_color_reset
63    end
64
65    # Ask the user which directory from their history they want to cd to.
66    set -l msg (_ 'Select directory by letter or number: ')
67    read -l -p "echo '$msg'" choice
68    if test -z "$choice"
69        return 0
70    else if string match -q -r '^[a-z]$' $choice
71        # Convert the letter to an index number.
72        set choice (contains -i $choice $letters)
73    end
74
75    set -l msg (_ 'Error: expected a number between 1 and %d or letter in that range, got "%s"')
76    if string match -q -r '^\d+$' $choice
77        if test $choice -ge 1 -a $choice -le $dirc
78            cd $uniq_dirs[$choice]
79            return
80        else
81            printf "$msg\n" $dirc $choice >&2
82            return 1
83        end
84    else
85        printf "$msg\n" $dirc $choice >&2
86        return 1
87    end
88end