@@ -12,10 +12,6 @@ const cmd = new Command("Monitor program output", {
1212 ) => {
1313 const echo = ! ( options [ "no-echo" ] as boolean ) ;
1414
15- const rl = readline . createInterface ( { input : process . stdin } ) ;
16- readline . emitKeypressEvents ( process . stdin , rl ) ;
17- process . stdin . setRawMode ( true ) ;
18-
1915 const port = options [ "port" ] as string ;
2016 const baudrate = options [ "baudrate" ] as string ;
2117 const socket = options [ "socket" ] as string ;
@@ -29,30 +25,51 @@ const cmd = new Command("Monitor program output", {
2925 stdout . write ( chalk . red ( new TextDecoder ( ) . decode ( data ) ) ) ;
3026 } ) ;
3127
32- await new Promise ( ( resolve ) => {
33- process . stdin . on ( "keypress" , ( str , key ) => {
34- if ( key . ctrl && key . name === "c" ) {
35- device . programOutput . onData ( undefined ) ;
36- device . programError . onData ( undefined ) ;
37- process . stdin . setRawMode ( false ) ;
38- rl . close ( ) ;
39- resolve ( null ) ;
40- } else {
41- if ( echo ) {
42- if ( key . sequence === "\r" ) {
43- stdout . write ( "\r\n" ) ;
44- } else if ( str ) {
45- stdout . write ( str ) ;
28+ const cleanup = ( ) => {
29+ device . programOutput . onData ( undefined ) ;
30+ device . programError . onData ( undefined ) ;
31+ } ;
32+
33+ if ( process . stdin . isTTY ) {
34+ const rl = readline . createInterface ( { input : process . stdin } ) ;
35+ readline . emitKeypressEvents ( process . stdin , rl ) ;
36+ process . stdin . setRawMode ( true ) ;
37+
38+ await new Promise ( ( resolve ) => {
39+ process . stdin . on ( "keypress" , ( str , key ) => {
40+ if ( key . ctrl && key . name === "c" ) {
41+ cleanup ( ) ;
42+ process . stdin . setRawMode ( false ) ;
43+ rl . close ( ) ;
44+ resolve ( null ) ;
45+ } else {
46+ if ( echo ) {
47+ if ( key . sequence === "\r" ) {
48+ stdout . write ( "\r\n" ) ;
49+ } else if ( str ) {
50+ stdout . write ( str ) ;
51+ }
4652 }
53+ let out = key . sequence ;
54+ if ( out === "\r" ) {
55+ out = "\n" ;
56+ }
57+ device . programInput . write ( new TextEncoder ( ) . encode ( out ) ) ;
4758 }
48- let out = key . sequence ;
49- if ( out === "\r" ) {
50- out = "\n" ;
51- }
52- device . programInput . write ( new TextEncoder ( ) . encode ( out ) ) ;
53- }
59+ } ) ;
5460 } ) ;
55- } ) ;
61+ } else {
62+ const rl = readline . createInterface ( { input : process . stdin } ) ;
63+ rl . on ( "line" , ( line ) => {
64+ device . programInput . write ( new TextEncoder ( ) . encode ( line + "\n" ) ) ;
65+ } ) ;
66+ await new Promise ( ( resolve ) => {
67+ rl . on ( "close" , ( ) => {
68+ cleanup ( ) ;
69+ resolve ( null ) ;
70+ } ) ;
71+ } ) ;
72+ }
5673 } ,
5774 options : {
5875 "no-echo" : new Opt ( "Echo input" , { isFlag : true } ) ,
0 commit comments