refactor the __*_users functions

Per my comment in issue #3980 this implements `__fish_print_users` in
terms of `__fish_complete_users` so we don't have to modify both when a
change to how users are enumerated is needed.
This commit is contained in:
Kurtis Rader 2017-04-23 20:08:40 -07:00
parent bea0b17c9e
commit 37508d1f1e
2 changed files with 11 additions and 13 deletions

View File

@ -1,10 +1,12 @@
# This should be used where you want user names with a description. Such as in an argument
# completion. If you just want a list of user names use __fish_print_users.
function __fish_complete_users --description "Print a list of local users, with the real user name as a description"
if test -x /usr/bin/getent
getent passwd | cut -d : -f 1,5 | string replace -r ':' \t
/usr/bin/getent passwd | cut -d : -f 1,5 | string replace -r ':' \t
else if test -x /usr/bin/dscl
dscl . -list /Users RealName | string match -r -v '^_' | string replace -r ' {2,}' \t
else if test -e /etc/passwd
# This is the "Directory Service command line utility" used on macOS in place of getent.
/usr/bin/dscl . -list /Users RealName | string match -r -v '^_' | string replace -r ' {2,}' \t
else if test -r /etc/passwd
string match -v -r '^\s*#' </etc/passwd | cut -d : -f 1,5 | string replace ':' \t
end
end

View File

@ -1,11 +1,7 @@
# This should be used where you want user names without a description. If you also want
# a description, such as when getting a list of users for a completion, you probably want
# __fish_complete_users.
function __fish_print_users --description "Print a list of local users"
if test -x /usr/bin/getent
getent passwd | cut -d : -f 1
else if test -x /usr/bin/dscl # OS X support
dscl . -list /Users | string match -r -v '^_'
else if test -e /etc/passwd
string match -v -r '^\w*#' </etc/passwd | cut -d : -f 1
end
# Leave the heavy lifting to __fish_complete_users but strip the descriptions.
__fish_complete_users | string replace -r '^([^\t]*)\t.*' '$1'
end