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 isStdoutTty() and50 !environ.contains("NO_COLOR");51}52535455fn isStdoutTty() bool {56 _ = std.posix.tcgetattr(std.posix.STDOUT_FILENO) catch return false;57 return true;58}5960pub fn setTeach(enabled: bool) void {61 teach_enabled = enabled;62}6364pub fn flush() void {65 _w.flush() catch {};66}6768fn c(code: []const u8) []const u8 {69 return if (color_enabled) code else "";70}7172fn hdr() []const u8 {73 return if (color_enabled) HDR_C else HDR_P;74}7576fn step() []const u8 {77 return if (color_enabled) STEP_C else STEP_P;78}7980fn teach_pfx() []const u8 {81 return if (color_enabled) TEACH_C else TEACH_P;82}838485pub fn raw(bytes: []const u8) void {86 _w.writeAll(bytes) catch {};87}888990pub fn p(comptime fmt: []const u8, args: anytype) void {91 _w.print(fmt, args) catch {};92}9394pub fn cursorUp(n: usize) void {95 if (n > 0) p("\x1b[{d}A", .{n});96}9798pub fn clearLine() void {99 p("\r\x1b[K", .{});100}101102103104pub fn printError(message: []const u8, detail: ?[]const u8, suggestion: ?[]const u8) void {105 p("{s}{s}\n", .{ if (color_enabled) ERR_C else ERR_P, message });106 if (detail) |d| p(" {s}{s}{s}\n", .{ c(DIM), d, c(RESET) });107 if (suggestion) |s| p(" {s}-> {s}{s}\n", .{ c(DIM), s, c(RESET) });108}109110pub fn printWarning(message: []const u8) void {111 p("{s}{s}\n", .{ if (color_enabled) WARN_C else WARN_P, message });112}113114pub fn printTeach(cmd: []const u8) void {115 if (teach_enabled) p("{s}{s}{s}\n", .{ teach_pfx(), cmd, c(RESET) });116}117118pub fn printSubstep(comptime fmt: []const u8, args: anytype) void {119 p("{s}", .{step()});120 p(fmt, args);121 p("\n", .{});122}123124125126pub fn confirm(prompt: []const u8) bool {127 p(" {s}continue? [Y/n] ", .{prompt});128 flush();129 var buf: [64]u8 = undefined;130 var i: usize = 0;131 while (i < buf.len) {132 const n = std.posix.read(std.posix.STDIN_FILENO, buf[i .. i + 1]) catch break;133 if (n == 0) break;134 if (buf[i] == '\n') break;135 i += 1;136 }137 const input = std.mem.trim(u8, buf[0..i], " \t");138 if (input.len == 0) return true;139 return input[0] != 'n' and input[0] != 'N';140}141142143144pub fn applyStart(machine: []const u8) void {145 p("{s}rebuilding {s}...\n", .{ hdr(), machine });146}147148pub fn applyDone(gen: u32, elapsed_ms: u64) void {149 const secs = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0;150 if (elapsed_ms >= 1000) {151 p("{s}generation {s}{d}{s} {s}[{d:.1}s]{s}\n", .{ hdr(), c(BOLD), gen, c(RESET), c(DIM), secs, c(RESET) });152 } else {153 p("{s}generation {s}{d}{s}\n", .{ hdr(), c(BOLD), gen, c(RESET) });154 }155}156157pub fn applyFailed(translated: []const u8) void {158 p("{s}build failed\n", .{if (color_enabled) ERR_C else ERR_P});159 if (translated.len > 0) p(" {s}{s}{s}\n", .{ c(DIM), translated, c(RESET) });160 p(" {s}-> nina doctor{s}\n", .{ c(DIM), c(RESET) });161}162163pub fn applyDry(machine: []const u8) void {164 p("{s}dry run {s}\n", .{ hdr(), machine });165}166167pub fn applyDryDone() void {168 p("{s}no changes applied\n", .{hdr()});169}170171172173pub fn checkStart(machine: []const u8) void {174 p("{s}checking {s}...\n", .{ hdr(), machine });175}176177pub fn checkOk() void {178 p("{s}{s}ok{s}\n", .{ hdr(), c(GREEN), c(RESET) });179}180181pub fn checkFailed(msg: []const u8) void {182 p("{s}config invalid\n", .{if (color_enabled) ERR_C else ERR_P});183 if (msg.len > 0) p(" {s}{s}{s}\n", .{ c(DIM), msg, c(RESET) });184}185186187188pub fn backStart(machine: []const u8, from: u32, to: u32) void {189 p("{s}rolling back {s} gen {d} -> gen {d}\n", .{ hdr(), machine, from, to });190}191192pub fn goStart(machine: []const u8, current: u32, target: u32) void {193 p("{s}switching {s} gen {d} -> gen {d}\n", .{ hdr(), machine, current, target });194}195196pub fn generationDone(n: u32) void {197 p("{s}done gen {s}{d}{s}\n", .{ hdr(), c(BOLD), n, c(RESET) });198}199200201202pub fn historyHeader(machine: []const u8) void {203 p("{s}generations {s}\n\n", .{ hdr(), machine });204}205206pub fn historyRow(gen: types.GenerationInfo) void {207 if (gen.current) {208 p(" {s}{d}{s} {s} {s} {s}current{s}\n", .{ c(BOLD), gen.number, c(RESET), gen.date, gen.time, c(BOLD), c(RESET) });209 } else {210 p(" {s}{d} {s} {s}{s}\n", .{ c(DIM), gen.number, gen.date, gen.time, c(RESET) });211 }212}213214215216pub fn cleanStart(machine: []const u8, keep: u32, total: u32) void {217 p("{s}cleaning {s} keeping {d} of {d}\n", .{ hdr(), machine, keep, total });218}219220pub fn cleanDone(freed: []const u8) void {221 p("{s}freed {s}\n", .{ hdr(), freed });222}223224225226pub fn statusHeader() void {227 p("{s}status\n\n", .{hdr()});228}229230pub fn statusRow(machine: []const u8, gen: u32, time: []const u8, size: []const u8, ok: bool) void {231 if (ok) {232 p(" {s}{s} gen {d} {s} {s}{s}\n", .{ c(DIM), machine, gen, time, size, c(RESET) });233 } else {234 p(" {s}{s} {s}unreachable{s}\n", .{ c(RED), machine, "", c(RESET) });235 }236}237238239240pub fn diffHeader(a: u32, b: u32, machine: []const u8) void {241 p("{s}gen {d} -> gen {d} {s}\n\n", .{ hdr(), a, b, machine });242}243244pub fn diffRow(entry: types.DiffEntry) void {245 switch (entry.op) {246 .add => p(" {s}+{s} {s} {s}\n", .{ c(GREEN), c(RESET), entry.package, entry.new_version orelse "" }),247 .remove => p(" {s}-{s} {s} {s}\n", .{ c(RED), c(RESET), entry.package, entry.old_version orelse "" }),248 .change => p(" {s}^{s} {s} {s} -> {s}\n", .{ c(CYAN), c(RESET), entry.package, entry.old_version orelse "", entry.new_version orelse "" }),249 }250}251252253254pub fn doctorHeader(machine: []const u8) void {255 p("{s}diagnosing {s}\n\n", .{ hdr(), machine });256}257258pub fn doctorRow(check: types.DoctorCheck) void {259 const status_str = switch (check.status) {260 .ok => if (color_enabled) GREEN ++ "ok" ++ RESET else "ok",261 .warn => if (color_enabled) YELLOW ++ "warn" ++ RESET else "warn",262 .fail => if (color_enabled) RED ++ "fail" ++ RESET else "fail",263 };264 const note_str = check.note orelse "";265 p(" {s:<16} {s} {s}\n", .{ check.name, status_str, note_str });266}267268pub fn doctorSummary(warnings: u32, failures: u32) void {269 if (failures > 0) {270 p("{s}{d} problems\n", .{ hdr(), failures });271 } else if (warnings > 0) {272 p("{s}{d} warnings\n", .{ hdr(), warnings });273 } else {274 p("{s}{s}all good{s}\n", .{ hdr(), c(GREEN), c(RESET) });275 }276}277278279280pub fn updateStart(machine: []const u8) void {281 p("{s}updating channels {s}\n\n", .{ hdr(), machine });282}283284pub fn updateChannel(name: []const u8, old: ?[]const u8, new: ?[]const u8) void {285 if (old != null and new != null and !std.mem.eql(u8, old.?, new.?)) {286 p(" {s} {s} -> {s}\n", .{ name, old.?, new.? });287 } else {288 p(" {s} {s}no change{s}\n", .{ name, c(DIM), c(RESET) });289 }290}291292pub fn updateDone() void {293 p("\n{s}done\n", .{hdr()});294}295296297298pub fn upgradeStart(machine: []const u8) void {299 p("{s}upgrading {s}\n", .{ hdr(), machine });300}301302303304pub fn logHeader(machine: []const u8) void {305 p("{s}history {s}\n\n", .{ hdr(), machine });306}307308pub fn logRow(date: []const u8, time: []const u8, action: []const u8, detail: []const u8) void {309 p(" {s} {s} {s:<12} {s}\n", .{ date, time, action, detail });310}311312313314pub fn editOpen(path: []const u8, editor: []const u8) void {315 p("{s}opening {s}\n", .{ hdr(), path });316 printSubstep("{s}", .{editor});317 p("\n {s}run nina check to validate before applying{s}\n", .{ c(DIM), c(RESET) });318}319320321322pub fn fmtStart(path: []const u8) void {323 p("{s}formatting {s}\n", .{ hdr(), path });324}325326pub fn fmtDone() void {327 p("{s}done\n", .{hdr()});328}329330pub fn fmtNeedsFormat() void {331 p("{s}needs formatting {s}run nina fmt to fix{s}\n", .{ hdr(), c(DIM), c(RESET) });332}333334335336pub fn replStart() void {337 p("{s}nix repl {s}nixpkgs loaded{s} :q to exit\n", .{ hdr(), c(DIM), c(RESET) });338}339340341342pub fn developStart(pkgs: []const []const u8) void {343 p("{s}entering dev shell\n\n ", .{hdr()});344 for (pkgs, 0..) |pkg, i| {345 if (i > 0) p(" ", .{});346 p("{s}", .{pkg});347 }348 p("\n\n {s}exit to return{s}\n\n", .{ c(DIM), c(RESET) });349}350351pub fn developDone() void {352 p("{s}back\n", .{hdr()});353}354355356357pub fn infoHeader(machine: []const u8) void {358 p("{s}{s}\n\n", .{ hdr(), machine });359}360361pub fn infoRow(key: []const u8, val: []const u8) void {362 p(" {s:<12} {s}\n", .{ key, val });363}364365366367pub fn bootHeader(machine: []const u8) void {368 p("{s}boot entries {s}\n\n", .{ hdr(), machine });369}370371pub fn bootEntry(entry: types.BootEntry, idx: usize) void {372 if (entry.current) {373 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) });374 } else {375 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) });376 }377 _ = idx;378}379380381382pub fn serviceListHeader(machine: []const u8) void {383 p("{s}services {s}\n\n", .{ hdr(), machine });384}385386pub fn serviceRow(svc: types.ServiceInfo) void {387 const state_str = switch (svc.state) {388 .active => if (color_enabled) GREEN ++ "active" ++ RESET else "active",389 .failed => if (color_enabled) RED ++ "failed" ++ RESET else "failed",390 .inactive => "inactive",391 .unknown => "unknown",392 };393 const uptime = svc.uptime orelse "";394 p(" {s:<24} {s:<14} {s}\n", .{ svc.name, state_str, uptime });395}396397pub fn serviceVerb(verb: []const u8, name: []const u8, machine: []const u8) void {398 p("{s}{s} {s} {s}\n", .{ hdr(), verb, name, machine });399}400401pub fn serviceDone() void {402 p("{s}done\n", .{hdr()});403}404405pub fn serviceLogsHeader(name: []const u8, n: u32, machine: []const u8) void {406 p("{s}{s} last {d} lines {s}\n\n", .{ hdr(), name, n, machine });407}408409410411pub fn storeInfo(info: types.StoreInfo) void {412 p("{s}store\n\n", .{hdr()});413 p(" {s:<16} {s}\n", .{ "total", info.total_size });414 p(" {s:<16} {d} paths\n", .{ "live", info.live_paths });415 p(" {s:<16} {s}{d} paths{s} run nina clean\n", .{ "reclaimable", c(DIM), info.reclaimable_paths, c(RESET) });416}417418419420pub fn channelListHeader(machine: []const u8) void {421 p("{s}channels {s}\n\n", .{ hdr(), machine });422}423424pub fn channelRow(ch: types.ChannelInfo) void {425 p(" {s:<16} {s}\n", .{ ch.name, ch.url });426}427428429430pub fn flakeUpdateStart() void {431 p("{s}updating inputs\n\n", .{hdr()});432}433434pub fn flakeInput(input: types.FlakeInput) void {435 if (input.changed) {436 const old = if (input.old_rev) |r| r[0..@min(7, r.len)] else "?";437 const new = if (input.new_rev) |r| r[0..@min(7, r.len)] else "?";438 p(" {s:<20} {s} -> {s}\n", .{ input.name, old, new });439 } else {440 p(" {s:<20} {s}no change{s}\n", .{ input.name, c(DIM), c(RESET) });441 }442}443444pub fn flakeLockWritten() void {445 p("\n{s}flake.lock written\n", .{hdr()});446}447448pub fn flakeValid() void {449 p("{s}{s}valid{s}\n", .{ hdr(), c(GREEN), c(RESET) });450}451452pub fn flakeCheckStart() void {453 p("{s}checking flake\n", .{hdr()});454}455456457458pub fn profileListHeader() void {459 p("{s}profile packages\n\n", .{hdr()});460}461462pub fn profileRow(attr: []const u8, version: []const u8, description: []const u8) void {463 p(" {s:<24} {s:<12} {s}\n", .{ attr, version, description });464}465466467468pub fn installStart(pkg: []const u8, path: types.InstallPath) void {469 const path_str = switch (path) {470 .profile => "profile",471 .system => "system",472 };473 p("{s}installing {s}{s}{s} {s}\n", .{ hdr(), c(PINK), pkg, c(RESET), path_str });474}475476pub fn installDone(pkg: []const u8) void {477 p("{s}{s}{s}{s} installed {s}available now{s}\n", .{ hdr(), c(PINK), pkg, c(RESET), c(DIM), c(RESET) });478}479480pub fn installEditorPrompt(pkg: []const u8, config_path: []const u8, line: u32, editor: []const u8) void {481 p("{s}opening {s} add {s}{s}{s} to systemPackages\n", .{ hdr(), config_path, c(PINK), pkg, c(RESET) });482 const cmd = std.fmt.allocPrint(std.heap.page_allocator, "{s} {s} +{d}", .{ editor, config_path, line }) catch editor;483 printSubstep("{s}", .{cmd});484 p("\n {s}save and close when done{s}\n\n", .{ c(DIM), c(RESET) });485}486487pub fn installSelectPrompt(pkg: []const u8, version: []const u8) void {488 p("{s}{s}{s}{s} {s}\n\n", .{ hdr(), c(PINK), pkg, c(RESET), version });489 p(" [i] profile install {s}instant no rebuild{s}\n", .{ c(DIM), c(RESET) });490 p(" [s] system install {s}opens editor requires apply{s}\n", .{ c(DIM), c(RESET) });491 p(" [t] try now {s}exits when done{s}\n\n", .{ c(DIM), c(RESET) });492 p(" > ", .{});493 flush();494}495496pub fn tryStart(pkg: []const u8) void {497 p("{s}trying {s}{s}{s} exit when done\n", .{ hdr(), c(PINK), pkg, c(RESET) });498}499500pub fn tryDone() void {501 p("{s}back\n", .{hdr()});502}503504505506pub fn listHeader() void {507 p("{s}installed packages\n\n", .{hdr()});508}509510pub fn listSection(title: []const u8) void {511 p(" {s}{s}{s}\n", .{ c(DIM), title, c(RESET) });512}513514pub fn listPkg(name: []const u8, version: []const u8) void {515 p(" {s}{s}{s} {s}{s}{s}\n", .{ c(PINK), name, c(RESET), c(DIM), version, c(RESET) });516}517518519520pub fn hello(machines: []const types.Machine) void {521 p("{s}nina\n\n", .{hdr()});522 for (machines) |m| {523 const kind = if (m.local) "local" else "ssh";524 const detail = if (m.local) "default" else m.host orelse "";525 p(" {s}{s}{s} {s} {s}\n", .{ c(DIM), m.name, c(RESET), kind, detail });526 }527 p("\n", .{});528}529530531532pub fn help() void {533 p("{s}nina {s}nixos helper{s}\n\n", .{ hdr(), c(DIM), c(RESET) });534 const cmds = [_][2][]const u8{535 .{ "apply", "rebuild and switch" },536 .{ "back", "roll back one generation" },537 .{ "go <n>", "switch to generation n" },538 .{ "history", "list all generations" },539 .{ "clean", "remove old generations" },540 .{ "search <q>", "find packages" },541 .{ "install <pkg>", "add to profile or system" },542 .{ "remove <pkg>", "remove from profile or system" },543 .{ "try <pkg>", "test without installing" },544 .{ "list", "show installed packages" },545 .{ "edit", "open configuration.nix" },546 .{ "check", "validate config" },547 .{ "diff", "changes between generations" },548 .{ "status", "machine health" },549 .{ "update", "refresh channels" },550 .{ "upgrade", "update and rebuild" },551 .{ "log", "operation history" },552 .{ "doctor", "diagnose issues" },553 .{ "develop", "enter dev shell" },554 .{ "flake", "flake management" },555 .{ "build", "build a package" },556 .{ "run", "run without installing" },557 .{ "service", "manage services" },558 .{ "store", "nix store tools" },559 .{ "channel", "manage channels" },560 .{ "profile", "user profile packages" },561 .{ "info", "system information" },562 .{ "boot", "boot entries" },563 .{ "fmt", "format configuration.nix" },564 .{ "repl", "nix repl with nixpkgs" },565 .{ "option <q>", "search nixos options" },566 .{ "pkg", "inspect packages" },567 .{ "pin", "pin flake inputs" },568 .{ "hash", "compute nix hash" },569 .{ "fetch", "fetch and hash a url" },570 .{ "hello", "list machines" },571 .{ "help", "this message" },572 };573 for (cmds) |cmd| {574 p(" {s:<18} {s}{s}{s}\n", .{ cmd[0], c(DIM), cmd[1], c(RESET) });575 }576 p("\n {s}flags: --on <machine> --dry --check --no-apply --last <n> --all --json{s}\n", .{ c(DIM), c(RESET) });577}578579580581pub fn searchResult(pkg: types.NixPackage) void {582 p(" {s}{s}{s} {s}{s}{s} {s}\n", .{ c(PINK), pkg.pname, c(RESET), c(DIM), pkg.version, c(RESET), pkg.description });583}584585pub fn searchHeader(query: []const u8, count: usize) void {586 p("{s}search nixpkgs {s} {s}{d} results{s}\n\n", .{ hdr(), query, c(DIM), count, c(RESET) });587}588589590591pub fn genCurrent(n: u32) void {592 p("{d}\n", .{n});593}594595pub fn done() void {596 p("{s}done\n", .{hdr()});597}598599pub fn runStart(pkg: []const u8) void {600 p("{s}running {s}{s}{s}\n", .{ hdr(), c(PINK), pkg, c(RESET) });601}602603pub fn buildStart(attr: []const u8) void {604 p("{s}building {s}\n", .{ hdr(), attr });605}606607pub fn buildDone() void {608 p("{s}done\n", .{hdr()});609}610611pub fn pinDone(input: []const u8, commit: []const u8) void {612 p("{s}pinned {s} -> {s}\n", .{ hdr(), input, commit });613}614615pub fn unpinDone(input: []const u8) void {616 p("{s}unpinned {s}\n", .{ hdr(), input });617}618619pub fn hashResult(hash: []const u8) void {620 p("{s}\n", .{hash});621}622623pub fn fetchResult(hash: []const u8, url: []const u8) void {624 _ = url;625 p("{s}\n", .{hash});626}627628pub fn storeRepairStart() void {629 p("{s}repairing store\n", .{hdr()});630}631632pub fn storeVerifyStart() void {633 p("{s}verifying store\n", .{hdr()});634}635636pub fn storeGcDone(freed: []const u8) void {637 p("{s}freed {s}\n", .{ hdr(), freed });638}639640pub fn storePath(path: []const u8) void {641 p("{s}\n", .{path});642}643644pub fn pkgResult(key: []const u8, val: []const u8) void {645 p(" {s:<16} {s}\n", .{ key, val });646}647648pub fn channelDone() void {649 p("{s}done\n", .{hdr()});650}651652pub fn flakeShowHeader(name: []const u8) void {653 p("{s}{s}\n\n", .{ hdr(), name });654}655656pub fn flakeShowEntry(kind: []const u8, name: []const u8, desc: []const u8) void {657 p(" {s:<12} {s:<24} {s}\n", .{ kind, name, desc });658}