@@ -7,82 +7,49 @@ import { MonitorModel } from '../../monitor-model';
7
7
import { Unknown } from '../../../common/nls' ;
8
8
9
9
class HistoryList {
10
- private ring : string [ ] ;
11
- private size : number ;
12
- private begin : number ;
13
- private index : number ;
14
- private end : number ;
15
- private traverse : boolean ;
16
-
17
- constructor ( size : number = 100 ) {
18
- this . init = this . init . bind ( this ) ;
19
- this . push = this . push . bind ( this ) ;
20
- this . prev = this . prev . bind ( this ) ;
21
- this . next = this . next . bind ( this ) ;
22
- this . init ( size ) ;
23
- }
24
- private init ( size : number = 100 ) {
25
- this . ring = [ ] ;
26
- this . size = ( size > 0 ) ? size : 1 ;
27
- this . begin = 0 ;
28
- this . index = 0 ;
29
- this . end = - 1 ;
30
- this . traverse = false ;
31
- }
32
-
33
- push ( val : string ) : number {
34
- this . end ++ ;
35
- if ( this . ring . length >= this . size ) {
36
- if ( this . end >= this . size )
37
- this . end = 0 ;
38
- if ( this . end === this . begin ) {
39
- this . begin ++ ;
40
- if ( this . begin >= this . size )
41
- this . begin = 0 ;
42
- }
10
+ private readonly items : string [ ] = [ ] ;
11
+ private index = - 1 ;
12
+
13
+ constructor ( private readonly size = 100 ) { }
14
+
15
+ push ( val : string ) : void {
16
+ if ( val !== this . items [ this . items . length - 1 ] ) {
17
+ this . items . push ( val ) ;
18
+ }
19
+ while ( this . items . length > this . size ) {
20
+ this . items . shift ( ) ;
43
21
}
44
- this . ring [ this . end ] = val ;
45
- this . index = this . end ;
46
- this . traverse = false ;
47
- return this . index ;
22
+ this . index = - 1 ;
48
23
}
49
24
50
- prev ( ) : string {
51
- if ( this . ring . length < 1 ) {
52
- return '' ;
53
- }
54
- if ( this . index === this . end ) {
55
- this . traverse = true ;
56
- this . index -- ;
57
- return this . ring [ this . end ] ;
25
+ previous ( ) : string {
26
+ if ( this . index === - 1 ) {
27
+ this . index = this . items . length - 1 ;
28
+ return this . items [ this . index ] ;
58
29
}
59
- if ( this . index !== this . begin ) {
60
- if ( this . traverse ) {
61
- this . traverse = false ;
62
- }
63
- else
64
- this . index = ( this . index > 0 ) ? -- this . index : this . size - 1 ;
30
+ if ( this . hasPrevious ) {
31
+ return this . items [ -- this . index ] ;
65
32
}
33
+ return this . items [ this . index ] ;
34
+ }
66
35
67
- return this . ring [ this . index ] ;
36
+ private get hasPrevious ( ) : boolean {
37
+ return this . index >= 1 ;
68
38
}
69
39
70
40
next ( ) : string {
71
- if ( this . ring . length < 1 ) {
41
+ if ( this . index === this . items . length - 1 ) {
42
+ this . index = - 1 ;
72
43
return '' ;
73
44
}
74
- if ( this . index !== this . end ) {
75
- this . traverse = true ;
76
- this . index = ( ++ this . index < this . size ) ? this . index : 0 ;
77
- if ( this . index === this . end )
78
- this . traverse = false ;
45
+ if ( this . hasNext ) {
46
+ return this . items [ ++ this . index ] ;
79
47
}
80
- else {
81
- if ( ! this . traverse ) {
82
- return '' ;
83
- }
84
- }
85
- return this . ring [ this . index ] ;
48
+ return '' ;
49
+ }
50
+
51
+ private get hasNext ( ) : boolean {
52
+ return this . index >= 0 && this . index !== this . items . length - 1 ;
86
53
}
87
54
}
88
55
@@ -171,7 +138,7 @@ export class SerialMonitorSendInput extends React.Component<
171
138
) ;
172
139
}
173
140
174
- protected setRef = ( element : HTMLElement | null ) => {
141
+ protected setRef = ( element : HTMLElement | null ) : void => {
175
142
if ( this . props . resolveFocus ) {
176
143
this . props . resolveFocus ( element || undefined ) ;
177
144
}
@@ -191,22 +158,17 @@ export class SerialMonitorSendInput extends React.Component<
191
158
if ( keyCode ) {
192
159
const { key } = keyCode ;
193
160
if ( key === Key . ENTER ) {
194
- // NOTE: order of operations is critical here. Push the current state.text
195
- // onto the history stack before sending. After sending, state.text is empty
196
- // and you'd end up pushing '' onto the history stack.
197
- if ( this . state . text . length > 0 ) {
198
- this . state . history . push ( this . state . text ) ;
199
- }
161
+ const { text } = this . state ;
200
162
this . onSend ( ) ;
201
- }
202
- else if ( key === Key . ARROW_UP ) {
203
- this . setState ( { text : this . state . history . prev ( ) } ) ;
204
- }
205
- else if ( key === Key . ARROW_DOWN ) {
206
- this . setState ( { text : this . state . history . next ( ) } ) ;
207
- }
208
- else if ( key === Key . ESCAPE ) {
209
- this . setState ( { text : '' } ) ;
163
+ if ( text ) {
164
+ this . state . history . push ( text ) ;
165
+ }
166
+ } else if ( key === Key . ARROW_UP ) {
167
+ this . setState ( { text : this . state . history . previous ( ) } ) ;
168
+ } else if ( key === Key . ARROW_DOWN ) {
169
+ this . setState ( { text : this . state . history . next ( ) } ) ;
170
+ } else if ( key === Key . ESCAPE ) {
171
+ this . setState ( { text : '' } ) ;
210
172
}
211
173
}
212
174
}
0 commit comments