fork/fish-shell

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

Johannes Altmanningerstatus get-file: simplify wrapperd19c92776

master
9.3 KiB240 linesraw
1# localization: tier1
2function help --description 'Show help for the fish shell'
3    set -l options h/help
4    argparse -n help $options -- $argv
5    or return
6
7    if set -q _flag_help
8        __fish_print_help help
9        return
10    end
11
12    set -l fish_help_item $argv[1]
13    if test (count $argv) -gt 1
14        if test $argv[1] = string
15            set fish_help_item (string join -- '-' $argv[1] $argv[2])
16        else
17            echo "help: Expected at most 1 args, got 2" >&2
18            return 1
19        end
20    end
21
22    # Find a suitable browser for viewing the help pages.
23    # The first thing we try is $fish_help_browser.
24    set -l fish_browser $fish_help_browser
25
26    # A list of graphical browsers we know about.
27    set -l graphical_browsers (printf %s "\
28htmlview
29x-www-browser
30firefox
31galeon
32mozilla
33xdg-open
34konqueror
35epiphany
36opera
37netscape
38rekonq
39google-chrome
40chromium-browser
41")
42
43    # On mac we may have to write a temporary file that redirects to the desired
44    # help page, since `open` will drop fragments from file URIs (issue #4480).
45    set -l need_trampoline false
46
47    if not set -q fish_browser[1]
48        if set -q BROWSER
49            # User has manually set a preferred browser, so we respect that
50            echo $BROWSER | read -at fish_browser
51            if not type -q $fish_browser[1]
52                printf (_ 'help: %s is not a valid command: %s\n') '$fish_browser' "$fish_browser"
53                return 2
54            end
55        else
56            # No browser set up, inferring.
57            # We check a bunch and use the last we find.
58
59            # Check for a text-based browser.
60            for i in htmlview www-browser links elinks lynx w3m
61                if type -q -f $i
62                    set fish_browser $i
63                    break
64                end
65            end
66
67            # If we are in a graphical environment, check if there is a graphical
68            # browser to use instead.
69            set -l is_graphical false
70            if test -n "$DISPLAY" &&
71                    { test -z "$XAUTHORITY" ||
72                            test "$XAUTHORITY" = "$HOME/.Xauthority"
73                    }
74                set is_graphical true
75                for i in $graphical_browsers
76                    if type -q -f $i
77                        set fish_browser $i
78                        break
79                    end
80                end
81            end
82
83            # If we're SSH'd into a desktop installation, don't use a regular browser unless X is being forwarded
84            if not set -q SSH_CLIENT || $is_graphical
85                # We use the macOS open, but not otherwise.
86                # On Debian, there is an open command that's a symlink to openvt.
87                if uname | string match -q Darwin && command -sq open
88                    set fish_browser open
89                    # The open command needs a trampoline because the macOS version can't handle #-fragments.
90                    set need_trampoline true
91                end
92
93                # If the OS appears to be Windows (graphical), try to use cygstart
94                if type -q cygstart
95                    set fish_browser cygstart
96                else if type -q xdg-open
97                    # If xdg-open is available, just use that
98                    set fish_browser xdg-open
99                end
100
101                # Try to find cmd.exe via $PATH or one of the paths that it's often at.
102                #
103                # We use this instead of xdg-open because that's useless without a backend
104                # like wsl-open which we'll check in a minute.
105                if test -f /proc/version
106                    and string match -riq 'Microsoft|WSL|MSYS|MINGW' </proc/version
107                    and set -l cmd (command -s powershell.exe cmd.exe /mnt/c/Windows/System32/cmd.exe)
108                    # Use the first of these.
109                    set fish_browser $cmd[1]
110                end
111
112                if type -q wsl-open
113                    set fish_browser wsl-open
114                end
115            end
116        end
117    else
118        if not type -q $fish_browser[1]
119            printf (_ 'help: %s is not a valid command: %s\n') '$fish_help_browser' "$fish_browser"
120            return 2
121        end
122    end
123
124    # In Cygwin, start the user-specified browser using cygstart,
125    # only if a Windows browser is to be used.
126    if type -q cygstart
127        if test "$fish_browser" != cygstart
128            and set -q fish_browser[1]
129            and not command -sq $fish_browser[1]
130            # Escaped quotes are necessary to work with spaces in the path
131            # when the command is finally eval'd.
132            set fish_browser cygstart $fish_browser
133        else
134            set need_trampoline true
135        end
136    end
137
138    set fish_help_item (string replace -r -- '\b#$' '' $fish_help_item)
139    set -l fish_help_page
140    switch "$fish_help_item"
141        case ''
142            set fish_help_page index.html
143        case (status get-file help_sections | string replace -r "^index(#|\$)" introduction\$1)
144            set fish_help_page (
145                printf %s $fish_help_item |
146                    string replace -r '^introduction(#|$)' 'index$1' |
147                    string replace -r -- '(#|$)' '.html$1'
148            )
149
150        case {cmds/,}"!"
151            set fish_help_page cmds/not.html
152        case {cmds/,}"."
153            set fish_help_page cmds/source.html
154        case {cmds/,}":"
155            set fish_help_page cmds/true.html
156        case {cmds/,}"["
157            set fish_help_page cmds/test.html
158        case {cmds/,}"{"
159            set fish_help_page cmds/begin.html
160        case (__fish_print_commands)
161            set fish_help_page cmds/$fish_help_item.html
162
163        case "*"
164            printf (_ "%s: no fish help topic '%s', try 'man %s'\n") help $fish_help_item $fish_help_item
165            return 1
166    end
167
168    # In Crostini Chrome OS Linux, the default browser opens URLs in Chrome running outside the
169    # linux VM. This browser does not have access to the Linux filesystem. This uses Garcon, see e.g.
170    # https://chromium.googlesource.com/chromiumos/platform2/+/master/vm_tools/garcon/#opening-urls
171    # https://source.chromium.org/search?q=garcon-url-handler
172    string match -q '*garcon-url-handler*' $fish_browser[1]
173    and set -l chromeos_linux_garcon
174
175    # Generate the online URL, with one dot in the version string (major.minor)
176    set -l version_string (string match -rg '(\d+\.\d+(?:b\d+)?).*' -- $version)
177    set -l ext_url https://fishshell.com/docs/$version_string/$fish_help_page
178    set -l page_url
179    if set -q __fish_help_dir[1]; and test -f $__fish_help_dir/index.html; and not set -lq chromeos_linux_garcon
180        # Help is installed, use it
181        set page_url file://$__fish_help_dir/$fish_help_page
182
183        # For Windows (Cygwin, msys2 and WSL), we need to convert the base
184        # help dir to a Windows path before converting it to a file URL
185        # but only if a Windows browser is being used
186        if type -q cygpath
187            and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser[1]
188            set page_url file://(cygpath -m $__fish_help_dir)/$fish_help_page
189        else if type -q wslpath
190            and string match -qr '\.exe(\s+|$)' $fish_browser[1]
191            set page_url file://(wslpath -w $__fish_help_dir)/$fish_help_page
192        end
193    else
194        set page_url $ext_url
195        # We don't need a trampoline for a remote URL.
196        set need_trampoline false
197    end
198
199    if not set -q fish_browser[1]
200        printf (_ '%s: Could not find a web browser.\n') help >&2
201        printf (_ 'Please try `BROWSER=some_browser help`, `man fish-doc`, or `man fish-tutorial`.\n\n') >&2
202        printf (_ 'Or open %s in your browser of choice.\n') $ext_url >&2
203        return 1
204    end
205
206    if $need_trampoline && string match -rq '#' $page_url
207        # Write a temporary file that will redirect where we want.
208        set -l tmpdir (__fish_mktemp_relative -d fish-help)
209        or return 1
210        set -l tmpname $tmpdir/help.html
211        echo '<meta http-equiv="refresh" content="0;URL=\''$page_url'\'" />' >$tmpname
212        set page_url file://$tmpname
213
214        # For Windows (Cygwin, msys2 and WSL), we need to convert the base help dir to a Windows path
215        # before converting it to a file URL, but only if a Windows browser is being used
216        if type -q cygpath
217            and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser[1]
218            set page_url file://(cygpath -m $tmpname)
219        else if type -q wslpath
220            and string match -qr '\.exe(\s+|$)' $fish_browser[1]
221            set page_url file://(wslpath -w $tmpname)
222        end
223    end
224
225    printf (_ 'help: Help is being displayed in %s.\n') $fish_browser[1]
226    # cmd.exe and powershell needs more coaxing.
227    if string match -qr 'powershell\.exe$|cmd\.exe$' -- $fish_browser[1]
228        # The space before the /c is to prevent msys2 from expanding it to a path
229        $fish_browser " /c" start $page_url
230    else if contains -- $fish_browser[1] $graphical_browsers
231        set -l sh (__fish_posix_shell)
232        $sh -c '( "$@" ) &' -- $fish_browser $page_url
233    else
234        $fish_browser $page_url
235    end
236
237    # Show the online URL anyway in case we did not manage to open something successfully.
238    # (we can't check because we need to background it)
239    printf (_ 'help: If no help could be displayed, go to %s to view the documentation online.\n') $ext_url
240end