Add option to center env name (#683)

This commit is contained in:
llc0930 2024-08-09 16:44:49 +00:00 committed by GitHub
parent c87d5b4e7a
commit b84158e1c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 19 additions and 7 deletions

View File

@ -188,6 +188,9 @@ sleep_cmd = null
# Specifies the key used for sleep (F1-F12)
sleep_key = F3
# Center the session name.
text_in_center = false
# TTY in use
tty = $DEFAULT_TTY

View File

@ -50,6 +50,7 @@ shutdown_cmd: []const u8 = "/sbin/shutdown -a now",
shutdown_key: []const u8 = "F1",
sleep_cmd: ?[]const u8 = null,
sleep_key: []const u8 = "F3",
text_in_center: bool = false,
tty: u8 = build_options.tty,
vi_default_mode: ViMode = .normal,
vi_mode: bool = false,

View File

@ -283,8 +283,8 @@ pub fn main() !void {
buffer.drawBoxCenter(!config.hide_borders, config.blank_box);
const coordinates = buffer.calculateComponentCoordinates();
info_line.label.position(coordinates.start_x, coordinates.y, coordinates.full_visible_length);
session.label.position(coordinates.x, coordinates.y + 2, coordinates.visible_length);
info_line.label.position(coordinates.start_x, coordinates.y, coordinates.full_visible_length, null);
session.label.position(coordinates.x, coordinates.y + 2, coordinates.visible_length, config.text_in_center);
login.position(coordinates.x, coordinates.y + 4, coordinates.visible_length);
password.position(coordinates.x, coordinates.y + 6, coordinates.visible_length);
@ -405,8 +405,8 @@ pub fn main() !void {
if (resolution_changed) {
const coordinates = buffer.calculateComponentCoordinates();
info_line.label.position(coordinates.start_x, coordinates.y, coordinates.full_visible_length);
session.label.position(coordinates.x, coordinates.y + 2, coordinates.visible_length);
info_line.label.position(coordinates.start_x, coordinates.y, coordinates.full_visible_length, null);
session.label.position(coordinates.x, coordinates.y + 2, coordinates.visible_length, config.text_in_center);
login.position(coordinates.x, coordinates.y + 4, coordinates.visible_length);
password.position(coordinates.x, coordinates.y + 6, coordinates.visible_length);

View File

@ -54,7 +54,7 @@ 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;
label.first_char_x = x + message.width;
TerminalBuffer.drawColorLabel(message.text, x, label.y, message.fg, message.bg);
return true;

View File

@ -133,7 +133,10 @@ fn drawItem(label: *EnvironmentLabel, environment: Environment, x: usize, y: usi
const length = @min(environment.name.len, label.visible_length - 3);
if (length == 0) return false;
const nx = if (label.text_in_center) (label.x + (label.visible_length - environment.name.len) / 2) else (label.x + 2);
label.first_char_x = nx + environment.name.len;
label.buffer.drawLabel(environment.specifier, x, y);
label.buffer.drawLabel(environment.name, label.x + 2, label.y);
label.buffer.drawLabel(environment.name, nx, label.y);
return true;
}

View File

@ -21,6 +21,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
x: usize,
y: usize,
first_char_x: usize,
text_in_center: bool,
draw_item_fn: DrawItemFn,
pub fn init(allocator: Allocator, buffer: *TerminalBuffer, draw_item_fn: DrawItemFn) Self {
@ -33,6 +34,7 @@ pub fn CyclableLabel(comptime ItemType: type) type {
.x = 0,
.y = 0,
.first_char_x = 0,
.text_in_center = false,
.draw_item_fn = draw_item_fn,
};
}
@ -41,11 +43,14 @@ pub fn CyclableLabel(comptime ItemType: type) type {
self.list.deinit();
}
pub fn position(self: *Self, x: usize, y: usize, visible_length: usize) void {
pub fn position(self: *Self, x: usize, y: usize, visible_length: usize, text_in_center: ?bool) void {
self.x = x;
self.y = y;
self.visible_length = visible_length;
self.first_char_x = x + 2;
if (text_in_center) |value| {
self.text_in_center = value;
}
}
pub fn addItem(self: *Self, item: ItemType) !void {