Support multiple info lines in UI

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2024-07-31 13:58:49 +02:00
parent 961018e753
commit a393525212
6 changed files with 94 additions and 65 deletions

View File

@@ -142,17 +142,23 @@ pub fn drawBoxCenter(self: *TerminalBuffer, show_borders: bool, blank_box: bool)
}
pub fn calculateComponentCoordinates(self: TerminalBuffer) struct {
start_x: usize,
x: usize,
y: usize,
full_visible_length: usize,
visible_length: usize,
} {
const x = self.box_x + self.margin_box_h + self.labels_max_length + 1;
const start_x = self.box_x + self.margin_box_h;
const x = start_x + self.labels_max_length + 1;
const y = self.box_y + self.margin_box_v;
const full_visible_length = self.box_x + self.box_width - self.margin_box_h - start_x;
const visible_length = self.box_x + self.box_width - self.margin_box_h - x;
return .{
.start_x = start_x,
.x = x,
.y = y,
.full_visible_length = full_visible_length,
.visible_length = visible_length,
};
}

View File

@@ -1,6 +1,11 @@
const std = @import("std");
const utils = @import("../utils.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const generic = @import("generic.zig");
const utils = @import("../utils.zig");
const Allocator = std.mem.Allocator;
const MessageLabel = generic.CyclableLabel(Message);
const InfoLine = @This();
@@ -10,24 +15,23 @@ const Message = struct {
bg: u16,
fg: u16,
};
const MessageList = std.ArrayList(Message);
messages: MessageList,
label: MessageLabel,
pub fn init(allocator: std.mem.Allocator) InfoLine {
pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize) !InfoLine {
return .{
.messages = MessageList.init(allocator),
.label = try MessageLabel.init(allocator, buffer, max_length, drawItem),
};
}
pub fn deinit(self: InfoLine) void {
self.messages.deinit();
self.label.deinit();
}
pub fn addMessage(self: *InfoLine, text: []const u8, bg: u16, fg: u16) !void {
if (text.len == 0) return;
try self.messages.append(.{
try self.label.addItem(.{
.width = try utils.strWidth(text),
.text = text,
.bg = bg,
@@ -35,18 +39,7 @@ pub fn addMessage(self: *InfoLine, text: []const u8, bg: u16, fg: u16) !void {
});
}
pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void {
if (self.messages.items.len == 0) return;
const entry = self.messages.getLast();
if (entry.width == 0 or buffer.box_width <= entry.width) return;
const label_y = buffer.box_y + buffer.margin_box_v;
const x = buffer.box_x + ((buffer.box_width - entry.width) / 2);
TerminalBuffer.drawColorLabel(entry.text, x, label_y, entry.fg, entry.bg);
}
pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void {
pub fn clearRendered(allocator: Allocator, buffer: TerminalBuffer) !void {
// Draw over the area
const y = buffer.box_y + buffer.margin_box_v;
const spaces = try allocator.alloc(u8, buffer.box_width);
@@ -56,3 +49,13 @@ pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void
buffer.drawLabel(spaces, buffer.box_x, y);
}
fn drawItem(label: *MessageLabel, message: Message, _: usize, _: usize) bool {
if (message.width == 0 or label.buffer.box_width <= message.width) return false;
const x = label.buffer.box_x + ((label.buffer.box_width - message.width) / 2);
label.first_char_x = x;
TerminalBuffer.drawColorLabel(message.text, x, label.y, message.fg, message.bg);
return true;
}

View File

@@ -143,7 +143,7 @@ pub fn crawl(self: *Session, path: []const u8, display_server: DisplayServer) !v
}
}
fn drawItem(label: EnvironmentLabel, environment: Environment, x: usize, y: usize) bool {
fn drawItem(label: *EnvironmentLabel, environment: Environment, x: usize, y: usize) bool {
const length = @min(environment.name.len, label.visible_length - 3);
if (length == 0) return false;

View File

@@ -7,7 +7,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
return struct {
const Allocator = std.mem.Allocator;
const ItemList = std.ArrayList(ItemType);
const DrawItemFn = *const fn (Self, ItemType, usize, usize) bool;
const DrawItemFn = *const fn (*Self, ItemType, usize, usize) bool;
const termbox = interop.termbox;
@@ -20,6 +20,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
visible_length: usize,
x: usize,
y: usize,
first_char_x: usize,
draw_item_fn: DrawItemFn,
pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize, draw_item_fn: DrawItemFn) !Self {
@@ -31,6 +32,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
.visible_length = 0,
.x = 0,
.y = 0,
.first_char_x = 0,
.draw_item_fn = draw_item_fn,
};
}
@@ -43,6 +45,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
self.x = x;
self.y = y;
self.visible_length = visible_length;
self.first_char_x = x + 2;
}
pub fn addItem(self: *Self, item: ItemType) !void {
@@ -69,10 +72,12 @@ pub fn CyclableLabel(comptime ItemType: type) type {
}
}
_ = termbox.tb_set_cursor(@intCast(self.x + 2), @intCast(self.y));
_ = termbox.tb_set_cursor(@intCast(self.first_char_x), @intCast(self.y));
}
pub fn draw(self: Self) void {
pub fn draw(self: *Self) void {
if (self.list.items.len == 0) return;
const current_item = self.list.items[self.current];
const x = self.buffer.box_x + self.buffer.margin_box_h;
const y = self.buffer.box_y + self.buffer.margin_box_v + 2;