Repository navigation
Expand file tree
/
Copy pathterm.zig
More file actions
283 lines (245 loc) · 10.7 KB
/
Copy pathterm.zig
File metadata and controls
283 lines (245 loc) · 10.7 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const std = @import("std");
const ansi = @import("../ui/style.zig");
const builtin = @import("builtin");
const Io = std.Io;
const File = std.Io.File;
const unsupported_term = [_][]const u8{ "dumb", "cons25", "emacs" };
const is_windows = builtin.os.tag == .windows;
const termios = if (!is_windows) std.posix.termios else struct { inMode: w.DWORD, outMode: w.DWORD };
pub fn isUnsupportedTerm(env: *const std.process.Environ.Map) bool {
const env_var = env.get("TERM") orelse return false;
return for (unsupported_term) |t| {
if (std.ascii.eqlIgnoreCase(env_var, t))
break true;
} else false;
}
const w = struct {
const windows = std.os.windows;
pub const BOOL = windows.BOOL;
pub const CONSOLE_SCREEN_BUFFER_INFO = windows.CONSOLE_SCREEN_BUFFER_INFO;
pub const DWORD = windows.DWORD;
pub const HANDLE = windows.HANDLE;
pub const UINT = windows.UINT;
pub const WCHAR = windows.WCHAR;
pub const WORD = windows.WORD;
pub const ENABLE_VIRTUAL_TERMINAL_INPUT = @as(c_int, 0x200);
pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
pub const CP_UTF8 = @as(c_int, 65001);
pub const INPUT_RECORD = extern struct {
EventType: w.WORD,
_ignored: [16]u8,
};
};
const k32 = struct {
const kernel32 = std.os.windows.kernel32;
pub const GetConsoleMode = kernel32.GetConsoleMode;
pub const GetConsoleScreenBufferInfo = kernel32.GetConsoleScreenBufferInfo;
pub const SetConsoleMode = kernel32.SetConsoleMode;
pub const SetConsoleOutputCP = kernel32.SetConsoleOutputCP;
pub extern "kernel32" fn SetConsoleCP(wCodePageID: w.UINT) callconv(.winapi) w.BOOL;
pub extern "kernel32" fn PeekConsoleInputW(hConsoleInput: w.HANDLE, lpBuffer: [*]w.INPUT_RECORD, nLength: w.DWORD, lpNumberOfEventsRead: ?*w.DWORD) callconv(.winapi) w.BOOL;
pub extern "kernel32" fn ReadConsoleW(hConsoleInput: w.HANDLE, lpBuffer: [*]u16, nNumberOfCharsToRead: w.DWORD, lpNumberOfCharsRead: ?*w.DWORD, lpReserved: ?*anyopaque) callconv(.winapi) w.BOOL;
};
pub fn enableRawMode(in: File, out: File) !termios {
return enterRawMode(in, out, .FLUSH);
}
/// Raw mode that keeps bytes already typed but not yet read. A menu that takes
/// the keyboard over from the line editor needs this: the key the user pressed
/// just as the question arrived belongs to the menu, and flushing would swallow
/// it so that nothing happened until the key was pressed again.
pub fn enableRawModeKeepingInput(in: File, out: File) !termios {
return enterRawMode(in, out, .NOW);
}
fn enterRawMode(in: File, out: File, action: std.posix.TCSA) !termios {
if (is_windows) {
var result: termios = .{
.inMode = 0,
.outMode = 0,
};
var irec: [1]w.INPUT_RECORD = undefined;
var n: w.DWORD = 0;
if (k32.PeekConsoleInputW(in.handle, &irec, 1, &n) == 0 or
k32.GetConsoleMode(in.handle, &result.inMode) == 0 or
k32.GetConsoleMode(out.handle, &result.outMode) == 0)
return error.InitFailed;
_ = k32.SetConsoleMode(in.handle, w.ENABLE_VIRTUAL_TERMINAL_INPUT);
_ = k32.SetConsoleMode(out.handle, result.outMode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING);
_ = k32.SetConsoleCP(w.CP_UTF8);
_ = k32.SetConsoleOutputCP(w.CP_UTF8);
return result;
} else {
const orig = try std.posix.tcgetattr(in.handle);
var raw = orig;
raw.iflag.BRKINT = false;
raw.iflag.ICRNL = false;
raw.iflag.INPCK = false;
raw.iflag.ISTRIP = false;
raw.iflag.IXON = false;
raw.oflag.OPOST = false;
raw.cflag.CSIZE = .CS8;
raw.lflag.ECHO = false;
raw.lflag.ICANON = false;
raw.lflag.IEXTEN = false;
raw.lflag.ISIG = false;
// Do not inherit timed reads: a timeout returns zero bytes, which
// callers interpret as EOF. Escape lookahead sets its own timeout.
raw.cc[@backingInt(std.posix.V.MIN)] = 1;
raw.cc[@backingInt(std.posix.V.TIME)] = 0;
try std.posix.tcsetattr(in.handle, action, raw);
return orig;
}
}
pub fn disableRawMode(in: File, out: File, orig: termios) void {
if (is_windows) {
_ = k32.SetConsoleMode(in.handle, orig.inMode);
_ = k32.SetConsoleMode(out.handle, orig.outMode);
} else {
std.posix.tcsetattr(in.handle, std.posix.TCSA.FLUSH, orig) catch {};
}
}
/// Reads one `\n`-terminated line into a freshly allocated slice, growing as
/// needed. Returns `null` once the stream is exhausted. `max_len` bounds the
/// result the way the old `readUntilDelimiterAlloc` limit did.
///
/// Bytes are consumed one at a time on purpose. A buffered `File.Reader` cannot
/// be used here: it outlives the call that creates it in nobody's hands, so
/// anything it read past the newline would be silently dropped. Reading the fd
/// directly also keeps consecutive calls consistent, since the seek position
/// lives on the descriptor rather than in a per-call buffer.
pub fn readLineAlloc(io: Io, allocator: std.mem.Allocator, file: File, max_len: usize) !?[]u8 {
var line: std.ArrayList(u8) = .empty;
errdefer line.deinit(allocator);
var byte_buf: [1]u8 = undefined;
while (true) {
const n = try read(io, file, &byte_buf);
if (n == 0) return if (line.items.len == 0) null else try line.toOwnedSlice(allocator);
if (byte_buf[0] == '\n') return try line.toOwnedSlice(allocator);
if (line.items.len >= max_len) return error.StreamTooLong;
try line.append(allocator, byte_buf[0]);
}
}
fn getCursorPosition(io: Io, in: File, out: File) !usize {
var file_buffer: [256]u8 = undefined;
var file_reader = in.reader(io, &file_buffer);
// Tell terminal to report cursor to in
try out.writeStreamingAll(io, ansi.QUERY_CURSOR);
// Read answer
const answer = file_reader.interface.takeDelimiterExclusive('R') catch |err| switch (err) {
error.EndOfStream, error.StreamTooLong => return error.CursorPos,
else => return err,
};
// Parse answer
if (!std.mem.startsWith(u8, ansi.CSI, answer))
return error.CursorPos;
var iter = std.mem.splitScalar(u8, answer[2..], ';');
_ = iter.next() orelse return error.CursorPos;
const x = iter.next() orelse return error.CursorPos;
return try std.fmt.parseInt(usize, x, 10);
}
fn getColumnsFallback(io: Io, in: File, out: File) !usize {
var write_buf: [256]u8 = undefined;
var file_writer = out.writer(io, &write_buf);
const writer = &file_writer.interface;
const orig_cursor_pos = try getCursorPosition(io, in, out);
try writer.writeAll(ansi.RIGHT_MARGIN);
const cols = try getCursorPosition(io, in, out);
try writer.print(ansi.CURSOR_LEFT, .{orig_cursor_pos});
try writer.flush();
return cols;
}
pub fn getColumns(io: Io, in: File, out: File) !usize {
switch (builtin.os.tag) {
.windows => {
var csbi: w.CONSOLE_SCREEN_BUFFER_INFO = undefined;
_ = k32.GetConsoleScreenBufferInfo(out.handle, &csbi);
return @intCast(csbi.dwSize.X);
},
else => {
var winsize: std.posix.winsize = .{
.row = 0,
.col = 0,
.xpixel = 0,
.ypixel = 0,
};
const err = std.posix.system.ioctl(in.handle, std.posix.T.IOCGWINSZ, @intFromPtr(&winsize));
if (std.posix.errno(err) == .SUCCESS) {
return winsize.col;
} else {
return try getColumnsFallback(io, in, out);
}
},
}
}
pub fn getRows(file: File) usize {
if (is_windows) {
var info: w.CONSOLE_SCREEN_BUFFER_INFO = undefined;
if (k32.GetConsoleScreenBufferInfo(file.handle, &info) != 0)
return @intCast(info.srWindow.Bottom - info.srWindow.Top + 1);
} else {
var size: std.posix.winsize = .{ .row = 0, .col = 0, .xpixel = 0, .ypixel = 0 };
const rc = std.posix.system.ioctl(file.handle, std.posix.T.IOCGWINSZ, @intFromPtr(&size));
if (std.posix.errno(rc) == .SUCCESS and size.row > 0) return size.row;
}
return 24;
}
pub fn clearScreen(io: Io) !void {
try File.stderr().writeStreamingAll(io, ansi.HOME ++ ansi.CLEAR_SCREEN);
}
pub fn beep(io: Io) !void {
try File.stderr().writeStreamingAll(io, "\x07");
}
var utf8ConsoleBuffer: [10]u8 = @splat(0);
var utf8ConsoleReadBytes: usize = 0;
// this is needed due to a bug in win32 console: https://github.com/microsoft/terminal/issues/4551
fn readWin32Console(io: Io, file: File, buffer: []u8) !usize {
_ = io;
var toRead = buffer.len;
while (toRead > 0) {
if (utf8ConsoleReadBytes > 0) {
const existing = @min(toRead, utf8ConsoleReadBytes);
@memcpy(buffer[(buffer.len - toRead)..], utf8ConsoleBuffer[0..existing]);
utf8ConsoleReadBytes -= existing;
if (utf8ConsoleReadBytes > 0)
std.mem.copyForwards(u8, &utf8ConsoleBuffer, utf8ConsoleBuffer[existing..]);
toRead -= existing;
continue;
}
var charsRead: w.DWORD = 0;
var wideBuf: [2]w.WCHAR = undefined;
if (k32.ReadConsoleW(file.handle, &wideBuf, 1, &charsRead, null) == 0)
return 0;
if (charsRead == 0)
break;
const wideBufLen: u8 = if (wideBuf[0] >= 0xD800 and wideBuf[0] <= 0xDBFF) _: {
// read surrogate
if (k32.ReadConsoleW(file.handle, wideBuf[1..], 1, &charsRead, null) == 0)
return 0;
if (charsRead == 0)
break;
break :_ 2;
} else 1;
//WideCharToMultiByte(GetConsoleCP(), 0, buf, bufLen, converted, sizeof(converted), NULL, NULL);
utf8ConsoleReadBytes += try std.unicode.utf16LeToUtf8(&utf8ConsoleBuffer, wideBuf[0..wideBufLen]);
}
return buffer.len - toRead;
}
/// Like the old `File.read`, this reports end-of-stream as a 0 return so that
/// callers can keep testing `read(...) < 1`.
fn readPosix(io: Io, file: File, buffer: []u8) !usize {
return File.readStreaming(file, io, &[_][]u8{buffer}) catch |err| switch (err) {
error.EndOfStream => 0,
else => return err,
};
}
pub const read = if (is_windows) readWin32Console else readPosix;
/// A bare Escape dismisses a popup without waiting for another keystroke.
pub fn readEscape(io: Io, file: File, buffer: []u8) !usize {
if (is_windows) return read(io, file, buffer);
const original = try std.posix.tcgetattr(file.handle);
var timed = original;
timed.cc[@backingInt(std.posix.V.MIN)] = 0;
timed.cc[@backingInt(std.posix.V.TIME)] = 1;
try std.posix.tcsetattr(file.handle, .NOW, timed);
defer std.posix.tcsetattr(file.handle, .NOW, original) catch {};
return read(io, file, buffer);
}