diff options
author | ratijas <me@ratijas.tk> | 2020-11-12 21:32:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-12 19:32:17 +0100 |
commit | 9b119866dd0e2d5054abd992f4dfbf346ac81b0d (patch) | |
tree | fde68e7436c2b9f858633b966d58e084adc98cff /plugins/colored-man-pages | |
parent | 51772732f54f01522ad5720e8ed0ba16ca3147ae (diff) | |
download | zsh-9b119866dd0e2d5054abd992f4dfbf346ac81b0d.tar.gz zsh-9b119866dd0e2d5054abd992f4dfbf346ac81b0d.tar.bz2 zsh-9b119866dd0e2d5054abd992f4dfbf346ac81b0d.zip |
refactor(colored-man-pages): move nroff wrapper and refactor logic in `colored` function (#9437)
Diffstat (limited to 'plugins/colored-man-pages')
-rw-r--r-- | plugins/colored-man-pages/README.md | 14 | ||||
-rw-r--r-- | plugins/colored-man-pages/colored-man-pages.plugin.zsh | 73 | ||||
-rwxr-xr-x | plugins/colored-man-pages/nroff | 12 |
3 files changed, 67 insertions, 32 deletions
diff --git a/plugins/colored-man-pages/README.md b/plugins/colored-man-pages/README.md index f34941e73..4cbf64d3e 100644 --- a/plugins/colored-man-pages/README.md +++ b/plugins/colored-man-pages/README.md @@ -16,3 +16,17 @@ You can also try to color other pages by prefixing the respective command with ` ```zsh colored git help clone ``` + +## Customization + +The plugin declares global associative array `less_termcap`, which maps termcap capabilities to escape +sequences for the `less` pager. This mapping can be further customized by the user after the plugin is +loaded. Check out sources for more. + +For example: `less_termcap[md]` maps to `LESS_TERMCAP_md` which is the escape sequence that tells `less` +how to print something in bold. It's currently shown in bold red, but if you want to change it, you +can redefine `less_termcap[md]` in your zshrc file, after OMZ is sourced: + +```zsh +less_termcap[md]="${fg_bold[blue]}" # this tells less to print bold text in bold blue +``` diff --git a/plugins/colored-man-pages/colored-man-pages.plugin.zsh b/plugins/colored-man-pages/colored-man-pages.plugin.zsh index ec518472c..37faed672 100644 --- a/plugins/colored-man-pages/colored-man-pages.plugin.zsh +++ b/plugins/colored-man-pages/colored-man-pages.plugin.zsh @@ -1,39 +1,48 @@ -if [[ "$OSTYPE" = solaris* ]] -then - if [[ ! -x "$HOME/bin/nroff" ]] - then - mkdir -p "$HOME/bin" - cat > "$HOME/bin/nroff" <<EOF -#!/bin/sh -if [ -n "\$_NROFF_U" -a "\$1,\$2,\$3" = "-u0,-Tlp,-man" ]; then - shift - exec /usr/bin/nroff -u\$_NROFF_U "\$@" -fi -#-- Some other invocation of nroff -exec /usr/bin/nroff "\$@" -EOF - chmod +x "$HOME/bin/nroff" - fi -fi +# Requires colors autoload. +# See termcap(5). + +# Set up once, and then reuse. This way it supports user overrides after the +# plugin is loaded. +typeset -AHg less_termcap + +# bold & blinking mode +less_termcap[mb]="${fg_bold[red]}" +less_termcap[md]="${fg_bold[red]}" +less_termcap[me]="${reset_color}" +# standout mode +less_termcap[so]="${fg_bold[yellow]}${bg[blue]}" +less_termcap[se]="${reset_color}" +# underlining +less_termcap[us]="${fg_bold[green]}" +less_termcap[ue]="${reset_color}" + +# Absolute path to this file's directory. +typeset __colored_man_pages_dir="${0:A:h}" function colored() { - command env \ - LESS_TERMCAP_mb=$(printf "\e[1;31m") \ - LESS_TERMCAP_md=$(printf "\e[1;31m") \ - LESS_TERMCAP_me=$(printf "\e[0m") \ - LESS_TERMCAP_se=$(printf "\e[0m") \ - LESS_TERMCAP_so=$(printf "\e[1;44;33m") \ - LESS_TERMCAP_ue=$(printf "\e[0m") \ - LESS_TERMCAP_us=$(printf "\e[1;32m") \ - PAGER="${commands[less]:-$PAGER}" \ - _NROFF_U=1 \ - PATH="$HOME/bin:$PATH" \ - "$@" + local -a environment + + # Convert associative array to plain array of NAME=VALUE items. + local k v + for k v in "${(@kv)less_termcap}"; do + environment+=( "LESS_TERMCAP_${k}=${v}" ) + done + + # Prefer `less` whenever available, since we specifically configured + # environment for it. + environment+=( PAGER="${commands[less]:-$PAGER}" ) + + # See ./nroff script. + if [[ "$OSTYPE" = solaris* ]]; then + environment+=( PATH="${__colored_man_pages_dir}:$PATH" ) + fi + + command env $environment "$@" } # Colorize man and dman/debman (from debian-goodies) function man \ - dman \ - debman { - colored $0 "$@" + dman \ + debman { + colored $0 "$@" } diff --git a/plugins/colored-man-pages/nroff b/plugins/colored-man-pages/nroff new file mode 100755 index 000000000..4ae155d29 --- /dev/null +++ b/plugins/colored-man-pages/nroff @@ -0,0 +1,12 @@ +#!/bin/sh + +# The whole point of this wrapper is to replace emboldening factor -u0 with +# -u1 under certain circumstances on Solaris. + +if [ "$1,$2,$3" = "-u0,-Tlp,-man" ]; then + shift + exec /usr/bin/nroff -u1 "$@" +else + # Some other invocation of nroff + exec /usr/bin/nroff "$@" +fi |