123const std = @import("std");4const types = @import("types.zig");5const Io = std.Io;678const PINK = "\x1b[38;2;255;183;197m";9const BOLD = "\x1b[1m";10const GREEN = "\x1b[32m";11const YELLOW = "\x1b[33m";12const RED = "\x1b[31m";13const CYAN = "\x1b[36m";14const DIM = "\x1b[2m";15const RESET = "\x1b[0m";161718pub const C_PINK = PINK;19pub const C_BOLD = BOLD;20pub const C_GREEN = GREEN;21pub const C_YELLOW = YELLOW;22pub const C_RED = RED;23pub const C_CYAN = CYAN;24pub const C_DIM = DIM;25pub const C_RESET = RESET;26pub const C_CLEAR_EOL = "\x1b[K";27pub const C_CURSOR_UP_FMT = "\x1b[{d}A";282930const HDR_C = PINK ++ ":: " ++ RESET;31const HDR_P = ":: ";32const STEP_C = " " ++ DIM ++ "-> " ++ RESET;33const STEP_P = " -> ";34const TEACH_C = " " ++ DIM ++ "= " ++ RESET;35const TEACH_P = " = ";36const ERR_C = PINK ++ ":: " ++ RED ++ "error:" ++ RESET ++ " ";37const ERR_P = ":: error: ";38const WARN_C = PINK ++ ":: " ++ YELLOW ++ "warning:" ++ RESET ++ " ";39const WARN_P = ":: warning: ";4041var _fw: Io.File.Writer = undefined;42var _w: *Io.Writer = undefined;43var color_enabled: bool = true;44var teach_enabled: bool = false;4546pub fn init(writer: *Io.Writer, environ: *const std.process.Environ.Map, color_config: bool) void {47 _w = writer;48 color_enabled = color_config and49 (std.c.isatty(std.posix.STDOUT_FILENO) != 0) and50 !environ.contains("NO_COLOR");51}5253pub fn setTeach(enabled: bool) void {54 teach_enabled = enabled;55}5657pub fn flush() void {58 _w.flush() catch {};59}6061fn c(code: []const u8) []const u8 {62 return if (color_enabled) code else "";63}6465fn hdr() []const u8 {66 return if (color_enabled) HDR_C else HDR_P;67}6869fn step() []const u8 {70 return if (color_enabled) STEP_C else STEP_P;71}7273fn teach_pfx() []const u8 {74 return if (color_enabled) TEACH_C else TEACH_P;75}767778pub fn raw(bytes: []const u8) void {79 _w.writeAll(bytes) catch {};80}818283pub fn p(comptime fmt: []const u8, args: anytype) void {84 _w.print(fmt, args) catch {};85}8687pub fn cursorUp(n: usize) void {88 if (n > 0) p("\x1b[{d}A", .{n});89}9091pub fn clearLine() void {92 p("\r\x1b[K", .{});93}94959697pub fn printError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {98 p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, message });99 if (detail) |d| p(" {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });100 if (suggestion) |s| p(" {s}-> {s}{s}\n", .{ c(DIM), s, c(RESET) });101}102103pub fn printWarning(message: []const u8) void {104 p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, message });105}106107pub fn printTeach(cmd: []const u8) void {108 if (teach_enabled) p("{s}{s}{s}\n", .{ teach_pfx(), cmd, c(RESET) });109}110111pub fn printSubstep(comptime fmt: []const u8, args: anytype) void {112 p("{s}", .{step()});113 p(fmt, args);114 p("\n", .{});115}116117118119pub fn confirm(prompt: []const u8) bool {120 p(" {s}continue? [Y/n] ", .{prompt});121 flush();122 var buf: [64]u8 = undefined;123 var i: usize = 0;124 while (i < buf.len) {125 const n = std.posix.read(std.posix.STDIN_FILENO, buf[i .. i + 1]) catch break;126 if (n == 0) break;127 if (buf[i] == '\n') break;128 i += 1;129 }130 const input = std.mem.trim(u8, buf[0..i], " \t");131 if (input.len == 0) return true;132 return input[0] != 'n' and input[0] != 'N';133}134135136137pub fn applyStart(machine: []const u8) void {138 p("{s}rebuilding {s}...\n", .{ hdr(), machine });139}140141pub fn applyDone(gen: u32, elapsed_ms: u64) void {142 const secs = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;143 if (elapsed_ms >= 1000) {144 p("{s}generation {s}{d}{s} {s}[{d:.1}s]{s}\n", .{ hdr(), c(BOLD), gen, c(RESET), c(DIM), secs, c(RESET) });145 } else {146 p("{s}generation {s}{d}{s}\n", .{ hdr(), c(BOLD), gen, c(RESET) });147 }148}149150pub fn applyFailed(translated: []const u8) void {151 p("{s}build failed\n", .{if (color_enabled) ERR_C else ERR_P});152 if (translated.len > 0) p(" {s}{s}{s}\n", .{ c(DIM), translated, c(RESET) });153 p(" {s}-> nina doctor{s}\n", .{ c(DIM), c(RESET) });154}155156pub fn applyDry(machine: []const u8) void {157 p("{s}dry run {s}\n", .{ hdr(), machine });158}159160pub fn applyDryDone() void {161 p("{s}no changes applied\n", .{hdr()});162}163164165166pub fn checkStart(machine: []const u8) void {167 p("{s}checking {s}...\n", .{ hdr(), machine });168}169170pub fn checkOk() void {171 p("{s}{s}ok{s}\n", .{ hdr(), c(GREEN), c(RESET) });172}173174pub fn checkFailed(msg: []const u8) void {175 p("{s}config invalid\n", .{if (color_enabled) ERR_C else ERR_P});176 if (msg.len > 0) p(" {s}{s}{s}\n", .{ c(DIM), msg, c(RESET) });177}178179180181pub fn backStart(machine: []const u8, from: u32, to: u32) void {182 p("{s}rolling back {s} gen {d} -> gen {d}\n", .{ hdr(), machine, from, to });183}184185pub fn goStart(machine: []const u8, current: u32, target: u32) void {186 p("{s}switching {s} gen {d} -> gen {d}\n", .{ hdr(), machine, current, target });187}188189pub fn generationDone(n: u32) void {190 p("{s}done gen {s}{d}{s}\n", .{ hdr(), c(BOLD), n, c(RESET) });191}192193194195pub fn historyHeader(machine: []const u8) void {196 p("{s}generations {s}\n\n", .{ hdr(), machine });197}198199pub fn historyRow(gen: types.GenerationInfo) void {200 if (gen.current) {201 p(" {s}{d}{s} {s} {s} {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });202 } else {203 p(" {s}{d} {s} {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });204 }205}206207208209pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {210 p("{s}cleaning {s} keeping {d} of {d}\n", .{ hdr(), machine, keep, total });211}212213pub fn cleanDone(freed: []const u8) void {214 p("{s}freed {s}\n", .{ hdr(), freed });215}216217218219pub fn statusHeader() void {220 p("{s}status\n\n", .{hdr()});221}222223pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {224 if (ok) {225 p(" {s}{s} gen {d} {s} {s}{s}\n", .{ c(DIM), machine, gen, time, size, c(RESET) });226 } else {227 p(" {s}{s} {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });228 }229}230231232233pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {234 p("{s}gen {d} -> gen {d} {s}\n\n", .{ hdr(), a, b, machine });235}236237pub fn diffRow(entry: types.DiffEntry) void {238 switch (entry.op) {239 .add => p(" {s}+{s} {s} {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),240 .remove => p(" {s}-{s} {s} {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),241 .change => p(" {s}^{s} {s} {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),242 }243}244245246247pub fn doctorHeader(machine: []const u8) void {248 p("{s}diagnosing {s}\n\n", .{ hdr(), machine });249}250251pub fn doctorRow(check: types.DoctorCheck) void {252 const status_str = switch (check.status) {253 .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",254 .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",255 .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",256 };257 const note_str = check.note orelse "";258 p(" {s:<16} {s} {s}\n", .{ check.name, status_str, note_str });259}260261pub fn doctorSummary(warnings: u32, failures: u32) void {262 if (failures > 0) {263 p("{s}{d} problems\n", .{ hdr(), failures });264 } else if (warnings > 0) {265 p("{s}{d} warnings\n", .{ hdr(), warnings });266 } else {267 p("{s}{s}all good{s}\n", .{ hdr(), c(GREEN), c(RESET) });268 }269}270271272273pub fn updateStart(machine: []const u8) void {274 p("{s}updating channels {s}\n\n", .{ hdr(), machine });275}276277pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {278 if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {279 p(" {s} {s} -> {s}\n", .{ name, old.?, new.? });280 } else {281 p(" {s} {s}no change{s}\n", .{ name, c(DIM), c(RESET) });282 }283}284285pub fn updateDone() void {286 p("\n{s}done\n", .{hdr()});287}288289290291pub fn upgradeStart(machine: []const u8) void {292 p("{s}upgrading {s}\n", .{ hdr(), machine });293}294295296297pub fn logHeader(machine: []const u8) void {298 p("{s}history {s}\n\n", .{ hdr(), machine });299}300301pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {302 p(" {s} {s} {s:<12} {s}\n", .{ date, time, action, detail });303}304305306307pub fn editOpen(path: []const u8, editor: []const u8) void {308 p("{s}opening {s}\n", .{ hdr(), path });309 printSubstep("{s}", .{editor});310 p("\n {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });311}312313314315pub fn fmtStart(path: []const u8) void {316 p("{s}formatting {s}\n", .{ hdr(), path });317}318319pub fn fmtDone() void {320 p("{s}done\n", .{hdr()});321}322323pub fn fmtNeedsFormat() void {324 p("{s}needs formatting {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });325}326327328329pub fn replStart() void {330 p("{s}nix repl {s}nixpkgs loaded{s} :q to exit\n", .{ hdr(), c(DIM), c(RESET) });331}332333334335pub fn developStart(pkgs: []const []const u8) void {336 p("{s}entering dev shell\n\n ", .{hdr()});337 for (pkgs, 0..) |pkg, i| {338 if (i > 0) p(" ", .{});339 p("{s}", .{pkg});340 }341 p("\n\n {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });342}343344pub fn developDone() void {345 p("{s}back\n", .{hdr()});346}347348349350pub fn infoHeader(machine: []const u8) void {351 p("{s}{s}\n\n", .{ hdr(), machine });352}353354pub fn infoRow(key: []const u8, val: []const u8) void {355 p(" {s:<12} {s}\n", .{ key, val });356}357358359360pub fn bootHeader(machine: []const u8) void {361 p("{s}boot entries {s}\n\n", .{ hdr(), machine });362}363364pub fn bootEntry(entry: types.BootEntry, idx: usize) void {365 if (entry.current) {366 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) });367 } else {368 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) });369 }370 _ = idx;371}372373374375pub fn serviceListHeader(machine: []const u8) void {376 p("{s}services {s}\n\n", .{ hdr(), machine });377}378379pub fn serviceRow(svc: types.ServiceInfo) void {380 const state_str = switch (svc.state) {381 .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",382 .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",383 .inactive => "inactive",384 .unknown => "unknown",385 };386 const uptime = svc.uptime orelse "";387 p(" {s:<24} {s:<14} {s}\n", .{ svc.name, state_str, uptime });388}389390pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {391 p("{s}{s} {s} {s}\n", .{ hdr(), verb, name, machine });392}393394pub fn serviceDone() void {395 p("{s}done\n", .{hdr()});396}397398pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {399 p("{s}{s} last {d} lines {s}\n\n", .{ hdr(), name, n, machine });400}401402403404pub fn storeInfo(info: types.StoreInfo) void {405 p("{s}store\n\n", .{hdr()});406 p(" {s:<16} {s}\n", .{ "total", info.total_size });407 p(" {s:<16} {d} paths\n", .{ "live", info.live_paths });408 p(" {s:<16} {s}{d} paths{s} run nina clean\n", .{ "reclaimable", c(DIM), info.reclaimable_paths, c(RESET) });409}410411412413pub fn channelListHeader(machine: []const u8) void {414 p("{s}channels {s}\n\n", .{ hdr(), machine });415}416417pub fn channelRow(ch: types.ChannelInfo) void {418 p(" {s:<16} {s}\n", .{ ch.name, ch.url });419}420421422423pub fn flakeUpdateStart() void {424 p("{s}updating inputs\n\n", .{hdr()});425}426427pub fn flakeInput(input: types.FlakeInput) void {428 if (input.changed) {429 const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";430 const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";431 p(" {s:<20} {s} -> {s}\n", .{ input.name, old, new });432 } else {433 p(" {s:<20} {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });434 }435}436437pub fn flakeLockWritten() void {438 p("\n{s}flake.lock written\n", .{hdr()});439}440441pub fn flakeValid() void {442 p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });443}444445pub fn flakeCheckStart() void {446 p("{s}checking flake\n", .{hdr()});447}448449450451pub fn profileListHeader() void {452 p("{s}profile packages\n\n", .{hdr()});453}454455pub fn profileRow(attr: []const u8, version: []const u8, description: []const u8) void {456 p(" {s:<24} {s:<12} {s}\n", .{ attr, version, description });457}458459460461pub fn installStart(pkg: []const u8, path: types.InstallPath) void {462 const path_str = switch (path) {463 .profile => "profile",464 .system => "system",465 };466 p("{s}installing {s}{s}{s} {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });467}468469pub fn installDone(pkg: []const u8) void {470 p("{s}{s}{s}{s} installed {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });471}472473pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {474 p("{s}opening {s} add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });475 const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;476 printSubstep("{s}", .{cmd});477 p("\n {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });478}479480pub fn installSelectPrompt(pkg: []const u8, version: []const u8) void {481 p("{s}{s}{s}{s} {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), version });482 p(" [i] profile install {s}instant no rebuild{s}\n", .{ c(DIM), c(RESET) });483 p(" [s] system install {s}opens editor requires apply{s}\n", .{ c(DIM), c(RESET) });484 p(" [t] try now {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });485 p(" > ", .{});486 flush();487}488489pub fn tryStart(pkg: []const u8) void {490 p("{s}trying {s}{s}{s} exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });491}492493pub fn tryDone() void {494 p("{s}back\n", .{hdr()});495}496497498499pub fn listHeader() void {500 p("{s}installed packages\n\n", .{hdr()});501}502503pub fn listSection(title: []const u8) void {504 p(" {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });505}506507pub fn listPkg(name: []const u8, version: []const u8) void {508 p(" {s}{s}{s} {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), version, c(RESET) });509}510511512513pub fn hello(machines: []const types.Machine) void {514 p("{s}nina\n\n", .{hdr()});515 for (machines) |m| {516 const kind = if (m.local) "local" else "ssh";517 const detail = if (m.local) "default" else m.host orelse "";518 p(" {s}{s}{s} {s} {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });519 }520 p("\n", .{});521}522523524525pub fn help() void {526 p("{s}nina {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });527 const cmds = [_][2][]const u8{528 .{ "apply", "rebuild and switch" },529 .{ "back", "roll back one generation" },530 .{ "go <n>", "switch to generation n" },531 .{ "history", "list all generations" },532 .{ "clean", "remove old generations" },533 .{ "search <q>", "find packages" },534 .{ "install <pkg>", "add to profile or system" },535 .{ "remove <pkg>", "remove from profile or system" },536 .{ "try <pkg>", "test without installing" },537 .{ "list", "show installed packages" },538 .{ "edit", "open configuration.nix" },539 .{ "check", "validate config" },540 .{ "diff", "changes between generations" },541 .{ "status", "machine health" },542 .{ "update", "refresh channels" },543 .{ "upgrade", "update and rebuild" },544 .{ "log", "operation history" },545 .{ "doctor", "diagnose issues" },546 .{ "develop", "enter dev shell" },547 .{ "flake", "flake management" },548 .{ "build", "build a package" },549 .{ "run", "run without installing" },550 .{ "service", "manage services" },551 .{ "store", "nix store tools" },552 .{ "channel", "manage channels" },553 .{ "profile", "user profile packages" },554 .{ "info", "system information" },555 .{ "boot", "boot entries" },556 .{ "fmt", "format configuration.nix" },557 .{ "repl", "nix repl with nixpkgs" },558 .{ "option <q>", "search nixos options" },559 .{ "pkg", "inspect packages" },560 .{ "pin", "pin flake inputs" },561 .{ "hash", "compute nix hash" },562 .{ "fetch", "fetch and hash a url" },563 .{ "hello", "list machines" },564 .{ "help", "this message" },565 };566 for (cmds) |cmd| {567 p(" {s:<18} {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });568 }569 p("\n {s}flags: --on <machine> --dry --check --no-apply --last <n> --all --json{s}\n", .{ c(DIM), c(RESET) });570}571572573574pub fn searchResult(pkg: types.NixPackage) void {575 p(" {s}{s}{s} {s}{s}{s} {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });576}577578pub fn searchHeader(query: []const u8, count: usize) void {579 p("{s}search nixpkgs {s} {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });580}581582583584pub fn genCurrent(n: u32) void {585 p("{d}\n", .{n});586}587588pub fn done() void {589 p("{s}done\n", .{hdr()});590}591592pub fn runStart(pkg: []const u8) void {593 p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });594}595596pub fn buildStart(attr: []const u8) void {597 p("{s}building {s}\n", .{ hdr(), attr });598}599600pub fn buildDone() void {601 p("{s}done\n", .{hdr()});602}603604pub fn pinDone(input: []const u8, commit: []const u8) void {605 p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });606}607608pub fn unpinDone(input: []const u8) void {609 p("{s}unpinned {s}\n", .{ hdr(), input });610}611612pub fn hashResult(hash: []const u8) void {613 p("{s}\n", .{hash});614}615616pub fn fetchResult(hash: []const u8, url: []const u8) void {617 _ = url;618 p("{s}\n", .{hash});619}620621pub fn storeRepairStart() void {622 p("{s}repairing store\n", .{hdr()});623}624625pub fn storeVerifyStart() void {626 p("{s}verifying store\n", .{hdr()});627}628629pub fn storeGcDone(freed: []const u8) void {630 p("{s}freed {s}\n", .{ hdr(), freed });631}632633pub fn storePath(path: []const u8) void {634 p("{s}\n", .{path});635}636637pub fn pkgResult(key: []const u8, val: []const u8) void {638 p(" {s:<16} {s}\n", .{ key, val });639}640641pub fn channelDone() void {642 p("{s}done\n", .{hdr()});643}644645pub fn flakeShowHeader(name: []const u8) void {646 p("{s}{s}\n\n", .{ hdr(), name });647}648649pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {650 p(" {s:<12} {s:<24} {s}\n", .{ kind, name, desc });651}