Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit ca47682

Browse files
committed
Add arduino.changeTimestampFormat command
1 parent fdff64f commit ca47682

File tree

7 files changed

+51
-10
lines changed

7 files changed

+51
-10
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"onCommand:arduino.selectSerialPort",
4545
"onCommand:arduino.selectSketch",
4646
"onCommand:arduino.changeBaudRate",
47+
"onCommand:arduino.changeTimestampFormat",
4748
"onCommand:arduino.openSerialMonitor",
4849
"onCommand:arduino.sendMessageToSerialPort",
4950
"onCommand:arduino.closeSerialMonitor",
@@ -128,6 +129,10 @@
128129
"command": "arduino.changeBaudRate",
129130
"title": "Arduino: Change Baud Rate"
130131
},
132+
{
133+
"command": "arduino.changeTimestampFormat",
134+
"title": "Arduino: Change Timestamp Format"
135+
},
131136
{
132137
"command": "arduino.openSerialMonitor",
133138
"title": "Arduino: Open Serial Monitor"
@@ -532,7 +537,7 @@
532537
"default": false,
533538
"description": "When disabled vscode-arduino will not auto-generate an IntelliSense configuration (i.e. c_cpp_properties.json) by analyzing the compiler output."
534539
},
535-
"arduino.timestampFormat": {
540+
"arduino.defaultTimestampFormat": {
536541
"type": "string",
537542
"default": "",
538543
"description": "Format of timestamp added before each line. List of all possible formats: https://strftime.org"

src/arduino/vscodeSettings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const configKeys = {
1919
DEFAULT_BAUD_RATE: "arduino.defaultBaudRate",
2020
USE_ARDUINO_CLI: "arduino.useArduinoCli",
2121
DISABLE_INTELLISENSE_AUTO_GEN: "arduino.disableIntelliSenseAutoGen",
22-
TIMESTAMP_FORMAT: "arduino.timestampFormat",
22+
DEFAULT_TIMESTAMP_FORMAT: "arduino.defaultTimestampFormat",
2323
};
2424

2525
export interface IVscodeSettings {
@@ -121,7 +121,7 @@ export class VscodeSettings implements IVscodeSettings {
121121
}
122122

123123
public get defaultTimestampFormat(): string {
124-
return this.getConfigValue<string>(configKeys.TIMESTAMP_FORMAT);
124+
return this.getConfigValue<string>(configKeys.DEFAULT_TIMESTAMP_FORMAT);
125125
}
126126

127127
public async updateAdditionalUrls(value) {

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ export const statusBarPriority = {
4747
ENDING: 70,
4848
SKETCH: 80,
4949
PROGRAMMER: 90,
50+
TIMESTAMP_FORMAT: 100,
5051
};

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ export async function activate(context: vscode.ExtensionContext) {
284284
registerNonArduinoCommand("arduino.selectSerialPort", () => serialMonitor.selectSerialPort(null, null));
285285
registerNonArduinoCommand("arduino.openSerialMonitor", () => serialMonitor.openSerialMonitor());
286286
registerNonArduinoCommand("arduino.changeBaudRate", () => serialMonitor.changeBaudRate());
287+
registerNonArduinoCommand("arduino.changeTimestampFormat", () => serialMonitor.changeTimestampFormat());
287288
registerNonArduinoCommand("arduino.sendMessageToSerialPort", () => serialMonitor.sendMessageToSerialPort());
288289
registerNonArduinoCommand("arduino.closeSerialMonitor", (port, showWarning = true) => serialMonitor.closeSerialMonitor(port, showWarning));
289290

src/serialmonitor/serialMonitor.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,22 @@ export class SerialMonitor implements vscode.Disposable {
4242

4343
private _currentBaudRate: number;
4444

45+
private _currentTimestampFormat: string;
46+
4547
private _portsStatusBar: vscode.StatusBarItem;
4648

4749
private _openPortStatusBar: vscode.StatusBarItem;
4850

4951
private _baudRateStatusBar: vscode.StatusBarItem;
5052

53+
private _timestampFormatStatusBar: vscode.StatusBarItem;
54+
5155
private _serialPortCtrl: SerialPortCtrl = null;
5256

5357
private _outputChannel: vscode.OutputChannel;
5458

5559
private _bufferedOutputChannel: BufferedOutputChannel;
5660

57-
private _timestampFormat: string;
5861

5962
public initialize() {
6063
let defaultBaudRate;
@@ -64,15 +67,15 @@ export class SerialMonitor implements vscode.Disposable {
6467
defaultBaudRate = SerialMonitor.DEFAULT_BAUD_RATE;
6568
}
6669
let defaultTimestampFormat;
67-
if (ArduinoContext.arduinoApp && ArduinoContext.arduinoApp.settings && ArduinoContext.arduinoApp.settings.defaultBaudRate) {
70+
if (ArduinoContext.arduinoApp && ArduinoContext.arduinoApp.settings && ArduinoContext.arduinoApp.settings.defaultTimestampFormat) {
6871
defaultTimestampFormat = ArduinoContext.arduinoApp.settings.defaultTimestampFormat;
6972
} else {
7073
defaultTimestampFormat = SerialMonitor.DEFAULT_TIMESTAMP_FORMAT;
7174
}
7275
this._outputChannel = vscode.window.createOutputChannel(SerialMonitor.SERIAL_MONITOR);
7376
this._bufferedOutputChannel = new BufferedOutputChannel(this._outputChannel.append, 300);
7477
this._currentBaudRate = defaultBaudRate;
75-
this._timestampFormat = defaultTimestampFormat;
78+
this._currentTimestampFormat = defaultTimestampFormat;
7679
this._portsStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.PORT);
7780
this._portsStatusBar.command = "arduino.selectSerialPort";
7881
this._portsStatusBar.tooltip = "Select Serial Port";
@@ -88,6 +91,11 @@ export class SerialMonitor implements vscode.Disposable {
8891
this._baudRateStatusBar.command = "arduino.changeBaudRate";
8992
this._baudRateStatusBar.tooltip = "Baud Rate";
9093
this._baudRateStatusBar.text = defaultBaudRate.toString();
94+
95+
this._timestampFormatStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.TIMESTAMP_FORMAT);
96+
this._timestampFormatStatusBar.command = "arduino.changeTimestampFormat";
97+
this._timestampFormatStatusBar.tooltip = "Timestamp Format";
98+
this._timestampFormatStatusBar.text = defaultTimestampFormat;
9199
this.updatePortListStatus();
92100

93101
const dc = DeviceContext.getInstance();
@@ -165,7 +173,7 @@ export class SerialMonitor implements vscode.Disposable {
165173
this._serialPortCtrl = new SerialPortCtrl(
166174
this._currentPort,
167175
this._currentBaudRate,
168-
this._timestampFormat,
176+
this._currentTimestampFormat,
169177
this._bufferedOutputChannel,
170178
this._outputChannel.show);
171179
}
@@ -218,6 +226,13 @@ export class SerialMonitor implements vscode.Disposable {
218226
this._baudRateStatusBar.text = chosen;
219227
}
220228

229+
public async changeTimestampFormat() {
230+
const timestampFormat = await vscode.window.showInputBox();
231+
await this._serialPortCtrl.changeTimestampFormat(timestampFormat);
232+
this._currentTimestampFormat = timestampFormat;
233+
this._timestampFormatStatusBar.text = timestampFormat;
234+
}
235+
221236
public async closeSerialMonitor(port: string, showWarning: boolean = true): Promise<boolean> {
222237
if (this._serialPortCtrl) {
223238
if (port && port !== this._serialPortCtrl.currentPort) {

src/serialmonitor/serialportctrl.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class SerialPortCtrl {
6262
private _currentPort: string;
6363
private _currentBaudRate: number;
6464
private _currentSerialPort = null;
65-
private _timestampFormat: string;
65+
private _currentTimestampFormat: string;
6666

6767
public constructor(
6868
port: string,
@@ -72,7 +72,7 @@ export class SerialPortCtrl {
7272
private showOutputChannel: (preserveFocus?: boolean) => void) {
7373
this._currentBaudRate = baudRate;
7474
this._currentPort = port;
75-
this._timestampFormat = timestampFormat;
75+
this._currentTimestampFormat = timestampFormat;
7676
}
7777

7878
/*
@@ -96,7 +96,7 @@ export class SerialPortCtrl {
9696

9797
return new Promise((resolve, reject) => {
9898
this._child = spawn(SerialPortCtrl._serialCliPath,
99-
["open", this._currentPort, "-b", this._currentBaudRate.toString(), "-t", this._timestampFormat, "--json"])
99+
["open", this._currentPort, "-b", this._currentBaudRate.toString(), "-t", this._currentTimestampFormat, "--json"])
100100

101101
this._child.on("error", (err) => {
102102
reject(err)
@@ -192,4 +192,22 @@ export class SerialPortCtrl {
192192
}
193193
});
194194
}
195+
196+
public changeTimestampFormat(newTimestampFormat: string): Promise<void> {
197+
return new Promise((resolve, reject) => {
198+
this._currentTimestampFormat = newTimestampFormat;
199+
if (!this._child || !this.isActive) {
200+
resolve();
201+
return;
202+
} else {
203+
try {
204+
this.stop();
205+
this.open();
206+
resolve();
207+
} catch (error) {
208+
reject(error);
209+
}
210+
}
211+
});
212+
}
195213
}

test/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ suite("Arduino: Extension Tests", () => {
4848
"arduino.selectSerialPort",
4949
"arduino.openSerialMonitor",
5050
"arduino.changeBaudRate",
51+
"arduino.changeTimestampFormat",
5152
"arduino.sendMessageToSerialPort",
5253
"arduino.closeSerialMonitor",
5354
"arduino.reloadExample",

0 commit comments

Comments
 (0)