Add changes from review for width calculation

This commit is contained in:
Moabeat 2024-07-28 12:45:08 +02:00
parent ed33879066
commit 1eb3048315
No known key found for this signature in database
GPG Key ID: 315E0CCBC16F3D53
2 changed files with 28 additions and 15 deletions

View File

@ -102,7 +102,8 @@ pub fn main() !void {
defer allocator.free(config_path); defer allocator.free(config_path);
config = config_ini.readFileToStructWithMap(config_path, mapped_config_fields) catch _config: { config = config_ini.readFileToStructWithMap(config_path, mapped_config_fields) catch _config: {
try info_line.addError("Unable to parse config file"); // literal error message, due to language file not yet available
try info_line.addError("unable to parse config file");
break :_config Config{}; break :_config Config{};
}; };
@ -120,7 +121,8 @@ pub fn main() !void {
} }
} else { } else {
config = config_ini.readFileToStructWithMap(build_options.data_directory ++ "/config.ini", mapped_config_fields) catch _config: { config = config_ini.readFileToStructWithMap(build_options.data_directory ++ "/config.ini", mapped_config_fields) catch _config: {
try info_line.addError("Unable to parse config file"); // literal error message, due to language file not yet available
try info_line.addError("unable to parse config file");
break :_config Config{}; break :_config Config{};
}; };
@ -138,20 +140,20 @@ pub fn main() !void {
info_line.error_bg = config.error_bg; info_line.error_bg = config.error_bg;
info_line.error_fg = config.error_fg; info_line.error_fg = config.error_fg;
if (!build_options.enable_x11_support) info_line.setText(lang.no_x11_support); if (!build_options.enable_x11_support) try info_line.setText(lang.no_x11_support);
interop.setNumlock(config.numlock) catch {}; interop.setNumlock(config.numlock) catch {};
if (config.initial_info_text) |text| { if (config.initial_info_text) |text| {
info_line.setText(text); try info_line.setText(text);
} else get_host_name: { } else get_host_name: {
// Initialize information line with host name // Initialize information line with host name
var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined; var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
const hostname = std.posix.gethostname(&name_buf) catch { const hostname = std.posix.gethostname(&name_buf) catch {
info_line.setText(lang.err_hostname); try info_line.setText(lang.err_hostname);
break :get_host_name; break :get_host_name;
}; };
info_line.setText(hostname); try info_line.setText(hostname);
} }
// Initialize termbox // Initialize termbox
@ -603,7 +605,7 @@ pub fn main() !void {
const password_text = try allocator.dupeZ(u8, password.text.items); const password_text = try allocator.dupeZ(u8, password.text.items);
defer allocator.free(password_text); defer allocator.free(password_text);
info_line.setText(lang.authenticating); try info_line.setText(lang.authenticating);
InfoLine.clearRendered(allocator, buffer) catch {}; InfoLine.clearRendered(allocator, buffer) catch {};
try info_line.draw(buffer); try info_line.draw(buffer);
_ = termbox.tb_present(); _ = termbox.tb_present();
@ -630,7 +632,7 @@ pub fn main() !void {
if (config.clear_password or err != error.PamAuthError) password.clear(); if (config.clear_password or err != error.PamAuthError) password.clear();
} else { } else {
password.clear(); password.clear();
info_line.setText(lang.logout); try info_line.setText(lang.logout);
} }
try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios); try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios);

View File

@ -3,20 +3,23 @@ const utils = @import("../utils.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig"); const TerminalBuffer = @import("../TerminalBuffer.zig");
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const ErrorMessage = struct { width: u8, text: []const u8 };
const InfoLine = @This(); const InfoLine = @This();
error_list: ArrayList([]const u8), error_list: ArrayList(ErrorMessage),
error_bg: u16, error_bg: u16,
error_fg: u16, error_fg: u16,
text: []const u8, text: []const u8,
width: u8,
pub fn init(allocator: std.mem.Allocator) InfoLine { pub fn init(allocator: std.mem.Allocator) InfoLine {
return .{ return .{
.error_list = ArrayList([]const u8).init(allocator), .error_list = ArrayList(ErrorMessage).init(allocator),
.error_bg = 0, .error_bg = 0,
.error_fg = 258, .error_fg = 258,
.text = "", .text = "",
.width = 0,
}; };
} }
@ -24,27 +27,35 @@ pub fn deinit(self: InfoLine) void {
self.error_list.deinit(); self.error_list.deinit();
} }
pub fn setText(self: *InfoLine, text: []const u8) void { pub fn setText(self: *InfoLine, text: []const u8) !void {
self.width = if (text.len > 0) try utils.strWidth(text) else 0;
self.text = text; self.text = text;
} }
pub fn addError(self: *InfoLine, error_message: []const u8) !void { pub fn addError(self: *InfoLine, error_message: []const u8) !void {
try self.error_list.append(error_message); 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 { pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void {
var text: []const u8 = self.text; var text: []const u8 = self.text;
var bg: u16 = buffer.bg; var bg: u16 = buffer.bg;
var fg: u16 = buffer.fg; var fg: u16 = buffer.fg;
var width: u8 = self.width;
if (self.error_list.items.len > 0) { if (self.error_list.items.len > 0) {
text = self.error_list.getLast(); const entry = self.error_list.getLast();
text = entry.text;
bg = self.error_bg; bg = self.error_bg;
fg = self.error_fg; fg = self.error_fg;
width = entry.width;
} }
const width: u8 = if (text.len > 0) try utils.strWidth(text) else 0;
if (width > 0 and buffer.box_width > width) { if (width > 0 and buffer.box_width > width) {
const label_y = buffer.box_y + buffer.margin_box_v; const label_y = buffer.box_y + buffer.margin_box_v;
const x = buffer.box_x + ((buffer.box_width - width) / 2); const x = buffer.box_x + ((buffer.box_width - width) / 2);