From 4c194047d210ad9cf571c6019731d00d1e3615a4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:14:58 -0300 Subject: idioms #4, string chains are an enum and a switch: main's flags (each exact one a Flag, the prefixed ones as they were), --ps's rows and manage's verb, the daemon's verbs on both ends and its registry's rows, an upgrade record's words, the window driver's mouse kinds and finger names, a frame script's click target and a mount name's reserved words go through std.meta.stringToEnum; with them main asks std.Io for its working directory, its chdir, a directory's access and /proc's comm, not libc Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/gui/gui.zig | 70 +++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 37 deletions(-) (limited to 'src/gui') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 3bb304f0..c149d913 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -856,7 +856,6 @@ fn stopPipe() ![2]c_int { return fds; } - fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { while (true) { var fds = [_]libc.pollfd{ @@ -873,8 +872,6 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } } - - test "GUI worker setup failures finish matching LSP and pipe requests" { const gpa = std.testing.allocator; var failing_vtable = std.testing.io.vtable.*; @@ -2235,29 +2232,33 @@ const StdinFeed = struct { // counts them: shift 4, meta (alt) 8, ctrl 16. const held = std.fmt.parseInt(u8, parts.next() orelse "0", 10) catch 0; var sev = std.mem.zeroes(c.SDL_Event); - if (std.mem.eql(u8, kind, "wheel")) { - // ESC]777;mouse;wheel;;;BEL: - // a whole number is a mouse's notches, else a touchpad's. - const amount = std.fmt.parseFloat(f32, second) catch 0; - sev.type = c.SDL_EVENT_MOUSE_WHEEL; - sev.wheel.y = amount; - sev.wheel.integer_y = @intFromFloat(@trunc(amount)); - sev.wheel.mouse_x = x; - sev.wheel.mouse_y = y; - } else if (std.mem.eql(u8, kind, "focus")) { + const k = std.meta.stringToEnum(enum { wheel, focus, motion, down, up }, kind) orelse .up; + switch (k) { + .wheel => { + // ESC]777;mouse;wheel;;;BEL: + // a whole number is a mouse's notches, else a touchpad's. + const amount = std.fmt.parseFloat(f32, second) catch 0; + sev.type = c.SDL_EVENT_MOUSE_WHEEL; + sev.wheel.y = amount; + sev.wheel.integer_y = @intFromFloat(@trunc(amount)); + sev.wheel.mouse_x = x; + sev.wheel.mouse_y = y; + }, // ESC]777;mouse;focus;<1 gained, 0 lost>;0;0BEL - sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST; - } else if (std.mem.eql(u8, kind, "motion")) { - sev.type = c.SDL_EVENT_MOUSE_MOTION; - sev.motion.x = x; - sev.motion.y = y; - sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); - } else { - sev.type = if (std.mem.eql(u8, kind, "down")) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; - sev.button.button = button; - sev.button.down = std.mem.eql(u8, kind, "down"); - sev.button.x = x; - sev.button.y = y; + .focus => sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST, + .motion => { + sev.type = c.SDL_EVENT_MOUSE_MOTION; + sev.motion.x = x; + sev.motion.y = y; + sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); + }, + .down, .up => { + sev.type = if (k == .down) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; + sev.button.button = button; + sev.button.down = k == .down; + sev.button.x = x; + sev.button.y = y; + }, } const before = c.SDL_GetModState(); var mods: c.SDL_Keymod = 0; @@ -2367,16 +2368,13 @@ fn parseFinger(payload: []const u8) ?Finger { const pressure_s = parts.next() orelse return null; if (parts.next() != null) return null; _ = std.fmt.parseFloat(f32, pressure_s) catch return null; - const kind: @FieldType(Finger, "kind") = if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_DOWN") or std.mem.eql(u8, type_name, "down")) - .down - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_MOTION") or std.mem.eql(u8, type_name, "motion")) - .motion - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_UP") or std.mem.eql(u8, type_name, "up")) - .up - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_CANCELED") or std.mem.eql(u8, type_name, "cancel")) - .cancel - else - return null; + const Name = enum { SDL_EVENT_FINGER_DOWN, down, SDL_EVENT_FINGER_MOTION, motion, SDL_EVENT_FINGER_UP, up, SDL_EVENT_FINGER_CANCELED, cancel }; + const kind: @FieldType(Finger, "kind") = switch (std.meta.stringToEnum(Name, type_name) orelse return null) { + .SDL_EVENT_FINGER_DOWN, .down => .down, + .SDL_EVENT_FINGER_MOTION, .motion => .motion, + .SDL_EVENT_FINGER_UP, .up => .up, + .SDL_EVENT_FINGER_CANCELED, .cancel => .cancel, + }; return .{ .kind = kind, .id = std.fmt.parseInt(u64, id_s, 10) catch return null, @@ -3452,7 +3450,6 @@ const Shell = struct { } }; - fn wakeFs(ctx: ?*anyopaque) void { const q: *Queue = @ptrCast(@alignCast(ctx.?)); q.push(.fs_ready); @@ -8176,7 +8173,6 @@ test "a caret eases a cell at a time, keeps up with typing, and glides a jump, l try std.testing.expect(!caret.moving()); } - /// The rows a caret whose line rode the wheel's glide is drawn above its /// cell: the text's own offset, so it moves with its line as a glyph does /// (its springs landed on the cell, Caret.see). -- cgit v1.3