Skip to content

Commit

Permalink
feat: promise-based type() API that now supports treating text as a…
Browse files Browse the repository at this point in the history
… command
  • Loading branch information
mkrl committed Jul 22, 2023
1 parent eab39e1 commit d1c1db9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ initTerminal(settings)

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

| Method | Description | Parameters |
| ------------- | ------------- | ------------- |
| `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) |
| `run(text)` | Emulates a command execution in a terminal (acts the same way as a user would have typed and pressed Enter) | `text` - String |
| `start()` | Starts a "foreground process": user input is blocked and command prompt never appears. | |
| `stop()` | Stops "foreground process". | |
| `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. |
| `setPrompt()` | Set terminal prompt | `newPrompt` - String |
| Method | Description | Parameters |
|----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
| `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) |
| `run(text)` | Emulates a command execution in a terminal (acts the same way as a user would have typed and pressed Enter) | `text` - String |
| `start()` | Starts a "foreground process": user input is blocked and command prompt never appears. | |
| `stop()` | Stops "foreground process". | |
| `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) |
| `setPrompt()` | Set terminal prompt | `newPrompt` - String |

### settings object

Expand Down
23 changes: 18 additions & 5 deletions preview/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,24 @@ const terminal = initTerminal({
func: (terminal) => {
terminal.print('Downloading more RAM...')
terminal.print('Press Ctrl + C if you think you have enough RAM')
terminal.type('............................................', 160, () => {
if (terminal.isProcessRunning) {
terminal.print('Successfully downloaded 42gb of RAM!')
}
})
terminal.type('............................................', 100)
.then((finished) => {
if (finished) {
terminal.print('Successfully downloaded 42gb of RAM!')
}
})
}
},
enqueue: {
name: 'enqueue',
description: 'pretend like someone else is typing in the terminal',
func: (terminal) => {
terminal.type('test', 70, true)
.then((finished) => {
if (finished) {
terminal.run('test')
}
})
}
},
color: {
Expand Down
2 changes: 1 addition & 1 deletion src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const initTerminal = ({
run: (cmd: string) => evalCommand(cmd, terminal),
start: () => startProcess(terminal),
stop: () => stopProcess(terminal),
type: (text: string, speed = 60, callback) => typeText(text, speed, terminal, callback),
type: (text: string, speed = 60, isCommand) => typeText(text, speed, terminal, isCommand),
setPrompt: (newPrompt: string) => setPrompt(newPrompt, inputContainer, settings)
}
if (enableHelp) {
Expand Down
15 changes: 10 additions & 5 deletions src/api/typeText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ import { TerminalInstance } from '../types'
import { startProcess, stopProcess } from './process'
import { create } from '../helpers/dom'

const typeText = (text: string, speed: number, terminal: TerminalInstance, callback?: () => void) => {
const typeText = (text: string, speed: number, terminal: TerminalInstance, isCommand?: boolean) => new Promise<boolean>(resolve => {
startProcess(terminal)
const line = create('p')
const line = create('p', undefined, isCommand ? terminal.settings.prompt : '')
const cmd = create('span', 'terminal-command', '')
if (isCommand) {
line.append(cmd)
}
terminal.commandContainer.append(line)
let i = 0
const typeTarget = isCommand ? cmd : line
const type = () => {
if (i < text.length && terminal.isProcessRunning) {
line.innerHTML += text.charAt(i)
typeTarget.innerHTML += text.charAt(i)
i++
setTimeout(type, speed)
} else {
callback && callback()
stopProcess(terminal)
resolve(i === text.length)
}
}
setTimeout(type, speed)
}
})

export default typeText
2 changes: 1 addition & 1 deletion src/types/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type TerminalInstance = {
run: (cmd: string) => void
start: () => void
stop: () => void
type: (text: string, speed?: number, callback?: () => void) => void
type: (text: string, speed?: number, isCommand?: boolean) => Promise<boolean>
setPrompt: (newPrompt: string) => void
isProcessRunning: boolean
}

0 comments on commit d1c1db9

Please sign in to comment.