mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 00:36:07 +03:00
49 lines
1.2 KiB
Zig
49 lines
1.2 KiB
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
/// Append a value to an environment variable such as PATH.
|
|
/// The returned value is always allocated so it must be freed.
|
|
pub fn appendEnv(
|
|
alloc: Allocator,
|
|
current: []const u8,
|
|
value: []const u8,
|
|
) ![]u8 {
|
|
// If there is no prior value, we return it as-is
|
|
if (current.len == 0) return try alloc.dupe(u8, value);
|
|
|
|
// Otherwise we must prefix.
|
|
const sep = switch (builtin.os.tag) {
|
|
.windows => ";",
|
|
else => ":",
|
|
};
|
|
|
|
return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{
|
|
current,
|
|
sep,
|
|
value,
|
|
});
|
|
}
|
|
|
|
test "appendEnv empty" {
|
|
const testing = std.testing;
|
|
const alloc = testing.allocator;
|
|
|
|
const result = try appendEnv(alloc, "", "foo");
|
|
defer alloc.free(result);
|
|
try testing.expectEqualStrings(result, "foo");
|
|
}
|
|
|
|
test "appendEnv existing" {
|
|
const testing = std.testing;
|
|
const alloc = testing.allocator;
|
|
|
|
const result = try appendEnv(alloc, "a:b", "foo");
|
|
defer alloc.free(result);
|
|
if (builtin.os.tag == .windows) {
|
|
try testing.expectEqualStrings(result, "a:b;foo");
|
|
} else {
|
|
try testing.expectEqualStrings(result, "a:b:foo");
|
|
}
|
|
}
|