prompt: add nix segment

This commit is contained in:
LordMZTE 2024-04-14 22:06:02 +02:00
parent 101dbc7181
commit 67e4503e7e
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 35 additions and 0 deletions

View file

@ -51,6 +51,7 @@ pub fn main() !void {
return error.MissingEnv,
10,
),
.nix_name = @import("nix.zig").findNixShellName(),
};
var buf: [1024 * 8]u8 = undefined;

View file

@ -0,0 +1,22 @@
const std = @import("std");
/// If we're likely in a Nix shell, return the name of that shell or "?" if it's unknown,
/// null otherwise.
pub fn findNixShellName() ?[]const u8 {
return if (isInNixShell()) std.posix.getenv("name") orelse "?" else null;
}
fn isInNixShell() bool {
if (std.posix.getenv("IN_NIX_SHELL")) |_| return true;
var path_iter = std.mem.splitScalar(
u8,
std.posix.getenv("PATH") orelse return false,
':',
);
while (path_iter.next()) |p|
if (std.mem.startsWith(u8, p, "/nix/store")) return true;
return false;
}

View file

@ -22,6 +22,7 @@ const symbols = struct {
const root = "";
const watch = "";
const jobs = "";
const nix = "󱄅";
};
pub const Options = struct {
@ -29,6 +30,7 @@ pub const Options = struct {
mode: FishMode,
duration: u32,
jobs: u32,
nix_name: ?[]const u8,
};
pub fn render(writer: anytype, options: Options) !void {
@ -56,6 +58,7 @@ fn Renderer(comptime Writer: type) type {
try self.setStyle(.{ .background = left_color });
try self.renderDuration();
try self.renderJobs();
try self.renderNix();
try self.renderCwd();
self.renderGit() catch |err| {
switch (err) {
@ -147,6 +150,15 @@ fn Renderer(comptime Writer: type) type {
try self.writer.print(" {s} {}", .{ symbols.jobs, self.options.jobs });
}
fn renderNix(self: *Self) !void {
if (self.options.nix_name) |name| {
try self.drawLeftSep(.Blue);
try self.setStyle(.{ .background = .Blue, .foreground = .Black});
try self.writer.print(" {s} {s}", .{symbols.nix, name });
}
}
fn renderCwd(self: *Self) !void {
var cwd_buf: [512]u8 = undefined;
const cwd = try std.posix.getcwd(&cwd_buf);