11import { CmdLet } from '../../commands/cmdlet.js' ;
2- import { Command } from '../../commands/command .js' ;
2+ import { CharCode } from '../../io/char .js' ;
33import { Input } from '../../io/input.js' ;
44import { Output } from '../../io/output.js' ;
5- import { Parser } from '../../io/parser.js' ;
65import { Token } from '../../io/token.js' ;
6+ import { History } from '../../terminal/history.js' ;
77import { Var } from '../../vars/var.js' ;
8- import { Vars } from '../../vars/vars.js' ;
98
109export class SrcCmdLet extends CmdLet {
1110 name = 'src' ;
@@ -19,55 +18,57 @@ src path - run commands from file
1918
2019 private static lastPath : string | null = null ;
2120
22- public static loadInitScript ( path : string ) {
21+ public static async loadInitScript ( path : string ) {
2322 this . lastPath = path ;
2423 const src = new SrcCmdLet ( ) ;
25- src . runScript ( path ) ;
24+ await src . runScript ( path ) ;
2625 }
2726
28- public runSync ( tokens : Token [ ] ) : Var {
27+ public override runSync ( _tokens : Token [ ] ) : Var {
28+ throw new Error ( "can't run in synchronous mode" ) ;
29+ }
30+
31+ public override async run ( tokens : Token [ ] ) : Promise < Var > {
2932 const vars = this . transformOptional ( tokens , [ ] , [ this . parseLiteral ] ) ;
3033 if ( vars === null ) return this . usage ( ) ;
3134 // eslint-disable-next-line prefer-const
3235 let [ _ , [ name ] ] = vars as [ [ ] , [ string | null ] ] ;
3336 if ( name === null ) {
3437 if ( SrcCmdLet . lastPath === null ) throw new Error ( 'path not initialized' ) ;
3538
36- Output . writeln ( `Loading: ${ SrcCmdLet . lastPath } ` ) ;
37- this . runScript ( SrcCmdLet . lastPath ) ;
39+ await this . runScript ( SrcCmdLet . lastPath ) ;
3840 return Var . ZERO ;
3941 } else {
4042 if ( name . length > 1 && name . startsWith ( '"' ) && name . endsWith ( '"' ) ) {
4143 name = name . slice ( 1 , name . length - 1 ) ;
4244 }
4345
44- Output . writeln ( `Loading: ${ name } ` ) ;
4546 SrcCmdLet . lastPath = name ;
46- this . runScript ( name ) ;
47+ await this . runScript ( name ) ;
4748
4849 return Var . ZERO ;
4950 }
5051 }
5152
52- private runScript ( path : string ) {
53+ private async runScript ( path : string ) {
5354 try {
55+ Output . writeln ( `Loading: ${ Output . green ( path ) } ` ) ;
56+
5457 const initScript = File . readAllText ( path ) ;
5558 const lines = initScript . split ( '\n' ) ;
59+
60+ History . clearLine ( ) ;
61+ Input . prompt ( ) ;
62+
5663 for ( const line of lines ) {
5764 if ( line . length === 0 ) continue ;
5865 if ( line . charAt ( 0 ) === '#' ) continue ;
59-
60- Output . writeln ( `${ Output . bold ( Input . PROMPT ) } ${ line } ` ) ;
61-
62- if ( line . trim ( ) . length === 0 ) continue ;
63-
64- const parser = new Parser ( line . toString ( ) ) ;
65- const tokens = parser . tokenize ( ) ;
66- const ret = Command . runSync ( tokens ) ;
67- Vars . setRet ( ret ) ;
68- Output . writeRet ( ) ;
69- Output . writeln ( ) ;
66+ Output . write ( line ) ;
67+ await Input . read ( `${ line } ${ String . fromCharCode ( CharCode . CR ) } ` ) ;
7068 }
69+
70+ Output . clearLine ( ) ;
71+ Output . writeln ( `Loaded: ${ Output . green ( path ) } ` ) ;
7172 } catch ( _ ) {
7273 /* Ignore the error */
7374 }
0 commit comments