@@ -7,82 +7,49 @@ import { MonitorModel } from '../../monitor-model';
77import { Unknown } from '../../../common/nls' ;
88
99class 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 ( ) ;
4321 }
44- this . ring [ this . end ] = val ;
45- this . index = this . end ;
46- this . traverse = false ;
47- return this . index ;
22+ this . index = - 1 ;
4823 }
4924
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 ] ;
5829 }
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 ] ;
6532 }
33+ return this . items [ this . index ] ;
34+ }
6635
67- return this . ring [ this . index ] ;
36+ private get hasPrevious ( ) : boolean {
37+ return this . index >= 1 ;
6838 }
6939
7040 next ( ) : string {
71- if ( this . ring . length < 1 ) {
41+ if ( this . index === this . items . length - 1 ) {
42+ this . index = - 1 ;
7243 return '' ;
7344 }
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 ] ;
7947 }
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 ;
8653 }
8754}
8855
@@ -171,7 +138,7 @@ export class SerialMonitorSendInput extends React.Component<
171138 ) ;
172139 }
173140
174- protected setRef = ( element : HTMLElement | null ) => {
141+ protected setRef = ( element : HTMLElement | null ) : void => {
175142 if ( this . props . resolveFocus ) {
176143 this . props . resolveFocus ( element || undefined ) ;
177144 }
@@ -191,22 +158,17 @@ export class SerialMonitorSendInput extends React.Component<
191158 if ( keyCode ) {
192159 const { key } = keyCode ;
193160 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 ;
200162 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 : '' } ) ;
210172 }
211173 }
212174 }
0 commit comments