9b119866dd0e2d5054abd992f4dfbf346ac81b0d

Author
ratijas <me@ratijas.tk>
Committer
GitHub <noreply@github.com>
Date

Message

refactor(colored-man-pages): move nroff wrapper and refactor logic in `colored` function (#9437)

Diff

  1diff --git a/plugins/colored-man-pages/README.md b/plugins/colored-man-pages/README.md
  2index f34941e732102aa414b85aa6e8b9ba176e89fd29..4cbf64d3ea41c6f1bf175c21163180366c063782 100644
  3--- a/plugins/colored-man-pages/README.md
  4+++ b/plugins/colored-man-pages/README.md
  5@@ -16,3 +16,17 @@ You can also try to color other pages by prefixing the respective command with `
  6 ```zsh
  7 colored git help clone
  8 ```
  9+
 10+## Customization
 11+
 12+The plugin declares global associative array `less_termcap`, which maps termcap capabilities to escape
 13+sequences for the `less` pager. This mapping can be further customized by the user after the plugin is
 14+loaded. Check out sources for more.
 15+
 16+For example: `less_termcap[md]` maps to `LESS_TERMCAP_md` which is the escape sequence that tells `less`
 17+how to print something in bold. It's currently shown in bold red, but if you want to change it, you
 18+can redefine `less_termcap[md]` in your zshrc file, after OMZ is sourced:
 19+
 20+```zsh
 21+less_termcap[md]="${fg_bold[blue]}" # this tells less to print bold text in bold blue
 22+```
 23diff --git a/plugins/colored-man-pages/colored-man-pages.plugin.zsh b/plugins/colored-man-pages/colored-man-pages.plugin.zsh
 24index ec518472c8c6035adef0f8e9d6c4fbed8a34e80c..37faed6723c80e9c1cd6c4b56f08aa8c5c58a06a 100644
 25--- a/plugins/colored-man-pages/colored-man-pages.plugin.zsh
 26+++ b/plugins/colored-man-pages/colored-man-pages.plugin.zsh
 27@@ -1,39 +1,48 @@
 28-if [[ "$OSTYPE" = solaris* ]]
 29-then
 30-	if [[ ! -x "$HOME/bin/nroff" ]]
 31-	then
 32-		mkdir -p "$HOME/bin"
 33-		cat > "$HOME/bin/nroff" <<EOF
 34-#!/bin/sh
 35-if [ -n "\$_NROFF_U" -a "\$1,\$2,\$3" = "-u0,-Tlp,-man" ]; then
 36-	shift
 37-	exec /usr/bin/nroff -u\$_NROFF_U "\$@"
 38-fi
 39-#-- Some other invocation of nroff
 40-exec /usr/bin/nroff "\$@"
 41-EOF
 42-		chmod +x "$HOME/bin/nroff"
 43-	fi
 44-fi
 45+# Requires colors autoload.
 46+# See termcap(5).
 47+
 48+# Set up once, and then reuse. This way it supports user overrides after the
 49+# plugin is loaded.
 50+typeset -AHg less_termcap
 51+
 52+# bold & blinking mode
 53+less_termcap[mb]="${fg_bold[red]}"
 54+less_termcap[md]="${fg_bold[red]}"
 55+less_termcap[me]="${reset_color}"
 56+# standout mode
 57+less_termcap[so]="${fg_bold[yellow]}${bg[blue]}"
 58+less_termcap[se]="${reset_color}"
 59+# underlining
 60+less_termcap[us]="${fg_bold[green]}"
 61+less_termcap[ue]="${reset_color}"
 62+
 63+# Absolute path to this file's directory.
 64+typeset __colored_man_pages_dir="${0:A:h}"
 65 
 66 function colored() {
 67-	command env \
 68-		LESS_TERMCAP_mb=$(printf "\e[1;31m") \
 69-		LESS_TERMCAP_md=$(printf "\e[1;31m") \
 70-		LESS_TERMCAP_me=$(printf "\e[0m") \
 71-		LESS_TERMCAP_se=$(printf "\e[0m") \
 72-		LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
 73-		LESS_TERMCAP_ue=$(printf "\e[0m") \
 74-		LESS_TERMCAP_us=$(printf "\e[1;32m") \
 75-		PAGER="${commands[less]:-$PAGER}" \
 76-		_NROFF_U=1 \
 77-		PATH="$HOME/bin:$PATH" \
 78-			"$@"
 79+  local -a environment
 80+
 81+  # Convert associative array to plain array of NAME=VALUE items.
 82+  local k v
 83+  for k v in "${(@kv)less_termcap}"; do
 84+    environment+=( "LESS_TERMCAP_${k}=${v}" )
 85+  done
 86+
 87+  # Prefer `less` whenever available, since we specifically configured
 88+  # environment for it.
 89+  environment+=( PAGER="${commands[less]:-$PAGER}" )
 90+
 91+  # See ./nroff script.
 92+  if [[ "$OSTYPE" = solaris* ]]; then
 93+    environment+=( PATH="${__colored_man_pages_dir}:$PATH" )
 94+  fi
 95+
 96+  command env $environment "$@"
 97 }
 98 
 99 # Colorize man and dman/debman (from debian-goodies)
100 function man \
101-	dman \
102-	debman {
103-	colored $0 "$@"
104+  dman \
105+  debman {
106+  colored $0 "$@"
107 }
108diff --git a/plugins/colored-man-pages/nroff b/plugins/colored-man-pages/nroff
109new file mode 100755
110index 0000000000000000000000000000000000000000..4ae155d293872c71060e73adfcc56b5677ae35a5
111--- /dev/null
112+++ b/plugins/colored-man-pages/nroff
113@@ -0,0 +1,12 @@
114+#!/bin/sh
115+
116+# The whole point of this wrapper is to replace emboldening factor -u0 with
117+# -u1 under certain circumstances on Solaris.
118+
119+if [ "$1,$2,$3" = "-u0,-Tlp,-man" ]; then
120+  shift
121+  exec /usr/bin/nroff -u1 "$@"
122+else
123+  # Some other invocation of nroff
124+  exec /usr/bin/nroff "$@"
125+fi