diff --git a/src/main.zig b/src/main.zig index 0c81518..5ea3feb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -120,6 +120,13 @@ pub fn main() !void { else => area.allocator(), }; + var threaded_io: std.Io.Threaded = .init(allocator, .{}); + errdefer threaded_io.deinit(); + const io = threaded_io.io(); + + var diff_input: [:0]u8 = undefined; + defer allocator.free(diff_input); + // argument handling { var arg_it = try std.process.argsWithAllocator(allocator); @@ -127,21 +134,39 @@ pub fn main() !void { // TODO may there be other options? // usage: tui-diff - // skip own executable name _ = arg_it.skip(); + + // only handle the first argument otherwise ignore! + if (arg_it.next()) |file| { + var buffer: [4096]u8 = undefined; + var stream = std.Io.File.readerStreaming( + try std.Io.Dir.openFileAbsolute( + io, + file, + .{ .mode = .read_only }, + ), + io, + &buffer, + ); + const reader = &stream.interface; + diff_input = try reader.allocRemainingAlignedSentinel( + allocator, + .unlimited, + .of(u8), + 0, + ); + } else { + // TODO detect VCS in the current working directory (and traversing upwards if none found at point?) + } } // tui creation errdefer |err| log.err("Application Error: {any}", .{err}); - var threaded_io: std.Io.Threaded = .init(allocator, .{}); - errdefer threaded_io.deinit(); - const io = threaded_io.io(); - var renderer = zterm.Renderer.Buffered.init(allocator); defer renderer.deinit(); - var app: App = .init(io, try .init(allocator, diff)); + var app: App = .init(io, try .init(allocator, diff_input)); defer app.model.deinit(allocator); var element_root: tui_diff.elements.Root(App) = .init(allocator);