-
Notifications
You must be signed in to change notification settings - Fork 62
/
Terminal.jsx
276 lines (234 loc) Β· 9.07 KB
/
Terminal.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { Component } from 'react'
import defaults from 'defaults'
import isEqual from 'react-fast-compare'
// Components
import TerminalMessage from './TerminalMessage'
// Handlers
import validateCommands from './handlers/validateCommands'
import scrollHistory from './handlers/scrollHistory'
import parseEOL from './handlers/parseEOL'
// Definitions
import sourceStyles from './defs/styles/Terminal'
import types from './defs/types/Terminal'
// Utils
import commandExists from './utils/commandExists'
import constructEcho from './utils/constructEcho'
import shouldPromptBeVisible from './utils/shouldPromptBeVisible'
export default class Terminal extends Component {
constructor (props) {
super(props)
this.state = {
commands: {},
stdout: [],
history: [],
historyPosition: null,
previousHistoryPosition: null,
processing: false
}
this.terminalRoot = React.createRef()
this.terminalInput = React.createRef()
}
static propTypes = types
/* istanbul ignore next: Covered by interactivity tests */
focusTerminal = () => {
// Only focus the terminal if text isn't being copied
const isTextSelected = window.getSelection().type === 'Range'
// Only focus if input is there (Goes away for read-only terminals)
if (!isTextSelected) this.terminalInput.current.focus()
}
/* istanbul ignore next: Covered by interactivity tests */
scrollToBottom = () => {
const rootNode = this.terminalRoot.current
// This may look ridiculous, but it is necessary to decouple execution for just a millisecond in order to scroll all the way
setTimeout(() => { rootNode.scrollTop = rootNode.scrollHeight }, 1)
}
validateCommands = () => {
const { commands, noDefaults, ignoreCommandCase } = this.props
const validCommands = validateCommands(commands, this.showHelp, this.clearStdout, { noDefaults, ignoreCommandCase })
this.setState({ commands: validCommands })
}
showWelcomeMessage = () => {
const msg = this.props.welcomeMessage
if (typeof msg === 'boolean') this.pushToStdout('Welcome to the React terminal! Type \'help\' to get a list of commands.')
else if (Array.isArray(msg)) msg.map(item => this.pushToStdout(item))
else this.pushToStdout(msg)
}
/* istanbul ignore next: Covered by interactivity tests */
showHelp = () => {
const { commands } = this.state
for (const c in commands) {
const cmdObj = commands[c]
const usage = cmdObj.usage ? ` - ${cmdObj.usage}` : ''
this.pushToStdout(`${c} - ${cmdObj.description}${usage}`)
}
}
/**
* @param {String} message
* @param {Object} options {
* rawInput: Raw input from the terminal (For history),
* isEcho: For distinguishing echo messages (Exemption from message styling)
* }
*/
pushToStdout = (message, options) => {
const { stdout } = this.state
if (this.props.locked) stdout.pop()
stdout.push({ message, isEcho: options?.isEcho || false })
/* istanbul ignore next: Covered by interactivity tests */
if (options?.rawInput) this.pushToHistory(options.rawInput)
this.setState({ stdout: stdout })
}
/**
* @param {String} rawInput Raw command input from the terminal
*/
/* istanbul ignore next: Covered by interactivity tests */
pushToHistory = rawInput => {
const { history } = this.state
history.push(rawInput)
this.setState({ history: history, historyPosition: null })
}
getStdout = () => {
// Parse EOL if it isn't disabled
const stdout = !this.props.noNewlineParsing ? parseEOL(this.state.stdout) : this.state.stdout
return stdout.map((line, i) => {
return <TerminalMessage
key={i}
content={line.message}
dangerMode={this.props.dangerMode}
className={!line.isEcho ? this.props.messageClassName : /* istanbul ignore next: Covered by interactivity tests */ undefined}
style={!line.isEcho ? this.props.messageStyle : /* istanbul ignore next: Covered by interactivity tests */ undefined}
/>
})
}
/* istanbul ignore next: Covered by interactivity tests */
clearStdout = () => {
this.setState({ stdout: [] })
}
/* istanbul ignore next: Covered by interactivity tests */
clearInput = () => {
this.setState({ historyPosition: null })
this.terminalInput.current.value = ''
}
/* istanbul ignore next: Covered by interactivity tests */
processCommand = () => {
this.setState({ processing: true }, async () => {
// Initialise command result object
const commandResult = { command: null, args: [], rawInput: null, result: null }
const rawInput = this.terminalInput.current.value
if (!this.props.noHistory) this.pushToHistory(rawInput)
if (!this.props.noEchoBack) {
// Mimic native terminal by echoing command back
this.pushToStdout(constructEcho(this.props.promptLabel || '$', rawInput, this.props), { isEcho: true })
}
if (rawInput) {
const input = rawInput.split(' ')
const rawCommand = input.splice(0, 1)[0] // Removed portion is returned...
const args = input // ...and the rest can be used
commandResult.rawInput = rawInput
commandResult.command = rawCommand
commandResult.args = args
const { exists, command } = commandExists(this.state.commands, rawCommand, this.props.ignoreCommandCase)
if (!exists) {
this.pushToStdout(this.props.errorText
? this.props.errorText.replace(/\[command\]/gi, command)
: `Command '${rawCommand}' not found!`
)
} else {
const cmd = this.state.commands[command]
const res = await cmd.fn(...args)
this.pushToStdout(res)
commandResult.result = res
if (cmd.explicitExec) await cmd.fn(...args)
}
}
this.setState({ processing: false }, () => {
this.clearInput()
if (!this.props.noAutoScroll) this.scrollToBottom()
if (this.props.commandCallback) this.props.commandCallback(commandResult)
})
})
}
/* istanbul ignore next: Covered by interactivity tests */
scrollHistory = direction => {
const { history, historyPosition, previousHistoryPosition } = this.state
const toUpdate = scrollHistory(direction, {
history,
historyPosition,
previousHistoryPosition,
terminalInput: this.terminalInput
})
// Only update if there is something to update
if (toUpdate) this.setState(toUpdate)
}
/* istanbul ignore next: Covered by interactivity tests */
handleInput = event => {
switch (event.key) {
case 'Enter': this.processCommand(); break
case 'ArrowUp': this.scrollHistory('up'); break
case 'ArrowDown': this.scrollHistory('down'); break
}
}
componentDidUpdate (prevProps) {
// If there was a change in commands, re-validate
if (!isEqual(prevProps.commands, this.props.commands)) this.validateCommands()
}
componentDidMount () {
this.validateCommands()
if (this.props.welcomeMessage) this.showWelcomeMessage()
/* istanbul ignore next: Covered by interactivity tests */
if (this.props.autoFocus) this.focusTerminal()
}
render () {
const styles = {
container: defaults(this.props.style, sourceStyles.container),
content: defaults(this.props.contentStyle, sourceStyles.content),
inputArea: defaults(this.props.inputAreaStyle, sourceStyles.inputArea),
promptLabel: defaults(this.props.promptLabelStyle, sourceStyles.promptLabel),
input: defaults({ ...this.props.inputStyle, ...this.props.inputTextStyle }, { ...sourceStyles.input, ...sourceStyles.inputText })
}
return (
<div
ref={this.terminalRoot}
name='react-console-emulator'
className={this.props.className}
style={styles.container}
onClick={this.focusTerminal}
>
{/* Content */}
<div
name='react-console-emulator__content'
className={this.props.contentClassName}
style={styles.content}
>
{/* Stdout */}
{this.getStdout()}
{/* Input area */}
<div
name='react-console-emulator__inputArea'
className={this.props.inputAreaClassName}
style={shouldPromptBeVisible(this.state, this.props) ? styles.inputArea : { display: 'none' }}
>
{/* Prompt label */}
<span
name='react-console-emulator__promptLabel'
className={this.props.promptLabelClassName}
style={styles.promptLabel}
>
{this.props.promptLabel || '$'}
</span>
{/* Input */}
<input
ref={this.terminalInput}
name='react-console-emulator__input'
className={this.props.inputClassName}
style={styles.input}
onKeyDown={this.handleInput}
type='text'
autoComplete='off'
disabled={this.props.disabled || (this.props.disableOnProcess && /* istanbul ignore next: Covered by interactivity tests */ this.state.processing)}
/>
</div>
</div>
</div>
)
}
}