Skip to content

Commit 2a553b0

Browse files
author
Your Name
committed
Add echo commandlet
1 parent 5905687 commit 2a553b0

6 files changed

Lines changed: 91 additions & 17 deletions

File tree

src/cmdlets/development/js.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
} from '../trace/trace.js';
8181
import { MacroCmdLet } from '../misc/macro.js';
8282
import { ReplaceCmdLet } from '../breakpoints/replace.js';
83+
import { EchoCmdLet } from '../misc/echo.js';
8384

8485
export class JsCmdLet extends CmdLetBase {
8586
name = 'js';
@@ -121,6 +122,7 @@ js path - load commandlet JS script
121122
CoverageBpCmdLet: CoverageBpCmdLet,
122123
DivCmdLet: DivCmdLet,
123124
DumpCmdLet: DumpCmdLet,
125+
EchoCmdLet: EchoCmdLet,
124126
EqCmdLet: EqCmdLet,
125127
ExitCmdLet: ExitCmdLet,
126128
FalseCmdLet: FalseCmdLet,

src/cmdlets/misc/echo.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { CmdLetBase } from '../../commands/cmdlet.js';
2+
import { Output } from '../../io/output.js';
3+
import { Token } from '../../io/token.js';
4+
import { Var } from '../../vars/var.js';
5+
6+
export class EchoCmdLet extends CmdLetBase {
7+
name = 'echo';
8+
category = 'misc';
9+
help = 'toggle echo mode';
10+
11+
public static echo: boolean = true;
12+
13+
private static readonly USAGE: string = `Usage: echo
14+
echo on - enable echo (default)
15+
16+
echo off - disable echo`;
17+
18+
public runSync(tokens: Token[]): Var {
19+
const vars = this.transformOptional(tokens, [], [this.parseSwitch]);
20+
if (vars === null) return this.usage();
21+
const [, [state]] = vars as [[], [boolean | null]];
22+
if (state === null) {
23+
Output.writeln(
24+
`echo is [${EchoCmdLet.echo ? Output.green('on') : Output.red('off')}]`,
25+
);
26+
return Var.ZERO;
27+
}
28+
EchoCmdLet.echo = state;
29+
return Var.ZERO;
30+
}
31+
32+
protected parseSwitch(token: Token): boolean | null {
33+
const literal = token.getLiteral();
34+
if (literal === 'on') return true;
35+
if (literal === 'off') return false;
36+
return null;
37+
}
38+
39+
public usage(): Var {
40+
Output.writeln(EchoCmdLet.USAGE);
41+
return Var.ZERO;
42+
}
43+
}

src/commands/cmdlets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
} from '../cmdlets/trace/trace.js';
6868
import { MacroCmdLet } from '../cmdlets/misc/macro.js';
6969
import { ReplaceCmdLet } from '../cmdlets/breakpoints/replace.js';
70+
import { EchoCmdLet } from '../cmdlets/misc/echo.js';
7071

