codecin 7.22.10__py3-none-win_amd64.whl
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.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
package engine
|
|
2
|
+
|
|
3
|
+
// 跨平台交互式行输入 (input / input_str 的终端路径)。
|
|
4
|
+
//
|
|
5
|
+
// 与 get_key 共用同一套键码队列与 raw 终端 (keyEnsure / keyOutSink):
|
|
6
|
+
// 行编辑由引擎自己完成, 不再依赖操作系统的行规程 (Windows ReadConsoleW /
|
|
7
|
+
// Unix canonical 模式 + 内核行编辑)。三条输入通道统一为一种事件监听:
|
|
8
|
+
// - 真实终端: 逐字节/逐事件解码后的键码 (Windows ReadConsoleInputW,
|
|
9
|
+
// Unix termios + 转义序列/UTF-8 解码);
|
|
10
|
+
// - GUI 窗口: 窗口过程把按键注入 keyQueue (guiPressKey);
|
|
11
|
+
// - 虚拟键盘: 系统输入注入的按键同样进入上面的通道。
|
|
12
|
+
//
|
|
13
|
+
// 支持: 字符输入 / 退格 / Delete / 左右 / Home / End / Ctrl+A / Ctrl+E /
|
|
14
|
+
// Ctrl+U / Ctrl+K / Ctrl+W / Ctrl+左/右 / Enter / Ctrl+D(EOF) /
|
|
15
|
+
// Ctrl+C(中断)。光标重绘用 VT/ANSI 相对移动 (Windows 控制台已开 VT 输出,
|
|
16
|
+
// Unix 终端原生支持)。
|
|
17
|
+
//
|
|
18
|
+
// 既没有真实终端也没有 GUI 窗口时返回 used=false, 由 stdin.go 回退到
|
|
19
|
+
// 字节流读取 (管道/重定向语义与旧版本完全一致)。
|
|
20
|
+
|
|
21
|
+
import (
|
|
22
|
+
"strconv"
|
|
23
|
+
"time"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const (
|
|
27
|
+
lineCodeEnter = 13
|
|
28
|
+
lineCodeLF = 10
|
|
29
|
+
lineCodeTab = 9
|
|
30
|
+
lineCodeBackspace = 8
|
|
31
|
+
lineCodeDeleteCh = 127
|
|
32
|
+
lineCodeCtrlA = 1
|
|
33
|
+
lineCodeCtrlC = 3
|
|
34
|
+
lineCodeCtrlD = 4
|
|
35
|
+
lineCodeCtrlE = 5
|
|
36
|
+
lineCodeCtrlK = 11
|
|
37
|
+
lineCodeCtrlU = 21
|
|
38
|
+
lineCodeCtrlW = 23
|
|
39
|
+
lineCodeCtrlZ = 26
|
|
40
|
+
linePollInterval = 5 * time.Millisecond
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
// stdinLineEditor 用键码队列读取一行。返回 (行, 是否成功, 是否接管)。
|
|
44
|
+
// 接管时行编辑与回显已由本函数完成; 未接管 (非终端且无 GUI 窗口) 时
|
|
45
|
+
// 调用方走通用字节流路径。
|
|
46
|
+
func stdinLineEditor() (string, bool, bool) {
|
|
47
|
+
if keyForcedOff {
|
|
48
|
+
return "", false, false
|
|
49
|
+
}
|
|
50
|
+
// 真实终端: 启用 raw 模式, 之后所有按键统一走键码队列。
|
|
51
|
+
keyEnsure()
|
|
52
|
+
keyMu.Lock()
|
|
53
|
+
raw := keyReady
|
|
54
|
+
keyMu.Unlock()
|
|
55
|
+
if !raw && !guiWindowOpen() {
|
|
56
|
+
return "", false, false
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var buf []rune
|
|
60
|
+
pos := 0
|
|
61
|
+
for {
|
|
62
|
+
// 窗口已关闭: 不再等待按键, 按 EOF 返回 (避免永久阻塞)
|
|
63
|
+
if guiUserClosed() {
|
|
64
|
+
return "", false, true
|
|
65
|
+
}
|
|
66
|
+
code, ok := lineKeyPoll()
|
|
67
|
+
if !ok {
|
|
68
|
+
time.Sleep(linePollInterval)
|
|
69
|
+
continue
|
|
70
|
+
}
|
|
71
|
+
switch code {
|
|
72
|
+
case lineCodeEnter, lineCodeLF:
|
|
73
|
+
keyOutSink("\r\n")
|
|
74
|
+
return string(buf), true, true
|
|
75
|
+
case lineCodeBackspace, lineCodeDeleteCh:
|
|
76
|
+
if pos > 0 {
|
|
77
|
+
w := runeWidth(buf[pos-1])
|
|
78
|
+
buf = append(buf[:pos-1], buf[pos:]...)
|
|
79
|
+
pos--
|
|
80
|
+
cursorLeft(w)
|
|
81
|
+
lineRewrite(buf, pos)
|
|
82
|
+
}
|
|
83
|
+
case keyDel:
|
|
84
|
+
if pos < len(buf) {
|
|
85
|
+
buf = append(buf[:pos], buf[pos+1:]...)
|
|
86
|
+
lineRewrite(buf, pos)
|
|
87
|
+
}
|
|
88
|
+
case keyLeft:
|
|
89
|
+
if pos > 0 {
|
|
90
|
+
pos--
|
|
91
|
+
cursorLeft(runeWidth(buf[pos]))
|
|
92
|
+
}
|
|
93
|
+
case keyRight:
|
|
94
|
+
if pos < len(buf) {
|
|
95
|
+
cursorRight(runeWidth(buf[pos]))
|
|
96
|
+
pos++
|
|
97
|
+
}
|
|
98
|
+
case keyHome, lineCodeCtrlA:
|
|
99
|
+
cursorLeft(totalWidth(buf[:pos]))
|
|
100
|
+
pos = 0
|
|
101
|
+
case keyEnd, lineCodeCtrlE:
|
|
102
|
+
cursorRight(totalWidth(buf[pos:]))
|
|
103
|
+
pos = len(buf)
|
|
104
|
+
case keyCtrlLeft:
|
|
105
|
+
np := wordLeftPos(buf, pos)
|
|
106
|
+
cursorLeft(totalWidth(buf[np:pos]))
|
|
107
|
+
pos = np
|
|
108
|
+
case keyCtrlRight:
|
|
109
|
+
np := wordRightPos(buf, pos)
|
|
110
|
+
cursorRight(totalWidth(buf[pos:np]))
|
|
111
|
+
pos = np
|
|
112
|
+
case lineCodeCtrlU:
|
|
113
|
+
if pos > 0 {
|
|
114
|
+
cursorLeft(totalWidth(buf[:pos]))
|
|
115
|
+
buf = append([]rune(nil), buf[pos:]...)
|
|
116
|
+
pos = 0
|
|
117
|
+
lineRewrite(buf, 0)
|
|
118
|
+
}
|
|
119
|
+
case lineCodeCtrlK:
|
|
120
|
+
buf = buf[:pos]
|
|
121
|
+
keyOutSink("\x1b[K")
|
|
122
|
+
case lineCodeCtrlW:
|
|
123
|
+
start := wordLeftPos(buf, pos)
|
|
124
|
+
if start < pos {
|
|
125
|
+
cursorLeft(totalWidth(buf[start:pos]))
|
|
126
|
+
buf = append(buf[:start], buf[pos:]...)
|
|
127
|
+
pos = start
|
|
128
|
+
lineRewrite(buf, pos)
|
|
129
|
+
}
|
|
130
|
+
case lineCodeCtrlC:
|
|
131
|
+
// 与行规程 (ISIG) 下 Ctrl+C 一致: 恢复终端后中断进程。
|
|
132
|
+
keyOutSink("^C\r\n")
|
|
133
|
+
raiseLineInterrupt()
|
|
134
|
+
return "", false, true
|
|
135
|
+
case lineCodeCtrlZ:
|
|
136
|
+
keyOutSink("\r\n")
|
|
137
|
+
return "", false, true
|
|
138
|
+
case lineCodeCtrlD:
|
|
139
|
+
if len(buf) == 0 {
|
|
140
|
+
keyOutSink("\r\n")
|
|
141
|
+
return "", false, true // EOF
|
|
142
|
+
}
|
|
143
|
+
if pos < len(buf) { // 非空行: 删除光标处字符 (与 readline 一致)
|
|
144
|
+
buf = append(buf[:pos], buf[pos+1:]...)
|
|
145
|
+
lineRewrite(buf, pos)
|
|
146
|
+
}
|
|
147
|
+
default:
|
|
148
|
+
// 扩展键码 (方向键/功能键/修饰组合, 1001..1109) 在单行编辑中无含义,
|
|
149
|
+
// 不能当成 Unicode 码点插入。
|
|
150
|
+
if code >= keyUp && code <= keyShiftTab {
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
if code > 0x10FFFF {
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
r := rune(code)
|
|
157
|
+
if r == lineCodeTab {
|
|
158
|
+
r = '\t'
|
|
159
|
+
} else if r < 0x20 || r == lineCodeDeleteCh {
|
|
160
|
+
continue // 其余控制字符忽略 (功能键/修饰键等)
|
|
161
|
+
}
|
|
162
|
+
old := pos
|
|
163
|
+
buf = append(buf, 0)
|
|
164
|
+
copy(buf[old+1:], buf[old:])
|
|
165
|
+
buf[old] = r
|
|
166
|
+
pos = old + 1
|
|
167
|
+
lineRewrite(buf, old) // 重画插入点之后的整段, 光标回到 old
|
|
168
|
+
cursorRight(runeWidth(r))
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// lineKeyPoll 非阻塞取一个键码 (与 get_key 相同的队列语义)。
|
|
174
|
+
func lineKeyPoll() (uint64, bool) {
|
|
175
|
+
keyMu.Lock()
|
|
176
|
+
defer keyMu.Unlock()
|
|
177
|
+
if len(keyQueue) == 0 {
|
|
178
|
+
keyDrainLocked()
|
|
179
|
+
}
|
|
180
|
+
if len(keyQueue) == 0 {
|
|
181
|
+
return 0, false
|
|
182
|
+
}
|
|
183
|
+
code := keyQueue[0]
|
|
184
|
+
keyQueue = keyQueue[1:]
|
|
185
|
+
return code, true
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// lineRewrite 从 from 处重画到行尾并清除残留, 结束时光标回到 from。
|
|
189
|
+
// 调用前光标必须已在 from 对应的列上。
|
|
190
|
+
func lineRewrite(buf []rune, from int) {
|
|
191
|
+
tail := string(buf[from:])
|
|
192
|
+
keyOutSink(tail)
|
|
193
|
+
keyOutSink("\x1b[K")
|
|
194
|
+
cursorLeft(totalWidth(buf[from:]))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
func cursorLeft(n int) {
|
|
198
|
+
if n > 0 {
|
|
199
|
+
keyOutSink("\x1b[" + strconv.Itoa(n) + "D")
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func cursorRight(n int) {
|
|
204
|
+
if n > 0 {
|
|
205
|
+
keyOutSink("\x1b[" + strconv.Itoa(n) + "C")
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// totalWidth 一段文本在终端上的显示列数 (CJK 等宽字符按 2 列)。
|
|
210
|
+
func totalWidth(rs []rune) int {
|
|
211
|
+
w := 0
|
|
212
|
+
for _, r := range rs {
|
|
213
|
+
w += runeWidth(r)
|
|
214
|
+
}
|
|
215
|
+
return w
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// runeWidth 单字符显示宽度 (近似 East Asian Width: 宽字符 2 列)。
|
|
219
|
+
func runeWidth(r rune) int {
|
|
220
|
+
switch {
|
|
221
|
+
case r == '\t':
|
|
222
|
+
return 4
|
|
223
|
+
case r < 0x20 || (r >= 0x7F && r < 0xA0):
|
|
224
|
+
return 0
|
|
225
|
+
case r >= 0x1100 && (r <= 0x115F || r == 0x2329 || r == 0x232A ||
|
|
226
|
+
(r >= 0x2E80 && r <= 0xA4CF && r != 0x303F) ||
|
|
227
|
+
(r >= 0xAC00 && r <= 0xD7A3) ||
|
|
228
|
+
(r >= 0xF900 && r <= 0xFAFF) ||
|
|
229
|
+
(r >= 0xFE30 && r <= 0xFE6F) ||
|
|
230
|
+
(r >= 0xFF00 && r <= 0xFF60) ||
|
|
231
|
+
(r >= 0xFFE0 && r <= 0xFFE6) ||
|
|
232
|
+
(r >= 0x1F300 && r <= 0x1F64F) ||
|
|
233
|
+
(r >= 0x1F900 && r <= 0x1F9FF) ||
|
|
234
|
+
(r >= 0x20000 && r <= 0x3FFFD)):
|
|
235
|
+
return 2
|
|
236
|
+
}
|
|
237
|
+
return 1
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// wordLeftPos 上一个词首 (Ctrl+Left / Ctrl+W)。
|
|
241
|
+
func wordLeftPos(buf []rune, pos int) int {
|
|
242
|
+
i := pos
|
|
243
|
+
for i > 0 && buf[i-1] == ' ' {
|
|
244
|
+
i--
|
|
245
|
+
}
|
|
246
|
+
for i > 0 && buf[i-1] != ' ' {
|
|
247
|
+
i--
|
|
248
|
+
}
|
|
249
|
+
return i
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// wordRightPos 下一个词尾 (Ctrl+Right)。
|
|
253
|
+
func wordRightPos(buf []rune, pos int) int {
|
|
254
|
+
i := pos
|
|
255
|
+
for i < len(buf) && buf[i] != ' ' {
|
|
256
|
+
i++
|
|
257
|
+
}
|
|
258
|
+
for i < len(buf) && buf[i] == ' ' {
|
|
259
|
+
i++
|
|
260
|
+
}
|
|
261
|
+
return i
|
|
262
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
package engine
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"strings"
|
|
5
|
+
"testing"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
// lineEditorTestEnv 启用伪键盘监听 (keyReady=true, 不触碰真实终端) 并捕获回显。
|
|
9
|
+
func lineEditorTestEnv(t *testing.T, keys []uint64) *strings.Builder {
|
|
10
|
+
t.Helper()
|
|
11
|
+
resetKeyState()
|
|
12
|
+
keyForcedOff = false
|
|
13
|
+
keyMu.Lock()
|
|
14
|
+
keyReady = true
|
|
15
|
+
keyQueue = append(keyQueue[:0], keys...)
|
|
16
|
+
keyMu.Unlock()
|
|
17
|
+
out := &strings.Builder{}
|
|
18
|
+
saved := keyOutSink
|
|
19
|
+
keyOutSink = func(s string) { out.WriteString(s) }
|
|
20
|
+
t.Cleanup(func() {
|
|
21
|
+
keyOutSink = saved
|
|
22
|
+
resetKeyState()
|
|
23
|
+
})
|
|
24
|
+
return out
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func TestLineEditorInsertAndCursor(t *testing.T) {
|
|
28
|
+
lineEditorTestEnv(t, []uint64{'a', 'b', 'c', keyLeft, 'X', lineCodeEnter})
|
|
29
|
+
line, ok, used := stdinLineEditor()
|
|
30
|
+
if !used || !ok {
|
|
31
|
+
t.Fatalf("应接管并成功读取: used=%v ok=%v", used, ok)
|
|
32
|
+
}
|
|
33
|
+
if line != "abXc" {
|
|
34
|
+
t.Fatalf("光标左移后插入应得到 abXc, 实际 %q", line)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func TestLineEditorBackspaceDeleteAndEdit(t *testing.T) {
|
|
39
|
+
// "abd" + 退格 + "c" -> "abc"
|
|
40
|
+
lineEditorTestEnv(t, []uint64{'a', 'b', 'd', lineCodeBackspace, 'c', lineCodeEnter})
|
|
41
|
+
if line, _, _ := stdinLineEditor(); line != "abc" {
|
|
42
|
+
t.Fatalf("退格后输入应得到 abc, 实际 %q", line)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Home + Delete: 删除首字符
|
|
46
|
+
resetKeyState()
|
|
47
|
+
lineEditorTestEnv(t, []uint64{'x', 'y', keyHome, keyDel, lineCodeEnter})
|
|
48
|
+
if line, _, _ := stdinLineEditor(); line != "y" {
|
|
49
|
+
t.Fatalf("Home+Delete 应删除首字符, 实际 %q", line)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Ctrl+U 清空光标前内容
|
|
53
|
+
resetKeyState()
|
|
54
|
+
lineEditorTestEnv(t, []uint64{'a', 'b', 'c', keyLeft, lineCodeCtrlU, 'z', lineCodeEnter})
|
|
55
|
+
if line, _, _ := stdinLineEditor(); line != "zc" {
|
|
56
|
+
t.Fatalf("Ctrl+U 后输入应得到 zc, 实际 %q", line)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Ctrl+W 删除前一个词
|
|
60
|
+
resetKeyState()
|
|
61
|
+
lineEditorTestEnv(t, []uint64{'a', 'b', ' ', 'c', 'd', lineCodeCtrlW, lineCodeEnter})
|
|
62
|
+
if line, _, _ := stdinLineEditor(); line != "ab " {
|
|
63
|
+
t.Fatalf("Ctrl+W 应删除最后一个词, 实际 %q", line)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
func TestLineEditorUnicodeAndSpecialKeysIgnored(t *testing.T) {
|
|
68
|
+
// 中 (U+4E2D) 与 emoji (U+1F600) 按码点插入; 方向键/功能键不插入字符
|
|
69
|
+
lineEditorTestEnv(t, []uint64{0x4E2D, keyUp, keyDown, keyF1, 0x1F600, lineCodeEnter})
|
|
70
|
+
if line, _, _ := stdinLineEditor(); line != "中😀" {
|
|
71
|
+
t.Fatalf("Unicode 输入/扩展键过滤异常, 实际 %q", line)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func TestLineEditorSelectionKeys(t *testing.T) {
|
|
76
|
+
// Enter 的两种编码 (\r / \n) 都结束输入; Ctrl+D 空行 = EOF
|
|
77
|
+
lineEditorTestEnv(t, []uint64{'o', 'k', lineCodeLF})
|
|
78
|
+
if line, ok, _ := stdinLineEditor(); line != "ok" || !ok {
|
|
79
|
+
t.Fatalf("LF 应结束输入, 实际 %q ok=%v", line, ok)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
resetKeyState()
|
|
83
|
+
lineEditorTestEnv(t, []uint64{lineCodeCtrlD})
|
|
84
|
+
if line, ok, used := stdinLineEditor(); !used || ok || line != "" {
|
|
85
|
+
t.Fatalf("空行 Ctrl+D 应为 EOF, 实际 %q ok=%v used=%v", line, ok, used)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 无真实终端且无 GUI 窗口: 不接管, 由字节流路径处理。
|
|
90
|
+
func TestLineEditorNotUsedWithoutTerminal(t *testing.T) {
|
|
91
|
+
resetKeyState() // keyForcedOff = true, keyReady = false
|
|
92
|
+
defer resetKeyState()
|
|
93
|
+
if line, ok, used := stdinLineEditor(); used || ok || line != "" {
|
|
94
|
+
t.Fatalf("非交互环境不应接管: line=%q ok=%v used=%v", line, ok, used)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func TestRuneWidthAndTotalWidth(t *testing.T) {
|
|
99
|
+
if got := totalWidth([]rune("ab中")); got != 4 {
|
|
100
|
+
t.Fatalf("ab中 应为 4 列, 实际 %d", got)
|
|
101
|
+
}
|
|
102
|
+
if runeWidth('\n') != 0 || runeWidth('a') != 1 || runeWidth('中') != 2 {
|
|
103
|
+
t.Fatalf("显示宽度计算异常")
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//go:build !windows
|
|
2
|
+
|
|
3
|
+
package engine
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"os"
|
|
7
|
+
"syscall"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// raiseLineInterrupt raw 行编辑收到 Ctrl+C 时调用: 先恢复终端 (关 raw),
|
|
11
|
+
// 再把 SIGINT 重新投递给自身, 与行规程 (ISIG) 下的 Ctrl+C 语义一致。
|
|
12
|
+
// Go 运行时对 SIGINT 的默认动作是终止进程; 若宿主已接管信号 (自定义
|
|
13
|
+
// handler), 信号被处理后会返回, 调用方按 EOF 继续。
|
|
14
|
+
func raiseLineInterrupt() {
|
|
15
|
+
keyboardRestore()
|
|
16
|
+
_ = syscall.Kill(os.Getpid(), syscall.SIGINT)
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//go:build windows
|
|
2
|
+
|
|
3
|
+
package engine
|
|
4
|
+
|
|
5
|
+
import "time"
|
|
6
|
+
|
|
7
|
+
var procGenerateCtrlEvent = kernel32.NewProc("GenerateConsoleCtrlEvent")
|
|
8
|
+
|
|
9
|
+
const ctrlCEvent = 0
|
|
10
|
+
|
|
11
|
+
// raiseLineInterrupt raw 行编辑收到 Ctrl+C 时调用: 先恢复控制台模式,
|
|
12
|
+
// 再投递 CTRL_C_EVENT, 与行规程下 Ctrl+C 的行为一致。
|
|
13
|
+
// 无控制台 (GUI 子系统) 时投递失败, 调用方按 EOF 继续。
|
|
14
|
+
func raiseLineInterrupt() {
|
|
15
|
+
keyboardRestore()
|
|
16
|
+
_, _, _ = procGenerateCtrlEvent.Call(ctrlCEvent, 0)
|
|
17
|
+
// 事件由控制台线程分发; 留出时间让默认处理生效
|
|
18
|
+
time.Sleep(50 * time.Millisecond)
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//go:build !windows
|
|
2
|
+
|
|
3
|
+
package engine
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"os"
|
|
7
|
+
"strconv"
|
|
8
|
+
"strings"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func SystemMemoryKB() (uint64, uint64, bool) {
|
|
12
|
+
data, err := os.ReadFile("/proc/meminfo")
|
|
13
|
+
if err != nil {
|
|
14
|
+
return 0, 0, false
|
|
15
|
+
}
|
|
16
|
+
var total, avail uint64
|
|
17
|
+
for _, line := range strings.Split(string(data), "\n") {
|
|
18
|
+
fields := strings.Fields(line)
|
|
19
|
+
if len(fields) < 2 {
|
|
20
|
+
continue
|
|
21
|
+
}
|
|
22
|
+
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
23
|
+
if err != nil {
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
switch fields[0] {
|
|
27
|
+
case "MemTotal:":
|
|
28
|
+
total = value
|
|
29
|
+
case "MemAvailable:":
|
|
30
|
+
avail = value
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if total == 0 {
|
|
34
|
+
return 0, 0, false
|
|
35
|
+
}
|
|
36
|
+
return total, avail, true
|
|
37
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//go:build windows
|
|
2
|
+
|
|
3
|
+
package engine
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"syscall"
|
|
7
|
+
"unsafe"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// memoryStatusEx 对应 Windows MEMORYSTATUSEX 结构。
|
|
11
|
+
type memoryStatusEx struct {
|
|
12
|
+
Length uint32
|
|
13
|
+
MemoryLoad uint32
|
|
14
|
+
TotalPhys uint64
|
|
15
|
+
AvailPhys uint64
|
|
16
|
+
TotalPageFile uint64
|
|
17
|
+
AvailPageFile uint64
|
|
18
|
+
TotalVirtual uint64
|
|
19
|
+
AvailVirtual uint64
|
|
20
|
+
AvailExtendedVirtual uint64
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func SystemMemoryKB() (uint64, uint64, bool) {
|
|
24
|
+
kernel32 := syscall.NewLazyDLL("kernel32.dll")
|
|
25
|
+
proc := kernel32.NewProc("GlobalMemoryStatusEx")
|
|
26
|
+
var status memoryStatusEx
|
|
27
|
+
status.Length = uint32(unsafe.Sizeof(status))
|
|
28
|
+
ret, _, _ := proc.Call(uintptr(unsafe.Pointer(&status)))
|
|
29
|
+
if ret == 0 {
|
|
30
|
+
return 0, 0, false
|
|
31
|
+
}
|
|
32
|
+
return status.TotalPhys / 1024, status.AvailPhys / 1024, true
|
|
33
|
+
}
|