Skip to content

Commit 353955d

Browse files
authored
fix: #16: types for commands API (#18)
* fix: #16: types for commands API * fix: merged terminal and terminalInstance into one type
1 parent 879d889 commit 353955d

13 files changed

+55
-50
lines changed

src/api/evalCommand.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Terminal } from '../types'
1+
import { TerminalInstance } from '../types'
22
import { dispatchEvent, TerminalEvent } from '../helpers/events'
33

4-
const evalCommand = (cmd: string, instance: Terminal) => {
4+
const evalCommand = (cmd: string, instance: TerminalInstance) => {
55
const { print } = instance
66
const splitCommand = cmd.split(' ')
77
const command = splitCommand[0]

src/api/init.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Terminal, TerminalSettings } from '../types'
1+
import { TerminalInstance, TerminalSettings } from '../types'
22
import buildTree from '../helpers/tree'
33
import print from './print'
44
import evalCommand from './evalCommand'
@@ -26,7 +26,7 @@ const initTerminal = ({
2626
}
2727
const { commandContainer, input, inputContainer } = buildTree(host, prompt)
2828

29-
const terminal: Terminal = {
29+
const terminal: TerminalInstance = {
3030
history: [],
3131
lastHistoryIndex: 0,
3232
isProcessRunning: false,

src/api/process.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Terminal } from '../types'
1+
import { TerminalInstance } from '../types'
22
import { dispatchEvent, TerminalEvent } from '../helpers/events'
33
import { toggleInput } from '../helpers/dom'
44

5-
export const startProcess = (terminal: Terminal) => {
5+
export const startProcess = (terminal: TerminalInstance) => {
66
const { settings: { host } } = terminal
77
toggleInput(terminal)
88
terminal.isProcessRunning = true
99
dispatchEvent(TerminalEvent.ON_PROCESS_START, host)
1010
}
1111

12-
export const stopProcess = (terminal: Terminal) => {
12+
export const stopProcess = (terminal: TerminalInstance) => {
1313
const { input, settings: { host } } = terminal
1414
toggleInput(terminal, true)
1515
terminal.isProcessRunning = false

src/api/typeText.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Terminal } from '../types'
1+
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: Terminal, callback?: () => void) => {
5+
const typeText = (text: string, speed: number, terminal: TerminalInstance, callback?: () => void) => {
66
startProcess(terminal)
77
const line = create('p')
88
terminal.commandContainer.append(line)

src/helpers/dom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Terminal } from '../types'
1+
import { TerminalInstance } from '../types'
22

33
export const create = (tagName: string, className?: string, content?: string) => {
44
const element = document.createElement(tagName)
@@ -7,7 +7,7 @@ export const create = (tagName: string, className?: string, content?: string) =>
77
return element
88
}
99

10-
export const toggleInput = (terminal: Terminal, enable = false) => {
10+
export const toggleInput = (terminal: TerminalInstance, enable = false) => {
1111
// Has to be done in order to preserve the focus on terminal click when disabled
1212
terminal.input.readOnly = !enable
1313
terminal.inputContainer.style.opacity = enable ? '' : '0'

src/helpers/help.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Terminal, TerminalCommand } from '../types'
1+
import { TerminalInstance, TerminalCommand } from '../types'
22

3-
const createHelp = (terminal: Terminal): TerminalCommand => ({
3+
const createHelp = (terminal: TerminalInstance): TerminalCommand => ({
44
name: 'help',
55
description: 'shows a full list of all available commands',
66
func: ({ print }) => {

src/helpers/history.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Terminal } from '../types'
1+
import { TerminalInstance } from '../types'
22

3-
export const populateHistory = (instance: Terminal) => {
3+
export const populateHistory = (instance: TerminalInstance) => {
44
const { input: { value }, history, settings: { historyLength } } = instance
55
if (value === history[0]) {
66
return
@@ -13,7 +13,7 @@ export const populateHistory = (instance: Terminal) => {
1313
instance.lastHistoryIndex = 0
1414
}
1515

16-
export const searchHistory = (instance: Terminal, isDown?: boolean) => {
16+
export const searchHistory = (instance: TerminalInstance, isDown?: boolean) => {
1717
const { history, lastHistoryIndex } = instance
1818
const endOfHistory = history.length - 1
1919
let newIndex: number

src/helpers/keyboard.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Terminal } from '../types'
1+
import { TerminalInstance } from '../types'
22
import evalCommand from '../api/evalCommand'
33
import { populateHistory, searchHistory } from './history'
44
import { stopProcess } from '../api/process'
55
import { dispatchEvent, TerminalEvent } from './events'
66

7-
export const attachKeyboardListener = (host: HTMLElement, instance: Terminal) => {
7+
export const attachKeyboardListener = (host: HTMLElement, instance: TerminalInstance) => {
88
const { input, print } = instance
99
host.addEventListener('keyup', ({ key, ctrlKey }) => {
1010
if (ctrlKey && key === 'c' && instance.isProcessRunning) {

src/types/index.ts

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
1-
export type TerminalInstance = {
2-
print: (text: string, isCommand?: boolean) => void
3-
run: (cmd: string) => void
4-
start: () => void
5-
stop: () => void
6-
type: (text: string, speed?: number, callback?: () => void) => void
7-
isProcessRunning: boolean
8-
}
9-
export type TerminalCommand = {
10-
name: string
11-
description: string
12-
argDescriptions?: string[]
13-
func: (terminal: TerminalInstance, ...args: string[]) => void
14-
}
15-
16-
export type TerminalSettings = {
17-
host: HTMLElement,
18-
commands: Record<string, TerminalCommand>
19-
welcomeMessage?: string,
20-
prompt?: string
21-
historyLength?: number
22-
enableHelp?: boolean
23-
}
24-
25-
export type Terminal = {
26-
settings: TerminalSettings
27-
commandContainer: HTMLElement
28-
inputContainer: HTMLElement
29-
input: HTMLInputElement
30-
history: string[]
31-
lastHistoryIndex: number
32-
} & TerminalInstance
1+
export * from './terminal'
2+
export * from './terminalCommand'
3+
export * from './terminalSettings'

src/types/terminal.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TerminalSettings } from './terminalSettings'
2+
3+
export type TerminalInstance = {
4+
settings: TerminalSettings
5+
commandContainer: HTMLElement
6+
inputContainer: HTMLElement
7+
input: HTMLInputElement
8+
history: string[]
9+
lastHistoryIndex: number
10+
print: (text: string, isCommand?: boolean) => void
11+
run: (cmd: string) => void
12+
start: () => void
13+
stop: () => void
14+
type: (text: string, speed?: number, callback?: () => void) => void
15+
isProcessRunning: boolean
16+
}

src/types/terminalCommand.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { TerminalInstance } from './terminal'
2+
3+
export type TerminalCommand = {
4+
name: string
5+
description: string
6+
argDescriptions?: string[]
7+
func: (terminal: TerminalInstance, ...args: string[]) => void
8+
}

src/types/terminalInstance.ts

Whitespace-only changes.

src/types/terminalSettings.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TerminalCommand } from './terminalCommand'
2+
3+
export type TerminalSettings = {
4+
host: HTMLElement,
5+
commands: Record<string, TerminalCommand>
6+
welcomeMessage?: string,
7+
prompt?: string
8+
historyLength?: number
9+
enableHelp?: boolean
10+
}

0 commit comments

Comments
 (0)