7172
export class CmdLets {
7273
private static byName: Map<string, CmdLet> = new Map<string, CmdLet>();
@@ -85,6 +86,7 @@ export class CmdLets {
8586
this.registerCmdletType(DivCmdLet);
8687
this.registerCmdletType(DumpCmdLet);
8788
this.registerCmdletType(DumpStringCmdLet);
89+
this.registerCmdletType(EchoCmdLet);
8890
this.registerCmdletType(EndianCmdLet);
8991
this.registerCmdletType(EqCmdLet);
9092
this.registerCmdletType(ExitCmdLet);

src/commands/command.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,54 @@ import { Token } from '../io/token.js';
66
import { CmdLet } from './cmdlet.js';
77
import { Macro, Macros } from '../macros/macros.js';
88
import { MacroCmdLet } from '../cmdlets/misc/macro.js';
9+
import { EchoCmdLet } from '../cmdlets/misc/echo.js';
910

1011
export class Command {
1112
private static readonly MACRO_PREFIX: string = '!';
1213
public static async run(tokens: Token[]): Promise<Var> {
13-
const cmdlet = this.getCmdlet(tokens);
14-
if (cmdlet !== null) {
15-
return cmdlet.run(tokens.slice(1));
14+
let suppressed = false;
15+
if (!EchoCmdLet.echo && !Output.isSuppressed()) {
16+
Output.suppress(true);
17+
suppressed = true;
1618
}
19+
try {
20+
const cmdlet = this.getCmdlet(tokens);
21+
if (cmdlet !== null) {
22+
return cmdlet.run(tokens.slice(1));
23+
}
1724

18-
const macro = this.getMacro(tokens);
19-
if (macro !== null) {
20-
return MacroCmdLet.runSync(macro, tokens.slice(1));
21-
}
25+
const macro = this.getMacro(tokens);
26+
if (macro !== null) {
27+
return MacroCmdLet.runSync(macro, tokens.slice(1));
28+
}
2229

23-
return this.runFunction(tokens);
30+
return this.runFunction(tokens);
31+
} finally {
32+
if (suppressed) Output.suppress(false);
33+
}
2434
}
2535

2636
public static runSync(tokens: Token[]): Var {
27-
const cmdlet = this.getCmdlet(tokens);
28-
if (cmdlet !== null) {
29-
return cmdlet.runSync(tokens.slice(1));
37+
let suppressed = false;
38+
if (!EchoCmdLet.echo && !Output.isSuppressed()) {
39+
Output.suppress(true);
40+
suppressed = true;
3041
}
42+
try {
43+
const cmdlet = this.getCmdlet(tokens);
44+
if (cmdlet !== null) {
45+
return cmdlet.runSync(tokens.slice(1));
46+
}
3147

32-
const macro = this.getMacro(tokens);
33-
if (macro !== null) {
34-
return MacroCmdLet.runSync(macro, tokens.slice(1));
35-
}
48+
const macro = this.getMacro(tokens);
49+
if (macro !== null) {
50+
return MacroCmdLet.runSync(macro, tokens.slice(1));
51+
}
3652

37-
return this.runFunction(tokens);
53+
return this.runFunction(tokens);
54+
} finally {
55+
if (suppressed) Output.suppress(false);
56+
}
3857
}
3958

4059
private static getCmdlet(tokens: Token[]): CmdLet | null {

src/io/input.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Output } from './output.js';
22
import { History } from '../terminal/history.js';
33
import { Vars } from '../vars/vars.js';
44
import { CharCode, Vt } from './char.js';
5+
import { EchoCmdLet } from '../cmdlets/misc/echo.js';
56

67
enum InputState {
78
Default,
@@ -12,6 +13,7 @@ enum InputState {
1213
export class Input {
1314
public static readonly PROMPT: string = '-> ';
1415
public static readonly FILTERED_PROMPT: string = '~> ';
16+
public static readonly NO_ECHO_PROMPT: string = '#> ';
1517
private static readonly EDIT_PROMPT: string = '. ';
1618

1719
private static readonly QUIT_CHAR: string = 'q';
@@ -121,7 +123,9 @@ export class Input {
121123
public static prompt() {
122124
Output.clearLine();
123125
if (this.interceptLine === null) {
124-
if (Output.isFiltered()) {
126+
if (!EchoCmdLet.echo) {
127+
Output.write(Output.bold(this.NO_ECHO_PROMPT));
128+
} else if (Output.isFiltered()) {
125129
Output.write(Output.bold(this.FILTERED_PROMPT));
126130
} else {
127131
Output.write(Output.bold(this.PROMPT));

src/io/output.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@ export class Output {
182182
public static suppress(suppressed: boolean) {
183183
this.suppressed = suppressed;
184184
}
185+
186+
public static isSuppressed(): boolean {
187+
return this.suppressed;
188+
}
185189
}

0 commit comments

Comments
 (0)