From d78fa4f9d90437dac60d9150e3a6eca868895856 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:56:32 -0300 Subject: idioms #18, std.Io's names and fewer forms: the core's path functions are std.Io.Dir.path's and max_path_bytes std.Io.Dir's (std.fs's are deprecated aliases), pinOf takes the *const PageList it is always given, normal's @hasDecl guard goes (@field says the same), deleteSelection returns edit.Error, and a Look miss too long for its buffer is said cut where it said `no match` Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Core.zig | 10 +++++----- src/Effect.zig | 2 +- src/colors.zig | 2 +- src/edit.zig | 22 ++++++++-------------- src/look.zig | 24 +++++++++++------------- src/mini.zig | 4 ++-- src/normal.zig | 2 -- src/panes.zig | 4 ++-- src/screen_state.zig | 2 +- src/tagline.zig | 2 +- 10 files changed, 32 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/Core.zig b/src/Core.zig index c7d2fd48..413de233 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -551,7 +551,7 @@ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { }; const content = try std.fmt.allocPrint(gpa, "cannot open\n\n\t{s}\n\n{s}\n", .{ path, why }); errdefer gpa.free(content); - _ = try pardes.panes.Output.open(p, 0, std.fs.path.dirname(path) orelse "/", .errors, "", content); + _ = try pardes.panes.Output.open(p, 0, std.Io.Dir.path.dirname(path) orelse "/", .errors, "", content); } p.oneColumn(); }, @@ -574,7 +574,7 @@ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { const dir = p.sessionDir(); const shown = try pardes.panes.Output.open(p, 0, dir, .{ .cmd = .New }, "", content); // Named +Pager, as `pardes -` inside a session names its pane. - const named = try std.fs.path.join(gpa, &.{ dir, "+Pager" }); + const named = try std.Io.Dir.path.join(gpa, &.{ dir, "+Pager" }); gpa.free(shown.file.?.path); shown.file.?.path = named; shown.file.?.pager = true; @@ -981,7 +981,7 @@ pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_ma if (pane.command == null) { // A shell given as a path keeps it: its bare name would find // nothing, or another program, on the usual paths. - const base = if (pane.shell) |given| (if (std.mem.indexOfScalar(u8, given, '/') != null) given else std.fs.path.basename(executable)) else std.fs.path.basename(executable); + const base = if (pane.shell) |given| (if (std.mem.indexOfScalar(u8, given, '/') != null) given else std.Io.Dir.path.basename(executable)) else std.Io.Dir.path.basename(executable); const rest = tagline.terminal_pane_tail[" Term".len..]; if (std.fmt.bufPrint(&pane.shell_tail, " Term+{s}{s}", .{ base, rest })) |tail| { pane.shell_tail_len = @intCast(tail.len); @@ -1311,7 +1311,7 @@ fn sayShellFailure(p: *Pardes, id: u8, err: anyerror) void { /// The program the shell's `#!` names that is not there, in `out`. fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 { const Shell = @import("host_io.zig").Shell; - var buf: [std.fs.max_path_bytes]u8 = undefined; + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; return Shell.missingInterpreter(Shell.find(bin, &buf) orelse return null, out); } @@ -1319,7 +1319,7 @@ fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 { /// what is there (a directory when it is no local path), whether it is /// missing, and whether that is because one above refuses (EACCES). fn parentTrouble(path: []const u8, err: anyerror) struct { dir: []const u8, kind: ?std.Io.File.Kind, missing: bool, denied: bool } { - const dir = std.fs.path.dirname(path) orelse "/"; + const dir = std.Io.Dir.path.dirname(path) orelse "/"; const local = filesystem.localPath(dir); const kind: ?std.Io.File.Kind = if (local) |l| exec.kindOf(l) else .directory; const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; diff --git a/src/Effect.zig b/src/Effect.zig index 4dfa72e1..a97a280c 100644 --- a/src/Effect.zig +++ b/src/Effect.zig @@ -257,7 +257,7 @@ pub fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: [] // A Save makes the directories its file goes in, and any above them, // first: one rule, for the pane's own name and `Save path` alike. A // directory it cannot make leaves the write to fail and say why. - if (filesystem.localPath(path)) |local| filesystem.makeDirs(std.fs.path.dirname(local) orelse "/"); + if (filesystem.localPath(path)) |local| filesystem.makeDirs(std.Io.Dir.path.dirname(local) orelse "/"); // The in-process filesystem reports the same way a real host does, // so an OOM there leaves the pane dirty rather than looking saved. if (p.host.vtable.write_file) |f| f(p.host.ctx, id, path, bytes) else if (!p.fallback.writeFile(path, bytes)) p.saveFailed(id, path, error.OutOfMemory); diff --git a/src/colors.zig b/src/colors.zig index bf68c256..65fd4056 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -613,7 +613,7 @@ pub fn requestThemeFile(p: *Pardes, id: usize, argument: []const u8) void { return; } - const absolute = std.fs.path.isAbsolute(input); + const absolute = std.Io.Dir.path.isAbsolute(input); const config_dir = if (absolute) null else p.configDir(); if (!absolute and config_dir == null) { p.reportError(id, "theme file", error.NoConfigDirectory); diff --git a/src/edit.zig b/src/edit.zig index a7b9bb7a..d4795c94 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -28,8 +28,7 @@ const sel_slot = pardes.sel_slot; const Effect = pardes.Effect; const Pardes = pardes.Pardes; const Registers = pardes.Registers; -/// What an edit fails with, memory or a terminal's rows not read -/// (terminal.shellRows); the key that ran it says so (`edit: out of memory`). +/// An edit's failure, memory or a terminal's rows not read, said by its key. pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint }; /// Write `text` as this range's value of the register the running command @@ -1092,7 +1091,7 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void { t.vsel.active = false; return; }; - const new = deleteSelection(p, t, eb) orelse return; + const new = try deleteSelection(p, t, eb); setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; @@ -1203,7 +1202,7 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { } setYank(p, try vselText(p, t)); pushUndo(p, t); - const new = deleteSelection(p, t, eb) orelse return; + const new = try deleteSelection(p, t, eb); setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; @@ -1224,14 +1223,10 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.normal.clear(); } -/// `eb` without the primary range, or null (the selection dropped) when -/// there is no memory for it. -fn deleteSelection(p: *Pardes, t: *Text, eb: panes.EditText) ?[]u8 { +/// `eb` without the primary range. +fn deleteSelection(p: *Pardes, t: *Text, eb: panes.EditText) Error![]u8 { const r = t.primaryRange(eb.text, eb.row0); - return modal.spliceAlloc(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), "") catch { - t.vsel.active = false; - return null; - }; + return modal.spliceAlloc(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), ""); } /// the rows an op's selection spans, absolute (what editText must cover) @@ -1279,8 +1274,7 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { var at = lo; while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at); var encoded: [4]u8 = undefined; - if (!std.unicode.utf8ValidCodepoint(ch)) return; // a surrogate half types nothing - const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch unreachable; // a valid code point, checked above + const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; // a surrogate half types nothing const rep = try p.scratch.allocator().alloc(u8, graphemes * encoded_len); for (0..graphemes) |i| @memcpy(rep[i * encoded_len ..][0..encoded_len], encoded[0..encoded_len]); pushUndo(p, t); @@ -1609,7 +1603,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { next = @max(next, end); } const file = if (t.what != .body) null else if (t.pane()) |pane| pane.file else null; - const ext = if (file) |f| std.fs.path.extension(f.path) else ""; + const ext = if (file) |f| std.Io.Dir.path.extension(f.path) else ""; var token: []const u8 = config.comment_token_default; lang: for (config.comment_tokens) |row| { for (row.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) { diff --git a/src/look.zig b/src/look.zig index 22aa4882..8f5ed900 100644 --- a/src/look.zig +++ b/src/look.zig @@ -293,7 +293,7 @@ test "lookableLineSpan prefers the longest run, so a blank inside a path is one try tmp.dir.createDir(io, "subdir", .default_dir); var path: [4096]u8 = undefined; const len = try tmp.dir.realPathFile(io, name, &path); - const cwd = std.fs.path.dirname(path[0..len]).?; + const cwd = std.Io.Dir.path.dirname(path[0..len]).?; try std.testing.expectEqualDeep( @as(?Span, .{ .start = 0, .end = name.len }), lineSpan(null, name, cwd, &realbuf), @@ -633,7 +633,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, start: SearchStart, // relative to `dir`, like Find's rows — and `dir` IS this file's // own directory, so that is exactly its basename const loc: []const u8 = if (has_path) - std.fs.path.basename(pane.file.?.path) + std.Io.Dir.path.basename(pane.file.?.path) else std.fmt.bufPrint(&idbuf, config.pane_addr ++ "{d}", .{pane.serial}) catch unreachable; // `@p` and a u32: 12 bytes const cl: usize = @intCast(@max(0, pane.body.cur_row)); @@ -1217,9 +1217,7 @@ test "a zero column after a path is found, in every spelling a look takes" { fn missed(p: *Pardes, id: usize, comptime fmt: []const u8, args: anytype) void { p.look_missed = true; var said: [miss_path_cap + 64]u8 = undefined; - var w = std.Io.Writer.fixed(&said); - w.print(fmt, args) catch {}; // a longer miss is said cut at the buffer's end - const why = w.buffered(); + const why = std.fmt.bufPrint(&said, fmt, args) catch &said; // a longer miss is cut at the buffer's end var msg: [said.len + 8]u8 = undefined; // Shown on the pane; logged once, as the err below, not a msg too. p.fs.unlogged = true; @@ -1342,7 +1340,7 @@ fn lookCommit(p: *Pardes, id: usize, pane: *Pane, word: []const u8) bool { var buf: [4096]u8 = undefined; const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false; if (exec.kindOf(dot_git) != null) break; - dir = std.fs.path.dirname(dir) orelse return false; + dir = std.Io.Dir.path.dirname(dir) orelse return false; } var line: [64]u8 = undefined; const dst = exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{word}) catch return false) orelse return true; @@ -1417,7 +1415,7 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { var cut: [miss_path_cap + "…".len]u8 = undefined; return missed(p, id, "{s} is no text to address", .{missText(subject, &cut)}); }; - if (!by_serial and path.len > 0 and (f.output != null or !std.mem.endsWith(u8, f.path, std.fs.path.basename(path)))) return; + if (!by_serial and path.len > 0 and (f.output != null or !std.mem.endsWith(u8, f.path, std.Io.Dir.path.basename(path)))) return; var a: Addr = .{ .text = f.content, .lim = null, .expr = expr }; // A miss names the file it was looked for in, and is that file's pane's. var cut: [miss_path_cap + "…".len]u8 = undefined; @@ -2049,18 +2047,18 @@ const DiffFixture = struct { errdefer f.tmp.cleanup(); if (repo) try f.tmp.dir.createDir(io, ".git", .default_dir); for (files) |file| { - if (std.fs.path.dirname(file[0])) |sub| try f.tmp.dir.createDirPath(io, sub); + if (std.Io.Dir.path.dirname(file[0])) |sub| try f.tmp.dir.createDirPath(io, sub); try f.tmp.dir.writeFile(io, .{ .sub_path = file[0], .data = file[1] }); } try f.tmp.dir.writeFile(io, .{ .sub_path = "change.diff", .data = diff_text }); const len = try f.tmp.dir.realPathFile(io, "change.diff", &f.dir_buf); - f.dir = std.fs.path.dirname(f.dir_buf[0..len]).?; + f.dir = std.Io.Dir.path.dirname(f.dir_buf[0..len]).?; } fn open(f: *DiffFixture, p: *Pardes, diff_text: []const u8) !*Pane { const pane = try p.setTestFile(diff_text); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ f.dir, "change.diff" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ f.dir, "change.diff" }); return pane; } @@ -2348,7 +2346,7 @@ test "every entry point to Look takes one expansion: a diff line and a file:12:5 defer p.deinit(); const pane = try fx.open(p, case.text); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ fx.dir, case.name }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ fx.dir, case.name }); pane.tag_expanded = false; var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); @@ -2495,7 +2493,7 @@ test "Incl's directories find a name found nowhere else, too; bare Incl l try std.testing.expectEqualStrings("/usr/include", p.incl.items[0]); const pane = try p.setTestFile("#include \n"); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "src", "main.c" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "src", "main.c" }); var line: [4200]u8 = undefined; try std.testing.expect(p.executeBuiltinLine(0, try std.fmt.bufPrint(&line, "Incl {s}/inc", .{root}))); for ([_][]const u8{ "pardes_incl_probe.h", "" }) |word| { @@ -2531,7 +2529,7 @@ test "LookNext in a command pane walks its own file:line places, whoever walked defer p.deinit(); const doc = try p.setTestFile("x\n"); p.gpa.free(doc.file.?.path); - doc.file.?.path = try std.fs.path.join(p.gpa, &.{ dir, "main.c" }); + doc.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ dir, "main.c" }); while (p.nextEffect()) |_| {} const dst = exec.execute(p, 0, "make") orelse return error.NoCommandPane; while (p.nextEffect()) |_| {} diff --git a/src/mini.zig b/src/mini.zig index 91870674..14b7d973 100644 --- a/src/mini.zig +++ b/src/mini.zig @@ -171,8 +171,8 @@ pub fn open(p: *Pardes, id: usize, argument: []const u8) !void { return; } const free = p.freeSlot() orelse return error.NoPaneSlots; - const dir = std.fs.path.dirname(source) orelse "/"; - const path = try std.fmt.allocPrint(p.gpa, "{s}/Mini {s}", .{ std.mem.trimEnd(u8, dir, "/"), std.fs.path.basename(source) }); + const dir = std.Io.Dir.path.dirname(source) orelse "/"; + const path = try std.fmt.allocPrint(p.gpa, "{s}/Mini {s}", .{ std.mem.trimEnd(u8, dir, "/"), std.Io.Dir.path.basename(source) }); errdefer p.gpa.free(path); const history = try File.History.create(p.gpa); errdefer p.gpa.destroy(history); diff --git a/src/normal.zig b/src/normal.zig index bd62482e..4d3cc96b 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -33,8 +33,6 @@ fn normalInput(key: Key) modal.Normal.Input { }; inline for (std.enums.values(modal.Normal.Role)) |role| { const name = @tagName(role); - if (!@hasDecl(config, name)) - @compileError("normal input role has no config binding: " ++ name); const binding = @field(config, name); const matched = if (@TypeOf(binding) == comptime_int) isPrefix(key, binding) diff --git a/src/panes.zig b/src/panes.zig index ffaaba37..b9504b1c 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -925,7 +925,7 @@ pub fn openTutorView(p: *Pardes, id: usize) !*Pane { errdefer p.gpa.free(content); // A generated pane, named as every one is: `+` in the session's // directory, never a bare path Save could write at the root. - const path = try std.fs.path.join(p.gpa, &.{ p.sessionDir(), "+Tutor" }); + const path = try std.Io.Dir.path.join(p.gpa, &.{ p.sessionDir(), "+Tutor" }); errdefer p.gpa.free(path); const history = try panes.File.History.create(p.gpa); errdefer p.gpa.destroy(history); @@ -962,7 +962,7 @@ pub fn paneDir(pane: *const Pane) []const u8 { .none => {}, } if (pane.file) |f| return panes.Dir.ofName(f.path); - if (pane.addon) |k| if (k.path.len > 0) return std.fs.path.dirname(k.path) orelse "/"; + if (pane.addon) |k| if (k.path.len > 0) return std.Io.Dir.path.dirname(k.path) orelse "/"; return pane.cwdSlice(); } diff --git a/src/screen_state.zig b/src/screen_state.zig index bcf4b137..6bef70a6 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -119,7 +119,7 @@ pub const Rows = struct { } }; -fn pinOf(pages: anytype, top: u64, abs: u64) ?Pin { +fn pinOf(pages: *const vt.PageList, top: u64, abs: u64) ?Pin { if (abs >= top) return pages.pin(.{ .active = .{ .y = std.math.cast(u32, abs - top) orelse return null } }); const d = std.math.cast(usize, top - abs) orelse return null; return pages.getTopLeft(.active).up(d); diff --git a/src/tagline.zig b/src/tagline.zig index a364732a..e847921e 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -387,7 +387,7 @@ pub fn commitTagName(p: *Pardes, pane: *Pane) bool { return false; }; var home_buf: [4096]u8 = undefined; - const full = std.fs.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(pane), pardes.filesystem.expandHome(name, &home_buf) }) catch |err| { + const full = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(pane), pardes.filesystem.expandHome(name, &home_buf) }) catch |err| { p.reportError(id, "name", err); return false; }; -- cgit v1.3