output.zig

28.6 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// kaomoji constants — all kaomoji defined here, never inlined elsewhere. Rare19// and earned: each marks one genuine moment and never repeats in one output20// block. They are plain text, not ANSI, so they survive pipes and NO_COLOR.21const KAO_FACE = "(˶ᵔ ᵕ ᵔ˶)  "; // hello — her face22const KAO_COZY = "(っ˘ω˘ς)  "; // apply start23const KAO_EXCITED = "╰(*°▽°*)╯  "; // apply success after a failure this session24const KAO_GENTLE = "( ˘ᵕ˘ )  "; // back done25const KAO_CONTENT = "( ´ ∀ ` )  "; // clean freed26const KAO_HAPPY = "( ´ ▽ ` )  "; // doctor all clear / mood all good27const KAO_SLEEPY = "(˘ω˘ )  "; // try / develop exit28const KAO_SAD = "(;ω;)  "; // network or ssh errors only — never user typos2930// Exported for TUI use (search.zig references these to avoid \x1b in that file)31pub const C_PINK = PINK;32pub const C_BOLD = BOLD;33pub const C_GREEN = GREEN;34pub const C_YELLOW = YELLOW;35pub const C_RED = RED;36pub const C_CYAN = CYAN;37pub const C_DIM = DIM;38pub const C_RESET = RESET;39pub const C_CLEAR_EOL = "\x1b[K";40pub const C_CURSOR_UP_FMT = "\x1b[{d}A";4142// Precomputed prefix strings43const HDR_C = PINK ++ ":: " ++ RESET;44const HDR_P = ":: ";45const STEP_C = "   " ++ DIM ++ "-> " ++ RESET;46const STEP_P = "   -> ";47const TEACH_C = "   " ++ DIM ++ "= " ++ RESET;48const TEACH_P = "   = ";49const ERR_C = PINK ++ ":: " ++ RED ++ "error:" ++ RESET ++ " ";50const ERR_P = ":: error: ";51const WARN_C = PINK ++ ":: " ++ YELLOW ++ "warning:" ++ RESET ++ " ";52const WARN_P = ":: warning: ";5354var _fw: Io.File.Writer = undefined;55var _w: *Io.Writer = undefined;56var color_enabled: bool = true;57var teach_enabled: bool = false;5859pub fn init(writer: *Io.Writer, io: std.Io, environ: *const std.process.Environ.Map, color_config: bool) void {60    _w = writer;61    color_enabled = color_config and62        (std.Io.File.stdout().isTty(io) catch false) and63        !environ.contains("NO_COLOR");64}6566pub fn setTeach(enabled: bool) void {67    teach_enabled = enabled;68}6970pub fn flush() void {71    _w.flush() catch {};72}7374// Whether ANSI color is active (tty + config + no NO_COLOR). Callers spawning75// child processes that colorize their own output use this to gate the child.76pub fn colorEnabled() bool {77    return color_enabled;78}7980fn c(code: []const u8) []const u8 {81    return if (color_enabled) code else "";82}8384fn hdr() []const u8 {85    return if (color_enabled) HDR_C else HDR_P;86}8788fn step() []const u8 {89    return if (color_enabled) STEP_C else STEP_P;90}9192fn teach_pfx() []const u8 {93    return if (color_enabled) TEACH_C else TEACH_P;94}9596// Raw write — used by TUI97pub fn raw(bytes: []const u8) void {98    _w.writeAll(bytes) catch {};99}100101// Formatted print — used by TUI for in-place rendering102pub fn p(comptime fmt: []const u8, args: anytype) void {103    _w.print(fmt, args) catch {};104}105106pub fn cursorUp(n: usize) void {107    if (n > 0) p("\x1b[{d}A", .{n});108}109110pub fn clearLine() void {111    p("\r\x1b[K", .{});112}113114// --- Error and warning output ---115116pub fn printError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {117    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, message });118    if (detail) |d| p("   {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });119    // Actionable suggestion in cyan — the next step should catch the eye.120    if (suggestion) |s| p("   {s}-> {s}{s}\n", .{ c(CYAN), s, c(RESET) });121}122123// Like printError, but for genuine system failures (network/ssh/search). The sad124// kaomoji signals "this is on the machine, not on you" — never used for typos or125// not-found errors, which go through printError.126pub fn printSystemError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {127    p("{s}{s}{s}\n", .{ KAO_SAD, if (color_enabled) ERR_C else ERR_P, message });128    if (detail) |d| p("   {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });129    if (suggestion) |s| p("   {s}-> {s}{s}\n", .{ c(CYAN), s, c(RESET) });130}131132pub fn printWarning(message: []const u8) void {133    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, message });134}135136pub fn printTeach(cmd: []const u8) void {137    if (teach_enabled) p("{s}{s}{s}\n", .{ teach_pfx(), cmd, c(RESET) });138}139140pub fn printSubstep(comptime fmt: []const u8, args: anytype) void {141    p("{s}", .{step()});142    p(fmt, args);143    p("\n", .{});144}145146// --- Confirmation prompt (returns true = yes) ---147148pub fn confirm(prompt: []const u8) bool {149    p("   {s}continue? [Y/n]  ", .{prompt});150    flush();151    var buf: [64]u8 = undefined;152    var i: usize = 0;153    while (i < buf.len) {154        const n = std.posix.read(std.posix.STDIN_FILENO, buf[i .. i + 1]) catch break;155        if (n == 0) break;156        if (buf[i] == '\n') break;157        i += 1;158    }159    const input = std.mem.trim(u8, buf[0..i], " \t");160    if (input.len == 0) return true;161    return input[0] != 'n' and input[0] != 'N';162}163164// --- Apply / rebuild ---165166pub fn applyStart(machine: []const u8) void {167    p("{s}{s}rebuilding {s}...\n", .{ KAO_COZY, hdr(), machine });168}169170// after_failure: true only when the previous apply this session failed and this171// one succeeded — the one moment that earns the excited kaomoji. Routine172// successes show no kaomoji.173pub fn applyDone(gen: u32, elapsed_ms: u64, after_failure: bool) void {174    const face = if (after_failure) KAO_EXCITED else "";175    const secs = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;176    if (elapsed_ms >= 1000) {177        p("{s}{s}generation {s}{d}{s}  {s}[{d:.1}s]{s}\n", .{ face, hdr(), c(BOLD), gen, c(RESET), c(DIM), secs, c(RESET) });178    } else {179        p("{s}{s}generation {s}{d}{s}\n", .{ face, hdr(), c(BOLD), gen, c(RESET) });180    }181}182183pub fn applyFailed(translated: []const u8) void {184    p("{s}build failed\n", .{if (color_enabled) ERR_C else ERR_P});185    if (translated.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), translated, c(RESET) });186    p("   {s}-> nina doctor{s}\n", .{ c(DIM), c(RESET) });187}188189pub fn applyDry(machine: []const u8) void {190    p("{s}dry run  {s}\n", .{ hdr(), machine });191}192193pub fn applyDryDone() void {194    p("{s}no changes applied\n", .{hdr()});195}196197// --- Check ---198199pub fn checkStart(machine: []const u8) void {200    p("{s}checking {s}...\n", .{ hdr(), machine });201}202203pub fn checkOk() void {204    p("{s}{s}ok{s}\n", .{ hdr(), c(GREEN), c(RESET) });205}206207pub fn checkFailed(msg: []const u8) void {208    p("{s}config invalid\n", .{if (color_enabled) ERR_C else ERR_P});209    if (msg.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), msg, c(RESET) });210}211212// --- Back / go ---213214pub fn backStart(machine: []const u8, from: u32, to: u32) void {215    p("{s}rolling back  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, from, to });216}217218pub fn goStart(machine: []const u8, current: u32, target: u32) void {219    p("{s}switching  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, current, target });220}221222pub fn generationDone(n: u32) void {223    p("{s}done  gen {s}{d}{s}\n", .{ hdr(), c(BOLD), n, c(RESET) });224}225226// Like generationDone but for `back` specifically — a successful rollback earns227// the gentle kaomoji. `go` keeps the plain generationDone line.228pub fn backDone(n: u32) void {229    p("{s}{s}done  gen {s}{d}{s}\n", .{ KAO_GENTLE, hdr(), c(BOLD), n, c(RESET) });230}231232// --- History ---233234pub fn historyHeader(machine: []const u8) void {235    p("{s}generations  {s}\n\n", .{ hdr(), machine });236}237238pub fn historyRow(gen: types.GenerationInfo) void {239    if (gen.current) {240        p("   {s}{d}{s}   {s}   {s}   {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });241    } else {242        p("   {s}{d}   {s}   {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });243    }244}245246// --- Clean ---247248pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {249    p("{s}cleaning  {s}  keeping {d} of {d}\n", .{ hdr(), machine, keep, total });250}251252pub fn cleanDone(freed: []const u8) void {253    p("{s}{s}freed {s}\n", .{ KAO_CONTENT, hdr(), freed });254}255256// --- Status ---257258pub fn statusHeader() void {259    p("{s}status\n\n", .{hdr()});260}261262pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {263    if (ok) {264        p("   {s}{s}{s}   gen {s}{d}{s}   {s}{s}   {s}{s}\n", .{ c(DIM), machine, c(RESET), c(BOLD), gen, c(RESET), c(DIM), time, size, c(RESET) });265    } else {266        p("   {s}{s}   {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });267    }268}269270// --- Diff ---271272pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {273    p("{s}gen {d} -> gen {d}  {s}\n\n", .{ hdr(), a, b, machine });274}275276pub fn diffRow(entry: types.DiffEntry) void {277    switch (entry.op) {278        .add => p("   {s}+{s} {s}   {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),279        .remove => p("   {s}-{s} {s}   {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),280        .change => p("   {s}^{s} {s}   {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),281    }282}283284// --- Doctor ---285286pub fn doctorHeader(machine: []const u8) void {287    p("{s}diagnosing  {s}\n\n", .{ hdr(), machine });288}289290pub fn doctorRow(check: types.DoctorCheck) void {291    const status_str = switch (check.status) {292        .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",293        .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",294        .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",295    };296    const note_str = check.note orelse "";297    p("   {s:<16}  {s}   {s}\n", .{ check.name, status_str, note_str });298}299300pub fn doctorSummary(warnings: u32, failures: u32) void {301    if (failures > 0) {302        p("{s}{d} problems\n", .{ hdr(), failures });303    } else if (warnings > 0) {304        p("{s}{d} warnings\n", .{ hdr(), warnings });305    } else {306        p("{s}{s}{s}all good{s}\n", .{ KAO_HAPPY, hdr(), c(GREEN), c(RESET) });307    }308}309310// --- Update ---311312pub fn updateStart(machine: []const u8) void {313    p("{s}updating channels  {s}\n\n", .{ hdr(), machine });314}315316pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {317    if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {318        p("   {s}   {s} -> {s}\n", .{ name, old.?, new.? });319    } else {320        p("   {s}   {s}no change{s}\n", .{ name, c(DIM), c(RESET) });321    }322}323324pub fn updateDone() void {325    p("\n{s}done\n", .{hdr()});326}327328// --- Upgrade ---329330pub fn upgradeStart(machine: []const u8) void {331    p("{s}upgrading  {s}\n", .{ hdr(), machine });332}333334// --- Log ---335336pub fn logHeader(machine: []const u8) void {337    p("{s}history  {s}\n\n", .{ hdr(), machine });338}339340pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {341    p("   {s}{s}  {s}{s}   {s:<12}  {s}\n", .{ c(DIM), date, time, c(RESET), action, detail });342}343344// --- Edit ---345346pub fn editOpen(path: []const u8, editor: []const u8) void {347    p("{s}opening {s}\n", .{ hdr(), path });348    printSubstep("{s}", .{editor});349    p("\n   {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });350}351352// --- Fmt ---353354pub fn fmtStart(path: []const u8) void {355    p("{s}formatting  {s}\n", .{ hdr(), path });356}357358pub fn fmtDone() void {359    p("{s}done\n", .{hdr()});360}361362pub fn fmtNeedsFormat() void {363    p("{s}needs formatting  {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });364}365366// --- Repl ---367368pub fn replStart() void {369    p("{s}nix repl  {s}nixpkgs loaded{s}  :q to exit\n", .{ hdr(), c(DIM), c(RESET) });370}371372// --- Develop ---373374pub fn developStart(pkgs: []const []const u8) void {375    p("{s}entering dev shell\n\n   ", .{hdr()});376    for (pkgs, 0..) |pkg, i| {377        if (i > 0) p("  ", .{});378        p("{s}", .{pkg});379    }380    p("\n\n   {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });381}382383pub fn developDone() void {384    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });385}386387// --- Info ---388389pub fn infoHeader(machine: []const u8) void {390    p("{s}{s}\n\n", .{ hdr(), machine });391}392393pub fn infoRow(key: []const u8, val: []const u8) void {394    p("   {s:<12}  {s}\n", .{ key, val });395}396397// --- Boot ---398399pub fn bootHeader(machine: []const u8) void {400    p("{s}boot entries  {s}\n\n", .{ hdr(), machine });401}402403pub fn bootEntry(entry: types.BootEntry, idx: usize) void {404    if (entry.current) {405        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) });406    } else {407        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) });408    }409    _ = idx;410}411412// --- Service ---413414pub fn serviceListHeader(machine: []const u8) void {415    p("{s}services  {s}\n\n", .{ hdr(), machine });416}417418pub fn serviceRow(svc: types.ServiceInfo) void {419    const state_str = switch (svc.state) {420        .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",421        .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",422        .inactive => "inactive",423        .unknown => "unknown",424    };425    const uptime = svc.uptime orelse "";426    p("   {s:<24}  {s:<14}  {s}\n", .{ svc.name, state_str, uptime });427}428429pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {430    p("{s}{s} {s}  {s}\n", .{ hdr(), verb, name, machine });431}432433pub fn serviceDone() void {434    p("{s}done\n", .{hdr()});435}436437pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {438    p("{s}{s}  last {d} lines  {s}\n\n", .{ hdr(), name, n, machine });439}440441// --- Store ---442443pub fn storeInfo(info: types.StoreInfo) void {444    p("{s}store\n\n", .{hdr()});445    p("   {s:<16}  {s}\n", .{ "total", info.total_size });446    p("   {s:<16}  {d} paths\n", .{ "live", info.live_paths });447    p("   {s:<16}  {s}{d} paths{s}   {s}run nina clean{s}\n", .{ "reclaimable", c(DIM), info.reclaimable_paths, c(RESET), c(CYAN), c(RESET) });448}449450// --- Channel ---451452pub fn channelListHeader(machine: []const u8) void {453    p("{s}channels  {s}\n\n", .{ hdr(), machine });454}455456pub fn channelRow(ch: types.ChannelInfo) void {457    p("   {s:<16}  {s}\n", .{ ch.name, ch.url });458}459460// --- Flake ---461462pub fn flakeUpdateStart() void {463    p("{s}updating inputs\n\n", .{hdr()});464}465466pub fn flakeInput(input: types.FlakeInput) void {467    if (input.changed) {468        const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";469        const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";470        p("   {s:<20}  {s} -> {s}\n", .{ input.name, old, new });471    } else {472        p("   {s:<20}  {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });473    }474}475476pub fn flakeLockWritten() void {477    p("\n{s}flake.lock written\n", .{hdr()});478}479480pub fn flakeValid() void {481    p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });482}483484pub fn flakeCheckStart() void {485    p("{s}checking flake\n", .{hdr()});486}487488// --- Profile ---489490pub fn profileListHeader() void {491    p("{s}profile packages\n\n", .{hdr()});492}493494pub fn profileRow(attr: []const u8, pkg_version: []const u8, description: []const u8) void {495    p("   {s:<24}  {s:<12}  {s}\n", .{ attr, pkg_version, description });496}497498// --- Install / Remove / Try ---499500pub fn installStart(pkg: []const u8, path: types.InstallPath) void {501    const path_str = switch (path) {502        .profile => "profile",503        .system => "system",504    };505    p("{s}installing {s}{s}{s}  {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });506}507508pub fn installDone(pkg: []const u8) void {509    p("{s}{s}{s}{s} installed  {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });510}511512pub fn removeStart(pkg: []const u8) void {513    p("{s}removing {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });514}515516// Plain-language health summary. No kaomoji, no hearts — just the facts.517pub fn mood(machine: []const u8, state: []const u8, gen: u32, age: []const u8, size: []const u8) void {518    const healthy = std.mem.eql(u8, state, "running");519    const verdict = if (healthy) "all good" else state;520    const color = if (healthy) GREEN else YELLOW;521    const face = if (healthy) KAO_HAPPY else "";522    p("{s}{s}{s}  {s}{s}{s}\n", .{ face, hdr(), machine, c(color), verdict, c(RESET) });523    if (age.len > 0 or size.len > 0) {524        p("   generation {d}, last applied {s}, {s} system closure\n", .{ gen, age, size });525    } else {526        p("   generation {d}\n", .{gen});527    }528}529530pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {531    p("{s}opening {s}  add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });532    const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;533    printSubstep("{s}", .{cmd});534    p("\n   {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });535}536537pub fn installSelectPrompt(pkg: []const u8, pkg_version: []const u8) void {538    p("{s}{s}{s}{s}  {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), pkg_version });539    p("   [i] profile install    {s}instant  no rebuild{s}\n", .{ c(DIM), c(RESET) });540    p("   [s] system install     {s}opens editor  requires apply{s}\n", .{ c(DIM), c(RESET) });541    p("   [t] try now            {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });542    p("   > ", .{});543    flush();544}545546pub fn tryStart(pkg: []const u8) void {547    p("{s}trying {s}{s}{s}  exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });548}549550pub fn tryDone() void {551    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });552}553554// --- List ---555556pub fn listHeader(machine: []const u8) void {557    p("{s}packages  {s}\n\n", .{ hdr(), machine });558}559560pub fn listSection(title: []const u8) void {561    p("   {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });562}563564// Padded name so the version column lines up into a clean two-column table.565pub fn listPkg(name: []const u8, pkg_version: []const u8) void {566    p("   {s}{s:<16}{s}  {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), pkg_version, c(RESET) });567}568569pub fn blankLine() void {570    p("\n", .{});571}572573// --- Hello ---574575pub fn hello(machines: []const types.Machine) void {576    p("{s}{s}nina\n\n", .{ KAO_FACE, hdr() });577    for (machines) |m| {578        const kind = if (m.local) "local" else "ssh";579        const detail = if (m.local) "default" else m.host orelse "";580        p("   {s}{s}{s}   {s}   {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });581    }582    p("\n", .{});583    const quick = [_][2][]const u8{584        .{ "apply",         "rebuild and switch" },585        .{ "search <q>",    "find packages" },586        .{ "install <pkg>", "add to profile or system" },587        .{ "status",        "machine health" },588        .{ "back",          "roll back one generation" },589        .{ "update",        "refresh channels" },590    };591    for (quick) |cmd| {592        p("   {s:<16}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });593    }594    p("\n", .{});595}596597// --- Help ---598599pub fn help() void {600    p("{s}nina  {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });601    const cmds = [_][2][]const u8{602        .{ "apply", "rebuild and switch" },603        .{ "back", "roll back one generation" },604        .{ "go <n>", "switch to generation n" },605        .{ "history", "list all generations" },606        .{ "clean", "remove old generations" },607        .{ "search <q>", "find packages" },608        .{ "install <pkg>", "add to profile or system" },609        .{ "remove <pkg>", "remove from profile or system" },610        .{ "try <pkg>", "test without installing" },611        .{ "list", "show installed packages" },612        .{ "edit", "open configuration.nix" },613        .{ "check", "validate config" },614        .{ "diff", "changes between generations" },615        .{ "status", "machine health" },616        .{ "update", "refresh channels" },617        .{ "upgrade", "update and rebuild" },618        .{ "log", "operation history" },619        .{ "doctor", "diagnose issues" },620        .{ "mood", "plain-language health" },621        .{ "develop", "enter dev shell" },622        .{ "flake", "flake management" },623        .{ "build", "build a package" },624        .{ "run", "run without installing" },625        .{ "service", "manage services" },626        .{ "store", "nix store tools" },627        .{ "channel", "manage channels" },628        .{ "profile", "user profile packages" },629        .{ "info", "system information" },630        .{ "boot", "boot entries" },631        .{ "fmt", "format configuration.nix" },632        .{ "repl", "nix repl with nixpkgs" },633        .{ "option <q>", "search nixos options" },634        .{ "pkg", "inspect packages" },635        .{ "pin", "pin flake inputs" },636        .{ "hash", "compute nix hash" },637        .{ "fetch", "fetch and hash a url" },638        .{ "goodbye",       "uninstall nina" },639        .{ "hello",        "list machines" },640        .{ "help",         "this message" },641    };642    for (cmds) |cmd| {643        p("   {s:<18}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });644    }645    p("\n   {s}flags: --on <machine>  --dry  --check  --no-apply  --last <n>  --all  --json{s}\n", .{ c(DIM), c(RESET) });646}647648pub fn version() void {649    p("{s}nina  v{s}  zig 0.16  zero dependencies\n", .{ hdr(), nina_version.VERSION });650}651652// --- Search (text output before TUI) ---653654pub fn searchResult(pkg: types.NixPackage) void {655    p("   {s}{s}{s}   {s}{s}{s}   {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });656}657658pub fn searchHeader(query: []const u8, count: usize) void {659    p("{s}search nixpkgs {s}  {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });660}661662// --- Gen commands ---663664pub fn genCurrent(n: u32) void {665    p("{d}\n", .{n});666}667668pub fn done() void {669    p("{s}done\n", .{hdr()});670}671672pub fn runStart(pkg: []const u8) void {673    p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });674}675676pub fn buildStart(attr: []const u8) void {677    p("{s}building {s}\n", .{ hdr(), attr });678}679680pub fn buildDone() void {681    p("{s}done\n", .{hdr()});682}683684// --- Self-update (nina update) ---685686pub fn updateSelfStart() void {687    p("{s}checking for updates\n", .{hdr()});688}689690pub fn updateSelfCurrent(ver: []const u8) void {691    p("{s}{s}nina {s} is up to date{s}\n", .{ hdr(), c(GREEN), ver, c(RESET) });692}693694pub fn updateSelfAvailable(current: []const u8, latest: []const u8) void {695    p("{s}current  {s}\n", .{ step(), current });696    p("{s}latest   {s}{s}{s}\n", .{ step(), c(BOLD), latest, c(RESET) });697}698699pub fn updateSelfDownloading(ver: []const u8, platform: []const u8) void {700    p("{s}downloading nina {s} for {s}\n", .{ step(), ver, platform });701}702703pub fn updateSelfVerifying() void {704    p("{s}verifying checksum\n", .{step()});705}706707pub fn updateSelfInstalling(install_path: []const u8) void {708    p("{s}installing to {s}\n", .{ step(), install_path });709}710711pub fn updateSelfDone(old_ver: []const u8, new_ver: []const u8) void {712    p("{s}updated  {s} {s}→{s} {s}\n", .{ hdr(), old_ver, c(DIM), c(RESET), new_ver });713}714715pub fn updateSelfNoPlatform(platform: []const u8) void {716    p("{s}no pre-built binary for {s}\n", .{ hdr(), platform });717    p("{s}check kepr.uk/nina/releases for available platforms\n", .{step()});718}719720pub fn updateSelfNetworkError() void {721    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, "could not reach kepr.uk — check your connection" });722}723724pub fn updateSelfChecksumFailed() void {725    p("{s}checksum mismatch — download may be corrupt\n", .{if (color_enabled) ERR_C else ERR_P});726    p("{s}try again or download manually from kepr.uk/nina/releases\n", .{step()});727}728729pub fn updateSelfNixManaged(new_ver: []const u8) void {730    p("{s}upgrading to {s}{s}{s} via nix profile\n", .{ step(), c(BOLD), new_ver, c(RESET) });731}732733pub fn updateSelfNixFailed() void {734    p("{s}nix profile upgrade failed\n", .{if (color_enabled) ERR_C else ERR_P});735    p("{s}try manually: nix profile upgrade '.*nina.*'\n", .{step()});736}737738pub fn updateSelfNixSystemManaged() void {739    p("{s}nina appears to be managed by your NixOS system configuration\n", .{hdr()});740    p("{s}to update: sudo nixos-rebuild switch\n", .{step()});741    p("{s}to reinstall as a profile package: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{step()});742}743744// --- Goodbye / uninstall ---745746pub fn goodbyeStart() void {747    p("{s}uninstalling nina\n", .{hdr()});748}749750pub fn goodbyeFile(path: []const u8) void {751    p("{s}will remove {s}\n", .{ step(), path });752}753754pub fn goodbyeNix() void {755    p("{s}will remove nina from nix profile\n", .{step()});756    p("{s}command: nix profile remove '.*nina.*'\n", .{step()});757}758759pub fn goodbyeNixSystemManaged() void {760    p("{s}nina is managed by your NixOS system configuration\n", .{hdr()});761    p("{s}to remove: delete ~/nina.nix, then run sudo nixos-rebuild switch\n", .{step()});762    p("{s}or remove the nina import from /etc/nixos/configuration.nix directly\n", .{step()});763}764765pub fn goodbyeNixFileFound(path: []const u8) void {766    p("{s}found {s} (NixOS config snippet from the installer)\n", .{ step(), path });767}768769pub fn goodbyeDone() void {770    p("{s}goodbye! reinstall anytime: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{hdr()});771}772773pub fn goodbyeFailed(msg: []const u8) void {774    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, msg });775}776777// --- Setup ---778779pub fn setupCheck() void {780    p("{s}checking nix features\n", .{hdr()});781}782783pub fn setupFeatures(nix_command: bool, flakes: bool) void {784    p("{s}nix-command  {s}{s}{s}\n", .{785        step(),786        if (nix_command) c(GREEN) else "",787        if (nix_command) "enabled" else "not enabled",788        if (nix_command) c(RESET) else "",789    });790    p("{s}flakes       {s}{s}{s}\n", .{791        step(),792        if (flakes) c(GREEN) else "",793        if (flakes) "enabled" else "not enabled",794        if (flakes) c(RESET) else "",795    });796}797798pub fn setupAllSet() void {799    p("{s}all set\n", .{hdr()});800}801802// Printed once, before the first search, when the nixpkgs registry still points803// at a remote URL. Flush so it lands before `nix registry pin` and any results.804pub fn pinningRegistry() void {805    p("{s}pinning nixpkgs for fast local search...\n", .{hdr()});806    flush();807}808809// --- Plain (non-tty) search rendering ---810// Color-gated equivalents of the raw-ANSI TUI widget, for piped/scripted search.811// Using the gated header + c() keeps ANSI out of non-tty output (NO_COLOR / pipes).812813pub fn searchPlainHeader(mode_str: []const u8, query: []const u8, count: u64) void {814    p("{s}search {s} {s}  {s}{d} results{s}\n", .{ hdr(), mode_str, query, c(DIM), count, c(RESET) });815}816817pub fn searchPlainNoResults() void {818    p("   no results\n", .{});819}820821pub fn searchPlainPackage(pname: []const u8, pkg_version: []const u8, description: []const u8, unfree: bool) void {822    if (unfree) {823        p("   {s}   {s}{s}{s}   {s}unfree{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), c(YELLOW), c(RESET), description });824    } else {825        p("   {s}   {s}{s}{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), description });826    }827}828829pub fn searchPlainOption(name: []const u8, type_str: []const u8) void {830    p("   {s}   {s}{s}{s}\n", .{ name, c(DIM), type_str, c(RESET) });831}832833pub fn searchEmptyQuery() void {834    p("{s}search needs a query  {s}e.g. nina search firefox{s}\n", .{ hdr(), c(DIM), c(RESET) });835}836837pub fn setupEnabling() void {838    p("{s}enabling experimental features\n", .{hdr()});839}840841pub fn setupDone(path: []const u8, is_nixos: bool) void {842    p("{s}wrote {s}\n", .{ step(), path });843    p("{s}done  open a new shell to pick up the change\n", .{hdr()});844    if (is_nixos) {845        printSubstep("to persist across nixos-rebuild, add to configuration.nix:", .{});846        printSubstep("  nix.settings.experimental-features = [ \"nix-command\" \"flakes\" ];", .{});847    }848}849850pub fn pinDone(input: []const u8, commit: []const u8) void {851    p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });852}853854pub fn unpinDone(input: []const u8) void {855    p("{s}unpinned {s}\n", .{ hdr(), input });856}857858pub fn hashResult(hash: []const u8) void {859    p("{s}\n", .{hash});860}861862pub fn fetchResult(hash: []const u8, url: []const u8) void {863    _ = url;864    p("{s}\n", .{hash});865}866867pub fn storeRepairStart() void {868    p("{s}repairing store\n", .{hdr()});869}870871pub fn storeVerifyStart() void {872    p("{s}verifying store\n", .{hdr()});873}874875pub fn storeGcDone(freed: []const u8) void {876    p("{s}freed {s}\n", .{ hdr(), freed });877}878879pub fn storePath(path: []const u8) void {880    p("{s}\n", .{path});881}882883pub fn pkgResult(key: []const u8, val: []const u8) void {884    p("   {s:<16}  {s}\n", .{ key, val });885}886887pub fn channelDone() void {888    p("{s}done\n", .{hdr()});889}890891pub fn flakeShowHeader(name: []const u8) void {892    p("{s}{s}\n\n", .{ hdr(), name });893}894895pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {896    p("   {s:<12}  {s:<24}  {s}\n", .{ kind, name, desc });897}