From 1d3148201c62fe3e9b65866cfe3c67ec495e0c4b Mon Sep 17 00:00:00 2001 From: Kinzie Date: Mon, 13 May 2024 13:21:02 +0100 Subject: [PATCH] Custom info text --- src/config/Config.zig | 2 ++ src/main.zig | 12 +++++++++--- src/tui/TerminalBuffer.zig | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 0c584d5..a3baab3 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -10,6 +10,7 @@ bg: u8 = 0, bigclock: bool = false, blank_box: bool = true, border_fg: u8 = 8, +box_title: ?[]const u8 = null, clear_password: bool = false, clock: ?[:0]const u8 = null, console_dev: [:0]const u8 = "/dev/console", @@ -17,6 +18,7 @@ default_input: Input = .login, fg: u8 = 8, hide_borders: bool = false, hide_key_hints: bool = false, +initial_info_text: ?[]const u8 = null, input_len: u8 = 34, lang: []const u8 = "en", load: bool = true, diff --git a/src/main.zig b/src/main.zig index a638a0b..bd2d4b0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -129,8 +129,10 @@ pub fn main() !void { interop.setNumlock(config.numlock) catch {}; - // Initialize information line with host name - get_host_name: { + if (config.initial_info_text) |text| { + try info_line.setText(text); + } else get_host_name: { + // Initialize information line with host name var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined; const hostname = std.posix.gethostname(&name_buf) catch { try info_line.setText(lang.err_hostname); @@ -395,9 +397,13 @@ pub fn main() !void { } } + if (config.box_title) |title| { + buffer.drawConfinedLabel(title, buffer.box_x, buffer.box_y - 1, buffer.box_width); + } + if (config.vi_mode) { const label_txt = if (insert_mode) lang.insert else lang.normal; - buffer.drawLabel(label_txt, buffer.box_x, buffer.box_y - 1); + buffer.drawLabel(label_txt, buffer.box_x, buffer.box_y + buffer.box_height); } draw_lock_state: { diff --git a/src/tui/TerminalBuffer.zig b/src/tui/TerminalBuffer.zig index 0a746a6..201aa1f 100644 --- a/src/tui/TerminalBuffer.zig +++ b/src/tui/TerminalBuffer.zig @@ -170,6 +170,20 @@ pub fn drawLabel(self: TerminalBuffer, text: []const u8, x: u64, y: u64) void { } } +pub fn drawConfinedLabel(self: TerminalBuffer, text: []const u8, x: u64, y: u64, max_length: u64) void { + var confined_text = text; + if (text.len > max_length) confined_text = text[0..max_length]; + + const yc: c_int = @intCast(y); + const utf8view = std.unicode.Utf8View.init(confined_text) catch return; + var utf8 = utf8view.iterator(); + + var i = x; + while (utf8.nextCodepoint()) |codepoint| : (i += 1) { + termbox.tb_change_cell(@intCast(i), yc, codepoint, self.fg, self.bg); + } +} + pub fn drawCharMultiple(self: TerminalBuffer, char: u8, x: u64, y: u64, length: u64) void { const yc: c_int = @intCast(y); const cell = utils.initCell(char, self.fg, self.bg);