Skip to content

Commit 773b8db

Browse files
AtifChyCarterLi
andauthored
Completion: add zsh-completion (#1213)
* feat: add zsh-completion * zsh completion: added missing custom options * cmake: update CMakeList.txt to install zsh completion * Update CMakeLists.txt --------- Co-authored-by: Carter Li <CarterLi@users.noreply.github.com>
1 parent d15d4c6 commit 773b8db

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,12 @@ install(
13371337
RENAME "${CMAKE_PROJECT_NAME}"
13381338
)
13391339

1340+
install(
1341+
FILES "${CMAKE_SOURCE_DIR}/completions/${CMAKE_PROJECT_NAME}.zsh"
1342+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/zsh/site-functions"
1343+
RENAME "_${CMAKE_PROJECT_NAME}"
1344+
)
1345+
13401346
install(
13411347
FILES "${CMAKE_SOURCE_DIR}/completions/${CMAKE_PROJECT_NAME}.fish"
13421348
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/fish/vendor_completions.d"

completions/fastfetch.zsh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#compdef fastfetch
2+
3+
function _fastfetch() {
4+
local state
5+
6+
local -a opts
7+
opts=(${(f)"$(
8+
python <<EOF
9+
import json
10+
import subprocess
11+
import sys
12+
13+
14+
def main():
15+
data: dict[str, list[dict]] = json.loads(
16+
subprocess.check_output(["fastfetch", "--help-raw"])
17+
)
18+
19+
for key in data:
20+
for flag in data[key]:
21+
if flag.get("pseudo", False):
22+
continue
23+
24+
if "short" in flag:
25+
command_prefix = f"""-{flag["short"]}[{flag["desc"]}]"""
26+
print_command(command_prefix, flag)
27+
28+
if "long" in flag:
29+
command_prefix = f"""--{flag["long"]}[{flag["desc"]}]"""
30+
print_command(command_prefix, flag)
31+
32+
33+
def print_command(command_prefix: str, flag: dict):
34+
if "arg" in flag:
35+
type: str = flag["arg"]["type"]
36+
if type == "bool":
37+
print(f"{command_prefix}:bool:(true false)")
38+
elif type == "color":
39+
print(f"{command_prefix}:color:(black red green yellow blue magenta cyan white default)")
40+
elif type == "command":
41+
print(f"{command_prefix}:module:->modules")
42+
elif type == "config":
43+
print(f"{command_prefix}:presets:->presets")
44+
elif type == "enum":
45+
temp: str = " ".join(flag["arg"]["enum"])
46+
print(f'{command_prefix}:type:( {temp} )')
47+
elif type == "logo":
48+
print(f"{command_prefix}:logo:->logo")
49+
elif type == "structure":
50+
print(f"{command_prefix}:structure:->structure")
51+
elif type == "path":
52+
print(f"{command_prefix}:path:_files -/")
53+
else:
54+
print(f"{command_prefix}:")
55+
else:
56+
print(f"{command_prefix}")
57+
58+
59+
if __name__ == "__main__":
60+
try:
61+
main()
62+
except Exception:
63+
sys.exit(1)
64+
EOF
65+
)"})
66+
67+
_arguments -C "$opts[@]"
68+
69+
case $state in
70+
modules)
71+
local -a modules=( ${(f)"$(fastfetch --list-modules autocompletion)"} )
72+
modules=( ${(L)^modules%%:*}-format format color )
73+
_describe 'module' modules
74+
;;
75+
presets)
76+
local -a presets=(
77+
${$(fastfetch --list-presets autocompletion):#.*}
78+
"none:Disable loading config file"
79+
)
80+
_describe 'preset' presets
81+
;;
82+
structure)
83+
local -a structures=( ${(f)"$(fastfetch --list-modules autocompletion)"} )
84+
_describe 'structure' structures
85+
;;
86+
logo)
87+
local -a logos=(
88+
$(fastfetch --list-logos autocompletion)
89+
"none:Don't print logo"
90+
"small:Print small ascii logo if available"
91+
)
92+
_describe 'logo' logos
93+
;;
94+
esac
95+
}
96+
97+
_fastfetch "$@"

0 commit comments

Comments
 (0)