Skip to content

Commit 7570c9a

Browse files
authored
feat: promise-based type() API that now supports treating text as a command (#42)
1 parent eab39e1 commit 7570c9a

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ initTerminal(settings)
9191

9292
An object that's being passed to every command function & returned by `initTerminal`
9393

94-
| Method | Description | Parameters |
95-
| ------------- | ------------- | ------------- |
96-
| `print(text, isCommand)` | Prints a given text in the terminal (accepts raw HTML) | `text` - String, `isCommand` - Boolean, optional, defaults to false. Count given string as a command (displays prompt & syntax highlight) |
97-
| `run(text)` | Emulates a command execution in a terminal (acts the same way as a user would have typed and pressed Enter) | `text` - String |
98-
| `start()` | Starts a "foreground process": user input is blocked and command prompt never appears. | |
99-
| `stop()` | Stops "foreground process". | |
100-
| `type(text, speed, callback)` | Prints a text with "typing" effect. Hides and blocks user input while typing. | `text` - String, text to be printed. `speed` - integer, miliseconds. The higher the number, the slower. `callback` - function, gets executed when the process is finished. |
101-
| `setPrompt()` | Set terminal prompt | `newPrompt` - String |
94+
| Method | Description | Parameters |
95+
|----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
96+
| `print(text, isCommand)` | Prints a given text in the terminal (accepts raw HTML) | `text` - String, `isCommand` - Boolean, optional, defaults to false. Count given string as a command (displays prompt & syntax highlight) |
97+
| `run(text)` | Emulates a command execution in a terminal (acts the same way as a user would have typed and pressed Enter) | `text` - String |
98+
| `start()` | Starts a "foreground process": user input is blocked and command prompt never appears. | |
99+
| `stop()` | Stops "foreground process". | |
100+
| `type(text, speed, isCommand) => Promise<boolean>` | Prints a text with "typing" effect. Hides and blocks user input while typing. Resolves to either `true` or `false` depending on process interruption by the user. | `text` - String, text to be printed. `speed` - integer, miliseconds. The higher the number, the slower. `isCommand` - Boolean, optional, defaults to false. Count given string as a command (displays prompt & syntax highlight) |
101+
| `setPrompt()` | Set terminal prompt | `newPrompt` - String |
102102

103103
### settings object
104104

preview/main.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,24 @@ const terminal = initTerminal({
3333
func: (terminal) => {
3434
terminal.print('Downloading more RAM...')
3535
terminal.print('Press Ctrl + C if you think you have enough RAM')
36-
terminal.type('............................................', 160, () => {
37-
if (terminal.isProcessRunning) {
38-
terminal.print('Successfully downloaded 42gb of RAM!')
39-
}
40-
})
36+
terminal.type('............................................', 100)
37+
.then((finished) => {
38+
if (finished) {
39+
terminal.print('Successfully downloaded 42gb of RAM!')
40+
}
41+
})
42+
}
43+
},
44+
enqueue: {
45+
name: 'enqueue',
46+
description: 'pretend like someone else is typing in the terminal',
47+
func: (terminal) => {
48+
terminal.type('test', 70, true)
49+
.then((finished) => {
50+
if (finished) {
51+
terminal.run('test')
52+
}
53+
})
4154
}
4255
},
4356
color: {

src/api/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const initTerminal = ({
4444
run: (cmd: string) => evalCommand(cmd, terminal),
4545
start: () => startProcess(terminal),
4646
stop: () => stopProcess(terminal),
47-
type: (text: string, speed = 60, callback) => typeText(text, speed, terminal, callback),
47+
type: (text: string, speed = 60, isCommand) => typeText(text, speed, terminal, isCommand),
4848
setPrompt: (newPrompt: string) => setPrompt(newPrompt, inputContainer, settings)
4949
}
5050
if (enableHelp) {

src/api/typeText.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ import { TerminalInstance } from '../types'
22
import { startProcess, stopProcess } from './process'
33
import { create } from '../helpers/dom'
44

5-
const typeText = (text: string, speed: number, terminal: TerminalInstance, callback?: () => void) => {
5+
const typeText = (text: string, speed: number, terminal: TerminalInstance, isCommand?: boolean) => new Promise<boolean>(resolve => {
66
startProcess(terminal)
7-
const line = create('p')
7+
const line = create('p', undefined, isCommand ? terminal.settings.prompt : '')
8+
const cmd = create('span', 'terminal-command', '')
9+
if (isCommand) {
10+
line.append(cmd)
11+
}
812
terminal.commandContainer.append(line)
913
let i = 0
14+
const typeTarget = isCommand ? cmd : line
1015
const type = () => {
1116
if (i < text.length && terminal.isProcessRunning) {
12-
line.innerHTML += text.charAt(i)
17+
typeTarget.innerHTML += text.charAt(i)
1318
i++
1419
setTimeout(type, speed)
1520
} else {
16-
callback && callback()
1721
stopProcess(terminal)
22+
resolve(i === text.length)
1823
}
1924
}
2025
setTimeout(type, speed)
21-
}
26+
})
2227

2328
export default typeText

src/types/terminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type TerminalInstance = {
1111
run: (cmd: string) => void
1212
start: () => void
1313
stop: () => void
14-
type: (text: string, speed?: number, callback?: () => void) => void
14+
type: (text: string, speed?: number, isCommand?: boolean) => Promise<boolean>
1515
setPrompt: (newPrompt: string) => void
1616
isProcessRunning: boolean
1717
}

0 commit comments

Comments
 (0)