-
-
Notifications
You must be signed in to change notification settings - Fork 443
#1404 Serial Monitor message history #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,74 @@ import { BoardsServiceProvider } from '../../boards/boards-service-provider'; | |
import { MonitorModel } from '../../monitor-model'; | ||
import { Unknown } from '../../../common/nls'; | ||
|
||
class RingList { | ||
private ring: string[]; | ||
private size: number; | ||
private begin: number; | ||
private index: number; | ||
private end: number; | ||
|
||
constructor(size: number = 100) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
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); | ||
} | ||
|
||
private init(size: number = 100) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be Please respect the position of the curly braces. |
||
{ | ||
this.ring = []; | ||
this.size = (size > 0) ? size : 1; | ||
this.begin = 0; | ||
this.index = 0; | ||
this.end = -1; | ||
} | ||
|
||
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; | ||
} | ||
|
||
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]; | ||
} | ||
|
||
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 +84,7 @@ export namespace SerialMonitorSendInput { | |
export interface State { | ||
text: string; | ||
connected: boolean; | ||
history: RingList; | ||
} | ||
} | ||
|
||
|
@@ -27,7 +96,7 @@ export class SerialMonitorSendInput extends React.Component< | |
|
||
constructor(props: Readonly<SerialMonitorSendInput.Props>) { | ||
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 +179,19 @@ 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()}); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RingList
implies that message history is a cycle.With the following sent messages:
one
,two
, andthree
.I would expect to get
three
afterone
. But no, so the class name is wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I debated with myself over that name. I'm changing it to HistoryStack. It's more descriptive.