-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt.zig
166 lines (147 loc) · 4.29 KB
/
prompt.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Configurable environment variables:
//!
//! $PROMPT_PREFIX
//! override to control what's displayed at the start of the prompt line
//!
//! $PROMPT_BARE
//! set to enable a very minimal prompt
//!
//! $PROMPT_FULL_HOST
//! shows the full hostname (bash: \H \h -- zsh: %M %m)
//!
//! $PROMPT_LONG
//! display username@host even if local
//!
//! $PROMPT_PATH
//! set to use things like Zsh's hashed paths
//! export PROMPT_PATH="$(print -P '%~')"
//!
//! $PROMPT_RETURN_CODE
//! set to display the exit code of the previous program
//! export PROMPT_RETURN_CODE=$?
//!
//! $PROMPT_JOBS
//! set to "{running} {suspended}" jobs (separated by space, defaults to 0 0)
//! for zsh: (https://unix.stackexchange.com/a/68635)
//! export PROMPT_JOBS=${(M)#${jobstates%%:*}:#running}\ ${(M)#${jobstates%%:*}:#suspended}
//!
//! $PROMPT_FULL_VENV
//! set to show the full name of virtualenvs vs an indicator
//!
//! $PROMPT_LINE_BEFORE, $PROMPT_LINE_AFTER
//! set for a multiline prompt. if set, add newline before/after each prompt
//!
//! $PROMPT_HR
//! set to $COLUMNS to print a horizontal rule before each prompt line
const std = @import("std");
const stdout = std.io.getStdOut().writer();
const os = std.os;
const Allocator = std.mem.Allocator;
const funcs = @import("funcs.zig");
pub const Escapes = struct {
o: [:0]const u8 = undefined,
c: [:0]const u8 = undefined,
pub fn init(open: [:0]const u8, close: [:0]const u8) Escapes {
return Escapes{ .o = open, .c = close };
}
};
const Shell = enum {
zsh,
bash,
unknown,
};
pub const C = .{
.reset = "\x1b[00m",
.bold = "\x1b[01m",
.italic = "\x1b[03m",
.underline = "\x1b[04m",
.reverse = "\x1b[07m",
.italic_off = "\x1b[23m",
.underline_off = "\x1b[24m",
.reverse_off = "\x1b[27m",
.default = "\x1b[91m",
.black = "\x1b[30m",
.red = "\x1b[31m",
.green = "\x1b[32m",
.yellow = "\x1b[33m",
.blue = "\x1b[34m",
.magenta = "\x1b[35m",
.cyan = "\x1b[36m",
.white = "\x1b[37m",
.bright_black = "\x1b[90m",
.bright_red = "\x1b[91m",
.bright_green = "\x1b[92m",
.bright_yellow = "\x1b[99m",
.bright_blue = "\x1b[94m",
.bright_magenta = "\x1b[95m",
.bright_cyan = "\x1b[96m",
.bright_white = "\x1b[97m",
};
pub var A: Allocator = undefined;
pub var E: Escapes = undefined;
pub var CWD: []u8 = undefined;
pub fn main() !void {
// allocator setup
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
A = arena.allocator();
// get the specified shell and initialize escape codes
var shell = Shell.unknown;
if (os.argv.len > 1) {
const arg = std.mem.span(os.argv[1]);
if (std.mem.eql(u8, arg, "zsh")) {
shell = Shell.zsh;
} else if ((std.mem.eql(u8, arg, "bash"))) {
shell = Shell.bash;
}
}
switch (shell) {
.zsh => {
E = Escapes.init("%{", "%}");
},
.bash => {
E = Escapes.init("\\[", "\\]");
},
else => {
E = Escapes.init("", "");
},
}
// state
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
CWD = try std.posix.getcwd(&buf);
const long = funcs.is_env_true("PROMPT_LONG");
// kick off any per-directory tasks
try funcs.set_kitty_tab_color();
// print prompt
try funcs.newline_if("PROMPT_LINE_BEFORE");
if (!funcs.is_env_true("PROMPT_BARE")) {
try funcs.hr();
try funcs.prefix();
try funcs.script();
try funcs.tab();
try funcs.screen();
try funcs.venv();
try funcs.date();
var show_sep = false;
// showing the host (and user, if not su/root) is unnecessary if local
if (funcs.is_root() or funcs.is_su() or long) {
show_sep = true;
try funcs.user();
}
if (!funcs.is_local() or long) {
show_sep = true;
try funcs.at();
try funcs.host();
}
// if no user or host, remove sep too
if (show_sep) {
try funcs.sep();
}
try funcs.path();
try funcs.repo();
try funcs.jobs();
try funcs.direnv();
try funcs.newline_if("PROMPT_LINE_AFTER");
}
try funcs.char();
}