|
| 1 | +import { CmdLet } from '../../commands/cmdlet.js'; |
| 2 | +import { Command } from '../../commands/command.js'; |
| 3 | +import { Input, InputInterceptLine } from '../../io/input.js'; |
| 4 | +import { Output } from '../../io/output.js'; |
| 5 | +import { Parser } from '../../io/parser.js'; |
| 6 | +import { Token } from '../../io/token.js'; |
| 7 | +import { Macro, Macros } from '../../macros/macros.js'; |
| 8 | +import { Var } from '../../vars/var.js'; |
| 9 | +import { Vars } from '../../vars/vars.js'; |
| 10 | + |
| 11 | +export class MacroCmdLet extends CmdLet implements InputInterceptLine { |
| 12 | + name = 'm'; |
| 13 | + category = 'misc'; |
| 14 | + help = 'manage macros'; |
| 15 | + private static readonly USAGE: string = `Usage: m |
| 16 | +m - show all macros |
| 17 | +
|
| 18 | +m name - create, modify or display a macro |
| 19 | + name the name of the macro |
| 20 | +
|
| 21 | +m name ${CmdLet.DELETE_CHAR} - delete a macro |
| 22 | + name the name of the macro to delete`; |
| 23 | + |
| 24 | + private current: string | null = null; |
| 25 | + private commands: string[] = []; |
| 26 | + |
| 27 | + public runSync(tokens: Token[]): Var { |
| 28 | + const retWithNameAndHash = this.runDelete(tokens); |
| 29 | + if (retWithNameAndHash !== null) return retWithNameAndHash; |
| 30 | + |
| 31 | + const retWithNameAndPointer = this.runSet(tokens); |
| 32 | + if (retWithNameAndPointer !== null) return retWithNameAndPointer; |
| 33 | + |
| 34 | + const retWithName = this.runShow(tokens); |
| 35 | + if (retWithName !== null) return retWithName; |
| 36 | + |
| 37 | + return this.usage(); |
| 38 | + } |
| 39 | + |
| 40 | + private runDelete(tokens: Token[]): Var | null { |
| 41 | + const vars = this.transform(tokens, [this.parseLiteral, this.parseDelete]); |
| 42 | + if (vars === null) return null; |
| 43 | + const [name, _] = vars as [string, string]; |
| 44 | + |
| 45 | + const macro = Macros.delete(name); |
| 46 | + if (macro === null) { |
| 47 | + Output.writeln(`macro ${Output.green(name)} not set`); |
| 48 | + } else { |
| 49 | + Output.writeln(`deleted macro ${Output.green(name)}`); |
| 50 | + } |
| 51 | + return Var.ZERO; |
| 52 | + } |
| 53 | + |
| 54 | + private runSet(tokens: Token[]): Var | null { |
| 55 | + const vars = this.transform(tokens, [this.parseLiteral]); |
| 56 | + if (vars === null) return null; |
| 57 | + const [name] = vars as [string]; |
| 58 | + this.commands = []; |
| 59 | + this.current = name; |
| 60 | + const macro = Macros.get(name); |
| 61 | + if (macro === null) { |
| 62 | + Output.writeln(`Creating macro '${Output.green(name)}'`); |
| 63 | + } else { |
| 64 | + Output.writeln(`Modifying macro '${Output.green(name)}'`); |
| 65 | + Output.writeln(macro.toString()); |
| 66 | + } |
| 67 | + Input.setInterceptLine(this); |
| 68 | + return Var.ZERO; |
| 69 | + } |
| 70 | + |
| 71 | + addLine(line: string): void { |
| 72 | + this.commands.push(line); |
| 73 | + } |
| 74 | + |
| 75 | + clear(): void { |
| 76 | + if (this.current != null) { |
| 77 | + const macro = new Macro(this.current, []); |
| 78 | + Macros.set(macro); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + done(): void { |
| 83 | + if (this.current != null) { |
| 84 | + const macro = new Macro(this.current, this.commands); |
| 85 | + Macros.set(macro); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + abort(): void {} |
| 90 | + |
| 91 | + private runShow(tokens: Token[]): Var | null { |
| 92 | + if (tokens.length !== 0) return null; |
| 93 | + Output.writeln(Output.blue('Macros:')); |
| 94 | + for (const macro of Macros.all()) { |
| 95 | + Output.writeln(`${Output.green(macro.name)}:`, true); |
| 96 | + |
| 97 | + Output.writeln(macro.toString(), true); |
| 98 | + |
| 99 | + Output.writeln(); |
| 100 | + } |
| 101 | + return Var.ZERO; |
| 102 | + } |
| 103 | + |
| 104 | + public usage(): Var { |
| 105 | + Output.writeln(MacroCmdLet.USAGE); |
| 106 | + return Var.ZERO; |
| 107 | + } |
| 108 | + |
| 109 | + public static run(macro: Macro): Var { |
| 110 | + let ret = Var.ZERO; |
| 111 | + for (const command of macro.commands) { |
| 112 | + if (command.length === 0) continue; |
| 113 | + if (command.charAt(0) === '#') continue; |
| 114 | + |
| 115 | + Output.writeln(`${Output.bold(Input.PROMPT)}${command}`); |
| 116 | + |
| 117 | + if (command.trim().length === 0) continue; |
| 118 | + |
| 119 | + const parser = new Parser(command.toString()); |
| 120 | + const tokens = parser.tokenize(); |
| 121 | + ret = Command.runSync(tokens); |
| 122 | + Vars.setRet(ret); |
| 123 | + Output.writeRet(); |
| 124 | + Output.writeln(); |
| 125 | + } |
| 126 | + return ret; |
| 127 | + } |
| 128 | +} |
0 commit comments