search.zig

17.8 KB · raw

1// search.zig — inline 18-line TUI widget, no alternate screen2// Raw terminal mode for interactive key handling. Linux-only (NixOS target).3const std = @import("std");4const builtin = @import("builtin");5const types = @import("types.zig");6const output = @import("output.zig");7const api = @import("api.zig");89const WIDGET_LINES: usize = 18;10const RESULT_ROWS: usize = 5;11const STDIN_FD = std.posix.STDIN_FILENO;1213// Debounce window: a keystroke defers the nix search until typing pauses this14// long. nix search is synchronous and blocks the widget for ~1s, so the window15// must be long enough that a normal between-keystroke pause doesn't trigger it —16// it should only fire once you've actually stopped typing.17const DEBOUNCE_MS: i64 = 500;1819// Milliseconds elapsed since `since` on the monotonic (awake) clock. std has no20// milliTimestamp in 0.16; this mirrors the Io.Clock pattern used in apply().21fn elapsedMs(io: std.Io, since: std.Io.Timestamp) i64 {22    const ns = @max(0, since.untilNow(io, .awake).toNanoseconds());23    return @intCast(@divFloor(ns, 1_000_000));24}2526// What the detail pane shows for the selected package. Tab toggles between the27// collapsed (description) view and the expanded meta view; the latter is fetched28// on demand, so it passes through loading and (on eval failure) unavailable.29const Detail = union(enum) {30    collapsed,31    loading,32    unavailable,33    meta: api.PkgMeta,34};3536pub fn run(37    gpa: std.mem.Allocator,38    io: std.Io,39    initial_query: []const u8,40    mode: Mode,41    environ: *const std.process.Environ.Map,42) !?types.NixPackage {43    // Non-interactive fallback: when stdin isn't a terminal (piped, scripted,44    // CI), print plain results and return instead of the raw-mode widget, which45    // needs a real tty and would block waiting for keys.46    if (!stdinIsTty(io)) return runPlain(gpa, io, initial_query, mode, environ);4748    var saved: std.posix.termios = undefined;49    try rawModeEnable(STDIN_FD, &saved);50    defer rawModeDisable(STDIN_FD, saved);5152    var arena = std.heap.ArenaAllocator.init(gpa);53    defer arena.deinit();5455    var query_buf: [256]u8 = undefined;56    var query_len: usize = @min(initial_query.len, query_buf.len - 1);57    @memcpy(query_buf[0..query_len], initial_query[0..query_len]);5859    var packages: []types.NixPackage = &.{};60    var opts: []api.NixOption = &.{};61    var selection: usize = 0;6263    // Expanded detail (Tab): the selected package's meta, fetched on demand into64    // its own arena and cached by attr so re-visiting is instant. Kept separate65    // from the search arena, which is reset on every new search.66    var expanded = false;67    var meta_arena = std.heap.ArenaAllocator.init(gpa);68    defer meta_arena.deinit();69    var meta: ?api.PkgMeta = null;70    var meta_attr: []const u8 = "";7172    // Initial search73    _ = arena.reset(.free_all);74    packages = doSearch(arena.allocator(), io, query_buf[0..query_len], mode, &opts, environ);7576    // Initial render (prints WIDGET_LINES lines)77    renderWidget(query_buf[0..query_len], packages, opts, selection, mode, false, .collapsed);78    output.flush();7980    // Key loop with debounced search. A keystroke marks the query dirty and81    // stamps the time; the nix search fires only once typing pauses for82    // DEBOUNCE_MS. Stale results stay on screen until the new search returns, so83    // there is no blank flash mid-typing.84    var chosen: ?types.NixPackage = null;85    var query_dirty = false;86    var query_changed_at = std.Io.Clock.awake.now(io);87    loop: while (true) {88        // Block until a key arrives, unless a search is pending — then wake when89        // the debounce window closes so we can fire it.90        var timeout_ms: i32 = -1;91        if (query_dirty) {92            const remaining = DEBOUNCE_MS - elapsedMs(io, query_changed_at);93            timeout_ms = if (remaining <= 0) 0 else @intCast(remaining);94        }9596        var pfd = [_]std.posix.pollfd{.{ .fd = STDIN_FD, .events = std.posix.POLL.IN, .revents = 0 }};97        const ready = std.posix.poll(&pfd, timeout_ms) catch 0;9899        if (ready == 0) {100            // Poll timed out. If no search is pending, nothing changed — keep waiting.101            if (!(query_dirty and elapsedMs(io, query_changed_at) >= DEBOUNCE_MS)) continue;102            query_dirty = false;103            // Paint "searching..." over the stale results before the blocking nix104            // search, so the ~1s pause reads as progress, not a freeze.105            output.cursorUp(WIDGET_LINES);106            renderWidget(query_buf[0..query_len], packages, opts, selection, mode, true, .collapsed);107            output.flush();108109            _ = arena.reset(.free_all);110            packages = doSearch(arena.allocator(), io, query_buf[0..query_len], mode, &opts, environ);111            // New result set: reset to the top and drop cached meta (it belonged to112            // the previous results). Navigating during the window could otherwise113            // leave selection past the new end and renderWidget would index OOB.114            selection = 0;115            meta = null;116            // fall through to the common render below117        } else {118            var buf: [4]u8 = undefined;119            const n = std.posix.read(STDIN_FD, &buf) catch break;120            if (n == 0) break;121122            // The query is a live text field, so every printable key types into it —123            // single-letter actions would collide with package names (the 'i' in124            // "ripgrep" must not trigger install). Only non-text keys act: Enter125            // selects, Tab expands detail, arrows navigate, Esc cancels.126            if (n >= 3 and buf[0] == 0x1b and buf[1] == '[') {127                switch (buf[2]) {128                    'A' => if (selection > 0) { selection -= 1; }, // up129                    'B' => if (selection + 1 < resultCount(packages, opts, mode)) { selection += 1; }, // down130                    else => {},131                }132            } else switch (buf[0]) {133                0x1b => break :loop, // Esc: cancel (chosen stays null)134                '\t' => expanded = !expanded, // Tab: toggle package detail135                '\r', '\n' => { // Enter: select the highlighted package136                    if (mode == .packages and packages.len > selection) {137                        chosen = packages[selection];138                    }139                    break :loop;140                },141                127, 8 => { // backspace142                    if (query_len > 0) {143                        query_len -= 1;144                        query_dirty = true;145                        query_changed_at = std.Io.Clock.awake.now(io);146                        selection = 0;147                    }148                },149                else => |ch| {150                    if (ch >= 32 and ch < 127 and query_len < query_buf.len - 1) {151                        query_buf[query_len] = ch;152                        query_len += 1;153                        query_dirty = true;154                        query_changed_at = std.Io.Clock.awake.now(io);155                        selection = 0;156                    }157                },158            }159        }160161        // Common render after a search or a key. When expanded, resolve the detail162        // pane by fetching the selected package's meta on demand (blocking eval),163        // painting a "loading" frame first and caching by attr so re-visiting is164        // instant.165        const count = resultCount(packages, opts, mode);166        const sel = if (count == 0) 0 else @min(selection, count - 1);167        var detail: Detail = .collapsed;168        if (expanded and mode == .packages and count > 0) {169            const cur = packages[sel].attr;170            if (meta == null or !std.mem.eql(u8, meta_attr, cur)) {171                output.cursorUp(WIDGET_LINES);172                renderWidget(query_buf[0..query_len], packages, opts, selection, mode, false, .loading);173                output.flush();174                _ = meta_arena.reset(.free_all);175                meta_attr = meta_arena.allocator().dupe(u8, cur) catch "";176                meta = api.packageMeta(meta_arena.allocator(), io, cur);177            }178            detail = if (meta) |mm| Detail{ .meta = mm } else Detail.unavailable;179        }180        output.cursorUp(WIDGET_LINES);181        renderWidget(query_buf[0..query_len], packages, opts, selection, mode, false, detail);182        output.flush();183    }184185    // Clear the widget area186    output.cursorUp(WIDGET_LINES);187    var i: usize = 0;188    while (i < WIDGET_LINES) : (i += 1) {189        output.raw(output.C_CLEAR_EOL ++ "\n");190    }191    output.cursorUp(WIDGET_LINES);192    output.flush();193194    // The chosen package's strings live in the search arena, which deinits as this195    // function returns — dupe into the caller's allocator so it survives.196    if (chosen) |pkg| return dupePackage(gpa, pkg);197    return null;198}199200fn dupePackage(gpa: std.mem.Allocator, pkg: types.NixPackage) types.NixPackage {201    return .{202        .attr = gpa.dupe(u8, pkg.attr) catch "",203        .pname = gpa.dupe(u8, pkg.pname) catch "",204        .version = gpa.dupe(u8, pkg.version) catch "",205        .description = gpa.dupe(u8, pkg.description) catch "",206    };207}208209pub const Mode = enum { packages, options };210211fn doSearch(arena: std.mem.Allocator, io: std.Io, query: []const u8, mode: Mode, opts: *[]api.NixOption, environ: *const std.process.Environ.Map) []types.NixPackage {212    // An empty query matches the entire catalog (100k+ rows) — a multi-second213    // fetch that would freeze the widget. Show nothing until there's a query.214    if (std.mem.trim(u8, query, " \t").len == 0) {215        opts.* = &.{};216        return &.{};217    }218    switch (mode) {219        .packages => return api.searchPackages(arena, io, environ, query) catch &.{},220        .options => {221            opts.* = api.searchOptions(arena, io, query) catch &.{};222            return &.{};223        },224    }225}226227fn resultCount(packages: []types.NixPackage, opts: []api.NixOption, mode: Mode) usize {228    return switch (mode) {229        .packages => packages.len,230        .options => opts.len,231    };232}233234fn stdinIsTty(io: std.Io) bool {235    return std.Io.File.stdin().isTty(io) catch false;236}237238// Plain, non-interactive rendering for non-tty stdin. Same pink :: header and239// result count the widget shows, then one line per result; no cursor control.240fn runPlain(gpa: std.mem.Allocator, io: std.Io, query: []const u8, mode: Mode, environ: *const std.process.Environ.Map) !?types.NixPackage {241    // An empty query matches the entire catalog (100k+ rows); refuse it cleanly.242    if (std.mem.trim(u8, query, " \t").len == 0) {243        output.searchEmptyQuery();244        output.flush();245        return null;246    }247    var arena = std.heap.ArenaAllocator.init(gpa);248    defer arena.deinit();249    var opts: []api.NixOption = &.{};250    const packages = doSearch(arena.allocator(), io, query, mode, &opts, environ);251    const count = resultCount(packages, opts, mode);252    const mode_str = if (mode == .packages) "nixpkgs" else "options";253    output.searchPlainHeader(mode_str, query, count);254    if (count == 0) {255        output.searchPlainNoResults();256        output.flush();257        return null;258    }259    switch (mode) {260        // unfree=false: per-result meta.unfree would need an eval per package (too261        // slow for the list); the TUI detail pane is the place to surface it.262        .packages => for (packages) |pkg| output.searchPlainPackage(pkg.pname, pkg.version, pkg.description, false),263        .options => for (opts) |opt| output.searchPlainOption(opt.name, opt.type_str),264    }265    output.flush();266    return null;267}268269// A key hint with dim brackets and a pink letter: dim '[', pink label, dim ']'.270// Comptime-built so it's a plain string literal — the widget always renders raw271// ANSI (it only runs on a real tty).272fn keyHint(comptime label: []const u8) []const u8 {273    return output.C_DIM ++ "[" ++ output.C_RESET ++ output.C_PINK ++ label ++ output.C_RESET ++ output.C_DIM ++ "]" ++ output.C_RESET;274}275276fn renderWidget(query: []const u8, packages: []types.NixPackage, opts: []api.NixOption, selection: usize, mode: Mode, searching: bool, detail: Detail) void {277    const count = resultCount(packages, opts, mode);278    const CLR = output.C_CLEAR_EOL;279    const RST = output.C_RESET;280    const PKN = output.C_PINK;281    const BLD = output.C_BOLD;282    const DIM = output.C_DIM;283    const CYN = output.C_CYAN;284285    // Clamp to the current result count: a caller could hand us a selection from a286    // larger, now-replaced result set. Never index past the end.287    const sel = if (count == 0) 0 else @min(selection, count - 1);288289    // Line 1: header. While the (blocking) search runs, show "searching..." so the290    // pause reads as work-in-progress, not a frozen widget.291    output.raw(CLR);292    const mode_str = if (mode == .packages) "nixpkgs" else "options";293    if (searching) {294        output.p("{s}:: {s}search {s} {s}  {s}searching...{s}\n", .{295            PKN, RST, mode_str, query, DIM, RST,296        });297    } else {298        output.p("{s}:: {s}search {s} {s}  {s}{d} results{s}\n", .{299            PKN, RST, mode_str, query, DIM, count, RST,300        });301    }302303    // Line 2: blank304    output.raw(CLR ++ "\n");305306    // Lines 3-7: results (up to RESULT_ROWS), scrolled so selection stays visible307    const scroll: usize = if (sel >= RESULT_ROWS) sel - RESULT_ROWS + 1 else 0;308    var i: usize = 0;309    while (i < RESULT_ROWS) : (i += 1) {310        const idx = scroll + i;311        output.raw(CLR);312        if (idx < count) {313            const selected = (idx == sel);314            switch (mode) {315                .packages => {316                    const pkg = packages[idx];317                    const marker = if (selected) PKN ++ "> " ++ RST else "  ";318                    const name_pre = if (selected) PKN ++ BLD else "";319                    const name_suf = if (selected) RST else "";320                    output.p("   {s}{s}{s}{s}   {s}{s}{s}   {s}\n", .{ marker, name_pre, pkg.pname, name_suf, CYN, pkg.version, RST, pkg.description });321                },322                .options => {323                    const opt = opts[idx];324                    const marker = if (selected) PKN ++ "> " ++ RST else "  ";325                    output.p("   {s}{s}   {s}{s}{s}\n", .{ marker, opt.name, DIM, opt.type_str, RST });326                },327            }328        } else {329            output.raw("\n");330        }331    }332333    // Line 8: blank334    output.raw(CLR ++ "\n");335336    // Lines 9-12: selected package detail. Line 9 (name+version) is shared; the337    // next three lines depend on `detail`. Every arm emits exactly 3 lines so the338    // widget stays WIDGET_LINES tall.339    if (count > 0 and mode == .packages) {340        const pkg = packages[sel];341        output.raw(CLR);342        output.p("   {s}{s}{s}{s}  {s}{s}{s}\n", .{ PKN, BLD, pkg.pname, RST, CYN, pkg.version, RST });343        switch (detail) {344            .collapsed => {345                output.raw(CLR);346                output.p("   {s}\n", .{pkg.description});347                output.raw(CLR ++ "\n");348                output.raw(CLR);349                output.p("   {s}attr{s}  {s}{s}{s}\n", .{ DIM, RST, CYN, pkg.attr, RST });350            },351            .loading => {352                output.raw(CLR);353                output.p("   {s}loading details...{s}\n", .{ DIM, RST });354                output.raw(CLR ++ "\n");355                output.raw(CLR);356                output.p("   {s}attr{s}  {s}{s}{s}\n", .{ DIM, RST, CYN, pkg.attr, RST });357            },358            .unavailable => {359                output.raw(CLR);360                output.p("   {s}details unavailable{s}\n", .{ DIM, RST });361                output.raw(CLR ++ "\n");362                output.raw(CLR);363                output.p("   {s}attr{s}  {s}{s}{s}\n", .{ DIM, RST, CYN, pkg.attr, RST });364            },365            .meta => |m| {366                const home = if (m.homepage.len > 0) m.homepage else "—";367                const lic = if (m.license.len > 0) m.license else "—";368                output.raw(CLR);369                output.p("   {s}{s:<8}{s}{s}{s}{s}\n", .{ DIM, "home", RST, CYN, home, RST });370                output.raw(CLR);371                output.p("   {s}{s:<8}{s}{s}\n", .{ DIM, "license", RST, lic });372                output.raw(CLR);373                output.p("   {s}{s:<8}{s}{s}{s}{s}\n", .{ DIM, "attr", RST, CYN, pkg.attr, RST });374            },375        }376    } else if (count > 0 and mode == .options) {377        const opt = opts[sel];378        output.raw(CLR);379        output.p("   {s}  {s}{s}{s}\n", .{ opt.name, DIM, opt.type_str, RST });380        output.raw(CLR);381        output.p("   {s}\n", .{opt.description});382        output.raw(CLR ++ "\n");383        output.raw(CLR ++ "\n");384    } else {385        output.raw(CLR ++ "\n");386        output.raw(CLR ++ "\n");387        output.raw(CLR ++ "\n");388        output.raw(CLR ++ "\n");389    }390391    // Line 13: blank392    output.raw(CLR ++ "\n");393394    // Lines 14-17: controls + padding to reach WIDGET_LINES395    output.raw(CLR);396    if (mode == .packages) {397        output.p("   {s} install  {s} info  {s}[up/down]{s} nav  {s} cancel\n", .{398            keyHint("enter"), keyHint("tab"), DIM, RST, keyHint("esc"),399        });400    } else {401        output.p("   {s}[up/down]{s} nav  {s} cancel\n", .{ DIM, RST, keyHint("esc") });402    }403    // Padding to reach WIDGET_LINES (18)404    var pad: usize = 14;405    while (pad < WIDGET_LINES) : (pad += 1) {406        output.raw(CLR ++ "\n");407    }408}409410fn rawModeEnable(fd: std.posix.fd_t, saved: *std.posix.termios) !void {411    var tio = try std.posix.tcgetattr(fd);412    saved.* = tio;413    if (comptime builtin.os.tag == .linux) {414        tio.lflag.ECHO = false;415        tio.lflag.ICANON = false;416        tio.lflag.ISIG = false;417        tio.lflag.IEXTEN = false;418        tio.iflag.ICRNL = false;419        tio.iflag.IXON = false;420        tio.cc[@intFromEnum(std.posix.V.MIN)] = 1;421        tio.cc[@intFromEnum(std.posix.V.TIME)] = 0;422    }423    // On non-Linux, terminal will not be in raw mode (TUI requires Linux)424    try std.posix.tcsetattr(fd, .FLUSH, tio);425}426427fn rawModeDisable(fd: std.posix.fd_t, saved: std.posix.termios) void {428    std.posix.tcsetattr(fd, .FLUSH, saved) catch {};429}