aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-05 01:15:09 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-05 01:15:09 -0700
commit3ef55d10ae43254e32afe8c4cec3391edbce9b9a (patch)
treeebf4e4cbcb1a14c475a853e304a2731ef17e4233 /src
parent5ce171d92c95461c288a4fd2fc2ff38cf8609b12 (diff)
downloadbun-3ef55d10ae43254e32afe8c4cec3391edbce9b9a.tar.gz
bun-3ef55d10ae43254e32afe8c4cec3391edbce9b9a.tar.zst
bun-3ef55d10ae43254e32afe8c4cec3391edbce9b9a.zip
[bun run] Improve `fish` completions by adding a description
Diffstat (limited to 'src')
-rw-r--r--src/cli.zig2
-rw-r--r--src/cli/run_command.zig62
-rw-r--r--src/cli/shell_completions.zig10
3 files changed, 72 insertions, 2 deletions
diff --git a/src/cli.zig b/src/cli.zig
index 51d22db45..28fafeb55 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -682,6 +682,8 @@ pub const Command = struct {
completions = try RunCommand.completions(ctx, null, .all_plus_bun_js);
} else if (strings.eqlComptime(filter[0], "j")) {
completions = try RunCommand.completions(ctx, null, .bun_js);
+ } else if (strings.eqlComptime(filter[0], "z")) {
+ completions = try RunCommand.completions(ctx, null, .script_and_descriptions);
}
completions.print();
diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig
index 49f7469f7..968866f49 100644
--- a/src/cli/run_command.zig
+++ b/src/cli/run_command.zig
@@ -295,6 +295,7 @@ pub const RunCommand = struct {
all,
bun_js,
all_plus_bun_js,
+ script_and_descriptions,
};
pub fn completions(ctx: Command.Context, default_completions: ?[]const string, comptime filter: Filter) !ShellCompletions {
@@ -341,6 +342,7 @@ pub const RunCommand = struct {
}
var results = ResultList.init(ctx.allocator);
+ var descriptions = std.ArrayList(string).init(ctx.allocator);
if (default_completions) |defaults| {
try results.ensureUnusedCapacity(defaults.len);
@@ -397,12 +399,67 @@ pub const RunCommand = struct {
}
}
- if (filter == Filter.script or filter == Filter.all or filter == Filter.all_plus_bun_js) {
+ if (filter == Filter.script or filter == Filter.all or filter == Filter.all_plus_bun_js or filter == Filter.script_and_descriptions) {
if (root_dir_info.enclosing_package_json) |package_json| {
if (package_json.scripts) |scripts| {
try results.ensureUnusedCapacity(scripts.count());
+ if (filter == Filter.script_and_descriptions) {
+ try descriptions.ensureUnusedCapacity(scripts.count());
+ }
+
+ var max_description_len: usize = 20;
+ if (this_bundler.env.map.get("MAX_DESCRIPTION_LEN")) |max| {
+ if (std.fmt.parseInt(usize, max, 10) catch null) |max_len| {
+ max_description_len = max_len;
+ }
+ }
+
for (scripts.keys()) |key| {
- _ = results.getOrPutAssumeCapacity(key);
+ var entry_item = results.getOrPutAssumeCapacity(key);
+
+ if (filter == Filter.script_and_descriptions and max_description_len > 0) {
+ var description = scripts.get(key).?;
+
+ // When the command starts with something like
+ // NODE_OPTIONS='--max-heap-size foo' bar
+ // ^--------------------------------^ trim that
+ // that way, you can see the real command that's being run
+ if (description.len > 0) {
+ trimmer: {
+ if (description.len > 0 and strings.startsWith(description, "NODE_OPTIONS=")) {
+ if (strings.indexOfChar(description, '=')) |i| {
+ const delimiter: u8 = if (description.len > i + 1)
+ @as(u8, switch (description[i + 1]) {
+ '\'' => '\'',
+ '"' => '"',
+ else => ' ',
+ })
+ else
+ break :trimmer;
+
+ const delimiter_offset = @as(usize, if (delimiter == ' ') 1 else 2);
+ if (description.len > delimiter_offset + i) {
+ if (strings.indexOfChar(description[delimiter_offset + i ..], delimiter)) |j| {
+ description = std.mem.trim(u8, description[delimiter_offset + i ..][j + 1 ..], " ");
+ } else {
+ break :trimmer;
+ }
+ } else {
+ break :trimmer;
+ }
+ } else {
+ break :trimmer;
+ }
+ }
+ }
+
+ if (description.len > max_description_len) {
+ description = description[0..max_description_len];
+ }
+ }
+
+ try descriptions.insert(entry_item.index, description);
+ }
}
}
}
@@ -412,6 +469,7 @@ pub const RunCommand = struct {
strings.sortAsc(all_keys);
shell_out.commands = all_keys;
+ shell_out.descriptions = descriptions.toOwnedSlice();
return shell_out;
}
diff --git a/src/cli/shell_completions.zig b/src/cli/shell_completions.zig
index 295593d49..68938d480 100644
--- a/src/cli/shell_completions.zig
+++ b/src/cli/shell_completions.zig
@@ -35,6 +35,7 @@ pub const Shell = enum {
};
commands: []const []const u8 = &[_][]u8{},
+descriptions: []const []const u8 = &[_][]u8{},
flags: []const []const u8 = &[_][]u8{},
shell: Shell = Shell.unknown,
@@ -46,12 +47,21 @@ pub fn print(this: @This()) void {
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;
+ }
}
}
}