Display error messages differently in info line (#661)

* Add list of error messages to InfoLine.zig

* Change info and error cases according to review

* Add changes from review for width calculation
This commit is contained in:
Moabeat
2024-07-28 13:02:42 +02:00
committed by GitHub
parent 93554d9ba3
commit 19d4b195f3
6 changed files with 101 additions and 34 deletions

View File

@@ -14,8 +14,8 @@ random: Random,
width: u64,
height: u64,
buffer: [*]termbox.tb_cell,
fg: u8,
bg: u8,
fg: u16,
bg: u16,
border_fg: u8,
box_chars: struct {
left_up: u32,
@@ -158,13 +158,17 @@ pub fn calculateComponentCoordinates(self: TerminalBuffer) struct {
}
pub fn drawLabel(self: TerminalBuffer, text: []const u8, x: u64, y: u64) void {
drawColorLabel(text, x, y, self.fg, self.bg);
}
pub fn drawColorLabel(text: []const u8, x: u64, y: u64, fg: u16, bg: u16) void {
const yc: c_int = @intCast(y);
const utf8view = std.unicode.Utf8View.init(text) catch return;
var utf8 = utf8view.iterator();
var i = x;
while (utf8.nextCodepoint()) |codepoint| : (i += 1) {
_ = termbox.tb_set_cell(@intCast(i), yc, codepoint, self.fg, self.bg);
_ = termbox.tb_set_cell(@intCast(i), yc, codepoint, fg, bg);
}
}

View File

@@ -2,25 +2,67 @@ const std = @import("std");
const utils = @import("../utils.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const ArrayList = std.ArrayList;
const ErrorMessage = struct { width: u8, text: []const u8 };
const InfoLine = @This();
text: []const u8 = "",
width: u8 = 0,
error_list: ArrayList(ErrorMessage),
error_bg: u16,
error_fg: u16,
text: []const u8,
width: u8,
pub fn init(allocator: std.mem.Allocator) InfoLine {
return .{
.error_list = ArrayList(ErrorMessage).init(allocator),
.error_bg = 0,
.error_fg = 258,
.text = "",
.width = 0,
};
}
pub fn deinit(self: InfoLine) void {
self.error_list.deinit();
}
pub fn setText(self: *InfoLine, text: []const u8) !void {
self.width = if (text.len > 0) try utils.strWidth(text) else 0;
self.text = text;
}
pub fn draw(self: InfoLine, buffer: TerminalBuffer) void {
if (self.width > 0 and buffer.box_width > self.width) {
const label_y = buffer.box_y + buffer.margin_box_v;
const x = buffer.box_x + ((buffer.box_width - self.width) / 2);
buffer.drawLabel(self.text, x, label_y);
pub fn addError(self: *InfoLine, error_message: []const u8) !void {
if (error_message.len > 0) {
const entry = .{
.width = try utils.strWidth(error_message),
.text = error_message,
};
try self.error_list.append(entry);
}
}
pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void {
var text: []const u8 = self.text;
var bg: u16 = buffer.bg;
var fg: u16 = buffer.fg;
var width: u8 = self.width;
if (self.error_list.items.len > 0) {
const entry = self.error_list.getLast();
text = entry.text;
bg = self.error_bg;
fg = self.error_fg;
width = entry.width;
}
if (width > 0 and buffer.box_width > width) {
const label_y = buffer.box_y + buffer.margin_box_v;
const x = buffer.box_x + ((buffer.box_width - width) / 2);
TerminalBuffer.drawColorLabel(text, x, label_y, fg, bg);
}
}
pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void {
// draw over the area
const y = buffer.box_y + buffer.margin_box_v;