Parent directory

gnu-utils.plugin.zsh

2729 bytes
 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
 8# Detect if GNU coreutils are installed by looking for gwhoami
 9if [[ ! -x "${commands[gwhoami]}" ]]; then
10  return
11fi
12
13__gnu_utils() {
14  local -a gcmds
15  local gcmd
16
17  # coreutils
18  gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
19  'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
20  'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
21  'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
22  'gid' 'gindent' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
23  'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
24  'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
25  'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
26  'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
27  'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
28  'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
29  'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
30  'gwhoami' 'gyes')
31
32  # findutils
33  gcmds+=('gfind' 'gxargs' 'glocate')
34
35  # Not part of either coreutils or findutils, installed separately.
36  gcmds+=('gsed' 'gtar' 'gtime' 'gmake' 'ggrep')
37
38  # can be built optionally
39  gcmds+=('ghostname')
40
41  for gcmd in "${gcmds[@]}"; do
42    # Do nothing if the command isn't found
43    (( ${+commands[$gcmd]} )) || continue
44
45    # This method allows for builtin commands to be primary but it's
46    # lost if hash -r or rehash is executed, or if $PATH is updated.
47    # Thus, a preexec hook is needed, which will only run if whoami
48    # is not already rehashed.
49    #
50    hash ${gcmd[2,-1]}=${commands[$gcmd]}
51  done
52
53  return 0
54}
55
56__gnu_utils_preexec() {
57  # Run __gnu_utils when the whoami command is not already rehashed.
58  # This acts as a sign that we need to rehash all GNU utils.
59  [[ "${commands[whoami]}" = "${commands[gwhoami]}" ]] || __gnu_utils
60}
61
62autoload -Uz add-zsh-hook
63add-zsh-hook preexec __gnu_utils_preexec
64
65# lib/theme-and-appearance.zsh sets the alias for ls not knowing that
66# we'll be using GNU ls. We'll reset this to use GNU ls --color.
67# See https://github.com/ohmyzsh/ohmyzsh/issues/11503
68#
69# The ls alias might look like:
70# - ls='ls -G'
71# - ls='gls --color=tty'
72if [[ -x "${commands[gls]}" && "${aliases[ls]}" = (*-G*|gls*) ]]; then
73  alias ls='ls --color=tty'
74fi