Skip to content

Commit b0c8bc7

Browse files
Serial Monitor message history #1404
1 parent 4e590ab commit b0c8bc7

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,75 @@ import { BoardsServiceProvider } from '../../boards/boards-service-provider';
66
import { MonitorModel } from '../../monitor-model';
77
import { Unknown } from '../../../common/nls';
88

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+
978
export namespace SerialMonitorSendInput {
1079
export interface Props {
1180
readonly boardsServiceProvider: BoardsServiceProvider;
@@ -16,6 +85,7 @@ export namespace SerialMonitorSendInput {
1685
export interface State {
1786
text: string;
1887
connected: boolean;
88+
history: RingList;
1989
}
2090
}
2191

@@ -27,7 +97,7 @@ export class SerialMonitorSendInput extends React.Component<
2797

2898
constructor(props: Readonly<SerialMonitorSendInput.Props>) {
2999
super(props);
30-
this.state = { text: '', connected: true };
100+
this.state = { text: '', connected: true, history: new RingList() };
31101
this.onChange = this.onChange.bind(this);
32102
this.onSend = this.onSend.bind(this);
33103
this.onKeyDown = this.onKeyDown.bind(this);
@@ -110,7 +180,17 @@ export class SerialMonitorSendInput extends React.Component<
110180
if (keyCode) {
111181
const { key } = keyCode;
112182
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);
113187
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()});
114194
}
115195
}
116196
}

0 commit comments

Comments
 (0)