diff --git a/src/app.zig b/src/app.zig index 542957b..ffad8b9 100644 --- a/src/app.zig +++ b/src/app.zig @@ -94,7 +94,7 @@ pub const Image = struct { pub fn deinit(self: @This(), alloc: std.mem.Allocator) void { if (self.data) |data| { var d = data; - d.deinit(); + d.deinit(alloc); } if (self.path) |path| alloc.free(path); } @@ -108,6 +108,7 @@ const App = @This(); alloc: std.mem.Allocator, should_quit: bool, vx: vaxis.Vaxis = undefined, +tty_buffer: [1024]u8 = undefined, tty: vaxis.Tty = undefined, loop: vaxis.Loop(Event) = undefined, state: State = .normal, @@ -149,15 +150,14 @@ pub fn init(alloc: std.mem.Allocator, entry_dir: ?[]const u8) !App { .alloc = alloc, .should_quit = false, .vx = vx, - .tty = try vaxis.Tty.init(), .directories = try Directories.init(alloc, entry_dir), .help_menu = help_menu, - .text_input = vaxis.widgets.TextInput.init(alloc, &vx.unicode), + .text_input = vaxis.widgets.TextInput.init(alloc), .actions = CircStack(Action, actions_len).init(), .last_known_height = vx.window().height, .images = .{ .cache = .init(alloc) }, }; - + app.tty = try vaxis.Tty.init(&app.tty_buffer); app.loop = vaxis.Loop(Event){ .vaxis = &app.vx, .tty = &app.tty, @@ -191,7 +191,7 @@ pub fn deinit(self: *App) void { self.help_menu.deinit(); self.directories.deinit(); self.text_input.deinit(); - self.vx.deinit(self.alloc, self.tty.anyWriter()); + self.vx.deinit(self.alloc, self.tty.writer()); self.tty.deinit(); if (self.file_logger) |file_logger| file_logger.deinit(); @@ -222,8 +222,8 @@ pub fn run(self: *App) !void { try self.loop.start(); defer self.loop.stop(); - try self.vx.enterAltScreen(self.tty.anyWriter()); - try self.vx.queryTerminal(self.tty.anyWriter(), 1 * std.time.ns_per_s); + try self.vx.enterAltScreen(self.tty.writer()); + try self.vx.queryTerminal(self.tty.writer(), 1 * std.time.ns_per_s); self.vx.caps.kitty_graphics = true; while (!self.should_quit) { @@ -248,9 +248,7 @@ pub fn run(self: *App) !void { try self.drawer.draw(self); - var buffered = self.tty.bufferedWriter(); - try self.vx.render(buffered.writer().any()); - try buffered.flush(); + try self.vx.render(self.tty.writer()); } if (config.empty_trash_on_exit) { diff --git a/src/drawer.zig b/src/drawer.zig index cd99da0..31acc22 100644 --- a/src/drawer.zig +++ b/src/drawer.zig @@ -224,10 +224,11 @@ fn drawFilePreview( } else { if (cache_entry.data == null) { const path = try app.alloc.dupe(u8, self.current_item_path); - processImage(app, path) catch break :unsupported; + var buffer: [1024]u8 = undefined; + processImage(app, path, &buffer) catch break :unsupported; } - if (app.vx.transmitImage(app.alloc, app.tty.anyWriter(), &cache_entry.data.?, .rgba)) |img| { + if (app.vx.transmitImage(app.alloc, app.tty.writer(), &cache_entry.data.?, .rgba)) |img| { img.draw(preview_win, .{ .scale = .contain }) catch |err| { const message = try std.fmt.allocPrint(app.alloc, "Failed to draw image to screen - {}.", .{err}); defer app.alloc.free(message); @@ -240,7 +241,7 @@ fn drawFilePreview( break :file; }; cache_entry.image = img; - cache_entry.data.?.deinit(); + cache_entry.data.?.deinit(app.alloc); cache_entry.data = null; } else |_| { break :unsupported; @@ -250,7 +251,8 @@ fn drawFilePreview( break :file; } else { const path = try app.alloc.dupe(u8, self.current_item_path); - processImage(app, path) catch break :unsupported; + var buffer: [1024]u8 = undefined; + processImage(app, path, &buffer) catch break :unsupported; } break :file; @@ -350,12 +352,12 @@ fn drawFileInfo( // Time created / last modified if (self.verbose) lbl: { - var maybe_meta: ?std.fs.File.Metadata = null; + var maybe_meta: ?std.fs.File.Stat = null; if (entry.kind == .directory) { - maybe_meta = directories.dir.metadata() catch break :lbl; + maybe_meta = directories.dir.stat() catch break :lbl; } else if (entry.kind == .file) { var file = directories.dir.openFile(entry.name, .{}) catch break :lbl; - maybe_meta = file.metadata() catch break :lbl; + maybe_meta = file.stat() catch break :lbl; } const meta = maybe_meta orelse break :lbl; @@ -365,14 +367,14 @@ fn drawFileInfo( defer local.deinit(); const ctime_instant = zeit.instant(.{ - .source = .{ .unix_nano = meta.created().? }, + .source = .{ .unix_nano = meta.ctime }, .timezone = &local, }) catch break :lbl; const ctime = ctime_instant.time(); ctime.strftime(fbs.writer().any(), "Created: %Y-%m-%d %H:%M:%S\n") catch break :lbl; const mtime_instant = zeit.instant(.{ - .source = .{ .unix_nano = meta.modified() }, + .source = .{ .unix_nano = meta.mtime }, .timezone = &local, }) catch break :lbl; const mtime = mtime_instant.time(); @@ -441,9 +443,9 @@ fn drawFileInfo( break :lbl 0; }; - if (size) |s| try fbs.writer().print("{s}{:.2}\n", .{ + if (size) |s| try fbs.writer().print("{s}{B:.2}\n", .{ if (self.verbose) "Size: " else "", - std.fmt.fmtIntSizeDec(s), + s, }); // Extension. @@ -628,7 +630,7 @@ fn drawNotification( }, .{ .wrap = .word }); } -fn processImage(app: *App, path: []const u8) error{ Unsupported, OutOfMemory }!void { +fn processImage(app: *App, path: []const u8, buffer: *[1024]u8) error{ Unsupported, OutOfMemory }!void { app.images.cache.put(path, .{ .path = path, .status = .processing }) catch { const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to add image to cache.", .{path}); defer app.alloc.free(message); @@ -640,12 +642,13 @@ fn processImage(app: *App, path: []const u8) error{ Unsupported, OutOfMemory }!v const load_img_thread = std.Thread.spawn(.{}, loadImage, .{ app, path, + buffer, }) catch return error.Unsupported; load_img_thread.detach(); } -fn loadImage(app: *App, path: []const u8) error{ Unsupported, OutOfMemory }!void { - const data = vaxis.zigimg.Image.fromFilePath(app.alloc, path) catch { +fn loadImage(app: *App, path: []const u8, buffer: *[1024]u8) error{ Unsupported, OutOfMemory }!void { + const data = vaxis.zigimg.Image.fromFilePath(app.alloc, path, buffer) catch { const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to read image from path.", .{path}); defer app.alloc.free(message); app.notification.write(message, .err) catch {}; diff --git a/src/event_handlers.zig b/src/event_handlers.zig index cf1f245..fd58e1b 100644 --- a/src/event_handlers.zig +++ b/src/event_handlers.zig @@ -133,7 +133,7 @@ pub fn handleNormalEvent( } }, .image_ready => {}, - .winsize => |ws| try app.vx.resize(app.alloc, app.tty.anyWriter(), ws), + .winsize => |ws| try app.vx.resize(app.alloc, app.tty.writer(), ws), } } @@ -283,7 +283,7 @@ pub fn handleInputEvent(app: *App, event: App.Event) !void { } }, .image_ready => {}, - .winsize => |ws| try app.vx.resize(app.alloc, app.tty.anyWriter(), ws), + .winsize => |ws| try app.vx.resize(app.alloc, app.tty.writer(), ws), } } @@ -298,6 +298,6 @@ pub fn handleHelpMenuEvent(app: *App, event: App.Event) !void { } }, .image_ready => {}, - .winsize => |ws| try app.vx.resize(app.alloc, app.tty.anyWriter(), ws), + .winsize => |ws| try app.vx.resize(app.alloc, app.tty.writer(), ws), } } diff --git a/src/events.zig b/src/events.zig index f0d3110..6f0862b 100644 --- a/src/events.zig +++ b/src/events.zig @@ -419,8 +419,8 @@ pub fn traverseRight(app: *App) !void { }, .file => { if (environment.getEditor()) |editor| { - try app.vx.exitAltScreen(app.tty.anyWriter()); - try app.vx.resetState(app.tty.anyWriter()); + try app.vx.exitAltScreen(app.tty.writer()); + try app.vx.resetState(app.tty.writer()); app.loop.stop(); environment.openFile(app.alloc, app.directories.dir, entry.name, editor) catch |err| { @@ -430,8 +430,8 @@ pub fn traverseRight(app: *App) !void { }; try app.loop.start(); - try app.vx.enterAltScreen(app.tty.anyWriter()); - try app.vx.enableDetectedFeatures(app.tty.anyWriter()); + try app.vx.enterAltScreen(app.tty.writer()); + try app.vx.enableDetectedFeatures(app.tty.writer()); app.vx.queueRefresh(); } else { app.notification.write("Can not open file - $EDITOR not set.", .warn) catch {}; diff --git a/src/file_logger.zig b/src/file_logger.zig index 0c7f399..37f43e2 100644 --- a/src/file_logger.zig +++ b/src/file_logger.zig @@ -53,7 +53,9 @@ pub fn write(self: FileLogger, msg: []const u8, level: LogLevel) !void { defer file.unlock(); try file.seekFromEnd(0); - try file.writer().print( + var buffer: [1024]u8 = undefined; + var file_writer = file.writer(&buffer).interface; + try file_writer.print( "({d}) {s}: {s}\n", .{ std.time.timestamp(), LogLevel.toString(level), msg }, ); diff --git a/src/list.zig b/src/list.zig index a8e49d7..4365fdf 100644 --- a/src/list.zig +++ b/src/list.zig @@ -12,21 +12,21 @@ pub fn List(comptime T: type) type { pub fn init(alloc: std.mem.Allocator) Self { return Self{ .alloc = alloc, - .items = std.ArrayList(T).init(alloc), + .items = .empty, .selected = 0, }; } pub fn deinit(self: *Self) void { - self.items.deinit(); + self.items.deinit(self.alloc); } pub fn append(self: *Self, item: T) !void { - try self.items.append(item); + try self.items.append(self.alloc, item); } pub fn clear(self: *Self) void { - self.items.clearAndFree(); + self.items.clearAndFree(self.alloc); self.selected = 0; } diff --git a/src/main.zig b/src/main.zig index 457637e..adc9a00 100644 --- a/src/main.zig +++ b/src/main.zig @@ -104,7 +104,7 @@ pub fn main() !void { } if (opts.version) { - std.debug.print("jido v{}\n", .{options.version}); + std.debug.print("jido v{f}\n", .{options.version}); return; } @@ -151,8 +151,12 @@ pub fn main() !void { // Must be printed after app has deinit as part of that process clears // the screen. if (last_dir) |path| { - const stdout = std.io.getStdOut().writer(); + var stdout_buffer: [std.fs.max_path_bytes]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); + const stdout = &stdout_writer.interface; stdout.print("{s}\n", .{path}) catch {}; + stdout.flush() catch {}; + alloc.free(path); } }