Change completion output file.

This commit is contained in:
Sergey Vartanov 2021-10-11 00:44:54 +03:00
parent 9f770a35cc
commit b358e56223
3 changed files with 39 additions and 22 deletions

3
.gitignore vendored
View file

@ -22,5 +22,6 @@ cache/
work work
precommit.py precommit.py
venv
.idea .idea
temp
venv

View file

@ -1,4 +1,10 @@
"""
Creating fish shell autocompletion commands.
See https://fishshell.com/docs/current/completions.html
"""
import argparse import argparse
from pathlib import Path
from typing import Any from typing import Any
from map_machine import ui from map_machine import ui
@ -7,7 +13,7 @@ from map_machine.ui import COMMANDS
class ArgumentParser(argparse.ArgumentParser): class ArgumentParser(argparse.ArgumentParser):
""" """
Argument parser that stores arguments and creates help in Moire markup. Argument parser that generates fish shell autocompletion commands.
""" """
def __init__(self, *args, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
@ -25,36 +31,34 @@ class ArgumentParser(argparse.ArgumentParser):
self.arguments.append(argument) self.arguments.append(argument)
def get_complete(self, command: str) -> str: def get_complete(self, command: str) -> str:
""" """Return fish complete command."""
Return Moire table with "Option" and "Description" columns filled with result: str = ""
arguments.
"""
s = ""
for argument in self.arguments: for argument in self.arguments:
s += "complete -c map-machine" result += "complete -c map-machine"
s += f' -n "__fish_seen_subcommand_from {command}"' result += f' -n "__fish_seen_subcommand_from {command}"'
if len(argument["arguments"]) == 2: if len(argument["arguments"]) == 2:
s += f" -s {argument['arguments'][0][1:]}" result += f" -s {argument['arguments'][0][1:]}"
s += f" -l {argument['arguments'][1][2:]}" result += f" -l {argument['arguments'][1][2:]}"
else: else:
s += f" -l {argument['arguments'][0][2:]}" result += f" -l {argument['arguments'][0][2:]}"
if "help" in argument: if "help" in argument:
s += f" -d \"{argument['help']}\"" result += f' -d "{argument["help"]}"'
s += "\n" result += "\n"
return s return result
if __name__ == "__main__": def completion_commands() -> str:
"""Print fish completion commands."""
commands: str = " ".join(COMMANDS) commands: str = " ".join(COMMANDS)
print(f"set -l commands {commands}") result: str = ""
print("complete -c map-machine -f") result += f"set -l commands {commands}\n"
print( result += "complete -c map-machine -f\n"
result += (
f'complete -c map-machine -n "not __fish_seen_subcommand_from ' f'complete -c map-machine -n "not __fish_seen_subcommand_from '
f'$commands" -a "{commands}"' f'$commands" -a "{commands}"\n'
) )
for command in COMMANDS: for command in COMMANDS:
if command in ["icons", "taginfo"]: if command in ["icons", "taginfo"]:
continue continue
@ -75,4 +79,12 @@ if __name__ == "__main__":
raise NotImplementedError( raise NotImplementedError(
f"no separate function for parser creation for {command}" f"no separate function for parser creation for {command}"
) )
print(parser.get_complete(command)) result += parser.get_complete(command) + "\n"
return result
if __name__ == "__main__":
completions_path: Path = Path("~/.config/fish/completions/map-machine.fish")
with completions_path.open("w+") as output_file:
output_file.write(completion_commands())

View file

@ -1,6 +1,7 @@
""" """
Draw test nodes, ways, and relations. Draw test nodes, ways, and relations.
""" """
import logging
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -156,7 +157,10 @@ def draw(
with output_path.open("w") as output_file: with output_path.open("w") as output_file:
svg.write(output_file) svg.write(output_file)
logging.info(f"Map is drawn to {output_path}.")
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(format="%(levelname)s %(message)s", level=logging.INFO)
lanes() lanes()