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