master ohmu/fastfetch-openbsd / completions / fastfetch.fish
ohmu  ·  2026-10-15
  1if not type -q fastfetch
  2    exit
  3end
  4
  5command -q python3
  6if test $status -ne 0
  7    exit
  8end
  9
 10complete -c fastfetch -f
 11
 12function __fastfetch_complete_bool
 13    echo -e "true\tBool"
 14    echo -e "false\tBool"
 15end
 16
 17function __fastfetch_complete_color
 18    echo -e "black\tColor"
 19    echo -e "red\tColor"
 20    echo -e "green\tColor"
 21    echo -e "yellow\tColor"
 22    echo -e "blue\tColor"
 23    echo -e "magenta\tColor"
 24    echo -e "cyan\tColor"
 25    echo -e "white\tColor"
 26    echo -e "default\tColor"
 27end
 28
 29function __fastfetch_complete_command
 30    for line in (fastfetch --list-modules autocompletion)
 31        set -l pair (string split -m 2 : $line)
 32        set -l module (string lower $pair[1])
 33
 34        echo -e "$module-format\tModule format"
 35    end
 36    echo -e "format\tCustom format"
 37    echo -e "color\tColor format"
 38end
 39
 40function __fastfetch_complete_config
 41    for line in (fastfetch --list-presets autocompletion)
 42        echo -e "$line\tPreset"
 43    end
 44    echo -e "none\tDisable loading config file"
 45end
 46
 47function __fastfetch_complete_logo
 48    for line in (fastfetch --list-logos autocompletion)
 49        echo -e "$line\tBuiltin logo"
 50    end
 51    echo -e "none\tDon't print logo"
 52    echo -e "small\tPrint small ascii logo if available"
 53end
 54
 55function __fastfetch_complete_structure
 56    for line in (fastfetch --list-modules autocompletion)
 57        set -l pair (string split -m 2 : $line)
 58        echo -e "$pair[1]\t$pair[2]"
 59    end
 60end
 61
 62echo '
 63import json, subprocess, sys
 64
 65
 66def escape_fish_dq(s: str) -> str:
 67    """Escape special characters for Fish double quotes to prevent unexpected expansions. One slash in output needs four slashes in script"""
 68    return str(s).replace("\\\\", "\\\\\\\\").replace("\\"", "\\\\\\"").replace("$", "\\\\$")
 69
 70
 71def main():
 72    data: dict[str, list[dict]] = json.loads(subprocess.check_output(["fastfetch", "--help-raw"]))
 73
 74    enum_functions = []
 75    complete_commands = []
 76
 77    for key in data:
 78        for flag in data[key]:
 79            long_name = flag.get("long", "")
 80
 81            # Handle pseudo flag logo-color-[1-9]
 82            if long_name == "logo-color-[1-9]":
 83                safe_desc_global = escape_fish_dq(flag.get("desc", ""))
 84                for i in range(1, 10):
 85                    complete_commands.append(f"complete -c fastfetch -d \"{safe_desc_global}\" -l \"logo-color-{i}\" -x -a \"(__fastfetch_complete_color)\"")
 86                continue
 87
 88            if flag.get("pseudo", False):
 89                continue
 90
 91            safe_desc_global = escape_fish_dq(flag.get("desc", ""))
 92            command_prefix = f"complete -c fastfetch -d \"{safe_desc_global}\" -l \"{long_name}\""
 93
 94            if "short" in flag:
 95                short_name = flag["short"]
 96                command_prefix += f" -o {short_name}"
 97
 98            if "arg" in flag:
 99                arg_type: str = flag["arg"]["type"]
100                if arg_type == "bool":
101                    complete_commands.append(f"{command_prefix} -x -a \"(__fastfetch_complete_bool)\"")
102                elif arg_type == "color":
103                    complete_commands.append(f"{command_prefix} -x -a \"(__fastfetch_complete_color)\"")
104                elif arg_type == "command":
105                    complete_commands.append(f"{command_prefix} -x -a \"(__fastfetch_complete_command)\"")
106                elif arg_type == "config":
107                    complete_commands.append(f"{command_prefix} -x -a \"(__fastfetch_complete_config)\"")
108                elif arg_type == "enum":
109                    # Sanitize flag name to create a valid Fish function name
110                    func_name = long_name.replace("-", "_").replace("[", "_").replace("]", "_")
111                    func_name = f"__fastfetch_complete_enum_{func_name}"
112
113                    # Generate the enum function
114                    func_lines = [f"function {func_name}"]
115                    for enum_val, enum_desc in flag["arg"]["enum"].items():
116                        safe_val = escape_fish_dq(enum_val)
117                        safe_desc = escape_fish_dq(enum_desc)
118                        func_lines.append(f"    echo -e \"{safe_val}\\t{safe_desc}\"")
119                    func_lines.append("end\n")
120                    enum_functions.append("\n".join(func_lines))
121
122                    # Append complete command pointing to the new function
123                    complete_commands.append(f"{command_prefix} -x -a \"({func_name})\"")
124                elif arg_type == "logo":
125                    complete_commands.append(f"{command_prefix} -x -a \"(__fastfetch_complete_logo)\"")
126                elif arg_type == "structure":
127                    complete_commands.append(f"{command_prefix} -x -a \"(__fish_complete_list : __fastfetch_complete_structure)\"")
128                elif arg_type == "path":
129                    complete_commands.append(f"{command_prefix} -r -F")
130                else:
131                    complete_commands.append(f"{command_prefix} -x")
132            else:
133                complete_commands.append(f"{command_prefix} -f")
134
135    # Print generated enum functions first, then complete rules
136    print("\n".join(enum_functions))
137    print("\n".join(complete_commands))
138
139
140if __name__ == "__main__":
141    try:
142        main()
143    except:
144        sys.exit(1)
145' | python3 | source