From 990c12797c589ef752cbddc33fd57999a9a72744 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 10 Sep 2024 20:21:41 +0200 Subject: [PATCH] feat: add a very simple info view --- src/tui/info_view.zig | 64 +++++++++++++++++++++++++++++++++++++++++++ src/tui/mainview.zig | 10 +++++++ 2 files changed, 74 insertions(+) create mode 100644 src/tui/info_view.zig diff --git a/src/tui/info_view.zig b/src/tui/info_view.zig new file mode 100644 index 0000000..709bf5b --- /dev/null +++ b/src/tui/info_view.zig @@ -0,0 +1,64 @@ +const std = @import("std"); +const Allocator = @import("std").mem.Allocator; +const Plane = @import("renderer").Plane; +const Widget = @import("Widget.zig"); + +pub const name = @typeName(Self); + +const Self = @This(); + +allocator: std.mem.Allocator, +plane: Plane, + +view_rows: usize = 0, +lines: std.ArrayList([]const u8), + +pub fn create(allocator: Allocator, parent: Plane) !Widget { + const self: *Self = try allocator.create(Self); + self.* = .{ + .allocator = allocator, + .plane = try Plane.init(&(Widget.Box{}).opts(name), parent), + .lines = std.ArrayList([]const u8).init(allocator), + }; + return Widget.to(self); +} + +pub fn deinit(self: *Self, allocator: Allocator) void { + self.clear(); + self.lines.deinit(); + self.plane.deinit(); + allocator.destroy(self); +} + +pub fn clear(self: *Self) void { + for (self.lines.items) |line| + self.allocator.free(line); + self.lines.clearRetainingCapacity(); +} + +pub fn handle_resize(self: *Self, pos: Widget.Box) void { + self.plane.move_yx(@intCast(pos.y), @intCast(pos.x)) catch return; + self.plane.resize_simple(@intCast(pos.h), @intCast(pos.w)) catch return; + self.view_rows = pos.h; +} + +pub fn set_content(self: *Self, content: []const u8) !void { + self.clear(); + var iter = std.mem.splitScalar(u8, content, '\n'); + while (iter.next()) |line| + (try self.lines.addOne()).* = try self.allocator.dupe(u8, line); +} + +pub fn render(self: *Self, theme: *const Widget.Theme) bool { + self.plane.set_base_style(" ", theme.panel); + self.plane.erase(); + self.plane.home(); + for (self.lines.items) |line| { + _ = self.plane.putstr(line) catch {}; + if (self.plane.cursor_y() >= self.view_rows - 1) + return false; + self.plane.cursor_move_yx(-1, 0) catch {}; + self.plane.cursor_move_rel(1, 0) catch {}; + } + return false; +} diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 116b3e6..420d7de 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -24,6 +24,7 @@ const home = @import("home.zig"); const logview = @import("logview.zig"); const filelist_view = @import("filelist_view.zig"); +const info_view = @import("info_view.zig"); const Self = @This(); const Commands = command.Collection(cmds); @@ -366,6 +367,8 @@ const cmds = struct { pub fn toggle_panel(self: *Self, _: Ctx) Result { if (self.is_panel_view_showing(logview)) try self.toggle_panel_view(logview, false) + else if (self.is_panel_view_showing(info_view)) + try self.toggle_panel_view(info_view, false) else if (self.is_panel_view_showing(filelist_view)) try self.toggle_panel_view(filelist_view, false) else @@ -736,3 +739,10 @@ fn clear_find_in_files_results(self: *Self, file_list_type: FileListType) void { self.file_list_type = file_list_type; fl.reset(); } + +fn add_info_content(self: *Self, content: []const u8) tp.result { + if (!self.is_panel_view_showing(info_view)) + _ = self.toggle_panel_view(info_view, false) catch |e| return tp.exit_error(e, @errorReturnTrace()); + const info = self.get_panel_view(info_view) orelse @panic("info_view missing"); + info.set_content(content) catch |e| return tp.exit_error(e, @errorReturnTrace()); +}