summaryrefslogtreecommitdiff
path: root/src/cmd/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/lib')
-rw-r--r--src/cmd/lib/types.zig63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig
new file mode 100644
index 0000000..1f0e162
--- /dev/null
+++ b/src/cmd/lib/types.zig
@@ -0,0 +1,63 @@
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+const ArrayList = std.ArrayList;
+
+pub const CommandStatus = union(enum) {
+ Code: u16,
+ ExitShell,
+};
+
+pub const OutputCapture = struct {
+ buffer: ArrayList(u8),
+
+ pub fn init(allocator: Allocator) OutputCapture {
+ return OutputCapture{
+ .buffer = ArrayList(u8).init(allocator),
+ };
+ }
+
+ pub fn deinit(self: *OutputCapture) void {
+ self.buffer.deinit();
+ }
+
+ pub fn write(self: *OutputCapture, data: []const u8) !void {
+ try self.buffer.appendSlice(data);
+ }
+
+ pub fn getContents(self: *const OutputCapture) []const u8 {
+ return self.buffer.items;
+ }
+};
+
+pub const InputSource = struct {
+ data: []const u8,
+ position: usize,
+
+ pub fn init(data: []const u8) InputSource {
+ return InputSource{
+ .data = data,
+ .position = 0,
+ };
+ }
+
+ pub fn readLine(self: *InputSource, allocator: Allocator) !?[]const u8 {
+ if (self.position >= self.data.len) {
+ return null; // EOF
+ }
+
+ var line_end = self.position;
+ while (line_end < self.data.len and self.data[line_end] != '\n') {
+ line_end += 1;
+ }
+
+ const line = self.data[self.position..line_end];
+ self.position = if (line_end < self.data.len) line_end + 1 else self.data.len;
+
+ // Remove trailing \r if present (DOS line endings)
+ if (line.len > 0 and line[line.len - 1] == '\r') {
+ return try allocator.dupe(u8, line[0 .. line.len - 1]);
+ } else {
+ return try allocator.dupe(u8, line);
+ }
+ }
+};