forked from OIRNOIR/WackyWebM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal-ui.js
248 lines (225 loc) · 7.19 KB
/
terminal-ui.js
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
'use strict'
// do this before importing wackywebm or util, so that they use our modified locale
const { setLocale, localizeString } = require('./localization.js')
if (process.argv.length > 2)
setLocale(process.argv[2])
// spaghetti ahead; beware.
// i strongly advise no one to ever touch this again unless you're being paid a lot for it.
const { terminal: term } = require('terminal-kit')
const path = require('path')
const { modes, args, main } = require('./wackywebm.js')
const { getFileName } = require('./util')
const fs = require('fs')
// 1: select mode to use
// 2: optional and required flags
// 3: file selection
// 4: confirm
// 5: progress bar
let stage = 1
term.grabInput()
const modeList = Object.keys(modes)
let selectedMode = modeList.indexOf('bounce')
const redrawStage1 = () => {
term.clear()
term.bold.underline(`${localizeString('select_mode_arrows')}\n\n`)
for (let modeIx in modeList) {
// i dont know why js decided that iterating over an array's indices should give you strings...
modeIx = parseInt(modeIx)
if (modeIx === selectedMode)
term.italic.underline(modeList[modeIx])
else
term(modeList[modeIx])
term(' ')
}
}
const flags = {}
let editingText = false
let currentEdit = undefined
let currentText = ''
// very jank - half of this stage 2 code only works if all the arguments start in "--"
const keysToFlags = {
'b': '--bitrate',
't': '--thread',
'o': '--output',
'c': '--compression',
's': '--smoothing'
}
const redrawStage2 = () => {
term.clear()
if (currentEdit === undefined) {
term.bold.underline(`${modeList[selectedMode] === 'keyframes' ? localizeString('change_options_k') : localizeString('change_options')}\n\n`)
// the process of figuring out which options to display here could possibly be automated, but it seems too much
// trouble for the marginal benefit, considering how rarely new ones get added.
for (const key of Object.keys(keysToFlags)) {
term.italic(key)
term(`: ${args.filter(a => a.keys.includes(keysToFlags[key]))[0].description.replace(/%/g, '%%')}\n`)
}
term.bold.underline(`\n${localizeString('current_arg_values')}\n`)
for (const flag of Object.keys(flags))
term(`${flag} = "${flags[flag].replace(/%/g, '%%')}"\n`)
} else {
term.bold.underline(`${localizeString('enter_arg_value', { arg: currentEdit })}\n`)
term.italic(currentText.replace(/%/g, '%%'))
}
}
let filename = ''
const redrawStage3 = () => {
term.clear()
term.bold.underline(`${localizeString('enter_file_path')}\n\n`)
editingText = true
term(filename.replace(/%/g, '%%'))
}
const redrawStage4 = () => {
term.clear()
term.bold.underline(`${localizeString('review_settings')}\n\n`)
term.underline(localizeString('r_s_mode'))
term(` ${modeList[selectedMode]}\n`)
term.underline(localizeString('r_s_args'))
term('\n')
for (let argName of Object.keys(flags)) {
term.italic(`\t${argName}: `)
term(flags[argName].replace(/%/g, '%%') + '\n')
}
term.underline(localizeString('r_s_file'))
term(` ${filename.replace(/%/g, '%%')}\n`)
}
let mainTask
let mainTaskDone = false
const redrawStage5 = async () => {
if (!mainTaskDone) {
term.clear()
try {
await mainTask
term('\n\n\n')
term.bold.underline(localizeString('tui_done'))
} catch (e) {
term('\n\n\n')
term.bold.underline(localizeString('tui_error') + '\n')
console.error(e)
}
mainTaskDone = true
}
}
term.on('key', (name) => {
// console.log('key event: ', name)
if ((['Q', 'q'].includes(name) && !editingText) || name === 'CTRL_C')
process.exit(0)
// DO NOT EVER TOUCH STAGES 1 AND 2
// i have written them and lost track of how anything works about 5 minutes later. good luck to anyone else trying
// to find out.
// 3-5 are a little easier to understand, but still not particularly clean code, so be careful and dont break anything
if (stage === 1) {
if (name === 'LEFT')
selectedMode = Math.max(0, selectedMode - 1)
if (name === 'RIGHT')
selectedMode = Math.min(modeList.length - 1, selectedMode + 1)
if (name === 'ENTER') {
stage = 2
if (modeList[selectedMode] === 'keyframes')
keysToFlags['x'] = '--keyframes'
else if (modeList[selectedMode] === 'bounce' || modeList[selectedMode] === 'shutter')
keysToFlags['x'] = '--tempo'
else if (modeList[selectedMode] === 'angle')
keysToFlags['x'] = '--angle'
else if (modeList[selectedMode] === 'transparency')
keysToFlags['x'] = '--transparency'
}
} else if (stage === 2) {
if (editingText && name !== 'ENTER') {
if (name === 'BACKSPACE')
currentText = currentText.substring(0, currentText.length - 1)
else if (name === 'ESCAPE' || name === 'SHIFT_ESCAPE' || name === 'CTRL_ESCAPE') {
currentText = ""
editingText = false
currentEdit = undefined
}
// this catches SOME control characters, like F2, but also definitely *does not* catch some "normal" UTF-8 chars, as intended.
else if (name.length > 2) {
/* ignore inputs like LEFT, TAB, etc */
}
else
currentText += name
} else if (editingText && name === 'ENTER') {
if (currentText === "")
{
currentText = ""
editingText = false
currentEdit = undefined
} else {
editingText = false
flags[currentEdit] = currentText
currentEdit = undefined
currentText = ''
}
} else if (keysToFlags[name] !== undefined) {
editingText = true
currentEdit = keysToFlags[name]
currentText = flags[keysToFlags[name]] ?? ''
} else if (name === 'ENTER') {
if (modeList[selectedMode] === 'keyframes' && flags['--keyframes'] === undefined)
return redrawStage2() || term(`\n\n${localizeString('keyframes_file_needed')}`)
stage = 3
}
} else if (stage === 3) {
if (name === 'ENTER') {
if (!fs.existsSync(filename))
return redrawStage3() || term(`\n\n${localizeString('file_not_found')}`)
stage = 4
editingText = false
}
else {
if (name === 'BACKSPACE')
filename = filename.substring(0, filename.length - 1)
else if (name.length > 2) {
/* ignore inputs like LEFT, TAB, etc */
}
else
filename += name
}
} else if (stage === 4) {
if (name === 'ENTER') {
stage = 5
if (!flags['--bitrate'])
flags['--bitrate'] = '1M'
if (!flags['--thread'])
flags['--thread'] = 2
if (!flags['--tempo'])
flags['--tempo'] = 2
if (!flags['--angle'])
flags['--angle'] = 360
if (!flags['--compression'])
flags['--compression'] = 0
if (!flags['--transparency'])
flags['--transparency'] = 1
if (!flags['--smoothing'])
flags['--smoothing'] = 0
if (!flags['--output'])
// not perfect, but works well enough
flags['--output'] = `${path.join(path.dirname(filename), getFileName(filename))}_${modeList[selectedMode]}.webm`
mainTask = main([modeList[selectedMode]], filename, {
keyframes: flags['--keyframes'],
bitrate: flags['--bitrate'],
thread: flags['--thread'],
tempo: flags['--tempo'],
angle: flags['--angle'],
compression: flags['--compression'],
transparency: flags['--transparency'],
smoothing: flags['--smoothing']
}, flags['--output'])
}
} else if (stage === 5) {
if (mainTaskDone)
process.exit(0)
}
if (stage === 1)
redrawStage1()
if (stage === 2)
redrawStage2()
if (stage === 3)
redrawStage3()
if (stage === 4)
redrawStage4()
if (stage === 5)
redrawStage5()
})
redrawStage1()