diff options
Diffstat (limited to 'src/Editor.zig')
| -rw-r--r-- | src/Editor.zig | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/Editor.zig b/src/Editor.zig index 7e56783..aa57c15 100644 --- a/src/Editor.zig +++ b/src/Editor.zig | |||
| @@ -5,6 +5,7 @@ const Allocator = std.mem.Allocator; | |||
| 5 | const ArrayList = std.ArrayList; | 5 | const ArrayList = std.ArrayList; |
| 6 | const Buffer = es.Buffer; | 6 | const Buffer = es.Buffer; |
| 7 | const Editor = @This(); | 7 | const Editor = @This(); |
| 8 | const File = std.fs.File; | ||
| 8 | const Key = es.Key; | 9 | const Key = es.Key; |
| 9 | const KeyMap = es.KeyMap; | 10 | const KeyMap = es.KeyMap; |
| 10 | const KeyReader = es.KeyReader; | 11 | const KeyReader = es.KeyReader; |
| @@ -144,7 +145,8 @@ pub fn killCurrentBuffer(self: *Editor) !bool { | |||
| 144 | self.allocator.free(entry_to_kill.key); | 145 | self.allocator.free(entry_to_kill.key); |
| 145 | entry_to_kill.value.deinit(); | 146 | entry_to_kill.value.deinit(); |
| 146 | 147 | ||
| 147 | if (self.buffers.valueIterator().next()) |buffer| { | 148 | var it = self.buffers.valueIterator(); |
| 149 | if (it.next()) |buffer| { | ||
| 148 | self.buffer = buffer; | 150 | self.buffer = buffer; |
| 149 | } else { | 151 | } else { |
| 150 | self.buffer = try self.putBuffer("*scratch*"); | 152 | self.buffer = try self.putBuffer("*scratch*"); |
| @@ -245,11 +247,11 @@ pub fn promptEx( | |||
| 245 | try cb(self, buf.items, key, cb_data); | 247 | try cb(self, buf.items, key, cb_data); |
| 246 | } | 248 | } |
| 247 | 249 | ||
| 248 | return buf.toOwnedSlice(); | 250 | return try buf.toOwnedSlice(); |
| 249 | }, | 251 | }, |
| 250 | else => if (@enumToInt(key) < @enumToInt(Key.max_char)) { | 252 | else => if (@intFromEnum(key) < @intFromEnum(Key.max_char)) { |
| 251 | const key_char = @intCast(u8, @enumToInt(key)); | 253 | const key_char: u8 = @intCast(@intFromEnum(key)); |
| 252 | if (std.ascii.isSpace(key_char) or std.ascii.isGraph(key_char)) { | 254 | if (std.ascii.isWhitespace(key_char) or std.ascii.isPrint(key_char)) { |
| 253 | try buf.append(key_char); | 255 | try buf.append(key_char); |
| 254 | } | 256 | } |
| 255 | }, // TODO: else ?? | 257 | }, // TODO: else ?? |
| @@ -272,15 +274,7 @@ pub fn promptYN(self: *Editor, prompt_str: []const u8) !bool { | |||
| 272 | var response = try self.prompt(self.allocator, full_prompt); | 274 | var response = try self.prompt(self.allocator, full_prompt); |
| 273 | defer if (response) |str| self.allocator.free(str); | 275 | defer if (response) |str| self.allocator.free(str); |
| 274 | // TODO: This can be improved | 276 | // TODO: This can be improved |
| 275 | while (response != null | 277 | while (response != null and (response.?.len == 0 or (response.?[0] != 'y' and response.?[0] != 'Y' and response.?[0] != 'n' and response.?[0] != 'N'))) { |
| 276 | and (response.?.len == 0 | ||
| 277 | or (response.?[0] != 'y' | ||
| 278 | and response.?[0] != 'Y' | ||
| 279 | and response.?[0] != 'n' | ||
| 280 | and response.?[0] != 'N' | ||
| 281 | ) | ||
| 282 | ) | ||
| 283 | ) { | ||
| 284 | if (response) |str| self.allocator.free(str); | 278 | if (response) |str| self.allocator.free(str); |
| 285 | response = try self.prompt(self.allocator, full_prompt); | 279 | response = try self.prompt(self.allocator, full_prompt); |
| 286 | } | 280 | } |
| @@ -352,7 +346,7 @@ pub fn saveBuffersExit(self: *Editor) !void { | |||
| 352 | 346 | ||
| 353 | pub fn setStatusMessage(self: *Editor, comptime fmt: []const u8, args: anytype) !void { | 347 | pub fn setStatusMessage(self: *Editor, comptime fmt: []const u8, args: anytype) !void { |
| 354 | // Get new resources | 348 | // Get new resources |
| 355 | var new_msg = try std.fmt.allocPrint(self.allocator, fmt, args); | 349 | const new_msg = try std.fmt.allocPrint(self.allocator, fmt, args); |
| 356 | errdefer self.allocator.free(new_msg); | 350 | errdefer self.allocator.free(new_msg); |
| 357 | 351 | ||
| 358 | // Get rid of old resources (no errors) | 352 | // Get rid of old resources (no errors) |
| @@ -387,11 +381,17 @@ fn drawMessageBar(self: Editor, writer: anytype) !void { | |||
| 387 | } | 381 | } |
| 388 | 382 | ||
| 389 | if (self.statusmsg.?.len != 0 and std.time.milliTimestamp() - self.statusmsg_time < 5 * std.time.ms_per_s) { | 383 | if (self.statusmsg.?.len != 0 and std.time.milliTimestamp() - self.statusmsg_time < 5 * std.time.ms_per_s) { |
| 390 | try writer.writeAll(self.statusmsg.?[0..(std.math.min(self.statusmsg.?.len, self.screencols))]); | 384 | try writer.writeAll(self.statusmsg.?[0..(@min(self.statusmsg.?.len, self.screencols))]); |
| 391 | } | 385 | } |
| 392 | } | 386 | } |
| 393 | 387 | ||
| 394 | fn getCursorPosition(row: *usize, col: *usize) !void { | 388 | // TODO[zigbug]: https://github.com/ziglang/zig/issues/18177 |
| 389 | // Replace with an inferred set | ||
| 390 | const CursorPositionError = error{ | ||
| 391 | MisformedTerminalResponse, | ||
| 392 | } || File.WriteError; | ||
| 393 | |||
| 394 | fn getCursorPosition(row: *usize, col: *usize) CursorPositionError!void { | ||
| 395 | const std_out = std.io.getStdOut(); | 395 | const std_out = std.io.getStdOut(); |
| 396 | try std_out.writeAll("\x1b[6n\r\n"); | 396 | try std_out.writeAll("\x1b[6n\r\n"); |
| 397 | 397 | ||
| @@ -400,7 +400,7 @@ fn getCursorPosition(row: *usize, col: *usize) !void { | |||
| 400 | var response = std_in.readUntilDelimiter(&buf, 'R') catch |err| switch (err) { | 400 | var response = std_in.readUntilDelimiter(&buf, 'R') catch |err| switch (err) { |
| 401 | error.EndOfStream => return error.MisformedTerminalResponse, | 401 | error.EndOfStream => return error.MisformedTerminalResponse, |
| 402 | error.StreamTooLong => return error.MisformedTerminalResponse, | 402 | error.StreamTooLong => return error.MisformedTerminalResponse, |
| 403 | else => return @errSetCast(std.os.ReadError, err), | 403 | else => return @as(CursorPositionError, @errorCast(err)), |
| 404 | }; | 404 | }; |
| 405 | 405 | ||
| 406 | if (response.len < 2 or response[0] != '\x1b' or response[1] != '[') { | 406 | if (response.len < 2 or response[0] != '\x1b' or response[1] != '[') { |
| @@ -418,9 +418,9 @@ fn getCursorPosition(row: *usize, col: *usize) !void { | |||
| 418 | } | 418 | } |
| 419 | 419 | ||
| 420 | fn getWindowSize(rows: *usize, cols: *usize) !void { | 420 | fn getWindowSize(rows: *usize, cols: *usize) !void { |
| 421 | var ws: std.os.linux.winsize = undefined; | 421 | var ws: std.os.system.winsize = undefined; |
| 422 | const rc = std.os.linux.ioctl(std.os.STDIN_FILENO, std.os.linux.T.IOCGWINSZ, @ptrToInt(&ws)); | 422 | const rc = std.os.system.ioctl(std.os.STDIN_FILENO, std.os.system.T.IOCGWINSZ, @intFromPtr(&ws)); |
| 423 | switch (std.os.linux.getErrno(rc)) { | 423 | switch (std.os.system.getErrno(rc)) { |
| 424 | .SUCCESS => { | 424 | .SUCCESS => { |
| 425 | cols.* = ws.ws_col; | 425 | cols.* = ws.ws_col; |
| 426 | rows.* = ws.ws_row; | 426 | rows.* = ws.ws_row; |