const std = @import("std"); const Allocator = std.mem.Allocator; const Editor = @import("Editor.zig"); const File = std.fs.File; const GPA = std.heap.GeneralPurposeAllocator(.{}); const RawMode = @import("RawMode.zig"); var log_file: ?File = null; pub fn log( comptime level: std.log.Level, comptime scope: @TypeOf(.EnumLiteral), comptime format: []const u8, args: anytype, ) void { const file = if (log_file) |f| f else std.io.getStdErr(); const writer = file.writer(); nosuspend writer.print( "[" ++ @tagName(scope) ++ ":" ++ level.asText() ++ "] " ++ format ++ "\n", args, ) catch return; } pub fn main() !void { var gpa = GPA{}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); const log_file_name = try std.fmt.allocPrint( allocator, "/tmp/es.{}.log", .{std.os.linux.getpid()}, ); defer { std.log.info("Logs in {s}", .{log_file_name}); allocator.free(log_file_name); } log_file = try std.fs.createFileAbsolute( log_file_name, .{ .exclusive = true, .truncate = true }, ); defer { log_file.?.close(); log_file = null; } const raw_mode = try RawMode.init(); defer raw_mode.deinit(); var editor = try Editor.init(allocator); defer editor.deinit(); try processCommandLineArgs(allocator, &editor); try editor.setStatusMessage("C-x C-s = Save | C-x C-c = Quit | C-s = Search", .{}); while (!editor.should_exit) { try editor.refreshScreen(); try editor.processKeypress(); } } fn processCommandLineArgs(allocator: Allocator, editor: *Editor) !void { const args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args); if (args.len <= 1) { // Do nothing } else if (args.len == 2) { try editor.open(args[1]); } else { std.log.err("What am I to do with {} arguments?", .{args.len}); return error.CommandLineArgs; } }