-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.zig
More file actions
177 lines (164 loc) · 5.16 KB
/
Copy pathshell.zig
File metadata and controls
177 lines (164 loc) · 5.16 KB
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
167
168
169
170
171
172
173
174
175
176
177
const std = @import("std");
pub const usage = "gem init nushell|bash|zsh|fish|elvish";
pub fn writeScript(stdout: *std.Io.Writer, name: []const u8, gem_exe: []const u8) !bool {
if (std.mem.eql(u8, name, "nushell")) {
try stdout.writeAll(
\\def --env gem [...args] {
\\
);
try stdout.print(" let gem_exe = \"{s}\"\n", .{gem_exe});
try stdout.writeAll(nushell);
return true;
}
if (std.mem.eql(u8, name, "bash") or std.mem.eql(u8, name, "zsh")) {
try stdout.writeAll(
\\gem() {
\\
);
try stdout.print(" local gem_exe=\"{s}\"\n", .{gem_exe});
try stdout.writeAll(posix);
return true;
}
if (std.mem.eql(u8, name, "fish")) {
try stdout.writeAll(
\\function gem
\\
);
try stdout.print(" set gem_exe \"{s}\"\n", .{gem_exe});
try stdout.writeAll(fish);
return true;
}
if (std.mem.eql(u8, name, "elvish")) {
try stdout.writeAll(
\\use str
\\
\\fn gem {|@args|
\\
);
try stdout.print(" var gem_exe = \"{s}\"\n", .{gem_exe});
try stdout.writeAll(elvish);
return true;
}
return false;
}
const nushell =
\\ let command_result = (^$gem_exe --shell ...$args | complete)
\\ mut status = ""
\\ mut action = ""
\\ mut path = ""
\\ for line in ($command_result.stdout | lines) {
\\ let fields = ($line | split row "\t")
\\ let key = ($fields | get 0)
\\ let value = if (($fields | length) > 1) { $fields | get 1 } else { "" }
\\ if $key == "status" { $status = $value }
\\ if $key == "action" { $action = $value }
\\ if $key == "path" { $path = $value }
\\ if $key == "message" {
\\ if $status == "error" {
\\ print --stderr $value
\\ } else {
\\ print $value
\\ }
\\ }
\\ }
\\ if $command_result.exit_code != 0 { return }
\\ if (($action in ["entered" "cloned" "fixed"]) and ($path | is-not-empty)) { cd $path }
\\}
\\
;
const posix =
\\ local out code gem_status action gem_path key value
\\ out="$("$gem_exe" --shell "$@")"; code=$?
\\ gem_status=""
\\ action=""
\\ gem_path=""
\\ while IFS=$'\t' read -r key value; do
\\ case "$key" in
\\ status) gem_status="$value" ;;
\\ action) action="$value" ;;
\\ path) gem_path="$value" ;;
\\ message)
\\ if [ "$gem_status" = error ]; then
\\ printf '%s\n' "$value" >&2
\\ else
\\ printf '%s\n' "$value"
\\ fi
\\ ;;
\\ esac
\\ done <<< "$out"
\\ [ "$code" -ne 0 ] && return "$code"
\\ case "$action" in
\\ entered|cloned|fixed) [ -n "$gem_path" ] && cd "$gem_path" ;;
\\ esac
\\}
\\
;
const fish =
\\ set out ($gem_exe --shell $argv)
\\ set code $status
\\ set gem_status ''
\\ set action ''
\\ set gem_path ''
\\ for line in $out
\\ set fields (string split \t $line)
\\ switch $fields[1]
\\ case status
\\ set gem_status $fields[2]
\\ case action
\\ set action $fields[2]
\\ case path
\\ set gem_path $fields[2]
\\ case message
\\ if test "$gem_status" = error
\\ printf '%s\n' $fields[2] >&2
\\ else
\\ printf '%s\n' $fields[2]
\\ end
\\ end
\\ end
\\ test $code -ne 0; and return $code
\\ switch $action
\\ case entered cloned fixed
\\ test -n "$gem_path"; and cd "$gem_path"
\\ end
\\end
\\
;
const elvish =
\\ var out = [($gem_exe --shell $@args)]
\\ var action = ''
\\ var gem_path = ''
\\ for line $out {
\\ var fields = [(str:split "\t" $line)]
\\ if (eq $fields[0] action) {
\\ set action = $fields[1]
\\ }
\\ if (eq $fields[0] path) {
\\ set gem_path = $fields[1]
\\ }
\\ if (eq $fields[0] message) {
\\ echo $fields[1]
\\ }
\\ }
\\ if (and (has-value [entered cloned fixed] $action) (not-eq $gem_path '')) {
\\ cd $gem_path
\\ }
\\}
\\
;
test "writes scripts for supported shells" {
var buffer: [4096]u8 = undefined;
var writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(try writeScript(&writer, "nushell", "/bin/gem"));
try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), "/bin/gem") != null);
writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(try writeScript(&writer, "bash", "/bin/gem"));
writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(try writeScript(&writer, "zsh", "/bin/gem"));
writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(try writeScript(&writer, "fish", "/bin/gem"));
writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(try writeScript(&writer, "elvish", "/bin/gem"));
writer = std.Io.Writer.fixed(&buffer);
try std.testing.expect(!try writeScript(&writer, "unknown", "/bin/gem"));
}