From d5d5f7f06397d73f497d352f2f38b1a53d932b0d Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Mon, 19 Feb 2024 23:38:33 +0200 Subject: Big update to modern zig --- src/Editor.zig | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'src/Editor.zig') 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; const ArrayList = std.ArrayList; const Buffer = es.Buffer; const Editor = @This(); +const File = std.fs.File; const Key = es.Key; const KeyMap = es.KeyMap; const KeyReader = es.KeyReader; @@ -144,7 +145,8 @@ pub fn killCurrentBuffer(self: *Editor) !bool { self.allocator.free(entry_to_kill.key); entry_to_kill.value.deinit(); - if (self.buffers.valueIterator().next()) |buffer| { + var it = self.buffers.valueIterator(); + if (it.next()) |buffer| { self.buffer = buffer; } else { self.buffer = try self.putBuffer("*scratch*"); @@ -245,11 +247,11 @@ pub fn promptEx( try cb(self, buf.items, key, cb_data); } - return buf.toOwnedSlice(); + return try buf.toOwnedSlice(); }, - else => if (@enumToInt(key) < @enumToInt(Key.max_char)) { - const key_char = @intCast(u8, @enumToInt(key)); - if (std.ascii.isSpace(key_char) or std.ascii.isGraph(key_char)) { + else => if (@intFromEnum(key) < @intFromEnum(Key.max_char)) { + const key_char: u8 = @intCast(@intFromEnum(key)); + if (std.ascii.isWhitespace(key_char) or std.ascii.isPrint(key_char)) { try buf.append(key_char); } }, // TODO: else ?? @@ -272,15 +274,7 @@ pub fn promptYN(self: *Editor, prompt_str: []const u8) !bool { var response = try self.prompt(self.allocator, full_prompt); defer if (response) |str| self.allocator.free(str); // TODO: This can be improved - while (response != null - and (response.?.len == 0 - or (response.?[0] != 'y' - and response.?[0] != 'Y' - and response.?[0] != 'n' - and response.?[0] != 'N' - ) - ) - ) { + while (response != null and (response.?.len == 0 or (response.?[0] != 'y' and response.?[0] != 'Y' and response.?[0] != 'n' and response.?[0] != 'N'))) { if (response) |str| self.allocator.free(str); response = try self.prompt(self.allocator, full_prompt); } @@ -352,7 +346,7 @@ pub fn saveBuffersExit(self: *Editor) !void { pub fn setStatusMessage(self: *Editor, comptime fmt: []const u8, args: anytype) !void { // Get new resources - var new_msg = try std.fmt.allocPrint(self.allocator, fmt, args); + const new_msg = try std.fmt.allocPrint(self.allocator, fmt, args); errdefer self.allocator.free(new_msg); // Get rid of old resources (no errors) @@ -387,11 +381,17 @@ fn drawMessageBar(self: Editor, writer: anytype) !void { } if (self.statusmsg.?.len != 0 and std.time.milliTimestamp() - self.statusmsg_time < 5 * std.time.ms_per_s) { - try writer.writeAll(self.statusmsg.?[0..(std.math.min(self.statusmsg.?.len, self.screencols))]); + try writer.writeAll(self.statusmsg.?[0..(@min(self.statusmsg.?.len, self.screencols))]); } } -fn getCursorPosition(row: *usize, col: *usize) !void { +// TODO[zigbug]: https://github.com/ziglang/zig/issues/18177 +// Replace with an inferred set +const CursorPositionError = error{ + MisformedTerminalResponse, +} || File.WriteError; + +fn getCursorPosition(row: *usize, col: *usize) CursorPositionError!void { const std_out = std.io.getStdOut(); try std_out.writeAll("\x1b[6n\r\n"); @@ -400,7 +400,7 @@ fn getCursorPosition(row: *usize, col: *usize) !void { var response = std_in.readUntilDelimiter(&buf, 'R') catch |err| switch (err) { error.EndOfStream => return error.MisformedTerminalResponse, error.StreamTooLong => return error.MisformedTerminalResponse, - else => return @errSetCast(std.os.ReadError, err), + else => return @as(CursorPositionError, @errorCast(err)), }; if (response.len < 2 or response[0] != '\x1b' or response[1] != '[') { @@ -418,9 +418,9 @@ fn getCursorPosition(row: *usize, col: *usize) !void { } fn getWindowSize(rows: *usize, cols: *usize) !void { - var ws: std.os.linux.winsize = undefined; - const rc = std.os.linux.ioctl(std.os.STDIN_FILENO, std.os.linux.T.IOCGWINSZ, @ptrToInt(&ws)); - switch (std.os.linux.getErrno(rc)) { + var ws: std.os.system.winsize = undefined; + const rc = std.os.system.ioctl(std.os.STDIN_FILENO, std.os.system.T.IOCGWINSZ, @intFromPtr(&ws)); + switch (std.os.system.getErrno(rc)) { .SUCCESS => { cols.* = ws.ws_col; rows.* = ws.ws_row; -- cgit v1.2.3