gnu-utils.plugin.zsh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # ------------------------------------------------------------------------------
  2. # FILE: gnu-utils.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.0.0
  6. # ------------------------------------------------------------------------------
  7. # Detect if GNU coreutils are installed by looking for gwhoami
  8. if [[ ! -x "${commands[gwhoami]}" ]]; then
  9. return
  10. fi
  11. __gnu_utils() {
  12. local -a gcmds
  13. local gcmd
  14. # coreutils
  15. gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
  16. 'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
  17. 'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
  18. 'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
  19. 'gid' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
  20. 'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
  21. 'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
  22. 'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
  23. 'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
  24. 'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
  25. 'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
  26. 'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
  27. 'gwhoami' 'gyes')
  28. # findutils
  29. gcmds+=('gfind' 'gxargs' 'glocate')
  30. # Not part of either coreutils or findutils, installed separately.
  31. gcmds+=('gsed' 'gtar' 'gtime' 'gmake' 'ggrep')
  32. # can be built optionally
  33. gcmds+=('ghostname')
  34. for gcmd in "${gcmds[@]}"; do
  35. # Do nothing if the command isn't found
  36. (( ${+commands[$gcmd]} )) || continue
  37. # This method allows for builtin commands to be primary but it's
  38. # lost if hash -r or rehash is executed, or if $PATH is updated.
  39. # Thus, a preexec hook is needed, which will only run if whoami
  40. # is not already rehashed.
  41. #
  42. hash ${gcmd[2,-1]}=${commands[$gcmd]}
  43. done
  44. return 0
  45. }
  46. __gnu_utils_preexec() {
  47. # Run __gnu_utils when the whoami command is not already rehashed.
  48. # This acts as a sign that we need to rehash all GNU utils.
  49. [[ "${commands[whoami]}" = "${commands[gwhoami]}" ]] || __gnu_utils
  50. }
  51. autoload -Uz add-zsh-hook
  52. add-zsh-hook preexec __gnu_utils_preexec