@make-u-free/migi 0.3.9 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/migi.js +53 -17
- package/package.json +1 -1
package/bin/migi.js
CHANGED
|
@@ -81,56 +81,92 @@ function sepWithLabel(label) {
|
|
|
81
81
|
// ---- チャット入力(Enter送信 / Shift+Enter改行)----
|
|
82
82
|
async function readChatInput() {
|
|
83
83
|
return new Promise((resolve) => {
|
|
84
|
+
const PFIRST = ' > '
|
|
85
|
+
const PCONT = ' '
|
|
84
86
|
const lines = ['']
|
|
87
|
+
let curLine = 0
|
|
88
|
+
let drawnLines = 0
|
|
85
89
|
|
|
86
90
|
emitKeypressEvents(process.stdin)
|
|
87
91
|
if (process.stdin.isTTY) process.stdin.setRawMode(true)
|
|
88
|
-
|
|
92
|
+
|
|
93
|
+
function draw() {
|
|
94
|
+
const w = process.stdout.columns || 80
|
|
95
|
+
|
|
96
|
+
// 前回描画分をクリア
|
|
97
|
+
if (drawnLines > 0) {
|
|
98
|
+
process.stdout.write(`\x1b[${drawnLines}A\x1b[1G`)
|
|
99
|
+
for (let i = 0; i < drawnLines; i++) {
|
|
100
|
+
process.stdout.write('\x1b[2K\n')
|
|
101
|
+
}
|
|
102
|
+
process.stdout.write(`\x1b[${drawnLines}A\x1b[1G`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 入力行 + セパレーター + ガイド を描画
|
|
106
|
+
const allLines = [
|
|
107
|
+
...lines.map((l, i) => chalk.cyan(i === 0 ? PFIRST : PCONT) + l),
|
|
108
|
+
chalk.dim('─'.repeat(w)),
|
|
109
|
+
chalk.dim(` ✦ ${model} · Shift+Enterで改行 / Enterで送信`)
|
|
110
|
+
]
|
|
111
|
+
drawnLines = allLines.length
|
|
112
|
+
process.stdout.write(allLines.join('\n') + '\n')
|
|
113
|
+
|
|
114
|
+
// カーソルを入力行の末尾に移動
|
|
115
|
+
const linesBelow = drawnLines - curLine
|
|
116
|
+
if (linesBelow > 0) process.stdout.write(`\x1b[${linesBelow}A`)
|
|
117
|
+
const col = (curLine === 0 ? PFIRST : PCONT).length + lines[curLine].length + 1
|
|
118
|
+
process.stdout.write(`\x1b[${col}G`)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
draw()
|
|
89
122
|
|
|
90
123
|
const onKey = (str, key) => {
|
|
91
124
|
if (!key) {
|
|
92
|
-
|
|
93
|
-
if (str) { lines[lines.length - 1] += str; process.stdout.write(str) }
|
|
125
|
+
if (str) { lines[curLine] += str; draw() }
|
|
94
126
|
return
|
|
95
127
|
}
|
|
96
128
|
|
|
97
|
-
// Ctrl+C
|
|
98
129
|
if (key.ctrl && key.name === 'c') {
|
|
99
|
-
|
|
130
|
+
process.stdout.write(`\x1b[${drawnLines - curLine}B\n`)
|
|
131
|
+
process.stdin.removeListener('keypress', onKey)
|
|
132
|
+
if (process.stdin.isTTY) process.stdin.setRawMode(false)
|
|
133
|
+
console.log(chalk.cyan('\n お疲れ様でした!またね。\n'))
|
|
100
134
|
process.exit(0)
|
|
101
135
|
}
|
|
102
136
|
|
|
103
137
|
if (key.name === 'return') {
|
|
104
138
|
if (key.shift) {
|
|
105
139
|
// Shift+Enter → 改行
|
|
106
|
-
lines.
|
|
107
|
-
|
|
140
|
+
lines.splice(curLine + 1, 0, '')
|
|
141
|
+
curLine++
|
|
142
|
+
draw()
|
|
108
143
|
} else {
|
|
109
144
|
// Enter → 送信
|
|
110
145
|
const content = lines.join('\n').trim()
|
|
111
|
-
if (!content) return
|
|
146
|
+
if (!content) return
|
|
147
|
+
process.stdout.write(`\x1b[${drawnLines - curLine}B\n`)
|
|
112
148
|
process.stdin.removeListener('keypress', onKey)
|
|
113
149
|
if (process.stdin.isTTY) process.stdin.setRawMode(false)
|
|
114
|
-
process.stdout.write('\n')
|
|
115
|
-
console.log(sep())
|
|
116
|
-
console.log(chalk.dim(` ✦ ${model} · Shift+Enterで改行 / Enterで送信`))
|
|
117
150
|
resolve(content)
|
|
118
151
|
}
|
|
119
152
|
return
|
|
120
153
|
}
|
|
121
154
|
|
|
122
155
|
if (key.name === 'backspace') {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
156
|
+
if (lines[curLine].length > 0) {
|
|
157
|
+
lines[curLine] = lines[curLine].slice(0, -1)
|
|
158
|
+
draw()
|
|
159
|
+
} else if (curLine > 0) {
|
|
160
|
+
lines.splice(curLine, 1)
|
|
161
|
+
curLine--
|
|
162
|
+
draw()
|
|
127
163
|
}
|
|
128
164
|
return
|
|
129
165
|
}
|
|
130
166
|
|
|
131
167
|
if (str && !key.ctrl && !key.meta) {
|
|
132
|
-
lines[
|
|
133
|
-
|
|
168
|
+
lines[curLine] += str
|
|
169
|
+
draw()
|
|
134
170
|
}
|
|
135
171
|
}
|
|
136
172
|
|