a952854c122c0e728f7bb5577fabda6b6a0c6a79

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

gnu-utils: add README, simplify plugin

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/gnu-utils/README.md b/plugins/gnu-utils/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..f5fa81e2f4ea8f0bdd665b689575a663185fdc49
  4--- /dev/null
  5+++ b/plugins/gnu-utils/README.md
  6@@ -0,0 +1,38 @@
  7+# gnu-utils plugin
  8+
  9+This plugin binds GNU coreutils to their default names, so that you don't have
 10+to call them using their prefixed name, which starts with `g`. This is useful
 11+in systems which don't have GNU coreutils installed by default, mainly macOS
 12+or FreeBSD, which use BSD coreutils.
 13+
 14+To use it, add `gnu-utils` to the plugins array in your zshrc file:
 15+```zsh
 16+plugins=(... gnu-utils)
 17+```
 18+
 19+The plugin works by changing the path that the command hash points to, so
 20+instead of `ls` pointing to `/bin/ls`, it points to wherever `gls` is
 21+installed.
 22+
 23+Since `hash -rf` or `rehash` refreshes the command hashes, it also wraps
 24+`hash` and `rehash` so that the coreutils binding is always done again
 25+after calling these two commands.
 26+
 27+Look at the source code of the plugin to see which GNU coreutils are tried
 28+to rebind. Open an issue if there are some missing.
 29+
 30+## Other methods
 31+
 32+The plugin also documents two other ways to do this:
 33+
 34+1. Using a function wrapper, such that, for example, there exists a function
 35+named `ls` which calls `gls` instead. Since functions have a higher preference
 36+than commands, this ends up calling the GNU coreutil. It has also a higher
 37+preference over shell builtins (`gecho` is called instead of the builtin `echo`).
 38+
 39+2. Using an alias. This has an even higher preference than functions, but they
 40+could be overridden because of a user setting.
 41+
 42+## Author
 43+
 44+- [Sorin Ionescu](https://github.com/sorin-ionescu).
 45diff --git a/plugins/gnu-utils/gnu-utils.plugin.zsh b/plugins/gnu-utils/gnu-utils.plugin.zsh
 46index b66e25d7fc571bdee0e3e98f1e71900d7ce868fa..967b8b4eab6d2757d095eb19556c446d8ad1748e 100644
 47--- a/plugins/gnu-utils/gnu-utils.plugin.zsh
 48+++ b/plugins/gnu-utils/gnu-utils.plugin.zsh
 49@@ -5,79 +5,79 @@
 50 #       VERSION:  1.0.0
 51 # ------------------------------------------------------------------------------
 52 
 53+# Detect if GNU coreutils are installed by looking for gwhoami
 54+if [[ ! -x "${commands[gwhoami]}" ]]; then
 55+  return
 56+fi
 57 
 58-if [[ -x "${commands[gwhoami]}" ]]; then 
 59-  __gnu_utils() {
 60-    emulate -L zsh
 61-    local gcmds
 62-    local gcmd
 63-    local cmd
 64-    local prefix
 65+__gnu_utils() {
 66+  emulate -L zsh
 67+  local gcmds
 68+  local gcmd
 69+  local cmd
 70+  local prefix
 71 
 72-    # coreutils 
 73-    gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
 74-    'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
 75-    'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
 76-    'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
 77-    'gid' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
 78-    'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
 79-    'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
 80-    'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
 81-    'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
 82-    'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
 83-    'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
 84-    'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
 85-    'gwhoami' 'gyes')
 86+  # coreutils 
 87+  gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
 88+  'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
 89+  'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
 90+  'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
 91+  'gid' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
 92+  'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
 93+  'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
 94+  'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
 95+  'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
 96+  'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
 97+  'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
 98+  'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
 99+  'gwhoami' 'gyes')
100 
101-    # findutils
102-    gcmds+=('gfind' 'gxargs' 'glocate')
103+  # findutils
104+  gcmds+=('gfind' 'gxargs' 'glocate')
105 
106-    # Not part of either coreutils or findutils, installed separately.
107-    gcmds+=('gsed' 'gtar' 'gtime')
108+  # Not part of either coreutils or findutils, installed separately.
109+  gcmds+=('gsed' 'gtar' 'gtime')
110 
111-    for gcmd in "${gcmds[@]}"; do
112-      #
113-      # This method allows for builtin commands to be primary but it's
114-      # lost if hash -r or rehash -f is executed. Thus, those two 
115-      # functions have to be wrapped.
116-      #
117-      (( ${+commands[$gcmd]} )) && hash ${gcmd[2,-1]}=${commands[$gcmd]}
118+  for gcmd in "${gcmds[@]}"; do
119+    # Do nothing if the command isn't found
120+    (( ${+commands[$gcmd]} )) || continue
121+    
122+    # This method allows for builtin commands to be primary but it's
123+    # lost if hash -r or rehash -f is executed. Thus, those two 
124+    # functions have to be wrapped.
125+    #
126+    hash ${gcmd[2,-1]}=${commands[$gcmd]}
127 
128-      #
129-      # This method generates wrapper functions.
130-      # It will override shell builtins.
131-      #
132-      # (( ${+commands[$gcmd]} )) && \
133-      # eval "function $gcmd[2,-1]() { \"${prefix}/${gcmd//"["/"\\["}\" \"\$@\"; }"
134+    # This method generates wrapper functions.
135+    # It will override shell builtins.
136+    #
137+    # eval "function $gcmd[2,-1]() { \"${prefix}/${gcmd//"["/"\\["}\" \"\$@\"; }"
138 
139-      #
140-      # This method is inflexible since the aliases are at risk of being
141-      # overridden resulting in the BSD coreutils being called.
142-      #
143-      # (( ${+commands[$gcmd]} )) && \
144-      # alias "$gcmd[2,-1]"="${prefix}/${gcmd//"["/"\\["}"
145-    done
146+    # This method is inflexible since the aliases are at risk of being
147+    # overridden resulting in the BSD coreutils being called.
148+    #