forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_completions.zig
76 lines (65 loc) · 2.25 KB
/
shell_completions.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
const std = @import("std");
const bun = @import("../global.zig");
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
pub const Shell = enum {
unknown,
bash,
zsh,
fish,
const bash_completions = @embedFile("../../completions/bun.bash");
const zsh_completions = @embedFile("../../completions/bun.zsh");
const fish_completions = @embedFile("../../completions/bun.fish");
pub fn completions(this: Shell) []const u8 {
return switch (this) {
.bash => std.mem.span(bash_completions),
.zsh => std.mem.span(zsh_completions),
.fish => std.mem.span(fish_completions),
else => "",
};
}
pub fn fromEnv(comptime Type: type, SHELL: Type) Shell {
const basename = std.fs.path.basename(SHELL);
if (strings.eqlComptime(basename, "bash")) {
return Shell.bash;
} else if (strings.eqlComptime(basename, "zsh")) {
return Shell.zsh;
} else if (strings.eqlComptime(basename, "fish")) {
return Shell.fish;
} else {
return Shell.unknown;
}
}
};
commands: []const []const u8 = &[_][]u8{},
descriptions: []const []const u8 = &[_][]u8{},
flags: []const []const u8 = &[_][]u8{},
shell: Shell = Shell.unknown,
pub fn print(this: @This()) void {
defer Output.flush();
var writer = Output.writer();
if (this.commands.len == 0) return;
const delimiter = if (this.shell == Shell.fish) " " else "\n";
writer.writeAll(this.commands[0]) catch return;
if (this.descriptions.len > 0) {
writer.writeAll("\t") catch return;
writer.writeAll(this.descriptions[0]) catch return;
}
if (this.commands.len > 1) {
for (this.commands[1..]) |cmd, i| {
writer.writeAll(delimiter) catch return;
writer.writeAll(cmd) catch return;
if (this.descriptions.len > 0) {
writer.writeAll("\t") catch return;
writer.writeAll(this.descriptions[i]) catch return;
}
}
}
}