-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
stdin.ts
106 lines (89 loc) · 2.42 KB
/
stdin.ts
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
import readline from 'readline'
import c from 'picocolors'
import type { Vitest } from './core'
const keys = [
['a', 'rerun all tests'],
['f', 'rerun only failed tests'],
['u', 'update snapshot'],
['t', 'filter by a test name regex pattern'],
['q', 'quit'],
]
export function printShortcutsHelp() {
process.stdout.write(
`
${c.bold(' Watch Usage')}
${keys.map(i => c.dim(' press ') + c.reset(c.bold(i[0])) + c.dim(` to ${i[1]}`)).join('\n')}
`,
)
}
function useChangePattern(ctx: Vitest) {
let namePattern = ''
let changingPattern = false
function backspace() {
readline.moveCursor(process.stdout, -1, process.stdout.rows)
process.stdout.write(' ')
readline.moveCursor(process.stdout, -1, process.stdout.rows)
}
function end() {
ctx.changeNamePattern(namePattern, undefined, 'change pattern')
namePattern = ''
changingPattern = false
}
function start() {
process.stdout.write(`\n${c.bgMagenta(' FILTER ')} ${c.magenta('Filter tests by their name (using regexp):')} `)
changingPattern = true
}
function append(str: string, key: any) {
if (key.name === 'backspace') {
namePattern = namePattern.slice(0, namePattern.length - 1)
backspace()
}
else {
namePattern += str
process.stdout.write(str)
}
}
return {
get isChanging() {
return changingPattern
},
end,
start,
append,
}
}
export function registerConsoleShortcuts(ctx: Vitest) {
const pattern = useChangePattern(ctx)
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on('keypress', (str: string, key: any) => {
// ctrl-c or esc
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c'))
return ctx.exit(true)
// is running, ignore keypress
if (ctx.runningPromise)
return
const name = key?.name
if (pattern.isChanging) {
if (name === 'return')
return pattern.end()
return pattern.append(str, key)
}
// help
if (name === 'h')
return printShortcutsHelp()
// update snapshot
if (name === 'u')
return ctx.updateSnapshot()
// rerun all tests
if (name === 'a' || name === 'return')
return ctx.rerunFiles(undefined, 'rerun all')
// change testNamePattern
if (name === 't')
return pattern.start()
// quit
if (name === 'q')
return ctx.exit(true)
// TODO: add more commands
})
}