@make-u-free/migi 0.3.9 → 0.4.1

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.
Files changed (2) hide show
  1. package/bin/migi.js +53 -17
  2. 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
- process.stdout.write(chalk.cyan(' '))
92
+
93
+ function draw() {
94
+ const w = process.stdout.columns || 80
95
+ const allLines = [
96
+ ...lines.map((l, i) => chalk.cyan(i === 0 ? PFIRST : PCONT) + l),
97
+ chalk.dim('─'.repeat(w)),
98
+ chalk.dim(` ✦ ${model} · Shift+Enterで改行 / Enterで送信`)
99
+ ]
100
+
101
+ // すべての操作を1つのバッファにまとめてから一括書き込み(ちらつき防止)
102
+ let buf = ''
103
+
104
+ if (drawnLines > 0) {
105
+ buf += `\x1b[${drawnLines}A\x1b[1G`
106
+ for (let i = 0; i < drawnLines; i++) buf += '\x1b[2K\n'
107
+ buf += `\x1b[${drawnLines}A\x1b[1G`
108
+ }
109
+
110
+ drawnLines = allLines.length
111
+ buf += allLines.join('\n') + '\n'
112
+
113
+ const linesBelow = drawnLines - curLine
114
+ if (linesBelow > 0) buf += `\x1b[${linesBelow}A`
115
+ const col = (curLine === 0 ? PFIRST : PCONT).length + lines[curLine].length + 1
116
+ buf += `\x1b[${col}G`
117
+
118
+ process.stdout.write(buf)
119
+ }
120
+
121
+ draw()
89
122
 
90
123
  const onKey = (str, key) => {
91
124
  if (!key) {
92
- // IME確定などの複合文字
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
- console.log(chalk.cyan('\n\n お疲れ様でした!またね。\n'))
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.push('')
107
- process.stdout.write('\n ')
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
- const cur = lines[lines.length - 1]
124
- if (cur.length > 0) {
125
- lines[lines.length - 1] = cur.slice(0, -1)
126
- process.stdout.write('\b \b')
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[lines.length - 1] += str
133
- process.stdout.write(str)
168
+ lines[curLine] += str
169
+ draw()
134
170
  }
135
171
  }
136
172
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@make-u-free/migi",
3
- "version": "0.3.9",
3
+ "version": "0.4.1",
4
4
  "description": "Your AI right-hand agent. Works anywhere, with any LLM API.",
5
5
  "type": "module",
6
6
  "bin": {