master ohmu/fastfetch-openbsd / completions / fastfetch.bash
ohmu  ·  2026-10-15
  1#!/usr/bin/env bash
  2
  3_fastfetch() {
  4  # Use Bash built-in variables directly
  5  local cur="${COMP_WORDS[COMP_CWORD]}"
  6  local prev="${COMP_WORDS[COMP_CWORD-1]}"
  7
  8  # Check if Python is available
  9  if ! command -v python3 &>/dev/null; then
 10    return
 11  fi
 12
 13  # Handle standard completion cases
 14  case "$prev" in
 15    --color|--color-keys|--color-title|--color-output|--color-separator|--*-color|--*-key-color|--*-output-color|--logo-color-[1-9]|--percent-color-*|--temp-color-*)
 16      local -a colors=("black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" "default")
 17      COMPREPLY=($(compgen -W "${colors[*]}" -- "$cur"))
 18      return
 19      ;;
 20    --logo|-l)
 21      local -a logos
 22      readarray -t logos < <(fastfetch --list-logos autocompletion 2>/dev/null)
 23      logos+=("none" "small")
 24      COMPREPLY=($(compgen -W "${logos[*]}" -- "$cur"))
 25      return
 26      ;;
 27    --config|-c)
 28      local -a presets
 29      readarray -t presets < <(fastfetch --list-presets autocompletion 2>/dev/null)
 30      presets+=("none")
 31      COMPREPLY=($(compgen -W "${presets[*]}" -- "$cur"))
 32      # Also allow file path completion
 33      if type _filedir &>/dev/null; then
 34        _filedir
 35      elif type compgen &>/dev/null; then
 36        local -a files
 37        readarray -t files < <(compgen -f -- "$cur")
 38        COMPREPLY+=("${files[@]}")
 39      fi
 40      return
 41      ;;
 42    --structure|-s)
 43      # Get all module names in lowercase only
 44      local -a structures
 45      readarray -t structures < <(fastfetch --list-modules autocompletion 2>/dev/null | cut -d':' -f1 | tr '[:upper:]' '[:lower:]')
 46      COMPREPLY=($(compgen -W "${structures[*]}" -- "$cur"))
 47      return
 48      ;;
 49    --help|-h)
 50      local -a modules
 51      readarray -t modules < <(fastfetch --list-modules autocompletion 2>/dev/null)
 52      # Convert to lowercase and keep only module names
 53      local -a module_names=()
 54      for module in "${modules[@]}"; do
 55        module_names+=($(echo "$module" | cut -d':' -f1 | tr '[:upper:]' '[:lower:]')-format)
 56      done
 57      module_names+=("format" "color")
 58      COMPREPLY=($(compgen -W "${module_names[*]}" -- "$cur"))
 59      return
 60      ;;
 61    --format)
 62      COMPREPLY=($(compgen -W "json default" -- "$cur"))
 63      return
 64      ;;
 65    --*-format)
 66      # Format string completion, handle spaces
 67      return
 68      ;;
 69    --*path*|--*file*|--gen-config*|--*data*)
 70      # File path completion
 71      if type _filedir &>/dev/null; then
 72        _filedir
 73      elif type compgen &>/dev/null; then
 74        COMPREPLY=($(compgen -f -- "$cur"))
 75      fi
 76      return
 77      ;;
 78  esac
 79
 80  # If not a special option, generate all possible options
 81  if [[ "$cur" == -* ]]; then
 82    local -a opts
 83    readarray -t opts < <(python3 - "$cur" <<'EOF'
 84import json
 85import sys
 86import subprocess
 87
 88def main(current):
 89    try:
 90        # Use fastfetch --help-raw to get option data
 91        output = subprocess.check_output(['fastfetch', '--help-raw'], stderr=subprocess.DEVNULL)
 92        data = json.loads(output)
 93
 94        for category in data.values():
 95            for flag in category:
 96                if flag.get("pseudo", False):
 97                    continue
 98
 99                if "short" in flag:
100                    print(f"-{flag['short']}")
101
102                if "long" in flag:
103                    if flag["long"] == "logo-color-[1-9]":
104                        for i in range(1, 10):
105                            print(f"--logo-color-{i}")
106                    else:
107                        print(f"--{flag['long']}")
108    except Exception:
109        # If error occurs, return no options
110        pass
111
112if __name__ == "__main__":
113    main(sys.argv[1])
114EOF
115)
116    COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur"))
117  fi
118
119  return 0
120}
121
122# Register completion
123complete -F _fastfetch fastfetch