output.zig

29.0 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// `back` has nothing older to roll back to. This is a clean stop, not a build227// failure — the rollback would otherwise surface nixos-rebuild's confusing228// 'no profile version older than the current' as a fake build error.229pub fn alreadyOldest() void {230    p("{s}already at the oldest generation\n", .{if (color_enabled) ERR_C else ERR_P});231}232233// Like generationDone but for `back` specifically — a successful rollback earns234// the gentle kaomoji. `go` keeps the plain generationDone line.235pub fn backDone(n: u32) void {236    p("{s}{s}done  gen {s}{d}{s}\n", .{ KAO_GENTLE, hdr(), c(BOLD), n, c(RESET) });237}238239// --- History ---240241pub fn historyHeader(machine: []const u8) void {242    p("{s}generations  {s}\n\n", .{ hdr(), machine });243}244245pub fn historyRow(gen: types.GenerationInfo) void {246    if (gen.current) {247        p("   {s}{d}{s}   {s}   {s}   {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });248    } else {249        p("   {s}{d}   {s}   {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });250    }251}252253// --- Clean ---254255pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {256    p("{s}cleaning  {s}  keeping {d} of {d}\n", .{ hdr(), machine, keep, total });257}258259pub fn cleanDone(freed: []const u8) void {260    p("{s}{s}freed {s}\n", .{ KAO_CONTENT, hdr(), freed });261}262263// --- Status ---264265pub fn statusHeader() void {266    p("{s}status\n\n", .{hdr()});267}268269pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {270    if (ok) {271        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) });272    } else {273        p("   {s}{s}   {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });274    }275}276277// --- Diff ---278279pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {280    p("{s}gen {d} -> gen {d}  {s}\n\n", .{ hdr(), a, b, machine });281}282283pub fn diffRow(entry: types.DiffEntry) void {284    switch (entry.op) {285        .add => p("   {s}+{s} {s}   {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),286        .remove => p("   {s}-{s} {s}   {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),287        .change => p("   {s}^{s} {s}   {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),288    }289}290291// --- Doctor ---292293pub fn doctorHeader(machine: []const u8) void {294    p("{s}diagnosing  {s}\n\n", .{ hdr(), machine });295}296297pub fn doctorRow(check: types.DoctorCheck) void {298    const status_str = switch (check.status) {299        .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",300        .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",301        .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",302    };303    const note_str = check.note orelse "";304    p("   {s:<16}  {s}   {s}\n", .{ check.name, status_str, note_str });305}306307pub fn doctorSummary(warnings: u32, failures: u32) void {308    if (failures > 0) {309        p("{s}{d} problems\n", .{ hdr(), failures });310    } else if (warnings > 0) {311        p("{s}{d} warnings\n", .{ hdr(), warnings });312    } else {313        p("{s}{s}{s}all good{s}\n", .{ KAO_HAPPY, hdr(), c(GREEN), c(RESET) });314    }315}316317// --- Update ---318319pub fn updateStart(machine: []const u8) void {320    p("{s}updating channels  {s}\n\n", .{ hdr(), machine });321}322323pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {324    if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {325        p("   {s}   {s} -> {s}\n", .{ name, old.?, new.? });326    } else {327        p("   {s}   {s}no change{s}\n", .{ name, c(DIM), c(RESET) });328    }329}330331pub fn updateDone() void {332    p("\n{s}done\n", .{hdr()});333}334335// --- Upgrade ---336337pub fn upgradeStart(machine: []const u8) void {338    p("{s}upgrading  {s}\n", .{ hdr(), machine });339}340341// --- Log ---342343pub fn logHeader(machine: []const u8) void {344    p("{s}history  {s}\n\n", .{ hdr(), machine });345}346347pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {348    p("   {s}{s}  {s}{s}   {s:<12}  {s}\n", .{ c(DIM), date, time, c(RESET), action, detail });349}350351// --- Edit ---352353pub fn editOpen(path: []const u8, editor: []const u8) void {354    p("{s}opening {s}\n", .{ hdr(), path });355    printSubstep("{s}", .{editor});356    p("\n   {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });357}358359// --- Fmt ---360361pub fn fmtStart(path: []const u8) void {362    p("{s}formatting  {s}\n", .{ hdr(), path });363}364365pub fn fmtDone() void {366    p("{s}done\n", .{hdr()});367}368369pub fn fmtNeedsFormat() void {370    p("{s}needs formatting  {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });371}372373// --- Repl ---374375pub fn replStart() void {376    p("{s}nix repl  {s}nixpkgs loaded{s}  :q to exit\n", .{ hdr(), c(DIM), c(RESET) });377}378379// --- Develop ---380381pub fn developStart(pkgs: []const []const u8) void {382    p("{s}entering dev shell\n\n   ", .{hdr()});383    for (pkgs, 0..) |pkg, i| {384        if (i > 0) p("  ", .{});385        p("{s}", .{pkg});386    }387    p("\n\n   {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });388}389390pub fn developDone() void {391    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });392}393394// --- Info ---395396pub fn infoHeader(machine: []const u8) void {397    p("{s}{s}\n\n", .{ hdr(), machine });398}399400pub fn infoRow(key: []const u8, val: []const u8) void {401    p("   {s:<12}  {s}\n", .{ key, val });402}403404// --- Boot ---405406pub fn bootHeader(machine: []const u8) void {407    p("{s}boot entries  {s}\n\n", .{ hdr(), machine });408}409410pub fn bootEntry(entry: types.BootEntry, idx: usize) void {411    if (entry.current) {412        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) });413    } else {414        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) });415    }416    _ = idx;417}418419// --- Service ---420421pub fn serviceListHeader(machine: []const u8) void {422    p("{s}services  {s}\n\n", .{ hdr(), machine });423}424425pub fn serviceRow(svc: types.ServiceInfo) void {426    const state_str = switch (svc.state) {427        .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",428        .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",429        .inactive => "inactive",430        .unknown => "unknown",431    };432    const uptime = svc.uptime orelse "";433    p("   {s:<24}  {s:<14}  {s}\n", .{ svc.name, state_str, uptime });434}435436pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {437    p("{s}{s} {s}  {s}\n", .{ hdr(), verb, name, machine });438}439440pub fn serviceDone() void {441    p("{s}done\n", .{hdr()});442}443444pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {445    p("{s}{s}  last {d} lines  {s}\n\n", .{ hdr(), name, n, machine });446}447448// --- Store ---449450pub fn storeInfo(info: types.StoreInfo) void {451    p("{s}store\n\n", .{hdr()});452    p("   {s:<16}  {s}\n", .{ "total", info.total_size });453    p("   {s:<16}  {d} paths\n", .{ "live", info.live_paths });454    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) });455}456457// --- Channel ---458459pub fn channelListHeader(machine: []const u8) void {460    p("{s}channels  {s}\n\n", .{ hdr(), machine });461}462463pub fn channelRow(ch: types.ChannelInfo) void {464    p("   {s:<16}  {s}\n", .{ ch.name, ch.url });465}466467// --- Flake ---468469pub fn flakeUpdateStart() void {470    p("{s}updating inputs\n\n", .{hdr()});471}472473pub fn flakeInput(input: types.FlakeInput) void {474    if (input.changed) {475        const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";476        const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";477        p("   {s:<20}  {s} -> {s}\n", .{ input.name, old, new });478    } else {479        p("   {s:<20}  {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });480    }481}482483pub fn flakeLockWritten() void {484    p("\n{s}flake.lock written\n", .{hdr()});485}486487pub fn flakeValid() void {488    p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });489}490491pub fn flakeCheckStart() void {492    p("{s}checking flake\n", .{hdr()});493}494495// --- Profile ---496497pub fn profileListHeader() void {498    p("{s}profile packages\n\n", .{hdr()});499}500501pub fn profileRow(attr: []const u8, pkg_version: []const u8, description: []const u8) void {502    p("   {s:<24}  {s:<12}  {s}\n", .{ attr, pkg_version, description });503}504505// --- Install / Remove / Try ---506507pub fn installStart(pkg: []const u8, path: types.InstallPath) void {508    const path_str = switch (path) {509        .profile => "profile",510        .system => "system",511    };512    p("{s}installing {s}{s}{s}  {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });513}514515pub fn installDone(pkg: []const u8) void {516    p("{s}{s}{s}{s} installed  {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });517}518519pub fn removeStart(pkg: []const u8) void {520    p("{s}removing {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });521}522523// Plain-language health summary. No kaomoji, no hearts — just the facts.524pub fn mood(machine: []const u8, state: []const u8, gen: u32, age: []const u8, size: []const u8) void {525    const healthy = std.mem.eql(u8, state, "running");526    const verdict = if (healthy) "all good" else state;527    const color = if (healthy) GREEN else YELLOW;528    const face = if (healthy) KAO_HAPPY else "";529    p("{s}{s}{s}  {s}{s}{s}\n", .{ face, hdr(), machine, c(color), verdict, c(RESET) });530    if (age.len > 0 or size.len > 0) {531        p("   generation {d}, last applied {s}, {s} system closure\n", .{ gen, age, size });532    } else {533        p("   generation {d}\n", .{gen});534    }535}536537pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {538    p("{s}opening {s}  add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });539    const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;540    printSubstep("{s}", .{cmd});541    p("\n   {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });542}543544pub fn installSelectPrompt(pkg: []const u8, pkg_version: []const u8) void {545    p("{s}{s}{s}{s}  {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), pkg_version });546    p("   [i] profile install    {s}instant  no rebuild{s}\n", .{ c(DIM), c(RESET) });547    p("   [s] system install     {s}opens editor  requires apply{s}\n", .{ c(DIM), c(RESET) });548    p("   [t] try now            {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });549    p("   > ", .{});550    flush();551}552553pub fn tryStart(pkg: []const u8) void {554    p("{s}trying {s}{s}{s}  exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });555}556557pub fn tryDone() void {558    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });559}560561// --- List ---562563pub fn listHeader(machine: []const u8) void {564    p("{s}packages  {s}\n\n", .{ hdr(), machine });565}566567pub fn listSection(title: []const u8) void {568    p("   {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });569}570571// Padded name so the version column lines up into a clean two-column table.572pub fn listPkg(name: []const u8, pkg_version: []const u8) void {573    p("   {s}{s:<16}{s}  {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), pkg_version, c(RESET) });574}575576pub fn blankLine() void {577    p("\n", .{});578}579580// --- Hello ---581582pub fn hello(machines: []const types.Machine) void {583    p("{s}{s}nina\n\n", .{ KAO_FACE, hdr() });584    for (machines) |m| {585        const kind = if (m.local) "local" else "ssh";586        const detail = if (m.local) "default" else m.host orelse "";587        p("   {s}{s}{s}   {s}   {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });588    }589    p("\n", .{});590    const quick = [_][2][]const u8{591        .{ "apply",         "rebuild and switch" },592        .{ "search <q>",    "find packages" },593        .{ "install <pkg>", "add to profile or system" },594        .{ "status",        "machine health" },595        .{ "back",          "roll back one generation" },596        .{ "update",        "refresh channels" },597    };598    for (quick) |cmd| {599        p("   {s:<16}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });600    }601    p("\n", .{});602}603604// --- Help ---605606pub fn help() void {607    p("{s}nina  {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });608    const cmds = [_][2][]const u8{609        .{ "apply", "rebuild and switch" },610        .{ "back", "roll back one generation" },611        .{ "go <n>", "switch to generation n" },612        .{ "history", "list all generations" },613        .{ "clean", "remove old generations" },614        .{ "search <q>", "find packages" },615        .{ "install <pkg>", "add to profile or system" },616        .{ "remove <pkg>", "remove from profile or system" },617        .{ "try <pkg>", "test without installing" },618        .{ "list", "show installed packages" },619        .{ "edit", "open configuration.nix" },620        .{ "check", "validate config" },621        .{ "diff", "changes between generations" },622        .{ "status", "machine health" },623        .{ "update", "refresh channels" },624        .{ "upgrade", "update and rebuild" },625        .{ "log", "operation history" },626        .{ "doctor", "diagnose issues" },627        .{ "mood", "plain-language health" },628        .{ "develop", "enter dev shell" },629        .{ "flake", "flake management" },630        .{ "build", "build a package" },631        .{ "run", "run without installing" },632        .{ "service", "manage services" },633        .{ "store", "nix store tools" },634        .{ "channel", "manage channels" },635        .{ "profile", "user profile packages" },636        .{ "info", "system information" },637        .{ "boot", "boot entries" },638        .{ "fmt", "format configuration.nix" },639        .{ "repl", "nix repl with nixpkgs" },640        .{ "option <q>", "search nixos options" },641        .{ "pkg", "inspect packages" },642        .{ "pin", "pin flake inputs" },643        .{ "hash", "compute nix hash" },644        .{ "fetch", "fetch and hash a url" },645        .{ "goodbye",       "uninstall nina" },646        .{ "hello",        "list machines" },647        .{ "help",         "this message" },648    };649    for (cmds) |cmd| {650        p("   {s:<18}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });651    }652    p("\n   {s}flags: --on <machine>  --dry  --check  --no-apply  --last <n>  --all  --json{s}\n", .{ c(DIM), c(RESET) });653}654655pub fn version() void {656    p("{s}nina  v{s}  zig 0.16  zero dependencies\n", .{ hdr(), nina_version.VERSION });657}658659// --- Search (text output before TUI) ---660661pub fn searchResult(pkg: types.NixPackage) void {662    p("   {s}{s}{s}   {s}{s}{s}   {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });663}664665pub fn searchHeader(query: []const u8, count: usize) void {666    p("{s}search nixpkgs {s}  {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });667}668669// --- Gen commands ---670671pub fn genCurrent(n: u32) void {672    p("{d}\n", .{n});673}674675pub fn done() void {676    p("{s}done\n", .{hdr()});677}678679pub fn runStart(pkg: []const u8) void {680    p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });681}682683pub fn buildStart(attr: []const u8) void {684    p("{s}building {s}\n", .{ hdr(), attr });685}686687pub fn buildDone() void {688    p("{s}done\n", .{hdr()});689}690691// --- Self-update (nina update) ---692693pub fn updateSelfStart() void {694    p("{s}checking for updates\n", .{hdr()});695}696697pub fn updateSelfCurrent(ver: []const u8) void {698    p("{s}{s}nina {s} is up to date{s}\n", .{ hdr(), c(GREEN), ver, c(RESET) });699}700701pub fn updateSelfAvailable(current: []const u8, latest: []const u8) void {702    p("{s}current  {s}\n", .{ step(), current });703    p("{s}latest   {s}{s}{s}\n", .{ step(), c(BOLD), latest, c(RESET) });704}705706pub fn updateSelfDownloading(ver: []const u8, platform: []const u8) void {707    p("{s}downloading nina {s} for {s}\n", .{ step(), ver, platform });708}709710pub fn updateSelfVerifying() void {711    p("{s}verifying checksum\n", .{step()});712}713714pub fn updateSelfInstalling(install_path: []const u8) void {715    p("{s}installing to {s}\n", .{ step(), install_path });716}717718pub fn updateSelfDone(old_ver: []const u8, new_ver: []const u8) void {719    p("{s}updated  {s} {s}→{s} {s}\n", .{ hdr(), old_ver, c(DIM), c(RESET), new_ver });720}721722pub fn updateSelfNoPlatform(platform: []const u8) void {723    p("{s}no pre-built binary for {s}\n", .{ hdr(), platform });724    p("{s}check kepr.uk/nina/releases for available platforms\n", .{step()});725}726727pub fn updateSelfNetworkError() void {728    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, "could not reach kepr.uk — check your connection" });729}730731pub fn updateSelfChecksumFailed() void {732    p("{s}checksum mismatch — download may be corrupt\n", .{if (color_enabled) ERR_C else ERR_P});733    p("{s}try again or download manually from kepr.uk/nina/releases\n", .{step()});734}735736pub fn updateSelfNixManaged(new_ver: []const u8) void {737    p("{s}upgrading to {s}{s}{s} via nix profile\n", .{ step(), c(BOLD), new_ver, c(RESET) });738}739740pub fn updateSelfNixFailed() void {741    p("{s}nix profile upgrade failed\n", .{if (color_enabled) ERR_C else ERR_P});742    p("{s}try manually: nix profile upgrade '.*nina.*'\n", .{step()});743}744745pub fn updateSelfNixSystemManaged() void {746    p("{s}nina appears to be managed by your NixOS system configuration\n", .{hdr()});747    p("{s}to update: sudo nixos-rebuild switch\n", .{step()});748    p("{s}to reinstall as a profile package: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{step()});749}750751// --- Goodbye / uninstall ---752753pub fn goodbyeStart() void {754    p("{s}uninstalling nina\n", .{hdr()});755}756757pub fn goodbyeFile(path: []const u8) void {758    p("{s}will remove {s}\n", .{ step(), path });759}760761pub fn goodbyeNix() void {762    p("{s}will remove nina from nix profile\n", .{step()});763    p("{s}command: nix profile remove '.*nina.*'\n", .{step()});764}765766pub fn goodbyeNixSystemManaged() void {767    p("{s}nina is managed by your NixOS system configuration\n", .{hdr()});768    p("{s}to remove: delete ~/nina.nix, then run sudo nixos-rebuild switch\n", .{step()});769    p("{s}or remove the nina import from /etc/nixos/configuration.nix directly\n", .{step()});770}771772pub fn goodbyeNixFileFound(path: []const u8) void {773    p("{s}found {s} (NixOS config snippet from the installer)\n", .{ step(), path });774}775776pub fn goodbyeDone() void {777    p("{s}goodbye! reinstall anytime: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{hdr()});778}779780pub fn goodbyeFailed(msg: []const u8) void {781    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, msg });782}783784// --- Setup ---785786pub fn setupCheck() void {787    p("{s}checking nix features\n", .{hdr()});788}789790pub fn setupFeatures(nix_command: bool, flakes: bool) void {791    p("{s}nix-command  {s}{s}{s}\n", .{792        step(),793        if (nix_command) c(GREEN) else "",794        if (nix_command) "enabled" else "not enabled",795        if (nix_command) c(RESET) else "",796    });797    p("{s}flakes       {s}{s}{s}\n", .{798        step(),799        if (flakes) c(GREEN) else "",800        if (flakes) "enabled" else "not enabled",801        if (flakes) c(RESET) else "",802    });803}804805pub fn setupAllSet() void {806    p("{s}all set\n", .{hdr()});807}808809// Printed once, before the first search, when the nixpkgs registry still points810// at a remote URL. Flush so it lands before `nix registry pin` and any results.811pub fn pinningRegistry() void {812    p("{s}pinning nixpkgs for fast local search...\n", .{hdr()});813    flush();814}815816// --- Plain (non-tty) search rendering ---817// Color-gated equivalents of the raw-ANSI TUI widget, for piped/scripted search.818// Using the gated header + c() keeps ANSI out of non-tty output (NO_COLOR / pipes).819820pub fn searchPlainHeader(mode_str: []const u8, query: []const u8, count: u64) void {821    p("{s}search {s} {s}  {s}{d} results{s}\n", .{ hdr(), mode_str, query, c(DIM), count, c(RESET) });822}823824pub fn searchPlainNoResults() void {825    p("   no results\n", .{});826}827828pub fn searchPlainPackage(pname: []const u8, pkg_version: []const u8, description: []const u8, unfree: bool) void {829    if (unfree) {830        p("   {s}   {s}{s}{s}   {s}unfree{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), c(YELLOW), c(RESET), description });831    } else {832        p("   {s}   {s}{s}{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), description });833    }834}835836pub fn searchPlainOption(name: []const u8, type_str: []const u8) void {837    p("   {s}   {s}{s}{s}\n", .{ name, c(DIM), type_str, c(RESET) });838}839840pub fn searchEmptyQuery() void {841    p("{s}search needs a query  {s}e.g. nina search firefox{s}\n", .{ hdr(), c(DIM), c(RESET) });842}843844pub fn setupEnabling() void {845    p("{s}enabling experimental features\n", .{hdr()});846}847848pub fn setupDone(path: []const u8, is_nixos: bool) void {849    p("{s}wrote {s}\n", .{ step(), path });850    p("{s}done  open a new shell to pick up the change\n", .{hdr()});851    if (is_nixos) {852        printSubstep("to persist across nixos-rebuild, add to configuration.nix:", .{});853        printSubstep("  nix.settings.experimental-features = [ \"nix-command\" \"flakes\" ];", .{});854    }855}856857pub fn pinDone(input: []const u8, commit: []const u8) void {858    p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });859}860861pub fn unpinDone(input: []const u8) void {862    p("{s}unpinned {s}\n", .{ hdr(), input });863}864865pub fn hashResult(hash: []const u8) void {866    p("{s}\n", .{hash});867}868869pub fn fetchResult(hash: []const u8, url: []const u8) void {870    _ = url;871    p("{s}\n", .{hash});872}873874pub fn storeRepairStart() void {875    p("{s}repairing store\n", .{hdr()});876}877878pub fn storeVerifyStart() void {879    p("{s}verifying store\n", .{hdr()});880}881882pub fn storeGcDone(freed: []const u8) void {883    p("{s}freed {s}\n", .{ hdr(), freed });884}885886pub fn storePath(path: []const u8) void {887    p("{s}\n", .{path});888}889890pub fn pkgResult(key: []const u8, val: []const u8) void {891    p("   {s:<16}  {s}\n", .{ key, val });892}893894pub fn channelDone() void {895    p("{s}done\n", .{hdr()});896}897898pub fn flakeShowHeader(name: []const u8) void {899    p("{s}{s}\n\n", .{ hdr(), name });900}901902pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {903    p("   {s:<12}  {s:<24}  {s}\n", .{ kind, name, desc });904}