gsettings-bash-completion.sh 2.48 KB
Newer Older
1 2 3 4 5 6 7

# Check for bash
[ -z "$BASH_VERSION" ] && return

####################################################################################################

__gsettings() {
8
  local choices coffset schemadir
9

10 11 12 13 14 15 16 17 18 19 20 21 22
  if [ ${COMP_CWORD} -gt 2 ]; then
      if [ ${COMP_WORDS[1]} = --schemadir ]; then
	  # this complexity is needed to perform correct tilde expansion
	  schemadir=$(eval "echo --schemadir ${COMP_WORDS[2]}")
	  coffset=2
      else
	  coffset=0
      fi
  else
      coffset=0
  fi

  case "$((${COMP_CWORD}-$coffset))" in
23
    1)
24
      choices=$'--schemadir\nhelp \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nwritable \nmonitor'
25 26 27
      ;;

    2)
28 29 30 31 32 33
      case "${COMP_WORDS[$(($coffset+1))]}" in
	--schemadir)
	  COMPREPLY=($(compgen -o dirnames -- ${COMP_WORDS[${COMP_CWORD}]}))
	  return 0
	  ;;

34
        help)
35
          choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nwritable\nmonitor'
36
          ;;
37
        list-keys|list-children|list-recursively)
38
          choices="$(gsettings $schemadir list-schemas)"$'\n'"$(gsettings $schemadir list-relocatable-schemas | sed -e 's.$.:/.')"
39 40
          ;;

41
        get|range|set|reset|writable|monitor)
42
          choices="$(gsettings $schemadir list-schemas | sed -e 's.$. .')"$'\n'"$(gsettings $schemadir list-relocatable-schemas | sed -e 's.$.:/.')"
43 44 45 46 47
          ;;
      esac
      ;;

    3)
48
      case "${COMP_WORDS[$(($coffset+1))]}" in
49
        set)
50
          choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null | sed -e 's.$. .')"
51 52
          ;;

53
        get|range|reset|writable|monitor)
54
          choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null)"
55 56 57
          ;;
      esac
      ;;
58 59

    4)
60
      case "${COMP_WORDS[$(($coffset+2))]}" in
61
        set)
62
          range=($(gsettings $schemadir range ${COMP_WORDS[$(($coffset+2))]} ${COMP_WORDS[$(($coffset+3))]} 2> /dev/null))
63 64 65 66 67 68 69 70 71 72 73 74 75
          case "${range[0]}" in
            enum)
              unset range[0]
             ;;
            *)
              unset range
             ;;
          esac
          local IFS=$'\n'
          choices="${range[*]}"
          ;;
      esac
      ;;
76 77 78
  esac

  local IFS=$'\n'
79
  COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}"))
80 81 82 83 84
}

####################################################################################################

complete -o nospace -F __gsettings gsettings