Added mdel command

master
Mark 2022-10-22 07:48:17 -07:00
parent 8da3282edf
commit a7078f9a77
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 31 additions and 9 deletions

View File

@ -1,4 +1,5 @@
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import clear as clear_screen
from lamb.runstatus import CommandStatus
@ -16,11 +17,29 @@ def lamb_command(*, help_text: str):
return inner
def run(command, runner):
return commands[command](runner)
return commands[command.name](command, runner)
@lamb_command(help_text = "Delete a macro")
def mdel(command, runner):
if len(command.args) != 1:
return CommandStatus(
formatted_text = HTML(
"<red>Command <grey>:mdel</grey> takes exactly one argument.</red>"
)
)
target = command.args[0]
if target not in runner.macro_table:
return CommandStatus(
formatted_text = HTML(
f"<red>Macro \"{target}\" is not defined</red>"
)
)
del runner.macro_table[target]
@lamb_command(help_text = "Show macros")
def macros(runner):
def macros(command, runner):
return CommandStatus(
formatted_text = FormattedText([
("#FF6600 bold", "\nDefined Macros:\n"),
@ -33,13 +52,13 @@ def macros(runner):
)
@lamb_command(help_text = "Clear the screen")
def clear(runner):
def clear(command, runner):
clear_screen()
utils.show_greeting()
@lamb_command(help_text = "Print this help")
def help(runner):
def help(command, runner):
return CommandStatus(
formatted_text = FormattedText([
("#FF6600 bold", "\nUsage:\n"),

View File

@ -56,7 +56,7 @@ class Parser:
(lp + pp_call + rp)
)
pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_")
pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") + pp.Word(pp.alphas + "_")[0, ...]
pp_command.set_parse_action(tokens.command.from_parse)

View File

@ -1,3 +1,4 @@
from distutils.cmd import Command
from prompt_toolkit.formatted_text import FormattedText
import lamb.commands as commands
@ -15,8 +16,8 @@ class Runner:
# If None, no maximum is enforced.
self.reduction_limit: int | None = 300
def exec_command(self, command: str) -> rs.CommandStatus:
if command in commands.commands:
def exec_command(self, command: tokens.command) -> rs.CommandStatus:
if command.name in commands.commands:
return commands.run(command, self)
# Handle unknown commands
@ -80,7 +81,7 @@ class Runner:
# If this line is a command, do the command.
elif isinstance(e, tokens.command):
return self.exec_command(e.name)
return self.exec_command(e)
# If this line is a plain expression, reduce it.
elif isinstance(e, tokens.LambdaToken):

View File

@ -81,10 +81,12 @@ class command:
def from_parse(result):
return command(
result[0],
result[1:]
)
def __init__(self, name):
def __init__(self, name, args):
self.name = name
self.args = args
class macro(LambdaToken):
"""