output.zig

28.3 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}7374fn c(code: []const u8) []const u8 {75    return if (color_enabled) code else "";76}7778fn hdr() []const u8 {79    return if (color_enabled) HDR_C else HDR_P;80}8182fn step() []const u8 {83    return if (color_enabled) STEP_C else STEP_P;84}8586fn teach_pfx() []const u8 {87    return if (color_enabled) TEACH_C else TEACH_P;88}8990// Raw write — used by TUI91pub fn raw(bytes: []const u8) void {92    _w.writeAll(bytes) catch {};93}9495// Formatted print — used by TUI for in-place rendering96pub fn p(comptime fmt: []const u8, args: anytype) void {97    _w.print(fmt, args) catch {};98}99100pub fn cursorUp(n: usize) void {101    if (n > 0) p("\x1b[{d}A", .{n});102}103104pub fn clearLine() void {105    p("\r\x1b[K", .{});106}107108// --- Error and warning output ---109110pub fn printError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {111    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, message });112    if (detail) |d| p("   {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });113    // Actionable suggestion in cyan — the next step should catch the eye.114    if (suggestion) |s| p("   {s}-> {s}{s}\n", .{ c(CYAN), s, c(RESET) });115}116117// Like printError, but for genuine system failures (network/ssh/search). The sad118// kaomoji signals "this is on the machine, not on you" — never used for typos or119// not-found errors, which go through printError.120pub fn printSystemError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {121    p("{s}{s}{s}\n", .{ KAO_SAD, if (color_enabled) ERR_C else ERR_P, message });122    if (detail) |d| p("   {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });123    if (suggestion) |s| p("   {s}-> {s}{s}\n", .{ c(CYAN), s, c(RESET) });124}125126pub fn printWarning(message: []const u8) void {127    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, message });128}129130pub fn printTeach(cmd: []const u8) void {131    if (teach_enabled) p("{s}{s}{s}\n", .{ teach_pfx(), cmd, c(RESET) });132}133134pub fn printSubstep(comptime fmt: []const u8, args: anytype) void {135    p("{s}", .{step()});136    p(fmt, args);137    p("\n", .{});138}139140// --- Confirmation prompt (returns true = yes) ---141142pub fn confirm(prompt: []const u8) bool {143    p("   {s}continue? [Y/n]  ", .{prompt});144    flush();145    var buf: [64]u8 = undefined;146    var i: usize = 0;147    while (i < buf.len) {148        const n = std.posix.read(std.posix.STDIN_FILENO, buf[i .. i + 1]) catch break;149        if (n == 0) break;150        if (buf[i] == '\n') break;151        i += 1;152    }153    const input = std.mem.trim(u8, buf[0..i], " \t");154    if (input.len == 0) return true;155    return input[0] != 'n' and input[0] != 'N';156}157158// --- Apply / rebuild ---159160pub fn applyStart(machine: []const u8) void {161    p("{s}{s}rebuilding {s}...\n", .{ KAO_COZY, hdr(), machine });162}163164// after_failure: true only when the previous apply this session failed and this165// one succeeded — the one moment that earns the excited kaomoji. Routine166// successes show no kaomoji.167pub fn applyDone(gen: u32, elapsed_ms: u64, after_failure: bool) void {168    const face = if (after_failure) KAO_EXCITED else "";169    const secs = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;170    if (elapsed_ms >= 1000) {171        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) });172    } else {173        p("{s}{s}generation {s}{d}{s}\n", .{ face, hdr(), c(BOLD), gen, c(RESET) });174    }175}176177pub fn applyFailed(translated: []const u8) void {178    p("{s}build failed\n", .{if (color_enabled) ERR_C else ERR_P});179    if (translated.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), translated, c(RESET) });180    p("   {s}-> nina doctor{s}\n", .{ c(DIM), c(RESET) });181}182183pub fn applyDry(machine: []const u8) void {184    p("{s}dry run  {s}\n", .{ hdr(), machine });185}186187pub fn applyDryDone() void {188    p("{s}no changes applied\n", .{hdr()});189}190191// --- Check ---192193pub fn checkStart(machine: []const u8) void {194    p("{s}checking {s}...\n", .{ hdr(), machine });195}196197pub fn checkOk() void {198    p("{s}{s}ok{s}\n", .{ hdr(), c(GREEN), c(RESET) });199}200201pub fn checkFailed(msg: []const u8) void {202    p("{s}config invalid\n", .{if (color_enabled) ERR_C else ERR_P});203    if (msg.len > 0) p("   {s}{s}{s}\n", .{ c(DIM), msg, c(RESET) });204}205206// --- Back / go ---207208pub fn backStart(machine: []const u8, from: u32, to: u32) void {209    p("{s}rolling back  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, from, to });210}211212pub fn goStart(machine: []const u8, current: u32, target: u32) void {213    p("{s}switching  {s}  gen {d} -> gen {d}\n", .{ hdr(), machine, current, target });214}215216pub fn generationDone(n: u32) void {217    p("{s}done  gen {s}{d}{s}\n", .{ hdr(), c(BOLD), n, c(RESET) });218}219220// Like generationDone but for `back` specifically — a successful rollback earns221// the gentle kaomoji. `go` keeps the plain generationDone line.222pub fn backDone(n: u32) void {223    p("{s}{s}done  gen {s}{d}{s}\n", .{ KAO_GENTLE, hdr(), c(BOLD), n, c(RESET) });224}225226// --- History ---227228pub fn historyHeader(machine: []const u8) void {229    p("{s}generations  {s}\n\n", .{ hdr(), machine });230}231232pub fn historyRow(gen: types.GenerationInfo) void {233    if (gen.current) {234        p("   {s}{d}{s}   {s}   {s}   {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });235    } else {236        p("   {s}{d}   {s}   {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });237    }238}239240// --- Clean ---241242pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {243    p("{s}cleaning  {s}  keeping {d} of {d}\n", .{ hdr(), machine, keep, total });244}245246pub fn cleanDone(freed: []const u8) void {247    p("{s}{s}freed {s}\n", .{ KAO_CONTENT, hdr(), freed });248}249250// --- Status ---251252pub fn statusHeader() void {253    p("{s}status\n\n", .{hdr()});254}255256pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {257    if (ok) {258        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) });259    } else {260        p("   {s}{s}   {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });261    }262}263264// --- Diff ---265266pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {267    p("{s}gen {d} -> gen {d}  {s}\n\n", .{ hdr(), a, b, machine });268}269270pub fn diffRow(entry: types.DiffEntry) void {271    switch (entry.op) {272        .add => p("   {s}+{s} {s}   {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),273        .remove => p("   {s}-{s} {s}   {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),274        .change => p("   {s}^{s} {s}   {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),275    }276}277278// --- Doctor ---279280pub fn doctorHeader(machine: []const u8) void {281    p("{s}diagnosing  {s}\n\n", .{ hdr(), machine });282}283284pub fn doctorRow(check: types.DoctorCheck) void {285    const status_str = switch (check.status) {286        .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",287        .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",288        .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",289    };290    const note_str = check.note orelse "";291    p("   {s:<16}  {s}   {s}\n", .{ check.name, status_str, note_str });292}293294pub fn doctorSummary(warnings: u32, failures: u32) void {295    if (failures > 0) {296        p("{s}{d} problems\n", .{ hdr(), failures });297    } else if (warnings > 0) {298        p("{s}{d} warnings\n", .{ hdr(), warnings });299    } else {300        p("{s}{s}{s}all good{s}\n", .{ KAO_HAPPY, hdr(), c(GREEN), c(RESET) });301    }302}303304// --- Update ---305306pub fn updateStart(machine: []const u8) void {307    p("{s}updating channels  {s}\n\n", .{ hdr(), machine });308}309310pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {311    if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {312        p("   {s}   {s} -> {s}\n", .{ name, old.?, new.? });313    } else {314        p("   {s}   {s}no change{s}\n", .{ name, c(DIM), c(RESET) });315    }316}317318pub fn updateDone() void {319    p("\n{s}done\n", .{hdr()});320}321322// --- Upgrade ---323324pub fn upgradeStart(machine: []const u8) void {325    p("{s}upgrading  {s}\n", .{ hdr(), machine });326}327328// --- Log ---329330pub fn logHeader(machine: []const u8) void {331    p("{s}history  {s}\n\n", .{ hdr(), machine });332}333334pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {335    p("   {s}{s}  {s}{s}   {s:<12}  {s}\n", .{ c(DIM), date, time, c(RESET), action, detail });336}337338// --- Edit ---339340pub fn editOpen(path: []const u8, editor: []const u8) void {341    p("{s}opening {s}\n", .{ hdr(), path });342    printSubstep("{s}", .{editor});343    p("\n   {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });344}345346// --- Fmt ---347348pub fn fmtStart(path: []const u8) void {349    p("{s}formatting  {s}\n", .{ hdr(), path });350}351352pub fn fmtDone() void {353    p("{s}done\n", .{hdr()});354}355356pub fn fmtNeedsFormat() void {357    p("{s}needs formatting  {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });358}359360// --- Repl ---361362pub fn replStart() void {363    p("{s}nix repl  {s}nixpkgs loaded{s}  :q to exit\n", .{ hdr(), c(DIM), c(RESET) });364}365366// --- Develop ---367368pub fn developStart(pkgs: []const []const u8) void {369    p("{s}entering dev shell\n\n   ", .{hdr()});370    for (pkgs, 0..) |pkg, i| {371        if (i > 0) p("  ", .{});372        p("{s}", .{pkg});373    }374    p("\n\n   {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });375}376377pub fn developDone() void {378    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });379}380381// --- Info ---382383pub fn infoHeader(machine: []const u8) void {384    p("{s}{s}\n\n", .{ hdr(), machine });385}386387pub fn infoRow(key: []const u8, val: []const u8) void {388    p("   {s:<12}  {s}\n", .{ key, val });389}390391// --- Boot ---392393pub fn bootHeader(machine: []const u8) void {394    p("{s}boot entries  {s}\n\n", .{ hdr(), machine });395}396397pub fn bootEntry(entry: types.BootEntry, idx: usize) void {398    if (entry.current) {399        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) });400    } else {401        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) });402    }403    _ = idx;404}405406// --- Service ---407408pub fn serviceListHeader(machine: []const u8) void {409    p("{s}services  {s}\n\n", .{ hdr(), machine });410}411412pub fn serviceRow(svc: types.ServiceInfo) void {413    const state_str = switch (svc.state) {414        .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",415        .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",416        .inactive => "inactive",417        .unknown => "unknown",418    };419    const uptime = svc.uptime orelse "";420    p("   {s:<24}  {s:<14}  {s}\n", .{ svc.name, state_str, uptime });421}422423pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {424    p("{s}{s} {s}  {s}\n", .{ hdr(), verb, name, machine });425}426427pub fn serviceDone() void {428    p("{s}done\n", .{hdr()});429}430431pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {432    p("{s}{s}  last {d} lines  {s}\n\n", .{ hdr(), name, n, machine });433}434435// --- Store ---436437pub fn storeInfo(info: types.StoreInfo) void {438    p("{s}store\n\n", .{hdr()});439    p("   {s:<16}  {s}\n", .{ "total", info.total_size });440    p("   {s:<16}  {d} paths\n", .{ "live", info.live_paths });441    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) });442}443444// --- Channel ---445446pub fn channelListHeader(machine: []const u8) void {447    p("{s}channels  {s}\n\n", .{ hdr(), machine });448}449450pub fn channelRow(ch: types.ChannelInfo) void {451    p("   {s:<16}  {s}\n", .{ ch.name, ch.url });452}453454// --- Flake ---455456pub fn flakeUpdateStart() void {457    p("{s}updating inputs\n\n", .{hdr()});458}459460pub fn flakeInput(input: types.FlakeInput) void {461    if (input.changed) {462        const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";463        const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";464        p("   {s:<20}  {s} -> {s}\n", .{ input.name, old, new });465    } else {466        p("   {s:<20}  {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });467    }468}469470pub fn flakeLockWritten() void {471    p("\n{s}flake.lock written\n", .{hdr()});472}473474pub fn flakeValid() void {475    p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });476}477478pub fn flakeCheckStart() void {479    p("{s}checking flake\n", .{hdr()});480}481482// --- Profile ---483484pub fn profileListHeader() void {485    p("{s}profile packages\n\n", .{hdr()});486}487488pub fn profileRow(attr: []const u8, pkg_version: []const u8, description: []const u8) void {489    p("   {s:<24}  {s:<12}  {s}\n", .{ attr, pkg_version, description });490}491492// --- Install / Remove / Try ---493494pub fn installStart(pkg: []const u8, path: types.InstallPath) void {495    const path_str = switch (path) {496        .profile => "profile",497        .system => "system",498    };499    p("{s}installing {s}{s}{s}  {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });500}501502pub fn installDone(pkg: []const u8) void {503    p("{s}{s}{s}{s} installed  {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });504}505506pub fn removeStart(pkg: []const u8) void {507    p("{s}removing {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });508}509510// Plain-language health summary. No kaomoji, no hearts — just the facts.511pub fn mood(machine: []const u8, state: []const u8, gen: u32, age: []const u8, size: []const u8) void {512    const healthy = std.mem.eql(u8, state, "running");513    const verdict = if (healthy) "all good" else state;514    const color = if (healthy) GREEN else YELLOW;515    const face = if (healthy) KAO_HAPPY else "";516    p("{s}{s}{s}  {s}{s}{s}\n", .{ face, hdr(), machine, c(color), verdict, c(RESET) });517    if (age.len > 0 or size.len > 0) {518        p("   generation {d}, last applied {s}, {s} system closure\n", .{ gen, age, size });519    } else {520        p("   generation {d}\n", .{gen});521    }522}523524pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {525    p("{s}opening {s}  add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });526    const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;527    printSubstep("{s}", .{cmd});528    p("\n   {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });529}530531pub fn installSelectPrompt(pkg: []const u8, pkg_version: []const u8) void {532    p("{s}{s}{s}{s}  {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), pkg_version });533    p("   [i] profile install    {s}instant  no rebuild{s}\n", .{ c(DIM), c(RESET) });534    p("   [s] system install     {s}opens editor  requires apply{s}\n", .{ c(DIM), c(RESET) });535    p("   [t] try now            {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });536    p("   > ", .{});537    flush();538}539540pub fn tryStart(pkg: []const u8) void {541    p("{s}trying {s}{s}{s}  exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });542}543544pub fn tryDone() void {545    p("{s}{s}back\n", .{ KAO_SLEEPY, hdr() });546}547548// --- List ---549550pub fn listHeader() void {551    p("{s}installed packages\n\n", .{hdr()});552}553554pub fn listSection(title: []const u8) void {555    p("   {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });556}557558pub fn listPkg(name: []const u8, pkg_version: []const u8) void {559    p("   {s}{s}{s}   {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), pkg_version, c(RESET) });560}561562// --- Hello ---563564pub fn hello(machines: []const types.Machine) void {565    p("{s}{s}nina\n\n", .{ KAO_FACE, hdr() });566    for (machines) |m| {567        const kind = if (m.local) "local" else "ssh";568        const detail = if (m.local) "default" else m.host orelse "";569        p("   {s}{s}{s}   {s}   {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });570    }571    p("\n", .{});572    const quick = [_][2][]const u8{573        .{ "apply",         "rebuild and switch" },574        .{ "search <q>",    "find packages" },575        .{ "install <pkg>", "add to profile or system" },576        .{ "status",        "machine health" },577        .{ "back",          "roll back one generation" },578        .{ "update",        "refresh channels" },579    };580    for (quick) |cmd| {581        p("   {s:<16}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });582    }583    p("\n", .{});584}585586// --- Help ---587588pub fn help() void {589    p("{s}nina  {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });590    const cmds = [_][2][]const u8{591        .{ "apply", "rebuild and switch" },592        .{ "back", "roll back one generation" },593        .{ "go <n>", "switch to generation n" },594        .{ "history", "list all generations" },595        .{ "clean", "remove old generations" },596        .{ "search <q>", "find packages" },597        .{ "install <pkg>", "add to profile or system" },598        .{ "remove <pkg>", "remove from profile or system" },599        .{ "try <pkg>", "test without installing" },600        .{ "list", "show installed packages" },601        .{ "edit", "open configuration.nix" },602        .{ "check", "validate config" },603        .{ "diff", "changes between generations" },604        .{ "status", "machine health" },605        .{ "update", "refresh channels" },606        .{ "upgrade", "update and rebuild" },607        .{ "log", "operation history" },608        .{ "doctor", "diagnose issues" },609        .{ "mood", "plain-language health" },610        .{ "develop", "enter dev shell" },611        .{ "flake", "flake management" },612        .{ "build", "build a package" },613        .{ "run", "run without installing" },614        .{ "service", "manage services" },615        .{ "store", "nix store tools" },616        .{ "channel", "manage channels" },617        .{ "profile", "user profile packages" },618        .{ "info", "system information" },619        .{ "boot", "boot entries" },620        .{ "fmt", "format configuration.nix" },621        .{ "repl", "nix repl with nixpkgs" },622        .{ "option <q>", "search nixos options" },623        .{ "pkg", "inspect packages" },624        .{ "pin", "pin flake inputs" },625        .{ "hash", "compute nix hash" },626        .{ "fetch", "fetch and hash a url" },627        .{ "goodbye",       "uninstall nina" },628        .{ "hello",        "list machines" },629        .{ "help",         "this message" },630    };631    for (cmds) |cmd| {632        p("   {s:<18}  {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });633    }634    p("\n   {s}flags: --on <machine>  --dry  --check  --no-apply  --last <n>  --all  --json{s}\n", .{ c(DIM), c(RESET) });635}636637pub fn version() void {638    p("{s}nina  v{s}  zig 0.16  zero dependencies\n", .{ hdr(), nina_version.VERSION });639}640641// --- Search (text output before TUI) ---642643pub fn searchResult(pkg: types.NixPackage) void {644    p("   {s}{s}{s}   {s}{s}{s}   {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });645}646647pub fn searchHeader(query: []const u8, count: usize) void {648    p("{s}search nixpkgs {s}  {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });649}650651// --- Gen commands ---652653pub fn genCurrent(n: u32) void {654    p("{d}\n", .{n});655}656657pub fn done() void {658    p("{s}done\n", .{hdr()});659}660661pub fn runStart(pkg: []const u8) void {662    p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });663}664665pub fn buildStart(attr: []const u8) void {666    p("{s}building {s}\n", .{ hdr(), attr });667}668669pub fn buildDone() void {670    p("{s}done\n", .{hdr()});671}672673// --- Self-update (nina update) ---674675pub fn updateSelfStart() void {676    p("{s}checking for updates\n", .{hdr()});677}678679pub fn updateSelfCurrent(ver: []const u8) void {680    p("{s}{s}nina {s} is up to date{s}\n", .{ hdr(), c(GREEN), ver, c(RESET) });681}682683pub fn updateSelfAvailable(current: []const u8, latest: []const u8) void {684    p("{s}current  {s}\n", .{ step(), current });685    p("{s}latest   {s}{s}{s}\n", .{ step(), c(BOLD), latest, c(RESET) });686}687688pub fn updateSelfDownloading(ver: []const u8, platform: []const u8) void {689    p("{s}downloading nina {s} for {s}\n", .{ step(), ver, platform });690}691692pub fn updateSelfVerifying() void {693    p("{s}verifying checksum\n", .{step()});694}695696pub fn updateSelfInstalling(install_path: []const u8) void {697    p("{s}installing to {s}\n", .{ step(), install_path });698}699700pub fn updateSelfDone(old_ver: []const u8, new_ver: []const u8) void {701    p("{s}updated  {s} {s}→{s} {s}\n", .{ hdr(), old_ver, c(DIM), c(RESET), new_ver });702}703704pub fn updateSelfNoPlatform(platform: []const u8) void {705    p("{s}no pre-built binary for {s}\n", .{ hdr(), platform });706    p("{s}check kepr.uk/nina/releases for available platforms\n", .{step()});707}708709pub fn updateSelfNetworkError() void {710    p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, "could not reach kepr.uk — check your connection" });711}712713pub fn updateSelfChecksumFailed() void {714    p("{s}checksum mismatch — download may be corrupt\n", .{if (color_enabled) ERR_C else ERR_P});715    p("{s}try again or download manually from kepr.uk/nina/releases\n", .{step()});716}717718pub fn updateSelfNixManaged(new_ver: []const u8) void {719    p("{s}upgrading to {s}{s}{s} via nix profile\n", .{ step(), c(BOLD), new_ver, c(RESET) });720}721722pub fn updateSelfNixFailed() void {723    p("{s}nix profile upgrade failed\n", .{if (color_enabled) ERR_C else ERR_P});724    p("{s}try manually: nix profile upgrade '.*nina.*'\n", .{step()});725}726727pub fn updateSelfNixSystemManaged() void {728    p("{s}nina appears to be managed by your NixOS system configuration\n", .{hdr()});729    p("{s}to update: sudo nixos-rebuild switch\n", .{step()});730    p("{s}to reinstall as a profile package: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{step()});731}732733// --- Goodbye / uninstall ---734735pub fn goodbyeStart() void {736    p("{s}uninstalling nina\n", .{hdr()});737}738739pub fn goodbyeFile(path: []const u8) void {740    p("{s}will remove {s}\n", .{ step(), path });741}742743pub fn goodbyeNix() void {744    p("{s}will remove nina from nix profile\n", .{step()});745    p("{s}command: nix profile remove '.*nina.*'\n", .{step()});746}747748pub fn goodbyeNixSystemManaged() void {749    p("{s}nina is managed by your NixOS system configuration\n", .{hdr()});750    p("{s}to remove: delete ~/nina.nix, then run sudo nixos-rebuild switch\n", .{step()});751    p("{s}or remove the nina import from /etc/nixos/configuration.nix directly\n", .{step()});752}753754pub fn goodbyeNixFileFound(path: []const u8) void {755    p("{s}found {s} (NixOS config snippet from the installer)\n", .{ step(), path });756}757758pub fn goodbyeDone() void {759    p("{s}goodbye! reinstall anytime: curl -fsSL kepr.uk/nina/install.sh | sh\n", .{hdr()});760}761762pub fn goodbyeFailed(msg: []const u8) void {763    p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, msg });764}765766// --- Setup ---767768pub fn setupCheck() void {769    p("{s}checking nix features\n", .{hdr()});770}771772pub fn setupFeatures(nix_command: bool, flakes: bool) void {773    p("{s}nix-command  {s}{s}{s}\n", .{774        step(),775        if (nix_command) c(GREEN) else "",776        if (nix_command) "enabled" else "not enabled",777        if (nix_command) c(RESET) else "",778    });779    p("{s}flakes       {s}{s}{s}\n", .{780        step(),781        if (flakes) c(GREEN) else "",782        if (flakes) "enabled" else "not enabled",783        if (flakes) c(RESET) else "",784    });785}786787pub fn setupAllSet() void {788    p("{s}all set\n", .{hdr()});789}790791// Printed once, before the first search, when the nixpkgs registry still points792// at a remote URL. Flush so it lands before `nix registry pin` and any results.793pub fn pinningRegistry() void {794    p("{s}pinning nixpkgs for fast local search...\n", .{hdr()});795    flush();796}797798// --- Plain (non-tty) search rendering ---799// Color-gated equivalents of the raw-ANSI TUI widget, for piped/scripted search.800// Using the gated header + c() keeps ANSI out of non-tty output (NO_COLOR / pipes).801802pub fn searchPlainHeader(mode_str: []const u8, query: []const u8, count: u64) void {803    p("{s}search {s} {s}  {s}{d} results{s}\n", .{ hdr(), mode_str, query, c(DIM), count, c(RESET) });804}805806pub fn searchPlainNoResults() void {807    p("   no results\n", .{});808}809810pub fn searchPlainPackage(pname: []const u8, pkg_version: []const u8, description: []const u8, unfree: bool) void {811    if (unfree) {812        p("   {s}   {s}{s}{s}   {s}unfree{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), c(YELLOW), c(RESET), description });813    } else {814        p("   {s}   {s}{s}{s}   {s}\n", .{ pname, c(CYAN), pkg_version, c(RESET), description });815    }816}817818pub fn searchPlainOption(name: []const u8, type_str: []const u8) void {819    p("   {s}   {s}{s}{s}\n", .{ name, c(DIM), type_str, c(RESET) });820}821822pub fn searchEmptyQuery() void {823    p("{s}search needs a query  {s}e.g. nina search firefox{s}\n", .{ hdr(), c(DIM), c(RESET) });824}825826pub fn setupEnabling() void {827    p("{s}enabling experimental features\n", .{hdr()});828}829830pub fn setupDone(path: []const u8, is_nixos: bool) void {831    p("{s}wrote {s}\n", .{ step(), path });832    p("{s}done  open a new shell to pick up the change\n", .{hdr()});833    if (is_nixos) {834        printSubstep("to persist across nixos-rebuild, add to configuration.nix:", .{});835        printSubstep("  nix.settings.experimental-features = [ \"nix-command\" \"flakes\" ];", .{});836    }837}838839pub fn pinDone(input: []const u8, commit: []const u8) void {840    p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });841}842843pub fn unpinDone(input: []const u8) void {844    p("{s}unpinned {s}\n", .{ hdr(), input });845}846847pub fn hashResult(hash: []const u8) void {848    p("{s}\n", .{hash});849}850851pub fn fetchResult(hash: []const u8, url: []const u8) void {852    _ = url;853    p("{s}\n", .{hash});854}855856pub fn storeRepairStart() void {857    p("{s}repairing store\n", .{hdr()});858}859860pub fn storeVerifyStart() void {861    p("{s}verifying store\n", .{hdr()});862}863864pub fn storeGcDone(freed: []const u8) void {865    p("{s}freed {s}\n", .{ hdr(), freed });866}867868pub fn storePath(path: []const u8) void {869    p("{s}\n", .{path});870}871872pub fn pkgResult(key: []const u8, val: []const u8) void {873    p("   {s:<16}  {s}\n", .{ key, val });874}875876pub fn channelDone() void {877    p("{s}done\n", .{hdr()});878}879880pub fn flakeShowHeader(name: []const u8) void {881    p("{s}{s}\n\n", .{ hdr(), name });882}883884pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {885    p("   {s:<12}  {s:<24}  {s}\n", .{ kind, name, desc });886}