output.zig

26.2 KB · raw

1// output.zig — the only file that writes to stdout2// All ANSI codes live here. No raw escape sequences elsewhere.3const std = @import("std");4const types = @import("types.zig");5const nina_version = @import("version.zig");6const Io = std.Io;78// Color constants — all ANSI codes live here only9const PINK = "\x1b[38;2;255;183;197m";10const BOLD = "\x1b[1m";11const GREEN = "\x1b[32m";12const YELLOW = "\x1b[33m";13const RED = "\x1b[31m";14const CYAN = "\x1b[36m";15const DIM = "\x1b[2m";16const RESET = "\x1b[0m";1718// Exported for TUI use (search.zig references these to avoid \x1b in that file)19pub const C_PINK = PINK;20pub const C_BOLD = BOLD;21pub const C_GREEN = GREEN;22pub const C_YELLOW = YELLOW;23pub const C_RED = RED;24pub const C_CYAN = CYAN;25pub const C_DIM = DIM;26pub const C_RESET = RESET;27pub const C_CLEAR_EOL = "\x1b[K";28pub const C_CURSOR_UP_FMT = "\x1b[{d}A";2930// Precomputed prefix strings31const HDR_C = PINK ++ ":: " ++ RESET;32const HDR_P = ":: ";33const STEP_C = "   " ++ DIM ++ "-> " ++ RESET;34const STEP_P = "   -> ";35const TEACH_C = "   " ++ DIM ++ "= " ++ RESET;36const TEACH_P = "   = ";37const ERR_C = PINK ++ ":: " ++ RED ++ "error:" ++ RESET ++ " ";38const ERR_P = ":: error: ";39const WARN_C = PINK ++ ":: " ++ YELLOW ++ "warning:" ++ RESET ++ " ";40const WARN_P = ":: warning: ";4142var _fw: Io.File.Writer = undefined;43var _w: *Io.Writer = undefined;44var color_enabled: bool = true;45var teach_enabled: bool = false;4647pub fn init(writer: *Io.Writer, io: std.Io, environ: *const std.process.Environ.Map, color_config: bool) void {48    _w = writer;49    color_enabled = color_config and50        (std.Io.File.stdout().isTty(io) catch false) and51        !environ.contains("NO_COLOR");52}5354pub fn setTeach(enabled: bool) void {55    teach_enabled = enabled;56}5758pub fn flush() void {59    _w.flush() catch {};60}6162fn c(code: []const u8) []const u8 {63    return if (color_enabled) code else "";64}6566fn hdr() []const u8 {67    return if (color_enabled) HDR_C else HDR_P;68}6970fn step() []const u8 {71    return if (color_enabled) STEP_C else STEP_P;72}7374fn teach_pfx() []const u8 {75    return if (color_enabled) TEACH_C else TEACH_P;76}7778// Raw write — used by TUI79pub fn raw(bytes: []const u8) void {80    _w.writeAll(bytes) catch {};81}8283// Formatted print — used by TUI for in-place rendering84pub fn p(comptime fmt: []const u8, args: anytype) void {85    _w.print(fmt, args) catch {};86}8788pub fn cursorUp(n: usize) void {89    if (n > 0) p("\x1b[{d}A", .{n});90}9192pub fn clearLine() void {93    p("\r\x1b[K", .{});94}9596// --- Error and warning output ---9798pub fn printError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {99    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, message });100    if (detail) |d| p("   {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });101    if (suggestion) |s| p("   {s}-> {s}{s}\n", .{ c(DIM), s, c(RESET) });102}103104pub fn printWarning(message: []const u8) void {105    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, message });106}107108pub fn printTeach(cmd: []const u8) void {109    if (teach_enabled) p("{s}{s}{s}\n", .{ teach_pfx(), cmd, c(RESET) });110}111112pub fn printSubstep(comptime fmt: []const u8, args: anytype) void {113    p("{s}", .{step()});114    p(fmt, args);115    p("\n", .{});116}117118// --- Confirmation prompt (returns true = yes) ---119120pub fn confirm(prompt: []const u8) bool {121    p("   {s}continue? [Y/n]  ", .{prompt});122    flush();123    var buf: [64]u8 = undefined;124    var i: usize = 0;125    while (i < buf.len) {126        const n = std.posix.read(std.posix.STDIN_FILENO, buf[i .. i + 1]) catch break;127        if (n == 0) break;128        if (buf[i] == '\n') break;129        i += 1;130    }131    const input = std.mem.trim(u8, buf[0..i], " \t");132    if (input.len == 0) return true;133    return input[0] != 'n' and input[0] != 'N';134}135136// --- Apply / rebuild ---137138pub fn applyStart(machine: []const u8) void {139    p("{s}rebuilding {s}...\n", .{ hdr(), machine });140}141142pub fn applyDone(gen: u32, elapsed_ms: u64) void {143    const secs = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;144    if (elapsed_ms >= 1000) {145        p("{s}generation {s}{d}{s}  {s}[{d:.1}s]{s}\n", .{ hdr(), c(BOLD), gen, c(RESET), c(DIM), secs, c(RESET) });146    } else {147        p("{s}generation {s}{d}{s}\n", .{ hdr(), c(BOLD), gen, c(RESET) });148    }149}150151pub fn applyFailed(translated: []const u8) void {152    p("{s}build failed\n", .{if (color_enabled) ERR_C else ERR_P});153    if (translated.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), translated, c(RESET) });154    p("   {s}-> nina doctor{s}\n", .{ c(DIM), c(RESET) });155}156157pub fn applyDry(machine: []const u8) void {158    p("{s}dry run  {s}\n", .{ hdr(), machine });159}160161pub fn applyDryDone() void {162    p("{s}no changes applied\n", .{hdr()});163}164165// --- Check ---166167pub fn checkStart(machine: []const u8) void {168    p("{s}checking {s}...\n", .{ hdr(), machine });169}170171pub fn checkOk() void {172    p("{s}{s}ok{s}\n", .{ hdr(), c(GREEN), c(RESET) });173}174175pub fn checkFailed(msg: []const u8) void {176    p("{s}config invalid\n", .{if (color_enabled) ERR_C else ERR_P});177    if (msg.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), msg, c(RESET) });178}179180// --- Back / go ---181182pub fn backStart(machine: []const u8, from: u32, to: u32) void {183    p("{s}rolling back  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, from, to });184}185186pub fn goStart(machine: []const u8, current: u32, target: u32) void {187    p("{s}switching  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, current, target });188}189190pub fn generationDone(n: u32) void {191    p("{s}done  gen {s}{d}{s}\n", .{ hdr(), c(BOLD), n, c(RESET) });192}193194// --- History ---195196pub fn historyHeader(machine: []const u8) void {197    p("{s}generations  {s}\n\n", .{ hdr(), machine });198}199200pub fn historyRow(gen: types.GenerationInfo) void {201    if (gen.current) {202        p("   {s}{d}{s}   {s}   {s}   {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });203    } else {204        p("   {s}{d}   {s}   {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });205    }206}207208// --- Clean ---209210pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {211    p("{s}cleaning  {s}  keeping {d} of {d}\n", .{ hdr(), machine, keep, total });212}213214pub fn cleanDone(freed: []const u8) void {215    p("{s}freed {s}\n", .{ hdr(), freed });216}217218// --- Status ---219220pub fn statusHeader() void {221    p("{s}status\n\n", .{hdr()});222}223224pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {225    if (ok) {226        p("   {s}{s}   gen {d}   {s}   {s}{s}\n", .{ c(DIM), machine, gen, time, size, c(RESET) });227    } else {228        p("   {s}{s}   {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });229    }230}231232// --- Diff ---233234pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {235    p("{s}gen {d} -> gen {d}  {s}\n\n", .{ hdr(), a, b, machine });236}237238pub fn diffRow(entry: types.DiffEntry) void {239    switch (entry.op) {240        .add => p("   {s}+{s} {s}   {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),241        .remove => p("   {s}-{s} {s}   {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),242        .change => p("   {s}^{s} {s}   {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),243    }244}245246// --- Doctor ---247248pub fn doctorHeader(machine: []const u8) void {249    p("{s}diagnosing  {s}\n\n", .{ hdr(), machine });250}251252pub fn doctorRow(check: types.DoctorCheck) void {253    const status_str = switch (check.status) {254        .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",255        .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",256        .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",257    };258    const note_str = check.note orelse "";259    p("   {s:<16}  {s}   {s}\n", .{ check.name, status_str, note_str });260}261262pub fn doctorSummary(warnings: u32, failures: u32) void {263    if (failures > 0) {264        p("{s}{d} problems\n", .{ hdr(), failures });265    } else if (warnings > 0) {266        p("{s}{d} warnings\n", .{ hdr(), warnings });267    } else {268        p("{s}{s}all good{s}\n", .{ hdr(), c(GREEN), c(RESET) });269    }270}271272// --- Update ---273274pub fn updateStart(machine: []const u8) void {275    p("{s}updating channels  {s}\n\n", .{ hdr(), machine });276}277278pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {279    if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {280        p("   {s}   {s} -> {s}\n", .{ name, old.?, new.? });281    } else {282        p("   {s}   {s}no change{s}\n", .{ name, c(DIM), c(RESET) });283    }284}285286pub fn updateDone() void {287    p("\n{s}done\n", .{hdr()});288}289290// --- Upgrade ---291292pub fn upgradeStart(machine: []const u8) void {293    p("{s}upgrading  {s}\n", .{ hdr(), machine });294}295296// --- Log ---297298pub fn logHeader(machine: []const u8) void {299    p("{s}history  {s}\n\n", .{ hdr(), machine });300}301302pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {303    p("   {s}  {s}   {s:<12}  {s}\n", .{ date, time, action, detail });304}305306// --- Edit ---307308pub fn editOpen(path: []const u8, editor: []const u8) void {309    p("{s}opening {s}\n", .{ hdr(), path });310    printSubstep("{s}", .{editor});311    p("\n   {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });312}313314// --- Fmt ---315316pub fn fmtStart(path: []const u8) void {317    p("{s}formatting  {s}\n", .{ hdr(), path });318}319320pub fn fmtDone() void {321    p("{s}done\n", .{hdr()});322}323324pub fn fmtNeedsFormat() void {325    p("{s}needs formatting  {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });326}327328// --- Repl ---329330pub fn replStart() void {331    p("{s}nix repl  {s}nixpkgs loaded{s}  :q to exit\n", .{ hdr(), c(DIM), c(RESET) });332}333334// --- Develop ---335336pub fn developStart(pkgs: []const []const u8) void {337    p("{s}entering dev shell\n\n   ", .{hdr()});338    for (pkgs, 0..) |pkg, i| {339        if (i > 0) p("  ", .{});340        p("{s}", .{pkg});341    }342    p("\n\n   {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });343}344345pub fn developDone() void {346    p("{s}back\n", .{hdr()});347}348349// --- Info ---350351pub fn infoHeader(machine: []const u8) void {352    p("{s}{s}\n\n", .{ hdr(), machine });353}354355pub fn infoRow(key: []const u8, val: []const u8) void {356    p("   {s:<12}  {s}\n", .{ key, val });357}358359// --- Boot ---360361pub fn bootHeader(machine: []const u8) void {362    p("{s}boot entries  {s}\n\n", .{ hdr(), machine });363}364365pub fn bootEntry(entry: types.BootEntry, idx: usize) void {366    if (entry.current) {367        p("   {s}> {s}{s}{s}\n", .{ c(BOLD), entry.title, if (entry.date) |d| std.fmt.allocPrint(std.heap.page_allocator, "   {s}", .{d}) catch "   " else "", c(RESET) });368    } else {369        p("   {s}  {s}{s}{s}\n", .{ c(DIM), entry.title, if (entry.date) |d| std.fmt.allocPrint(std.heap.page_allocator, "   {s}", .{d}) catch "   " else "", c(RESET) });370    }371    _ = idx;372}373374// --- Service ---375376pub fn serviceListHeader(machine: []const u8) void {377    p("{s}services  {s}\n\n", .{ hdr(), machine });378}379380pub fn serviceRow(svc: types.ServiceInfo) void {381    const state_str = switch (svc.state) {382        .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",383        .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",384        .inactive => "inactive",385        .unknown => "unknown",386    };387    const uptime = svc.uptime orelse "";388    p("   {s:<24}  {s:<14}  {s}\n", .{ svc.name, state_str, uptime });389}390391pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {392    p("{s}{s} {s}  {s}\n", .{ hdr(), verb, name, machine });393}394395pub fn serviceDone() void {396    p("{s}done\n", .{hdr()});397}398399pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {400    p("{s}{s}  last {d} lines  {s}\n\n", .{ hdr(), name, n, machine });401}402403// --- Store ---404405pub fn storeInfo(info: types.StoreInfo) void {406    p("{s}store\n\n", .{hdr()});407    p("   {s:<16}  {s}\n", .{ "total", info.total_size });408    p("   {s:<16}  {d} paths\n", .{ "live", info.live_paths });409    p("   {s:<16}  {s}{d} paths{s}   run nina clean\n", .{ "reclaimable", c(DIM), info.reclaimable_paths, c(RESET) });410}411412// --- Channel ---413414pub fn channelListHeader(machine: []const u8) void {415    p("{s}channels  {s}\n\n", .{ hdr(), machine });416}417418pub fn channelRow(ch: types.ChannelInfo) void {419    p("   {s:<16}  {s}\n", .{ ch.name, ch.url });420}421422// --- Flake ---423424pub fn flakeUpdateStart() void {425    p("{s}updating inputs\n\n", .{hdr()});426}427428pub fn flakeInput(input: types.FlakeInput) void {429    if (input.changed) {430        const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";431        const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";432        p("   {s:<20}  {s} -> {s}\n", .{ input.name, old, new });433    } else {434        p("   {s:<20}  {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });435    }436}437438pub fn flakeLockWritten() void {439    p("\n{s}flake.lock written\n", .{hdr()});440}441442pub fn flakeValid() void {443    p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });444}445446pub fn flakeCheckStart() void {447    p("{s}checking flake\n", .{hdr()});448}449450// --- Profile ---451452pub fn profileListHeader() void {453    p("{s}profile packages\n\n", .{hdr()});454}455456pub fn profileRow(attr: []const u8, pkg_version: []const u8, description: []const u8) void {457    p("   {s:<24}  {s:<12}  {s}\n", .{ attr, pkg_version, description });458}459460// --- Install / Remove / Try ---461462pub fn installStart(pkg: []const u8, path: types.InstallPath) void {463    const path_str = switch (path) {464        .profile => "profile",465        .system => "system",466    };467    p("{s}installing {s}{s}{s}  {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });468}469470pub fn installDone(pkg: []const u8) void {471    p("{s}{s}{s}{s} installed  {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });472}473474pub fn removeStart(pkg: []const u8) void {475    p("{s}removing {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });476}477478// Plain-language health summary. No kaomoji, no hearts — just the facts.479pub fn mood(machine: []const u8, state: []const u8, gen: u32, age: []const u8, size: []const u8) void {480    const healthy = std.mem.eql(u8, state, "running");481    const verdict = if (healthy) "all good" else state;482    const color = if (healthy) GREEN else YELLOW;483    p("{s}{s}  {s}{s}{s}\n", .{ hdr(), machine, c(color), verdict, c(RESET) });484    if (age.len > 0 or size.len > 0) {485        p("   generation {d}, last applied {s}, {s} system closure\n", .{ gen, age, size });486    } else {487        p("   generation {d}\n", .{gen});488    }489}490491pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {492    p("{s}opening {s}  add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });493    const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;494    printSubstep("{s}", .{cmd});495    p("\n   {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });496}497498pub fn installSelectPrompt(pkg: []const u8, pkg_version: []const u8) void {499    p("{s}{s}{s}{s}  {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), pkg_version });500    p("   [i] profile install    {s}instant  no rebuild{s}\n", .{ c(DIM), c(RESET) });501    p("   [s] system install     {s}opens editor  requires apply{s}\n", .{ c(DIM), c(RESET) });502    p("   [t] try now            {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });503    p("   > ", .{});504    flush();505}506507pub fn tryStart(pkg: []const u8) void {508    p("{s}trying {s}{s}{s}  exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });509}510511pub fn tryDone() void {512    p("{s}back\n", .{hdr()});513}514515// --- List ---516517pub fn listHeader() void {518    p("{s}installed packages\n\n", .{hdr()});519}520521pub fn listSection(title: []const u8) void {522    p("   {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });523}524525pub fn listPkg(name: []const u8, pkg_version: []const u8) void {526    p("   {s}{s}{s}   {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), pkg_version, c(RESET) });527}528529// --- Hello ---530531pub fn hello(machines: []const types.Machine) void {532    p("{s}nina\n\n", .{hdr()});533    for (machines) |m| {534        const kind = if (m.local) "local" else "ssh";535        const detail = if (m.local) "default" else m.host orelse "";536        p("   {s}{s}{s}   {s}   {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });537    }538    p("\n", .{});539    const quick = [_][2][]const u8{540        .{ "apply",         "rebuild and switch" },541        .{ "search <q>",    "find packages" },542        .{ "install <pkg>", "add to profile or system" },543        .{ "status",        "machine health" },544        .{ "back",          "roll back one generation" },545        .{ "update",        "refresh channels" },546    };547    for (quick) |cmd| {548        p("   {s:<16}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });549    }550    p("\n", .{});551}552553// --- Help ---554555pub fn help() void {556    p("{s}nina  {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });557    const cmds = [_][2][]const u8{558        .{ "apply", "rebuild and switch" },559        .{ "back", "roll back one generation" },560        .{ "go <n>", "switch to generation n" },561        .{ "history", "list all generations" },562        .{ "clean", "remove old generations" },563        .{ "search <q>", "find packages" },564        .{ "install <pkg>", "add to profile or system" },565        .{ "remove <pkg>", "remove from profile or system" },566        .{ "try <pkg>", "test without installing" },567        .{ "list", "show installed packages" },568        .{ "edit", "open configuration.nix" },569        .{ "check", "validate config" },570        .{ "diff", "changes between generations" },571        .{ "status", "machine health" },572        .{ "update", "refresh channels" },573        .{ "upgrade", "update and rebuild" },574        .{ "log", "operation history" },575        .{ "doctor", "diagnose issues" },576        .{ "mood", "plain-language health" },577        .{ "develop", "enter dev shell" },578        .{ "flake", "flake management" },579        .{ "build", "build a package" },580        .{ "run", "run without installing" },581        .{ "service", "manage services" },582        .{ "store", "nix store tools" },583        .{ "channel", "manage channels" },584        .{ "profile", "user profile packages" },585        .{ "info", "system information" },586        .{ "boot", "boot entries" },587        .{ "fmt", "format configuration.nix" },588        .{ "repl", "nix repl with nixpkgs" },589        .{ "option <q>", "search nixos options" },590        .{ "pkg", "inspect packages" },591        .{ "pin", "pin flake inputs" },592        .{ "hash", "compute nix hash" },593        .{ "fetch", "fetch and hash a url" },594        .{ "goodbye",       "uninstall nina" },595        .{ "hello",        "list machines" },596        .{ "help",         "this message" },597    };598    for (cmds) |cmd| {599        p("   {s:<18}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });600    }601    p("\n   {s}flags: --on <machine>  --dry  --check  --no-apply  --last <n>  --all  --json{s}\n", .{ c(DIM), c(RESET) });602}603604pub fn version() void {605    p("{s}nina  v{s}  zig 0.16  zero dependencies\n", .{ hdr(), nina_version.VERSION });606}607608// --- Search (text output before TUI) ---609610pub fn searchResult(pkg: types.NixPackage) void {611    p("   {s}{s}{s}   {s}{s}{s}   {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });612}613614pub fn searchHeader(query: []const u8, count: usize) void {615    p("{s}search nixpkgs {s}  {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });616}617618// --- Gen commands ---619620pub fn genCurrent(n: u32) void {621    p("{d}\n", .{n});622}623624pub fn done() void {625    p("{s}done\n", .{hdr()});626}627628pub fn runStart(pkg: []const u8) void {629    p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });630}631632pub fn buildStart(attr: []const u8) void {633    p("{s}building {s}\n", .{ hdr(), attr });634}635636pub fn buildDone() void {637    p("{s}done\n", .{hdr()});638}639640// --- Self-update (nina update) ---641642pub fn updateSelfStart() void {643    p("{s}checking for updates\n", .{hdr()});644}645646pub fn updateSelfCurrent(ver: []const u8) void {647    p("{s}{s}nina {s} is up to date{s}\n", .{ hdr(), c(GREEN), ver, c(RESET) });648}649650pub fn updateSelfAvailable(current: []const u8, latest: []const u8) void {651    p("{s}current  {s}\n", .{ step(), current });652    p("{s}latest   {s}{s}{s}\n", .{ step(), c(BOLD), latest, c(RESET) });653}654655pub fn updateSelfDownloading(ver: []const u8, platform: []const u8) void {656    p("{s}downloading nina {s} for {s}\n", .{ step(), ver, platform });657}658659pub fn updateSelfVerifying() void {660    p("{s}verifying checksum\n", .{step()});661}662663pub fn updateSelfInstalling(install_path: []const u8) void {664    p("{s}installing to {s}\n", .{ step(), install_path });665}666667pub fn updateSelfDone(old_ver: []const u8, new_ver: []const u8) void {668    p("{s}updated  {s} {s}→{s} {s}\n", .{ hdr(), old_ver, c(DIM), c(RESET), new_ver });669}670671pub fn updateSelfNoPlatform(platform: []const u8) void {672    p("{s}no pre-built binary for {s}\n", .{ hdr(), platform });673    p("{s}check kepr.uk/nina/releases for available platforms\n", .{step()});674}675676pub fn updateSelfNetworkError() void {677    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, "could not reach kepr.uk — check your connection" });678}679680pub fn updateSelfChecksumFailed() void {681    p("{s}checksum mismatch — download may be corrupt\n", .{if (color_enabled) ERR_C else ERR_P});682    p("{s}try again or download manually from kepr.uk/nina/releases\n", .{step()});683}684685pub fn updateSelfNixManaged(new_ver: []const u8) void {686    p("{s}upgrading to {s}{s}{s} via nix profile\n", .{ step(), c(BOLD), new_ver, c(RESET) });687}688689pub fn updateSelfNixFailed() void {690    p("{s}nix profile upgrade failed\n", .{if (color_enabled) ERR_C else ERR_P});691    p("{s}try manually: nix profile upgrade '.*nina.*'\n", .{step()});692}693694pub fn updateSelfNixSystemManaged() void {695    p("{s}nina appears to be managed by your NixOS system configuration\n", .{hdr()});696    p("{s}to update: sudo nixos-rebuild switch\n", .{step()});697    p("{s}to reinstall as a profile package: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{step()});698}699700// --- Goodbye / uninstall ---701702pub fn goodbyeStart() void {703    p("{s}uninstalling nina\n", .{hdr()});704}705706pub fn goodbyeFile(path: []const u8) void {707    p("{s}will remove {s}\n", .{ step(), path });708}709710pub fn goodbyeNix() void {711    p("{s}will remove nina from nix profile\n", .{step()});712    p("{s}command: nix profile remove '.*nina.*'\n", .{step()});713}714715pub fn goodbyeNixSystemManaged() void {716    p("{s}nina is managed by your NixOS system configuration\n", .{hdr()});717    p("{s}to remove: delete ~/nina.nix, then run sudo nixos-rebuild switch\n", .{step()});718    p("{s}or remove the nina import from /etc/nixos/configuration.nix directly\n", .{step()});719}720721pub fn goodbyeNixFileFound(path: []const u8) void {722    p("{s}found {s} (NixOS config snippet from the installer)\n", .{ step(), path });723}724725pub fn goodbyeDone() void {726    p("{s}goodbye! reinstall anytime: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{hdr()});727}728729pub fn goodbyeFailed(msg: []const u8) void {730    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, msg });731}732733// --- Setup ---734735pub fn setupCheck() void {736    p("{s}checking nix features\n", .{hdr()});737}738739pub fn setupFeatures(nix_command: bool, flakes: bool) void {740    p("{s}nix-command  {s}{s}{s}\n", .{741        step(),742        if (nix_command) c(GREEN) else "",743        if (nix_command) "enabled" else "not enabled",744        if (nix_command) c(RESET) else "",745    });746    p("{s}flakes       {s}{s}{s}\n", .{747        step(),748        if (flakes) c(GREEN) else "",749        if (flakes) "enabled" else "not enabled",750        if (flakes) c(RESET) else "",751    });752}753754pub fn setupAllSet() void {755    p("{s}all set\n", .{hdr()});756}757758// Printed once, before the first search, when the nixpkgs registry still points759// at a remote URL. Flush so it lands before `nix registry pin` and any results.760pub fn pinningRegistry() void {761    p("{s}pinning nixpkgs for fast local search...\n", .{hdr()});762    flush();763}764765// --- Plain (non-tty) search rendering ---766// Color-gated equivalents of the raw-ANSI TUI widget, for piped/scripted search.767// Using the gated header + c() keeps ANSI out of non-tty output (NO_COLOR / pipes).768769pub fn searchPlainHeader(mode_str: []const u8, query: []const u8, count: u64) void {770    p("{s}search {s} {s}  {s}{d} results{s}\n", .{ hdr(), mode_str, query, c(DIM), count, c(RESET) });771}772773pub fn searchPlainNoResults() void {774    p("   no results\n", .{});775}776777pub fn searchPlainPackage(pname: []const u8, pkg_version: []const u8, description: []const u8, unfree: bool) void {778    if (unfree) {779        p("   {s}   {s}{s}{s}   {s}unfree{s}   {s}\n", .{ pname, c(DIM), pkg_version, c(RESET), c(YELLOW), c(RESET), description });780    } else {781        p("   {s}   {s}{s}{s}   {s}\n", .{ pname, c(DIM), pkg_version, c(RESET), description });782    }783}784785pub fn searchPlainOption(name: []const u8, type_str: []const u8) void {786    p("   {s}   {s}{s}{s}\n", .{ name, c(DIM), type_str, c(RESET) });787}788789pub fn searchEmptyQuery() void {790    p("{s}search needs a query  {s}e.g. nina search firefox{s}\n", .{ hdr(), c(DIM), c(RESET) });791}792793pub fn setupEnabling() void {794    p("{s}enabling experimental features\n", .{hdr()});795}796797pub fn setupDone(path: []const u8, is_nixos: bool) void {798    p("{s}wrote {s}\n", .{ step(), path });799    p("{s}done  open a new shell to pick up the change\n", .{hdr()});800    if (is_nixos) {801        printSubstep("to persist across nixos-rebuild, add to configuration.nix:", .{});802        printSubstep("  nix.settings.experimental-features = [ \"nix-command\" \"flakes\" ];", .{});803    }804}805806pub fn pinDone(input: []const u8, commit: []const u8) void {807    p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });808}809810pub fn unpinDone(input: []const u8) void {811    p("{s}unpinned {s}\n", .{ hdr(), input });812}813814pub fn hashResult(hash: []const u8) void {815    p("{s}\n", .{hash});816}817818pub fn fetchResult(hash: []const u8, url: []const u8) void {819    _ = url;820    p("{s}\n", .{hash});821}822823pub fn storeRepairStart() void {824    p("{s}repairing store\n", .{hdr()});825}826827pub fn storeVerifyStart() void {828    p("{s}verifying store\n", .{hdr()});829}830831pub fn storeGcDone(freed: []const u8) void {832    p("{s}freed {s}\n", .{ hdr(), freed });833}834835pub fn storePath(path: []const u8) void {836    p("{s}\n", .{path});837}838839pub fn pkgResult(key: []const u8, val: []const u8) void {840    p("   {s:<16}  {s}\n", .{ key, val });841}842843pub fn channelDone() void {844    p("{s}done\n", .{hdr()});845}846847pub fn flakeShowHeader(name: []const u8) void {848    p("{s}{s}\n\n", .{ hdr(), name });849}850851pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {852    p("   {s:<12}  {s:<24}  {s}\n", .{ kind, name, desc });853}