feat: add a very simple info view

This commit is contained in:
CJ van den Berg 2024-09-10 20:21:41 +02:00
parent b970031a7f
commit 990c12797c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 74 additions and 0 deletions

64
src/tui/info_view.zig Normal file
View file

@ -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;
}

View file

@ -24,6 +24,7 @@ const home = @import("home.zig");
const logview = @import("logview.zig"); const logview = @import("logview.zig");
const filelist_view = @import("filelist_view.zig"); const filelist_view = @import("filelist_view.zig");
const info_view = @import("info_view.zig");
const Self = @This(); const Self = @This();
const Commands = command.Collection(cmds); const Commands = command.Collection(cmds);
@ -366,6 +367,8 @@ const cmds = struct {
pub fn toggle_panel(self: *Self, _: Ctx) Result { pub fn toggle_panel(self: *Self, _: Ctx) Result {
if (self.is_panel_view_showing(logview)) if (self.is_panel_view_showing(logview))
try self.toggle_panel_view(logview, false) 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)) else if (self.is_panel_view_showing(filelist_view))
try self.toggle_panel_view(filelist_view, false) try self.toggle_panel_view(filelist_view, false)
else 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; self.file_list_type = file_list_type;
fl.reset(); 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());
}