Added mdel command
parent
8da3282edf
commit
a7078f9a77
|
@ -1,4 +1,5 @@
|
||||||
from prompt_toolkit.formatted_text import FormattedText
|
from prompt_toolkit.formatted_text import FormattedText
|
||||||
|
from prompt_toolkit.formatted_text import HTML
|
||||||
from prompt_toolkit.shortcuts import clear as clear_screen
|
from prompt_toolkit.shortcuts import clear as clear_screen
|
||||||
|
|
||||||
from lamb.runstatus import CommandStatus
|
from lamb.runstatus import CommandStatus
|
||||||
|
@ -16,11 +17,29 @@ def lamb_command(*, help_text: str):
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
def run(command, runner):
|
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")
|
@lamb_command(help_text = "Show macros")
|
||||||
def macros(runner):
|
def macros(command, runner):
|
||||||
return CommandStatus(
|
return CommandStatus(
|
||||||
formatted_text = FormattedText([
|
formatted_text = FormattedText([
|
||||||
("#FF6600 bold", "\nDefined Macros:\n"),
|
("#FF6600 bold", "\nDefined Macros:\n"),
|
||||||
|
@ -33,13 +52,13 @@ def macros(runner):
|
||||||
)
|
)
|
||||||
|
|
||||||
@lamb_command(help_text = "Clear the screen")
|
@lamb_command(help_text = "Clear the screen")
|
||||||
def clear(runner):
|
def clear(command, runner):
|
||||||
clear_screen()
|
clear_screen()
|
||||||
utils.show_greeting()
|
utils.show_greeting()
|
||||||
|
|
||||||
|
|
||||||
@lamb_command(help_text = "Print this help")
|
@lamb_command(help_text = "Print this help")
|
||||||
def help(runner):
|
def help(command, runner):
|
||||||
return CommandStatus(
|
return CommandStatus(
|
||||||
formatted_text = FormattedText([
|
formatted_text = FormattedText([
|
||||||
("#FF6600 bold", "\nUsage:\n"),
|
("#FF6600 bold", "\nUsage:\n"),
|
||||||
|
|
|
@ -56,7 +56,7 @@ class Parser:
|
||||||
(lp + pp_call + rp)
|
(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)
|
pp_command.set_parse_action(tokens.command.from_parse)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from distutils.cmd import Command
|
||||||
from prompt_toolkit.formatted_text import FormattedText
|
from prompt_toolkit.formatted_text import FormattedText
|
||||||
|
|
||||||
import lamb.commands as commands
|
import lamb.commands as commands
|
||||||
|
@ -15,8 +16,8 @@ class Runner:
|
||||||
# If None, no maximum is enforced.
|
# If None, no maximum is enforced.
|
||||||
self.reduction_limit: int | None = 300
|
self.reduction_limit: int | None = 300
|
||||||
|
|
||||||
def exec_command(self, command: str) -> rs.CommandStatus:
|
def exec_command(self, command: tokens.command) -> rs.CommandStatus:
|
||||||
if command in commands.commands:
|
if command.name in commands.commands:
|
||||||
return commands.run(command, self)
|
return commands.run(command, self)
|
||||||
|
|
||||||
# Handle unknown commands
|
# Handle unknown commands
|
||||||
|
@ -80,7 +81,7 @@ class Runner:
|
||||||
|
|
||||||
# If this line is a command, do the command.
|
# If this line is a command, do the command.
|
||||||
elif isinstance(e, tokens.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.
|
# If this line is a plain expression, reduce it.
|
||||||
elif isinstance(e, tokens.LambdaToken):
|
elif isinstance(e, tokens.LambdaToken):
|
||||||
|
|
|
@ -81,10 +81,12 @@ class command:
|
||||||
def from_parse(result):
|
def from_parse(result):
|
||||||
return command(
|
return command(
|
||||||
result[0],
|
result[0],
|
||||||
|
result[1:]
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name, args):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.args = args
|
||||||
|
|
||||||
class macro(LambdaToken):
|
class macro(LambdaToken):
|
||||||
"""
|
"""
|
||||||
|
|
Reference in New Issue