@@ -6,6 +6,75 @@ import { BoardsServiceProvider } from '../../boards/boards-service-provider';
6
6
import { MonitorModel } from '../../monitor-model' ;
7
7
import { Unknown } from '../../../common/nls' ;
8
8
9
+ class RingList {
10
+ protected ring : string [ ] ;
11
+ protected size : number ;
12
+ protected begin : number ;
13
+ protected index : number ;
14
+ protected end : number ;
15
+
16
+ constructor ( size : number = 100 ) {
17
+ this . Init = this . Init . bind ( this ) ;
18
+ this . Push = this . Push . bind ( this ) ;
19
+ this . Prev = this . Prev . bind ( this ) ;
20
+ this . Next = this . Next . bind ( this ) ;
21
+ this . Init ( size ) ;
22
+ }
23
+
24
+ public Init ( size : number = 100 )
25
+ {
26
+ this . ring = [ ] ;
27
+ this . size = ( size > 0 ) ? size : 1 ;
28
+ this . begin = 0 ;
29
+ this . index = 0 ;
30
+ this . end = - 1 ;
31
+ }
32
+
33
+ public Push ( val : string ) : number {
34
+ this . end ++ ;
35
+ if ( this . ring . length >= this . size )
36
+ {
37
+ if ( this . end >= this . size )
38
+ this . end = 0 ;
39
+ if ( this . end === this . begin )
40
+ {
41
+ this . begin ++ ;
42
+ if ( this . begin >= this . size )
43
+ this . begin = 0 ;
44
+ }
45
+ }
46
+ this . ring [ this . end ] = val ;
47
+ this . index = this . end ;
48
+
49
+ return this . index ;
50
+ }
51
+
52
+ public Prev ( ) : string {
53
+ if ( this . ring . length < 1 ) {
54
+ return "" ;
55
+ }
56
+
57
+ if ( this . index !== this . begin ) {
58
+ this . index = ( this . index > 0 ) ? -- this . index : this . size - 1 ;
59
+ }
60
+
61
+ return this . ring [ this . index ] ;
62
+ }
63
+
64
+ public Next ( ) : string {
65
+ if ( this . ring . length < 1 ) {
66
+ return "" ;
67
+ }
68
+
69
+ if ( this . index !== this . end ) {
70
+ this . index = ( ++ this . index < this . size ) ? this . index : 0 ;
71
+ }
72
+
73
+ return this . ring [ this . index ] ;
74
+ }
75
+
76
+ }
77
+
9
78
export namespace SerialMonitorSendInput {
10
79
export interface Props {
11
80
readonly boardsServiceProvider : BoardsServiceProvider ;
@@ -16,6 +85,7 @@ export namespace SerialMonitorSendInput {
16
85
export interface State {
17
86
text : string ;
18
87
connected : boolean ;
88
+ history : RingList ;
19
89
}
20
90
}
21
91
@@ -27,7 +97,7 @@ export class SerialMonitorSendInput extends React.Component<
27
97
28
98
constructor ( props : Readonly < SerialMonitorSendInput . Props > ) {
29
99
super ( props ) ;
30
- this . state = { text : '' , connected : true } ;
100
+ this . state = { text : '' , connected : true , history : new RingList ( ) } ;
31
101
this . onChange = this . onChange . bind ( this ) ;
32
102
this . onSend = this . onSend . bind ( this ) ;
33
103
this . onKeyDown = this . onKeyDown . bind ( this ) ;
@@ -110,7 +180,17 @@ export class SerialMonitorSendInput extends React.Component<
110
180
if ( keyCode ) {
111
181
const { key } = keyCode ;
112
182
if ( key === Key . ENTER ) {
183
+ // NOTE: order of operations is critical here. Push the current state.text
184
+ // onto the history stack before sending. After sending, state.text is empty
185
+ // and you'd end up pushing '' onto the history stack.
186
+ if ( this . state . text . length > 0 ) this . state . history . Push ( this . state . text ) ;
113
187
this . onSend ( ) ;
188
+ } else
189
+ if ( key === Key . ARROW_UP ) {
190
+ this . setState ( { text : this . state . history . Prev ( ) } ) ;
191
+ } else
192
+ if ( key === Key . ARROW_DOWN ) {
193
+ this . setState ( { text : this . state . history . Next ( ) } ) ;
114
194
}
115
195
}
116
196
}
0 commit comments