mirror of
https://mzte.de/git/LordMZTE/dotfiles.git
synced 2024-12-13 19:52:58 +01:00
prompt: add nix segment
This commit is contained in:
parent
101dbc7181
commit
67e4503e7e
3 changed files with 35 additions and 0 deletions
|
@ -51,6 +51,7 @@ pub fn main() !void {
|
||||||
return error.MissingEnv,
|
return error.MissingEnv,
|
||||||
10,
|
10,
|
||||||
),
|
),
|
||||||
|
.nix_name = @import("nix.zig").findNixShellName(),
|
||||||
};
|
};
|
||||||
|
|
||||||
var buf: [1024 * 8]u8 = undefined;
|
var buf: [1024 * 8]u8 = undefined;
|
||||||
|
|
22
scripts/prompt/src/nix.zig
Normal file
22
scripts/prompt/src/nix.zig
Normal 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;
|
||||||
|
}
|
|
@ -22,6 +22,7 @@ const symbols = struct {
|
||||||
const root = "";
|
const root = "";
|
||||||
const watch = "";
|
const watch = "";
|
||||||
const jobs = "";
|
const jobs = "";
|
||||||
|
const nix = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Options = struct {
|
pub const Options = struct {
|
||||||
|
@ -29,6 +30,7 @@ pub const Options = struct {
|
||||||
mode: FishMode,
|
mode: FishMode,
|
||||||
duration: u32,
|
duration: u32,
|
||||||
jobs: u32,
|
jobs: u32,
|
||||||
|
nix_name: ?[]const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn render(writer: anytype, options: Options) !void {
|
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.setStyle(.{ .background = left_color });
|
||||||
try self.renderDuration();
|
try self.renderDuration();
|
||||||
try self.renderJobs();
|
try self.renderJobs();
|
||||||
|
try self.renderNix();
|
||||||
try self.renderCwd();
|
try self.renderCwd();
|
||||||
self.renderGit() catch |err| {
|
self.renderGit() catch |err| {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
|
@ -147,6 +150,15 @@ fn Renderer(comptime Writer: type) type {
|
||||||
try self.writer.print(" {s} {}", .{ symbols.jobs, self.options.jobs });
|
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 {
|
fn renderCwd(self: *Self) !void {
|
||||||
var cwd_buf: [512]u8 = undefined;
|
var cwd_buf: [512]u8 = undefined;
|
||||||
const cwd = try std.posix.getcwd(&cwd_buf);
|
const cwd = try std.posix.getcwd(&cwd_buf);
|
||||||
|
|
Loading…
Reference in a new issue