@@ -6,6 +6,88 @@ 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 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
+ }
43
+ }
44
+ this . ring [ this . end ] = val ;
45
+ this . index = this . end ;
46
+ this . traverse = false ;
47
+ return this . index ;
48
+ }
49
+
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 ] ;
58
+ }
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 ;
65
+ }
66
+
67
+ return this . ring [ this . index ] ;
68
+ }
69
+
70
+ next ( ) : string {
71
+ if ( this . ring . length < 1 ) {
72
+ return '' ;
73
+ }
74
+ if ( this . index !== this . end ) {
75
+ this . traverse = true ;
76
+ this . index = ( ++ this . index < this . size ) ? this . index : 0 ;
77
+ return this . ring [ this . index ] ;
78
+ }
79
+ else {
80
+ if ( ! this . traverse ) {
81
+ return '' ;
82
+ }
83
+ else {
84
+ this . traverse = false ;
85
+ return this . ring [ this . index ] ;
86
+ }
87
+ }
88
+ }
89
+ }
90
+
9
91
export namespace SerialMonitorSendInput {
10
92
export interface Props {
11
93
readonly boardsServiceProvider : BoardsServiceProvider ;
@@ -16,6 +98,7 @@ export namespace SerialMonitorSendInput {
16
98
export interface State {
17
99
text : string ;
18
100
connected : boolean ;
101
+ history : HistoryList ;
19
102
}
20
103
}
21
104
@@ -27,7 +110,7 @@ export class SerialMonitorSendInput extends React.Component<
27
110
28
111
constructor ( props : Readonly < SerialMonitorSendInput . Props > ) {
29
112
super ( props ) ;
30
- this . state = { text : '' , connected : true } ;
113
+ this . state = { text : '' , connected : true , history : new HistoryList ( ) } ;
31
114
this . onChange = this . onChange . bind ( this ) ;
32
115
this . onSend = this . onSend . bind ( this ) ;
33
116
this . onKeyDown = this . onKeyDown . bind ( this ) ;
@@ -110,8 +193,23 @@ export class SerialMonitorSendInput extends React.Component<
110
193
if ( keyCode ) {
111
194
const { key } = keyCode ;
112
195
if ( key === Key . ENTER ) {
196
+ // NOTE: order of operations is critical here. Push the current state.text
197
+ // onto the history stack before sending. After sending, state.text is empty
198
+ // and you'd end up pushing '' onto the history stack.
199
+ if ( this . state . text . length > 0 ) {
200
+ this . state . history . push ( this . state . text ) ;
201
+ }
113
202
this . onSend ( ) ;
203
+ }
204
+ else if ( key === Key . ARROW_UP ) {
205
+ this . setState ( { text : this . state . history . prev ( ) } ) ;
206
+ }
207
+ else if ( key === Key . ARROW_DOWN ) {
208
+ this . setState ( { text : this . state . history . next ( ) } ) ;
209
+ }
210
+ else if ( key === Key . ESCAPE ) {
211
+ this . setState ( { text : '' } ) ;
114
212
}
115
213
}
116
214
}
117
- }
215
+ }
0 commit comments