-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_command.py
76 lines (57 loc) · 2.21 KB
/
base_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Provides the base command class."""
##############################################################################
# Textual imports.
from textual.widget import Widget
##############################################################################
class InputCommand:
"""Base class for input commands."""
COMMAND = ""
"""The command for the help."""
ALIASES = ""
"""Any aliases for the command for the help."""
ARGUMENTS = ""
"""Any arguments for the command for the help."""
@classmethod
def help_text(cls) -> str:
"""Get the help text for the command.
Returns:
The help text formatted as a Markdown table row.
"""
return f"| {cls.COMMAND} | {cls.ALIASES} | {cls.ARGUMENTS} | {cls.__doc__} |"
@classmethod
def handle(cls, text: str, for_widget: Widget) -> bool:
"""Handle the command.
Args:
text: The text of the command.
for_widget: The widget to handle the command for.
Returns:
`True` if the command was handled; `False` if not.
"""
return False
@staticmethod
def split_command(text: str) -> tuple[str, str]:
"""Split the command for further testing.
Args:
text: The text of the command.
Returns:
The command and its tail.
"""
command, _, tail = text.strip().partition(" ")
return command.strip(), tail.strip()
@classmethod
def is_command(cls, command: str) -> bool:
"""Does the given command appear to be a match?
Args:
command: The command to test.
Returns:
`True` if the given command seems to be a match, `False` if not.
"""
# Build up all the possible matches. These are built from the main
# command and also the aliases. By convention the code will often
# use `code` fences for commands, and the aliases will be a comma
# list, so we clean that up as we go...
return command.strip().lower() in (
candidate.strip().lower().removeprefix("`").removesuffix("`")
for candidate in (cls.COMMAND, *cls.ALIASES.split(","))
)
### base_command.py ends here