Skip to content

Commit 7195ec0

Browse files
committed
Add updateLastLog method, prevent exceptions for programmatic calls to log in empty console
1 parent 3abd15c commit 7195ec0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Properties you can pass to the console element
6767
| Member | Type | Description
6868
| ---- | ---- | ----
6969
| log | (...messages: any)=>void | Log messages to the console. If string, print the value, otherwise, print the JSON value of the message.
70+
| updateLastLog| (...messages: any)=>void | Replace the last message in the console. Useful for progress indicators.
7071
| logX | (type: string, ...messages: any)=>void | Log messages of a particular type to the console. The messages will be given the class `react-console-message-{type}`.
7172
| return | ()=>void | Signal the current command has finished and a new prompt should be displayed.
7273
| clearScreen | ()=>void | Clear the visible log in the console. Does not clear command history.

src/react-console.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,32 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
175175
focus?: HTMLElement;
176176
} = {};
177177
// Command API
178+
updateLastLog = (...messages: any[]) => {
179+
let log = this.state.log;
180+
if(!log.length){
181+
log.push({label: '', command: '', message: [] });
182+
}
183+
let indexToReplace = log[this.state.log.length-1].message.length > 0 ? log[this.state.log.length-1].message.length - 1 : 0;
184+
log[this.state.log.length-1].message[indexToReplace] = {value: messages};
185+
this.setState({
186+
log: log,
187+
}, this.scrollIfBottom() );
188+
}
178189
log = (...messages: any[]) => {
179190
let log = this.state.log;
191+
if(!log.length){
192+
log.push({label: '', command: '', message: [] });
193+
}
180194
log[this.state.log.length-1].message.push({value: messages});
181195
this.setState({
182196
log: log,
183197
}, this.scrollIfBottom() );
184198
}
185199
logX = (type: string, ...messages: any[]) => {
186200
let log = this.state.log;
201+
if(!log.length){
202+
log.push({label: '', command: '', message: [] });
203+
}
187204
log[this.state.log.length-1].message.push({type: type, value: messages});
188205
this.setState({
189206
log: log,

0 commit comments

Comments
 (0)