2007-09-23 21:07:30 +00:00
|
|
|
function __fish_print_help --description "Print help message for the specified fish function or builtin" --argument item
|
2014-10-10 07:11:23 +00:00
|
|
|
# special support for builtin_help_get()
|
|
|
|
set -l tty_width
|
|
|
|
if test "$item" = "--tty-width"
|
|
|
|
set tty_width $argv[2]
|
|
|
|
set item $argv[3]
|
|
|
|
end
|
|
|
|
|
2014-10-10 06:32:54 +00:00
|
|
|
if test "$item" = '.'
|
2007-09-22 22:38:28 +00:00
|
|
|
set item source
|
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-09-01 09:14:13 +00:00
|
|
|
# Do nothing if the file does not exist
|
|
|
|
if not test -e "$__fish_datadir/man/man1/$item.1"
|
|
|
|
return
|
|
|
|
end
|
2007-09-22 22:38:28 +00:00
|
|
|
|
2014-10-10 06:32:54 +00:00
|
|
|
set -l IFS \n\ \t
|
2006-11-19 23:27:34 +00:00
|
|
|
|
|
|
|
# Render help output, save output into the variable 'help'
|
2014-10-10 06:32:54 +00:00
|
|
|
set -l help
|
2014-10-10 07:11:23 +00:00
|
|
|
set -l cols
|
2014-10-10 06:32:54 +00:00
|
|
|
set -l rLL
|
2014-10-10 07:11:23 +00:00
|
|
|
if test "$tty_width" -gt 0
|
|
|
|
set cols $tty_width
|
|
|
|
else if command test -t 1
|
2014-10-10 06:32:54 +00:00
|
|
|
# We want to simulate `man`'s dynamic line length, because
|
|
|
|
# defaulting to 80 kind of sucks.
|
|
|
|
# Note: using `command test` instead of `test` because `test -t 1`
|
|
|
|
# doesn't seem to work right.
|
|
|
|
# Note: grab the size from the stdout terminal in case it's somehow
|
|
|
|
# different than the stdin of fish.
|
2014-10-10 07:11:23 +00:00
|
|
|
# use fd 3 to copy our stdout because we need to pipe the output of stty
|
2014-10-10 06:32:54 +00:00
|
|
|
begin
|
2014-11-15 02:21:21 +00:00
|
|
|
stty size 0<&3 | read __ cols
|
2014-10-10 06:32:54 +00:00
|
|
|
end 3<&1
|
2014-10-10 07:11:23 +00:00
|
|
|
end
|
|
|
|
if test -n "$cols"
|
2016-02-03 22:23:59 +00:00
|
|
|
set cols (math $cols - 4) # leave a bit of space on the right
|
2014-10-10 06:32:54 +00:00
|
|
|
set rLL -rLL=$cols[1]n
|
|
|
|
end
|
2015-03-10 21:16:13 +00:00
|
|
|
set help (nroff -man -c -t $rLL "$__fish_datadir/man/man1/$item.1" ^/dev/null)
|
2006-11-19 23:27:34 +00:00
|
|
|
|
2014-10-10 06:32:54 +00:00
|
|
|
# The original implementation trimmed off the top 5 lines and bottom 3 lines
|
|
|
|
# from the nroff output. Perhaps that's reliable, but the magic numbers make
|
|
|
|
# me extremely nervous. Instead, let's just strip out any lines that start
|
|
|
|
# in the first column. "normal" manpages put all section headers in the first
|
|
|
|
# column, but fish manpages only leave NAME like that, which we want to trim
|
|
|
|
# away anyway.
|
|
|
|
#
|
|
|
|
# While we're at it, let's compress sequences of blank lines down to a single
|
|
|
|
# blank line, to duplicate the default behavior of `man`, or more accurately,
|
|
|
|
# the `-s` flag to `less` that `man` passes.
|
|
|
|
set -l state blank
|
|
|
|
for line in $help
|
|
|
|
# categorize the line
|
|
|
|
set -l line_type
|
|
|
|
switch $line
|
|
|
|
case ' *' \t\*
|
|
|
|
# starts with whitespace, check if it has non-whitespace
|
2014-11-15 02:21:21 +00:00
|
|
|
printf "%s\n" $line | read -l word __
|
2014-10-10 06:32:54 +00:00
|
|
|
if test -n $word
|
|
|
|
set line_type normal
|
|
|
|
else
|
|
|
|
# lines with just spaces probably shouldn't happen
|
|
|
|
# but let's consider them to be blank
|
|
|
|
set line_type blank
|
|
|
|
end
|
|
|
|
case ''
|
|
|
|
set line_type blank
|
|
|
|
case '*'
|
|
|
|
# not leading space, and not empty, so must contain a non-space
|
|
|
|
# in the first column. That makes it a header/footer.
|
|
|
|
set line_type meta
|
|
|
|
end
|
2006-11-19 23:27:34 +00:00
|
|
|
|
2014-10-10 06:32:54 +00:00
|
|
|
switch $state
|
|
|
|
case normal
|
|
|
|
switch $line_type
|
|
|
|
case normal
|
|
|
|
printf "%s\n" $line
|
|
|
|
case blank
|
|
|
|
set state blank
|
|
|
|
case meta
|
|
|
|
# skip it
|
|
|
|
end
|
|
|
|
case blank
|
|
|
|
switch $line_type
|
|
|
|
case normal
|
|
|
|
echo # print the blank line
|
|
|
|
printf "%s\n" $line
|
|
|
|
set state normal
|
|
|
|
case blank meta
|
|
|
|
# skip it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end | ul # post-process with `ul`, to interpret the old-style grotty escapes
|
|
|
|
echo # print a trailing blank line
|
2012-09-01 09:14:13 +00:00
|
|
|
end
|