From b0c8bc79881037ce98d5d8a99def64b64e14bd50 Mon Sep 17 00:00:00 2001 From: Dwight Fowler Date: Sat, 17 Sep 2022 00:00:17 -0400 Subject: [PATCH 1/2] Serial Monitor message history #1404 --- .../monitor/serial-monitor-send-input.tsx | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx b/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx index f96636455..20ec58664 100644 --- a/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx +++ b/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx @@ -6,6 +6,75 @@ import { BoardsServiceProvider } from '../../boards/boards-service-provider'; import { MonitorModel } from '../../monitor-model'; import { Unknown } from '../../../common/nls'; +class RingList { + protected ring: string[]; + protected size: number; + protected begin: number; + protected index: number; + protected end: number; + + constructor(size: number = 100) { + this.Init = this.Init.bind(this); + this.Push = this.Push.bind(this); + this.Prev = this.Prev.bind(this); + this.Next = this.Next.bind(this); + this.Init(size); + } + + public Init(size: number = 100) + { + this.ring = []; + this.size = (size > 0) ? size : 1; + this.begin = 0; + this.index = 0; + this.end = -1; + } + + public Push(val: string): number { + this.end++; + if (this.ring.length >= this.size) + { + if (this.end >= this.size) + this.end = 0; + if (this.end === this.begin) + { + this.begin++; + if (this.begin >= this.size) + this.begin = 0; + } + } + this.ring[this.end] = val; + this.index = this.end; + + return this.index; + } + + public Prev(): string { + if (this.ring.length < 1) { + return ""; + } + + if (this.index !== this.begin) { + this.index = (this.index > 0) ? --this.index : this.size - 1; + } + + return this.ring[this.index]; + } + + public Next(): string { + if (this.ring.length < 1) { + return ""; + } + + if (this.index !== this.end) { + this.index = (++this.index < this.size) ? this.index : 0; + } + + return this.ring[this.index]; + } + +} + export namespace SerialMonitorSendInput { export interface Props { readonly boardsServiceProvider: BoardsServiceProvider; @@ -16,6 +85,7 @@ export namespace SerialMonitorSendInput { export interface State { text: string; connected: boolean; + history: RingList; } } @@ -27,7 +97,7 @@ export class SerialMonitorSendInput extends React.Component< constructor(props: Readonly) { super(props); - this.state = { text: '', connected: true }; + this.state = { text: '', connected: true, history: new RingList() }; this.onChange = this.onChange.bind(this); this.onSend = this.onSend.bind(this); this.onKeyDown = this.onKeyDown.bind(this); @@ -110,7 +180,17 @@ export class SerialMonitorSendInput extends React.Component< if (keyCode) { const { key } = keyCode; if (key === Key.ENTER) { + // NOTE: order of operations is critical here. Push the current state.text + // 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(); + } else + if (key === Key.ARROW_UP) { + this.setState({ text: this.state.history.Prev()}); + } else + if (key === Key.ARROW_DOWN) { + this.setState({ text: this.state.history.Next()}); } } } From 1dc5e58ca9fd046a7402bbc23124fcec137783d2 Mon Sep 17 00:00:00 2001 From: Dwight Fowler Date: Mon, 19 Sep 2022 15:23:35 -0400 Subject: [PATCH 2/2] Addressed PR#1453 comments --- .../monitor/serial-monitor-send-input.tsx | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx b/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx index 20ec58664..1211aad09 100644 --- a/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx +++ b/arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx @@ -7,21 +7,21 @@ import { MonitorModel } from '../../monitor-model'; import { Unknown } from '../../../common/nls'; class RingList { - protected ring: string[]; - protected size: number; - protected begin: number; - protected index: number; - protected end: number; + private ring: string[]; + private size: number; + private begin: number; + private index: number; + private end: number; constructor(size: number = 100) { - this.Init = this.Init.bind(this); - this.Push = this.Push.bind(this); - this.Prev = this.Prev.bind(this); - this.Next = this.Next.bind(this); - this.Init(size); + this.init = this.init.bind(this); + this.push = this.push.bind(this); + this.prev = this.prev.bind(this); + this.next = this.next.bind(this); + this.init(size); } - public Init(size: number = 100) + private init(size: number = 100) { this.ring = []; this.size = (size > 0) ? size : 1; @@ -30,7 +30,7 @@ class RingList { this.end = -1; } - public Push(val: string): number { + push(val: string): number { this.end++; if (this.ring.length >= this.size) { @@ -49,9 +49,9 @@ class RingList { return this.index; } - public Prev(): string { + prev(): string { if (this.ring.length < 1) { - return ""; + return ''; } if (this.index !== this.begin) { @@ -61,9 +61,9 @@ class RingList { return this.ring[this.index]; } - public Next(): string { + next(): string { if (this.ring.length < 1) { - return ""; + return ''; } if (this.index !== this.end) { @@ -72,7 +72,6 @@ class RingList { return this.ring[this.index]; } - } export namespace SerialMonitorSendInput { @@ -183,14 +182,16 @@ export class SerialMonitorSendInput extends React.Component< // NOTE: order of operations is critical here. Push the current state.text // 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); + if (this.state.text.length > 0) { + this.state.history.push(this.state.text); + } this.onSend(); - } else - if (key === Key.ARROW_UP) { - this.setState({ text: this.state.history.Prev()}); - } else - if (key === Key.ARROW_DOWN) { - this.setState({ text: this.state.history.Next()}); + } + else if (key === Key.ARROW_UP) { + this.setState({ text: this.state.history.prev()}); + } + else if (key === Key.ARROW_DOWN) { + this.setState({ text: this.state.history.next()}); } } }