| 1 |
# shellcheck shell=bash |
| 2 |
# localectl(1) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 6 |
# |
| 7 |
# Copyright © 2010 Ran Benita |
| 8 |
# |
| 9 |
# systemd is free software; you can redistribute it and/or modify it |
| 10 |
# under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# systemd is distributed in the hope that it will be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with systemd; If not, see <https://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
__contains_word () { |
| 23 |
local w word=$1; shift |
| 24 |
for w in "$@"; do |
| 25 |
[[ $w = "$word" ]] && return |
| 26 |
done |
| 27 |
} |
| 28 |
|
| 29 |
__locale_fields=( LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME |
| 30 |
LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER |
| 31 |
LC_NAME LC_ADDRESS LC_TELEPHONE |
| 32 |
LC_MEASUREMENT LC_IDENTIFICATION ) |
| 33 |
# LC_ALL is omitted on purpose |
| 34 |
|
| 35 |
_localectl() { |
| 36 |
local i verb comps locale_vals |
| 37 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
| 38 |
local OPTS='-h --help --version --no-convert --no-pager --no-ask-password |
| 39 |
-H --host -M --machine -l --full' |
| 40 |
|
| 41 |
if __contains_word "$prev" $OPTS; then |
| 42 |
case $prev in |
| 43 |
--host|-H) |
| 44 |
comps='' |
| 45 |
;; |
| 46 |
esac |
| 47 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 48 |
return 0 |
| 49 |
fi |
| 50 |
|
| 51 |
if [[ $cur = -* ]]; then |
| 52 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 53 |
return 0 |
| 54 |
fi |
| 55 |
|
| 56 |
local -A VERBS=( |
| 57 |
[STANDALONE]='status list-locales list-keymaps list-x11-keymap-models list-x11-keymap-layouts list-x11-keymap-options' |
| 58 |
[VARIANTS]='list-x11-keymap-variants' |
| 59 |
[LOCALES]='set-locale' |
| 60 |
[KEYMAPS]='set-keymap' |
| 61 |
[X11]='set-x11-keymap' |
| 62 |
) |
| 63 |
|
| 64 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 65 |
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then |
| 66 |
verb=${COMP_WORDS[i]} |
| 67 |
break |
| 68 |
fi |
| 69 |
done |
| 70 |
|
| 71 |
if [[ -z ${verb-} ]]; then |
| 72 |
comps=${VERBS[*]} |
| 73 |
elif __contains_word "$verb" ${VERBS[VARIANTS]}; then |
| 74 |
comps=$(command localectl list-x11-keymap-layouts) |
| 75 |
elif __contains_word "$verb" ${VERBS[LOCALES]}; then |
| 76 |
if [[ $cur = *=* ]]; then |
| 77 |
mapfile -t locale_vals < <(command localectl list-locales 2>/dev/null) |
| 78 |
COMPREPLY=( $(compgen -W '${locale_vals[*]}' -- "${cur#=}") ) |
| 79 |
elif [[ $prev = "=" ]]; then |
| 80 |
mapfile -t locale_vals < <(command localectl list-locales 2>/dev/null) |
| 81 |
COMPREPLY=( $(compgen -W '${locale_vals[*]}' -- "$cur") ) |
| 82 |
else |
| 83 |
compopt -o nospace |
| 84 |
COMPREPLY=( $(compgen -W '${__locale_fields[*]}' -S= -- "$cur") ) |
| 85 |
fi |
| 86 |
return 0 |
| 87 |
elif __contains_word "$verb" ${VERBS[KEYMAPS]}; then |
| 88 |
comps=$(command localectl list-keymaps) |
| 89 |
elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[X11]}; then |
| 90 |
comps='' |
| 91 |
fi |
| 92 |
|
| 93 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 94 |
return 0 |
| 95 |
} |
| 96 |
|
| 97 |
complete -F _localectl localectl |