output.zig

29.7 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}258259// Fewer generations than the keep limit — there's nothing to collect. Avoids the260// nonsensical 'keeping 5 of 1'.261pub fn cleanNothing(machine: []const u8, total: u32) void {262    p("{s}cleaning  {s}\n\n", .{ hdr(), machine });263    p("   only {d} generation{s} — nothing to remove.\n", .{ total, if (total == 1) "" else "s" });264}265266pub fn cleanDone(freed: []const u8) void {267    p("{s}{s}freed {s}\n", .{ KAO_CONTENT, hdr(), freed });268}269270// --- Status ---271272pub fn statusHeader() void {273    p("{s}status\n\n", .{hdr()});274}275276pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {277    if (ok) {278        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) });279    } else {280        p("   {s}{s}   {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });281    }282}283284// --- Diff ---285286pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {287    p("{s}gen {d} -> gen {d}  {s}\n\n", .{ hdr(), a, b, machine });288}289290pub fn diffRow(entry: types.DiffEntry) void {291    switch (entry.op) {292        .add => p("   {s}+{s} {s}   {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),293        .remove => p("   {s}-{s} {s}   {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),294        .change => p("   {s}^{s} {s}   {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),295    }296}297298// --- Doctor ---299300pub fn doctorHeader(machine: []const u8) void {301    p("{s}diagnosing  {s}\n\n", .{ hdr(), machine });302}303304pub fn doctorRow(check: types.DoctorCheck) void {305    const status_str = switch (check.status) {306        .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",307        .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",308        .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",309    };310    const note_str = check.note orelse "";311    p("   {s:<16}  {s}   {s}\n", .{ check.name, status_str, note_str });312}313314pub fn doctorSummary(warnings: u32, failures: u32) void {315    if (failures > 0) {316        p("{s}{d} problems\n", .{ hdr(), failures });317    } else if (warnings > 0) {318        p("{s}{d} warnings\n", .{ hdr(), warnings });319    } else {320        p("{s}{s}{s}all good{s}\n", .{ KAO_HAPPY, hdr(), c(GREEN), c(RESET) });321    }322}323324// --- Update ---325326pub fn updateStart(machine: []const u8) void {327    p("{s}updating channels  {s}\n\n", .{ hdr(), machine });328}329330pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {331    if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {332        p("   {s}   {s} -> {s}\n", .{ name, old.?, new.? });333    } else {334        p("   {s}   {s}no change{s}\n", .{ name, c(DIM), c(RESET) });335    }336}337338pub fn updateDone() void {339    p("\n{s}done\n", .{hdr()});340}341342// --- Upgrade ---343344pub fn upgradeStart(machine: []const u8) void {345    p("{s}upgrading  {s}\n", .{ hdr(), machine });346}347348// --- Log ---349350pub fn logHeader(machine: []const u8) void {351    p("{s}history  {s}\n\n", .{ hdr(), machine });352}353354pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {355    p("   {s}{s}  {s}{s}   {s:<12}  {s}\n", .{ c(DIM), date, time, c(RESET), action, detail });356}357358// --- Edit ---359360pub fn editOpen(path: []const u8, editor: []const u8) void {361    p("{s}opening {s}\n", .{ hdr(), path });362    printSubstep("{s}", .{editor});363    p("\n   {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });364}365366// --- Fmt ---367368pub fn fmtStart(path: []const u8) void {369    p("{s}formatting  {s}\n", .{ hdr(), path });370}371372pub fn fmtDone() void {373    p("{s}done\n", .{hdr()});374}375376pub fn fmtNeedsFormat() void {377    p("{s}needs formatting  {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });378}379380// --- Repl ---381382pub fn replStart() void {383    p("{s}nix repl  {s}nixpkgs loaded{s}  :q to exit\n", .{ hdr(), c(DIM), c(RESET) });384}385386// --- Develop ---387388pub fn developStart(pkgs: []const []const u8) void {389    p("{s}entering dev shell\n\n   ", .{hdr()});390    for (pkgs, 0..) |pkg, i| {391        if (i > 0) p("  ", .{});392        p("{s}", .{pkg});393    }394    p("\n\n   {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });395}396397pub fn developDone() void {398    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });399}400401// --- Info ---402403pub fn infoHeader(machine: []const u8) void {404    p("{s}{s}\n\n", .{ hdr(), machine });405}406407pub fn infoRow(key: []const u8, val: []const u8) void {408    p("   {s:<12}  {s}\n", .{ key, val });409}410411// --- Boot ---412413pub fn bootHeader(machine: []const u8) void {414    p("{s}boot entries  {s}\n\n", .{ hdr(), machine });415}416417pub fn bootEntry(entry: types.BootEntry, idx: usize) void {418    if (entry.current) {419        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) });420    } else {421        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) });422    }423    _ = idx;424}425426// --- Service ---427428pub fn serviceListHeader(machine: []const u8) void {429    p("{s}services  {s}\n\n", .{ hdr(), machine });430}431432pub fn serviceRow(svc: types.ServiceInfo) void {433    const state_str = switch (svc.state) {434        .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",435        .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",436        .inactive => "inactive",437        .unknown => "unknown",438    };439    const uptime = svc.uptime orelse "";440    p("   {s:<24}  {s:<14}  {s}\n", .{ svc.name, state_str, uptime });441}442443pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {444    p("{s}{s} {s}  {s}\n", .{ hdr(), verb, name, machine });445}446447pub fn serviceDone() void {448    p("{s}done\n", .{hdr()});449}450451pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {452    p("{s}{s}  last {d} lines  {s}\n\n", .{ hdr(), name, n, machine });453}454455// --- Store ---456457pub fn storeInfo(info: types.StoreInfo) void {458    p("{s}store\n\n", .{hdr()});459    p("   {s:<16}  {s}\n", .{ "total", info.total_size });460    p("   {s:<16}  {d} paths\n", .{ "live", info.live_paths });461    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) });462}463464// --- Channel ---465466pub fn channelListHeader(machine: []const u8) void {467    p("{s}channels  {s}\n\n", .{ hdr(), machine });468}469470pub fn channelRow(ch: types.ChannelInfo) void {471    p("   {s:<16}  {s}\n", .{ ch.name, ch.url });472}473474// --- Flake ---475476pub fn flakeUpdateStart() void {477    p("{s}updating inputs\n\n", .{hdr()});478}479480pub fn flakeInput(input: types.FlakeInput) void {481    if (input.changed) {482        const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";483        const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";484        p("   {s:<20}  {s} -> {s}\n", .{ input.name, old, new });485    } else {486        p("   {s:<20}  {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });487    }488}489490pub fn flakeLockWritten() void {491    p("\n{s}flake.lock written\n", .{hdr()});492}493494pub fn flakeValid() void {495    p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });496}497498pub fn flakeCheckStart() void {499    p("{s}checking flake\n", .{hdr()});500}501502// --- Profile ---503504pub fn profileListHeader() void {505    p("{s}profile packages\n\n", .{hdr()});506}507508pub fn profileRow(attr: []const u8, pkg_version: []const u8, description: []const u8) void {509    p("   {s:<24}  {s:<12}  {s}\n", .{ attr, pkg_version, description });510}511512// --- Install / Remove / Try ---513514// Typo help for install: the query matched nothing, so suggest the closest real515// package name (pink) and let the caller confirm.516pub fn didYouMean(query: []const u8, suggestion: []const u8) void {517    p("{s}{s}'{s}' not found{s}  —  did you mean {s}{s}{s}?\n", .{ hdr(), c(DIM), query, c(RESET), c(PINK), suggestion, c(RESET) });518}519520pub fn installStart(pkg: []const u8, path: types.InstallPath) void {521    const path_str = switch (path) {522        .profile => "profile",523        .system => "system",524    };525    p("{s}installing {s}{s}{s}  {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });526}527528pub fn installDone(pkg: []const u8) void {529    p("{s}{s}{s}{s} installed  {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });530}531532pub fn removeStart(pkg: []const u8) void {533    p("{s}removing {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });534}535536// Plain-language health summary. No kaomoji, no hearts — just the facts.537pub fn mood(machine: []const u8, state: []const u8, gen: u32, age: []const u8, size: []const u8) void {538    const healthy = std.mem.eql(u8, state, "running");539    const verdict = if (healthy) "all good" else state;540    const color = if (healthy) GREEN else YELLOW;541    const face = if (healthy) KAO_HAPPY else "";542    p("{s}{s}{s}  {s}{s}{s}\n", .{ face, hdr(), machine, c(color), verdict, c(RESET) });543    if (age.len > 0 or size.len > 0) {544        p("   generation {d}, last applied {s}, {s} system closure\n", .{ gen, age, size });545    } else {546        p("   generation {d}\n", .{gen});547    }548}549550pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {551    p("{s}opening {s}  add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });552    const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;553    printSubstep("{s}", .{cmd});554    p("\n   {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });555}556557pub fn installSelectPrompt(pkg: []const u8, pkg_version: []const u8) void {558    p("{s}{s}{s}{s}  {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), pkg_version });559    p("   [i] profile install    {s}instant  no rebuild{s}\n", .{ c(DIM), c(RESET) });560    p("   [s] system install     {s}opens editor  requires apply{s}\n", .{ c(DIM), c(RESET) });561    p("   [t] try now            {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });562    p("   > ", .{});563    flush();564}565566pub fn tryStart(pkg: []const u8) void {567    p("{s}trying {s}{s}{s}  exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });568}569570pub fn tryDone() void {571    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });572}573574// --- List ---575576pub fn listHeader(machine: []const u8) void {577    p("{s}packages  {s}\n\n", .{ hdr(), machine });578}579580pub fn listSection(title: []const u8) void {581    p("   {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });582}583584// Padded name so the version column lines up into a clean two-column table.585pub fn listPkg(name: []const u8, pkg_version: []const u8) void {586    p("   {s}{s:<16}{s}  {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), pkg_version, c(RESET) });587}588589pub fn blankLine() void {590    p("\n", .{});591}592593// --- Hello ---594595pub fn hello(machines: []const types.Machine) void {596    p("{s}{s}nina\n\n", .{ KAO_FACE, hdr() });597    for (machines) |m| {598        const kind = if (m.local) "local" else "ssh";599        const detail = if (m.local) "default" else m.host orelse "";600        p("   {s}{s}{s}   {s}   {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });601    }602    p("\n", .{});603    const quick = [_][2][]const u8{604        .{ "apply",         "rebuild and switch" },605        .{ "search <q>",    "find packages" },606        .{ "install <pkg>", "add to profile or system" },607        .{ "status",        "machine health" },608        .{ "back",          "roll back one generation" },609        .{ "update",        "refresh channels" },610    };611    for (quick) |cmd| {612        p("   {s:<16}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });613    }614    p("\n", .{});615}616617// --- Help ---618619pub fn help() void {620    p("{s}nina  {s}NixOS Intuitive Navigation Assistant by Asha Software{s}\n\n", .{ hdr(), c(DIM), c(RESET) });621    const cmds = [_][2][]const u8{622        .{ "apply", "rebuild and switch" },623        .{ "back", "roll back one generation" },624        .{ "go <n>", "switch to generation n" },625        .{ "history", "list all generations" },626        .{ "clean", "remove old generations" },627        .{ "search <q>", "find packages" },628        .{ "install <pkg>", "add to profile or system" },629        .{ "remove <pkg>", "remove from profile or system" },630        .{ "try <pkg>", "test without installing" },631        .{ "list", "show installed packages" },632        .{ "edit", "open configuration.nix" },633        .{ "check", "validate config" },634        .{ "diff", "changes between generations" },635        .{ "status", "machine health" },636        .{ "update", "refresh channels" },637        .{ "upgrade", "update and rebuild" },638        .{ "log", "operation history" },639        .{ "doctor", "diagnose issues" },640        .{ "mood", "plain-language health" },641        .{ "develop", "enter dev shell" },642        .{ "flake", "flake management" },643        .{ "build", "build a package" },644        .{ "run", "run without installing" },645        .{ "service", "manage services" },646        .{ "store", "nix store tools" },647        .{ "channel", "manage channels" },648        .{ "profile", "user profile packages" },649        .{ "info", "system information" },650        .{ "boot", "boot entries" },651        .{ "fmt", "format configuration.nix" },652        .{ "repl", "nix repl with nixpkgs" },653        .{ "option <q>", "search nixos options" },654        .{ "pkg", "inspect packages" },655        .{ "pin", "pin flake inputs" },656        .{ "hash", "compute nix hash" },657        .{ "fetch", "fetch and hash a url" },658        .{ "goodbye",       "uninstall nina" },659        .{ "hello",        "list machines" },660        .{ "help",         "this message" },661    };662    for (cmds) |cmd| {663        p("   {s:<18}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });664    }665    p("\n   {s}flags: --on <machine>  --dry  --check  --no-apply  --last <n>  --all  --json{s}\n", .{ c(DIM), c(RESET) });666}667668pub fn version() void {669    p("{s}nina  v{s}  zig 0.16  zero dependencies\n", .{ hdr(), nina_version.VERSION });670}671672// --- Search (text output before TUI) ---673674pub fn searchResult(pkg: types.NixPackage) void {675    p("   {s}{s}{s}   {s}{s}{s}   {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });676}677678pub fn searchHeader(query: []const u8, count: usize) void {679    p("{s}search nixpkgs {s}  {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });680}681682// --- Gen commands ---683684pub fn genCurrent(n: u32) void {685    p("{d}\n", .{n});686}687688pub fn done() void {689    p("{s}done\n", .{hdr()});690}691692pub fn runStart(pkg: []const u8) void {693    p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });694}695696pub fn buildStart(attr: []const u8) void {697    p("{s}building {s}\n", .{ hdr(), attr });698}699700pub fn buildDone() void {701    p("{s}done\n", .{hdr()});702}703704// --- Self-update (nina update) ---705706pub fn updateSelfStart() void {707    p("{s}checking for updates\n", .{hdr()});708}709710pub fn updateSelfCurrent(ver: []const u8) void {711    p("{s}{s}nina {s} is up to date{s}\n", .{ hdr(), c(GREEN), ver, c(RESET) });712}713714pub fn updateSelfAvailable(current: []const u8, latest: []const u8) void {715    p("{s}current  {s}\n", .{ step(), current });716    p("{s}latest   {s}{s}{s}\n", .{ step(), c(BOLD), latest, c(RESET) });717}718719pub fn updateSelfDownloading(ver: []const u8, platform: []const u8) void {720    p("{s}downloading nina {s} for {s}\n", .{ step(), ver, platform });721}722723pub fn updateSelfVerifying() void {724    p("{s}verifying checksum\n", .{step()});725}726727pub fn updateSelfInstalling(install_path: []const u8) void {728    p("{s}installing to {s}\n", .{ step(), install_path });729}730731pub fn updateSelfDone(old_ver: []const u8, new_ver: []const u8) void {732    p("{s}updated  {s} {s}→{s} {s}\n", .{ hdr(), old_ver, c(DIM), c(RESET), new_ver });733}734735pub fn updateSelfNoPlatform(platform: []const u8) void {736    p("{s}no pre-built binary for {s}\n", .{ hdr(), platform });737    p("{s}check kepr.uk/nina/releases for available platforms\n", .{step()});738}739740pub fn updateSelfNetworkError() void {741    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, "could not reach kepr.uk — check your connection" });742}743744pub fn updateSelfChecksumFailed() void {745    p("{s}checksum mismatch — download may be corrupt\n", .{if (color_enabled) ERR_C else ERR_P});746    p("{s}try again or download manually from kepr.uk/nina/releases\n", .{step()});747}748749pub fn updateSelfNixManaged(new_ver: []const u8) void {750    p("{s}upgrading to {s}{s}{s} via nix profile\n", .{ step(), c(BOLD), new_ver, c(RESET) });751}752753pub fn updateSelfNixFailed() void {754    p("{s}nix profile upgrade failed\n", .{if (color_enabled) ERR_C else ERR_P});755    p("{s}try manually: nix profile upgrade '.*nina.*'\n", .{step()});756}757758pub fn updateSelfNixSystemManaged() void {759    p("{s}nina appears to be managed by your NixOS system configuration\n", .{hdr()});760    p("{s}to update: sudo nixos-rebuild switch\n", .{step()});761    p("{s}to reinstall as a profile package: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{step()});762}763764// --- Goodbye / uninstall ---765766pub fn goodbyeStart() void {767    p("{s}uninstalling nina\n", .{hdr()});768}769770pub fn goodbyeFile(path: []const u8) void {771    p("{s}will remove {s}\n", .{ step(), path });772}773774pub fn goodbyeNix() void {775    p("{s}will remove nina from nix profile\n", .{step()});776    p("{s}command: nix profile remove '.*nina.*'\n", .{step()});777}778779pub fn goodbyeNixSystemManaged() void {780    p("{s}nina is managed by your NixOS system configuration\n", .{hdr()});781    p("{s}to remove: delete ~/nina.nix, then run sudo nixos-rebuild switch\n", .{step()});782    p("{s}or remove the nina import from /etc/nixos/configuration.nix directly\n", .{step()});783}784785pub fn goodbyeNixFileFound(path: []const u8) void {786    p("{s}found {s} (NixOS config snippet from the installer)\n", .{ step(), path });787}788789pub fn goodbyeDone() void {790    p("{s}goodbye! reinstall anytime: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{hdr()});791}792793pub fn goodbyeFailed(msg: []const u8) void {794    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, msg });795}796797// --- Setup ---798799pub fn setupCheck() void {800    p("{s}checking nix features\n", .{hdr()});801}802803pub fn setupFeatures(nix_command: bool, flakes: bool) void {804    p("{s}nix-command  {s}{s}{s}\n", .{805        step(),806        if (nix_command) c(GREEN) else "",807        if (nix_command) "enabled" else "not enabled",808        if (nix_command) c(RESET) else "",809    });810    p("{s}flakes       {s}{s}{s}\n", .{811        step(),812        if (flakes) c(GREEN) else "",813        if (flakes) "enabled" else "not enabled",814        if (flakes) c(RESET) else "",815    });816}817818pub fn setupAllSet() void {819    p("{s}all set\n", .{hdr()});820}821822// Printed once, before the first search, when the nixpkgs registry still points823// at a remote URL. Flush so it lands before `nix registry pin` and any results.824pub fn pinningRegistry() void {825    p("{s}pinning nixpkgs for fast local search...\n", .{hdr()});826    flush();827}828829// --- Plain (non-tty) search rendering ---830// Color-gated equivalents of the raw-ANSI TUI widget, for piped/scripted search.831// Using the gated header + c() keeps ANSI out of non-tty output (NO_COLOR / pipes).832833pub fn searchPlainHeader(mode_str: []const u8, query: []const u8, count: u64) void {834    p("{s}search {s} {s}  {s}{d} results{s}\n", .{ hdr(), mode_str, query, c(DIM), count, c(RESET) });835}836837pub fn searchPlainNoResults() void {838    p("   no results\n", .{});839}840841pub fn searchPlainPackage(pname: []const u8, pkg_version: []const u8, description: []const u8, unfree: bool) void {842    if (unfree) {843        p("   {s}   {s}{s}{s}   {s}unfree{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), c(YELLOW), c(RESET), description });844    } else {845        p("   {s}   {s}{s}{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), description });846    }847}848849pub fn searchPlainOption(name: []const u8, type_str: []const u8) void {850    p("   {s}   {s}{s}{s}\n", .{ name, c(DIM), type_str, c(RESET) });851}852853pub fn searchEmptyQuery() void {854    p("{s}search needs a query  {s}e.g. nina search firefox{s}\n", .{ hdr(), c(DIM), c(RESET) });855}856857pub fn setupEnabling() void {858    p("{s}enabling experimental features\n", .{hdr()});859}860861pub fn setupDone(path: []const u8, is_nixos: bool) void {862    p("{s}wrote {s}\n", .{ step(), path });863    p("{s}done  open a new shell to pick up the change\n", .{hdr()});864    if (is_nixos) {865        printSubstep("to persist across nixos-rebuild, add to configuration.nix:", .{});866        printSubstep("  nix.settings.experimental-features = [ \"nix-command\" \"flakes\" ];", .{});867    }868}869870pub fn pinDone(input: []const u8, commit: []const u8) void {871    p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });872}873874pub fn unpinDone(input: []const u8) void {875    p("{s}unpinned {s}\n", .{ hdr(), input });876}877878pub fn hashResult(hash: []const u8) void {879    p("{s}\n", .{hash});880}881882pub fn fetchResult(hash: []const u8, url: []const u8) void {883    _ = url;884    p("{s}\n", .{hash});885}886887pub fn storeRepairStart() void {888    p("{s}repairing store\n", .{hdr()});889}890891pub fn storeVerifyStart() void {892    p("{s}verifying store\n", .{hdr()});893}894895pub fn storeGcDone(freed: []const u8) void {896    p("{s}freed {s}\n", .{ hdr(), freed });897}898899pub fn storePath(path: []const u8) void {900    p("{s}\n", .{path});901}902903pub fn pkgResult(key: []const u8, val: []const u8) void {904    p("   {s:<16}  {s}\n", .{ key, val });905}906907pub fn channelDone() void {908    p("{s}done\n", .{hdr()});909}910911pub fn flakeShowHeader(name: []const u8) void {912    p("{s}{s}\n\n", .{ hdr(), name });913}914915pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {916    p("   {s:<12}  {s:<24}  {s}\n", .{ kind, name, desc });917}