]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/localectl
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / shell-completion / bash / localectl
CommitLineData
d611dadc 1# localectl(1) completion -*- shell-script -*-
db9ecf05 2# SPDX-License-Identifier: LGPL-2.1-or-later
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
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 () {
843cfcb1
ZJS
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
d611dadc
MB
26}
27
843cfcb1
ZJS
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
627a98d3
ZJS
31 LC_MEASUREMENT LC_IDENTIFICATION )
32# LC_ALL is omitted on purpose
33
d611dadc 34_localectl() {
843cfcb1
ZJS
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
f822279e 38 -H --host -M --machine'
d611dadc 39
843cfcb1
ZJS
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
d611dadc 49
843cfcb1
ZJS
50 if [[ $cur = -* ]]; then
51 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
52 return 0
53 fi
d611dadc 54
843cfcb1
ZJS
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 )
d611dadc 62
843cfcb1
ZJS
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
d611dadc 67 fi
843cfcb1 68 done
d611dadc 69
843cfcb1
ZJS
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
d611dadc 85 return 0
843cfcb1
ZJS
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
d611dadc
MB
94}
95
96complete -F _localectl localectl