mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-06-20 11:06:32 +00:00
Cleaner implementation of HistoryList
- The implementation has been taken from @kittaakos repo
d10de01736/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx
- The previous method has been modified to ensure the first element instead of an empty string is returned if the index is at the beginning of the list.
- The push method has been modified to check if the current command is same as the last command. If same then, it is not added to the list else it is added.
This commit is contained in:
parent
cdaaa5584d
commit
3b2d12eff9
@ -7,82 +7,49 @@ import { MonitorModel } from '../../monitor-model';
|
|||||||
import { Unknown } from '../../../common/nls';
|
import { Unknown } from '../../../common/nls';
|
||||||
|
|
||||||
class HistoryList {
|
class HistoryList {
|
||||||
private ring: string[];
|
private readonly items: string[] = [];
|
||||||
private size: number;
|
private index = -1;
|
||||||
private begin: number;
|
|
||||||
private index: number;
|
|
||||||
private end: number;
|
|
||||||
private traverse: boolean;
|
|
||||||
|
|
||||||
constructor(size: number = 100) {
|
constructor(private readonly size = 100) {}
|
||||||
this.init = this.init.bind(this);
|
|
||||||
this.push = this.push.bind(this);
|
push(val: string): void {
|
||||||
this.prev = this.prev.bind(this);
|
if (val !== this.items[this.items.length - 1]) {
|
||||||
this.next = this.next.bind(this);
|
this.items.push(val);
|
||||||
this.init(size);
|
}
|
||||||
}
|
while (this.items.length > this.size) {
|
||||||
private init(size: number = 100) {
|
this.items.shift();
|
||||||
this.ring = [];
|
}
|
||||||
this.size = (size > 0) ? size : 1;
|
this.index = -1;
|
||||||
this.begin = 0;
|
|
||||||
this.index = 0;
|
|
||||||
this.end = -1;
|
|
||||||
this.traverse = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
push(val: string): number {
|
previous(): string {
|
||||||
this.end++;
|
if (this.index === -1) {
|
||||||
if (this.ring.length >= this.size) {
|
this.index = this.items.length - 1;
|
||||||
if (this.end >= this.size)
|
return this.items[this.index];
|
||||||
this.end = 0;
|
|
||||||
if (this.end === this.begin) {
|
|
||||||
this.begin++;
|
|
||||||
if (this.begin >= this.size)
|
|
||||||
this.begin = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.ring[this.end] = val;
|
if (this.hasPrevious) {
|
||||||
this.index = this.end;
|
return this.items[--this.index];
|
||||||
this.traverse = false;
|
}
|
||||||
return this.index;
|
return this.items[this.index];
|
||||||
}
|
}
|
||||||
|
|
||||||
prev(): string {
|
private get hasPrevious(): boolean {
|
||||||
if (this.ring.length < 1) {
|
return this.index >= 1;
|
||||||
return '';
|
|
||||||
}
|
|
||||||
if (this.index === this.end) {
|
|
||||||
this.traverse = true;
|
|
||||||
this.index--;
|
|
||||||
return this.ring[this.end];
|
|
||||||
}
|
|
||||||
if (this.index !== this.begin) {
|
|
||||||
if (this.traverse) {
|
|
||||||
this.traverse = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
this.index = (this.index > 0) ? --this.index : this.size - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.ring[this.index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next(): string {
|
next(): string {
|
||||||
if (this.ring.length < 1) {
|
if (this.index === this.items.length - 1) {
|
||||||
|
this.index = -1;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
if (this.index !== this.end) {
|
if (this.hasNext) {
|
||||||
this.traverse = true;
|
return this.items[++this.index];
|
||||||
this.index = (++this.index < this.size) ? this.index : 0;
|
|
||||||
if(this.index === this.end)
|
|
||||||
this.traverse = false;
|
|
||||||
}
|
}
|
||||||
else {
|
return '';
|
||||||
if (!this.traverse) {
|
}
|
||||||
return '';
|
|
||||||
}
|
private get hasNext(): boolean {
|
||||||
}
|
return this.index >= 0 && this.index !== this.items.length - 1;
|
||||||
return this.ring[this.index];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +138,7 @@ export class SerialMonitorSendInput extends React.Component<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected setRef = (element: HTMLElement | null) => {
|
protected setRef = (element: HTMLElement | null): void => {
|
||||||
if (this.props.resolveFocus) {
|
if (this.props.resolveFocus) {
|
||||||
this.props.resolveFocus(element || undefined);
|
this.props.resolveFocus(element || undefined);
|
||||||
}
|
}
|
||||||
@ -191,22 +158,17 @@ export class SerialMonitorSendInput extends React.Component<
|
|||||||
if (keyCode) {
|
if (keyCode) {
|
||||||
const { key } = keyCode;
|
const { key } = keyCode;
|
||||||
if (key === Key.ENTER) {
|
if (key === Key.ENTER) {
|
||||||
// NOTE: order of operations is critical here. Push the current state.text
|
const { text } = this.state;
|
||||||
// onto the history stack before sending. After sending, state.text is empty
|
|
||||||
// and you'd end up pushing '' onto the history stack.
|
|
||||||
if (this.state.text.length > 0) {
|
|
||||||
this.state.history.push(this.state.text);
|
|
||||||
}
|
|
||||||
this.onSend();
|
this.onSend();
|
||||||
}
|
if (text) {
|
||||||
else if (key === Key.ARROW_UP) {
|
this.state.history.push(text);
|
||||||
this.setState({ text: this.state.history.prev()});
|
}
|
||||||
}
|
} else if (key === Key.ARROW_UP) {
|
||||||
else if (key === Key.ARROW_DOWN) {
|
this.setState({ text: this.state.history.previous() });
|
||||||
this.setState({ text: this.state.history.next()});
|
} else if (key === Key.ARROW_DOWN) {
|
||||||
}
|
this.setState({ text: this.state.history.next() });
|
||||||
else if (key === Key.ESCAPE) {
|
} else if (key === Key.ESCAPE) {
|
||||||
this.setState({ text: ''});
|
this.setState({ text: '' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user