Skip to content

#1404 Serial Monitor message history #1453

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +85,7 @@ export namespace SerialMonitorSendInput {
export interface State {
text: string;
connected: boolean;
history: RingList;
}
}

Expand All @@ -27,7 +97,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);
Expand Down Expand Up @@ -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()});
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions arduino-ide-extension/src/browser/style/monitor.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
font-family: monospace
}

.serial-monitor-messages pre {
margin: 0px;
}

.serial-monitor .head {
display: flex;
padding: 5px;
Expand Down