/ de-omarchy / default / bash / completions
swadhinbiswas Initial de-omarchy: Omarchy UI layer for Arch-based distros · 8/22/2026
9f393b2
130 lines · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
_omarchy_complete() {
  COMPREPLY=()
  local cur="${COMP_WORDS[COMP_CWORD]}"

  local omarchy_path bin_dir
  omarchy_path=$(command -v omarchy 2>/dev/null) || return 0
  bin_dir=$(dirname -- "$(readlink -f -- "$omarchy_path" 2>/dev/null || printf '%s' "$omarchy_path")")
  [[ -d $bin_dir ]] || return 0

  local prefix="omarchy"
  local i part
  for ((i = 1; i < COMP_CWORD; i++)); do
    part="${COMP_WORDS[i]}"
    [[ -z $part || $part == -* ]] && continue
    prefix+="-$part"
  done

  local -A seen=()
  local candidates=()
  local file basename rest next

  shopt -s nullglob
  for file in "$bin_dir/$prefix"-*; do
    [[ -f $file && -x $file ]] || continue
    basename="${file##*/}"
    rest="${basename#"$prefix"-}"
    next="${rest%%-*}"
    if [[ -n $next && -z ${seen[$next]:-} ]]; then
      seen[$next]=1
      candidates+=("$next")
    fi
  done
  shopt -u nullglob

  if (( COMP_CWORD == 1 )); then
    candidates+=("commands")
  fi

  if [[ ${COMP_WORDS[1]:-} == "commands" ]] && (( COMP_CWORD >= 2 )); then
    candidates+=("--all" "--json" "--markdown" "--check")
  fi

  if (( ${#candidates[@]} == 0 )); then
    local command_prefix command_path first_arg_index n j
    for ((n = COMP_CWORD - 1; n >= 0; n--)); do
      command_prefix="omarchy"
      for ((j = 1; j <= n; j++)); do
        part="${COMP_WORDS[j]}"
        [[ -z $part || $part == -* ]] && continue
        command_prefix+="-$part"
      done

      command_path="$bin_dir/$command_prefix"
      if [[ -x $command_path ]]; then
        first_arg_index=$((n + 1))
        break
      fi
    done

    if [[ -n ${command_path:-} && -x $command_path ]]; then
      local args spec alt token actual alts value match current_arg_index
      args=$(grep -m 1 '^# omarchy:args=' "$command_path" 2>/dev/null)
      args="${args#*omarchy:args=}"
      current_arg_index=$((COMP_CWORD - first_arg_index))

      alts="${args// | /$'\n'}"
      while IFS= read -r alt; do
        read -r -a spec <<<"$alt"
        match=true

        for ((j = 0; j < current_arg_index; j++)); do
          token="${spec[j]:-}"
          actual="${COMP_WORDS[first_arg_index + j]:-}"
          if [[ -z $token ]]; then
            match=false
            break
          fi

          if [[ $token == \<*\> || $token == \[*\] ]]; then
            value="${token#<}"
            value="${value#\[}"
            value="${value%>}"
            value="${value%\]}"
            if [[ $value == *"|"* ]]; then
              [[ " ${value//|/ } " == *" $actual "* ]] || match=false
            fi
          elif [[ $token != "$actual" ]]; then
            match=false
          fi
        done

        [[ $match == true ]] || continue

        token="${spec[current_arg_index]:-}"
        [[ -n $token ]] || continue

        if [[ $token == \<*\> || $token == \[*\] ]]; then
          value="${token#<}"
          value="${value#\[}"
          value="${value%>}"
          value="${value%\]}"
          if [[ $value == *"|"* ]]; then
            read -r -a spec <<<"${value//|/ }"
            for actual in "${spec[@]}"; do
              [[ -n ${seen[$actual]:-} ]] && continue
              seen[$actual]=1
              candidates+=("$actual")
            done
          fi
        else
          [[ -n ${seen[$token]:-} ]] && continue
          seen[$token]=1
          candidates+=("$token")
        fi
      done <<<"$alts"
    fi
  fi

  if (( ${#candidates[@]} > 0 )); then
    local IFS=$'\n'
    COMPREPLY=($(compgen -W "${candidates[*]}" -- "$cur"))
  fi
}

complete -o default -F _omarchy_complete omarchy

# Hide individual omarchy-* binaries from initial-word command completion;
# the unified `omarchy` dispatcher is the user-facing entry point.
complete -I -A command -X 'omarchy-*'