Skip to content

Commit 94b6f0f

Browse files
authored
fish completion: pipe straight to Python (#1076)
In fish-shell/fish-shell#10599, a user of an older version of fish has run into an issue with the fastfetch completion requiring a relatively new version. Rewrite the (really quite clever) embedded Python into a straight pipe, dropping the string transformation entirely. Does require some escaping of quotes to support Python < 3.12.
1 parent 3b1b814 commit 94b6f0f

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

completions/fastfetch.fish

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -54,51 +54,48 @@ function __fastfetch_complete_structure
5454
end
5555
end
5656

57-
string match -r -a -g "^###> ?(.*)" < (status -f) | string collect | python3 | source
57+
echo '
58+
import json, subprocess, sys
5859
59-
###> #!/usr/bin/env python3
60-
###>
61-
###> import json, subprocess, sys
62-
###>
63-
###> def main():
64-
###> data: dict[str, list[dict]] = json.loads(subprocess.check_output(['fastfetch', '--help-raw']))
65-
###>
66-
###> for key in data:
67-
###> for flag in data[key]:
68-
###> if flag.get('pseudo', False):
69-
###> continue
70-
###>
71-
###> command_prefix: str = f'complete -c fastfetch -d "{flag["desc"]}" -l "{flag["long"]}"';
72-
###> if 'short' in flag:
73-
###> command_prefix += f' -o "{flag["short"]}"'
74-
###>
75-
###> if 'arg' in flag:
76-
###> type: str = flag['arg']['type'];
77-
###> if type == 'bool':
78-
###> print(f'{command_prefix} -x -a "(__fastfetch_complete_bool)"')
79-
###> elif type == 'color':
80-
###> print(f'{command_prefix} -x -a "(__fastfetch_complete_color)"')
81-
###> elif type == 'command':
82-
###> print(f'{command_prefix} -x -a "(__fastfetch_complete_command)"')
83-
###> elif type == 'config':
84-
###> print(f'{command_prefix} -x -a "(__fastfetch_complete_config)"')
85-
###> elif type == 'enum':
86-
###> temp: str = ' '.join(flag["arg"]["enum"])
87-
###> print(f'{command_prefix} -x -a "{temp}"')
88-
###> elif type == 'logo':
89-
###> print(f'{command_prefix} -x -a "(__fastfetch_complete_logo)"')
90-
###> elif type == 'structure':
91-
###> print(f'{command_prefix} -x -a "(__fish_complete_list : __fastfetch_complete_structure)"')
92-
###> elif type == 'path':
93-
###> print(f'{command_prefix} -r -F')
94-
###> else:
95-
###> print(f'{command_prefix} -x')
96-
###> else:
97-
###> print(f'{command_prefix} -f')
98-
###>
99-
###> if __name__ == "__main__":
100-
###> try:
101-
###> main()
102-
###> except:
103-
###> sys.exit(1)
104-
###>
60+
def main():
61+
data: dict[str, list[dict]] = json.loads(subprocess.check_output(["fastfetch", "--help-raw"]))
62+
63+
for key in data:
64+
for flag in data[key]:
65+
if flag.get("pseudo", False):
66+
continue
67+
68+
command_prefix = f"""complete -c fastfetch -d "{flag["desc"]}" -l "{flag["long"]}\""""
69+
if "short" in flag:
70+
command_prefix += f""" -o {flag["short"]}"""
71+
72+
if "arg" in flag:
73+
type: str = flag["arg"]["type"];
74+
if type == "bool":
75+
print(f"{command_prefix} -x -a \"(__fastfetch_complete_bool)\"")
76+
elif type == "color":
77+
print(f"{command_prefix} -x -a \"(__fastfetch_complete_color)\"")
78+
elif type == "command":
79+
print(f"{command_prefix} -x -a \"(__fastfetch_complete_command)\"")
80+
elif type == "config":
81+
print(f"{command_prefix} -x -a \"(__fastfetch_complete_config)\"")
82+
elif type == "enum":
83+
temp: str = " ".join(flag["arg"]["enum"])
84+
print(f"{command_prefix} -x -a \"{temp}\"")
85+
elif type == "logo":
86+
print(f"{command_prefix} -x -a \"(__fastfetch_complete_logo)\"")
87+
elif type == "structure":
88+
print(f"{command_prefix} -x -a \"(__fish_complete_list : __fastfetch_complete_structure)\"")
89+
elif type == "path":
90+
print(f"{command_prefix} -r -F")
91+
else:
92+
print(f"{command_prefix} -x")
93+
else:
94+
print(f"{command_prefix} -f")
95+
96+
if __name__ == "__main__":
97+
try:
98+
main()
99+
except:
100+
sys.exit(1)
101+
' | python3 | source

0 commit comments

Comments
 (0)