Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaledin committed Jan 27, 2019
1 parent 1045da1 commit af27892
Show file tree
Hide file tree
Showing 28 changed files with 308 additions and 253 deletions.
362 changes: 171 additions & 191 deletions .idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions example/demo/main.bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/components/commandLine/commandLine.style.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export var commandLineInput = {
outline: 'none',
zIndex: -1,
position: 'absolute',
left: '-1000px',
};
8 changes: 6 additions & 2 deletions lib/components/terminalStateless.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/// <reference types="react" />
import * as React from 'react';
import { ITerminalStatelessProps } from './terminalStateless.interface';
export declare function TerminalStateless(props: ITerminalStatelessProps): JSX.Element;
export declare class TerminalStateless extends React.Component<ITerminalStatelessProps> {
private boxRef;
render(): JSX.Element;
componentDidUpdate(): void;
}
2 changes: 2 additions & 0 deletions lib/components/terminalStateless.interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export interface ITerminalStatelessProps {
history: string[];
prompt: string;
onReceiveCommand: (command: string) => void;
height?: string;
width?: string;
}
36 changes: 27 additions & 9 deletions lib/components/terminalStateless.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import * as tslib_1 from "tslib";
import * as React from 'react';
import { CommandLine } from './commandLine/commandLine';
import * as styles from './terminalStateless.style';
export function TerminalStateless(props) {
var history = props.history, prompt = props.prompt, onReceiveCommand = props.onReceiveCommand;
return (React.createElement("div", { style: styles.terminalBox },
React.createElement("div", { style: styles.terminalContainer },
history.map(function (line, index) {
return React.createElement("div", { key: index }, line);
}),
React.createElement(CommandLine, { onReceiveCommand: onReceiveCommand, prompt: prompt }))));
}
var TerminalStateless = /** @class */ (function (_super) {
tslib_1.__extends(TerminalStateless, _super);
function TerminalStateless() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.boxRef = React.createRef();
return _this;
}
TerminalStateless.prototype.render = function () {
var _a = this.props, history = _a.history, prompt = _a.prompt, onReceiveCommand = _a.onReceiveCommand, width = _a.width, height = _a.height;
var terminalBox = tslib_1.__assign({}, styles.terminalBox, { width: width || styles.terminalBox.width, height: height || styles.terminalBox.height });
return (React.createElement("div", { style: terminalBox, ref: this.boxRef },
React.createElement("div", { style: styles.terminalContainer },
history.map(function (line, index) {
return React.createElement("div", { key: index, style: styles.terminalContainerLine }, line);
}),
React.createElement(CommandLine, { onReceiveCommand: onReceiveCommand, prompt: prompt }))));
};
TerminalStateless.prototype.componentDidUpdate = function () {
var box = this.boxRef.current;
box.scroll({
top: box.scrollHeight,
});
};
return TerminalStateless;
}(React.Component));
export { TerminalStateless };
1 change: 1 addition & 0 deletions lib/components/terminalStateless.style.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CSSProperties } from 'react';
export declare const terminalBox: CSSProperties;
export declare const terminalContainer: CSSProperties;
export declare const terminalContainerLine: CSSProperties;
9 changes: 6 additions & 3 deletions lib/components/terminalStateless.style.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export var terminalBox = {
fontFamily: 'monospace',
display: 'flex',
backgroundColor: '#44494e',
fontWeight: 'bold',
height: '400px',
height: '500px',
overflowY: 'scroll',
};
export var terminalContainer = {
fontSize: '14px',
color: '#20e444',
padding: '8px',
};
export var terminalContainerLine = {
margin: 0,
whiteSpace: 'pre',
};
2 changes: 2 additions & 0 deletions lib/terminal.interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface ITerminalOwnProps {
terminalEmulator: {
execute: (command: string, setState: SetState) => void;
};
height?: string;
width?: string;
}
export interface ITerminalState {
history: string[];
Expand Down
3 changes: 2 additions & 1 deletion lib/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var Terminal = /** @class */ (function (_super) {
}
Terminal.prototype.render = function () {
var _a = this.state, history = _a.history, prompt = _a.prompt;
return (React.createElement(TerminalStateless, { history: history, prompt: prompt, onReceiveCommand: this.handleReceiveCommand }));
var _b = this.props, height = _b.height, width = _b.width;
return (React.createElement(TerminalStateless, { history: history, prompt: prompt, onReceiveCommand: this.handleReceiveCommand, height: height, width: width }));
};
Terminal.defaultProps = {
history: [],
Expand Down
3 changes: 2 additions & 1 deletion lib/terminalEmulator/executors/baseExecutor.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SetState } from '../terminalEmulator.interface';
export declare abstract class BaseExecutor {
protected commandPattern: RegExp;
abstract execute(command: string, setState: SetState): void;
checkCommand(command: string): boolean;
init(): void;
abstract isCompleted(): boolean;
protected abstract getCommandPattern(): RegExp;
}
6 changes: 5 additions & 1 deletion lib/terminalEmulator/executors/baseExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ var BaseExecutor = /** @class */ (function () {
function BaseExecutor() {
}
BaseExecutor.prototype.checkCommand = function (command) {
return this.commandPattern.test(command);
var commandPattern = this.getCommandPattern();
return commandPattern.test(command);
};
BaseExecutor.prototype.init = function () {
// it necessary for complicated command
};
return BaseExecutor;
}());
Expand Down
2 changes: 1 addition & 1 deletion lib/terminalEmulator/executors/clearExecutor.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SetState } from '../terminalEmulator.interface';
import { BaseExecutor } from './baseExecutor';
export declare class ClearExecutor extends BaseExecutor {
readonly commandPattern: RegExp;
execute(command: string, setState: SetState): void;
isCompleted(): boolean;
protected getCommandPattern(): RegExp;
}
7 changes: 4 additions & 3 deletions lib/terminalEmulator/executors/clearExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { BaseExecutor } from './baseExecutor';
var ClearExecutor = /** @class */ (function (_super) {
tslib_1.__extends(ClearExecutor, _super);
function ClearExecutor() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.commandPattern = new RegExp('^clear$', 'i');
return _this;
return _super !== null && _super.apply(this, arguments) || this;
}
ClearExecutor.prototype.execute = function (command, setState) {
setState(function (prevState, props) {
Expand All @@ -18,6 +16,9 @@ var ClearExecutor = /** @class */ (function (_super) {
ClearExecutor.prototype.isCompleted = function () {
return true;
};
ClearExecutor.prototype.getCommandPattern = function () {
return /^clear$/i;
};
return ClearExecutor;
}(BaseExecutor));
export { ClearExecutor };
2 changes: 1 addition & 1 deletion lib/terminalEmulator/executors/helpExecutor.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SetState } from '../terminalEmulator.interface';
import { BaseExecutor } from './baseExecutor';
export declare class HelpExecutor extends BaseExecutor {
constructor();
execute(command: string, setState: SetState): void;
isCompleted(): boolean;
protected getCommandPattern(): RegExp;
}
7 changes: 4 additions & 3 deletions lib/terminalEmulator/executors/helpExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { BaseExecutor } from './baseExecutor';
var HelpExecutor = /** @class */ (function (_super) {
tslib_1.__extends(HelpExecutor, _super);
function HelpExecutor() {
var _this = _super.call(this) || this;
_this.commandPattern = new RegExp('^help$', 'i');
return _this;
return _super !== null && _super.apply(this, arguments) || this;
}
HelpExecutor.prototype.execute = function (command, setState) {
setState(function (prevState, props) { return ({
Expand All @@ -18,6 +16,9 @@ var HelpExecutor = /** @class */ (function (_super) {
HelpExecutor.prototype.isCompleted = function () {
return true;
};
HelpExecutor.prototype.getCommandPattern = function () {
return /^help$/i;
};
return HelpExecutor;
}(BaseExecutor));
export { HelpExecutor };
2 changes: 1 addition & 1 deletion lib/terminalEmulator/terminalEmulator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ICommandExecutor, SetState } from './terminalEmulator.interface';
export declare class TerminalEmulator {
private command;
private readonly executors;
private ranExecutor;
private currentExecutor;
constructor(params: ICommandExecutor);
addExecutor(executor: BaseExecutor): void;
execute(command: string, setState: SetState): void;
Expand Down
9 changes: 5 additions & 4 deletions lib/terminalEmulator/terminalEmulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ var TerminalEmulator = /** @class */ (function () {
};
TerminalEmulator.prototype.execute = function (command, setState) {
this.command = command.trim();
if (this.ranExecutor && !this.ranExecutor.isCompleted()) {
this.ranExecutor.execute(command, setState);
if (this.currentExecutor && !this.currentExecutor.isCompleted()) {
this.currentExecutor.execute(command, setState);
}
else {
var matchedExecutor = this.executors.find(function (executor) { return executor.checkCommand(command); });
if (matchedExecutor) {
this.ranExecutor = matchedExecutor;
this.ranExecutor.execute(command, setState);
this.currentExecutor = matchedExecutor;
this.currentExecutor.init();
this.currentExecutor.execute(command, setState);
}
else if (command !== '') {
this.errorExecutor(setState);
Expand Down
1 change: 1 addition & 0 deletions src/components/commandLine/commandLine.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const commandLineInput: CSSProperties = {
outline: 'none',
zIndex: -1,
position: 'absolute',
left: '-1000px',
};
2 changes: 2 additions & 0 deletions src/components/terminalStateless.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export interface ITerminalStatelessProps {
history: string[];
prompt: string;
onReceiveCommand: (command: string) => void;
height?: string;
width?: string;
}
10 changes: 7 additions & 3 deletions src/components/terminalStateless.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import {CSSProperties} from 'react';

export const terminalBox: CSSProperties = {
fontFamily: 'monospace',
display: 'flex',
backgroundColor: '#44494e',
fontWeight: 'bold',
height: '400px',
height: '500px',
overflowY: 'scroll',
};

export const terminalContainer: CSSProperties = {
fontSize: '14px',
color: '#20e444',
padding: '8px',
};

export const terminalContainerLine: CSSProperties = {
margin: 0,
whiteSpace: 'pre',
};
37 changes: 27 additions & 10 deletions src/components/terminalStateless.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import {CSSProperties} from 'react';
import * as React from 'react';
import {CommandLine} from './commandLine/commandLine';
import {ITerminalStatelessProps} from './terminalStateless.interface';
import * as styles from './terminalStateless.style';

export function TerminalStateless(props: ITerminalStatelessProps) {
const {history, prompt, onReceiveCommand} = props;
export class TerminalStateless extends React.Component<ITerminalStatelessProps> {
private boxRef = React.createRef<HTMLDivElement>();

return (
<div style={styles.terminalBox}>
<div style={styles.terminalContainer}>
{history.map((line, index) =>
<div key={index}>{line}</div>)}
<CommandLine onReceiveCommand={onReceiveCommand} prompt={prompt}/>
public render() {
const {history, prompt, onReceiveCommand, width, height} = this.props;
const terminalBox: CSSProperties = {
...styles.terminalBox,
width: width || styles.terminalBox.width,
height: height || styles.terminalBox.height,
};

return (
<div style={terminalBox} ref={this.boxRef}>
<div style={styles.terminalContainer}>
{history.map((line, index) =>
<div key={index} style={styles.terminalContainerLine}>{line}</div>)}
<CommandLine onReceiveCommand={onReceiveCommand} prompt={prompt}/>
</div>
</div>
</div>
);
);
}

public componentDidUpdate() {
const box = this.boxRef.current!;
box.scroll({
top: box.scrollHeight,
});
}
}
2 changes: 2 additions & 0 deletions src/terminal.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface ITerminalOwnProps {
terminalEmulator: {
execute: (command: string, setState: SetState) => void;
};
height?: string;
width?: string;
}

export interface ITerminalState {
Expand Down
3 changes: 3 additions & 0 deletions src/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export class Terminal extends React.Component<ITerminalOwnProps, ITerminalState>

public render() {
const {history, prompt} = this.state;
const {height, width} = this.props;
return (
<TerminalStateless
history={history}
prompt={prompt}
onReceiveCommand={this.handleReceiveCommand}
height={height}
width={width}
/>
);
}
Expand Down
11 changes: 8 additions & 3 deletions src/terminalEmulator/executors/baseExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {SetState} from '../terminalEmulator.interface';

export abstract class BaseExecutor {
protected commandPattern: RegExp;

public abstract execute(command: string, setState: SetState): void;

public checkCommand(command: string): boolean {
return this.commandPattern.test(command);
const commandPattern = this.getCommandPattern();
return commandPattern.test(command);
}

public init(): void {
// it necessary for complicated command
}

public abstract isCompleted(): boolean;

protected abstract getCommandPattern(): RegExp;
}
6 changes: 4 additions & 2 deletions src/terminalEmulator/executors/clearExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {SetState} from '../terminalEmulator.interface';
import {BaseExecutor} from './baseExecutor';

export class ClearExecutor extends BaseExecutor {
public readonly commandPattern = new RegExp('^clear$', 'i');

public execute(command: string, setState: SetState): void {
setState((prevState, props) => {
return {
Expand All @@ -16,4 +14,8 @@ export class ClearExecutor extends BaseExecutor {
public isCompleted(): boolean {
return true;
}

protected getCommandPattern(): RegExp {
return /^clear$/i;
}
}
10 changes: 4 additions & 6 deletions src/terminalEmulator/executors/helpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import {SetState} from '../terminalEmulator.interface';
import {BaseExecutor} from './baseExecutor';

export class HelpExecutor extends BaseExecutor {
constructor() {
super();

this.commandPattern = new RegExp('^help$', 'i');
}

public execute(command: string, setState: SetState): void {
setState((prevState, props) => ({
history: [
Expand All @@ -21,4 +15,8 @@ export class HelpExecutor extends BaseExecutor {
public isCompleted(): boolean {
return true;
}

protected getCommandPattern(): RegExp {
return /^help$/i;
}
}
Loading

0 comments on commit af27892

Please sign in to comment.