]>
Commit | Line | Data |
---|---|---|
d611dadc | 1 | # localectl(1) completion -*- shell-script -*- |
7059062c | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
d611dadc MB |
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 () { | |
a72d698d DR |
22 | local w word=$1; shift |
23 | for w in "$@"; do | |
24 | [[ $w = "$word" ]] && return | |
25 | done | |
d611dadc MB |
26 | } |
27 | ||
627a98d3 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 \ | |
31 | LC_MEASUREMENT LC_IDENTIFICATION ) | |
32 | # LC_ALL is omitted on purpose | |
33 | ||
d611dadc | 34 | _localectl() { |
627a98d3 | 35 | local i verb comps locale_vals |
d611dadc MB |
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 MB |
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=( | |
f822279e YW |
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' | |
d611dadc MB |
61 | ) |
62 | ||
3ce09b7d | 63 | for ((i=0; i < COMP_CWORD; i++)); do |
d611dadc MB |
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[*]} | |
f822279e YW |
72 | elif __contains_word "$verb" ${VERBS[VARIANTS]}; then |
73 | comps=$(command localectl list-x11-keymap-layouts) | |
d611dadc | 74 | elif __contains_word "$verb" ${VERBS[LOCALES]}; then |
627a98d3 ZJS |
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 | |
d611dadc MB |
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 |