Skip to content

Support for providing commands to run from external source #26

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 29 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,44 @@ export default class ReactConsole extends React.Component<ReactConsoleProps, Rea

/**
* Takes current text of a main input and generates a string that will be outputted as a log.
* @param input the input string provided to the control; when omitted, retrieves from state.input
*/
private getCurrentTextSnapshot = (): string => {
private getCurrentTextSnapshot = (input:string): string => {
const {prompt} = this.props;
const inputString: string = this.state.input;
const inputString: string = input;
return `${prompt}\xa0${inputString}`;
};

private onSubmit = async (e: any) => {
e.preventDefault();
/**
* Accept an external command to run, and run it as if it were entered directly on the console
* @param input command to run
* @returns completion promise
*/
public handleInput = async (input:string) => {
return this.onProcessInput(input);
}

const inputString: string = this.state.input;
/**
* Process input submitted from the input control
* @param e input control reference
* @returns completion promise
*/
private onSubmit = async (e:any) => {
e.preventDefault(); // stop event bubbling
return this.onProcessInput(this.state.input);
}

/**
* Process input (from any source) and execute it as a command
* @param inputString command input string
* @returns completiong promise
*/
private onProcessInput = async (inputString:string) => {
if (inputString === null) {
return
}

const log = this.getCurrentTextSnapshot();
const log = this.getCurrentTextSnapshot(inputString);

if (inputString === '') {
this.setState({
Expand Down Expand Up @@ -414,7 +436,7 @@ export default class ReactConsole extends React.Component<ReactConsoleProps, Rea
event.preventDefault()
} else if (event.which === 67 && event.ctrlKey) { // ctrl + c
this.setState({
output: [...this.state.output, this.getCurrentTextSnapshot()],
output: [...this.state.output, this.getCurrentTextSnapshot(this.state.input)],
input: '',
});
this.scrollToBottom();
Expand Down