From 9f3fb64df105ae54805ec844738a4acd25656162 Mon Sep 17 00:00:00 2001 From: dthelegend Date: Thu, 2 May 2024 13:07:05 +0100 Subject: [PATCH 1/5] Add Text Align option --- src/greeter.rs | 24 +++++++++++++++++++++++- src/ui/prompt.rs | 17 +++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index 85d846d..a432605 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -90,6 +90,15 @@ impl SecretDisplay { } } +// This enum models text alignment options +#[derive(SmartDefault, Debug, Clone)] +pub enum TextAlign { + #[default] + Center, + Left, + Right +} + #[derive(SmartDefault)] pub struct Greeter { pub debug: bool, @@ -270,7 +279,7 @@ impl Greeter { self.connect().await; } - // Connect to `greetd` and return a strea we can safely write to. + // Connect to `greetd` and return a stream we can safely write to. pub async fn connect(&mut self) { match UnixStream::connect(&self.socket).await { Ok(stream) => self.stream = Some(Arc::new(RwLock::new(stream))), @@ -340,6 +349,18 @@ impl Greeter { 1 } + pub fn text_align(&self) -> TextAlign { + if let Some(value) = self.option("text-align") { + match value.to_uppercase().as_str() { + "LEFT" | "L" => TextAlign::Left, + "RIGHT" | "R" => TextAlign::Right, + _ => TextAlign::Center + } + } else { + TextAlign::default() + } + } + // Sets the locale that will be used for this invocation from environment. fn set_locale(&mut self) { let locale = DesktopLanguageRequester::requested_languages() @@ -384,6 +405,7 @@ impl Greeter { opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING"); opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING"); opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING"); + opts.optopt("", "text-align", "alignment of text in the prompt container (default: 'center')", "[left|center|right]"); opts.optopt("", "power-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'"); opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'"); diff --git a/src/ui/prompt.rs b/src/ui/prompt.rs index 53400c5..f7b9080 100644 --- a/src/ui/prompt.rs +++ b/src/ui/prompt.rs @@ -7,11 +7,7 @@ use tui::{ widgets::{Block, BorderType, Borders, Paragraph}, }; -use crate::{ - info::get_hostname, - ui::{prompt_value, util::*, Frame}, - Greeter, Mode, SecretDisplay, -}; +use crate::{info::get_hostname, ui::{prompt_value, util::*, Frame}, Greeter, Mode, SecretDisplay, TextAlign}; use super::common::style::Themed; @@ -27,6 +23,11 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Alignment::Center, + TextAlign::Left => Alignment::Left, + TextAlign::Right => Alignment::Right + }; let container = Rect::new(x, y, width, height); let frame = Rect::new(x + container_padding, y + container_padding, width - (2 * container_padding), height - (2 * container_padding)); @@ -59,7 +60,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Result<(u16, u16), Box Result<(u16, u16), Box Date: Mon, 13 May 2024 12:55:45 +0100 Subject: [PATCH 2/5] Rename text-align option to greet-align and limit to greeting only --- src/greeter.rs | 16 ++++++++-------- src/ui/prompt.rs | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index e72047d..3ccb641 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -92,7 +92,7 @@ impl SecretDisplay { // This enum models text alignment options #[derive(SmartDefault, Debug, Clone)] -pub enum TextAlign { +pub enum GreetAlign { #[default] Center, Left, @@ -349,15 +349,15 @@ impl Greeter { 1 } - pub fn text_align(&self) -> TextAlign { - if let Some(value) = self.option("text-align") { + pub fn greet_align(&self) -> GreetAlign { + if let Some(value) = self.option("greet-align") { match value.to_uppercase().as_str() { - "LEFT" | "L" => TextAlign::Left, - "RIGHT" | "R" => TextAlign::Right, - _ => TextAlign::Center + "left" => GreetAlign::Left, + "right" => GreetAlign::Right, + _ => GreetAlign::Center } } else { - TextAlign::default() + GreetAlign::default() } } @@ -405,7 +405,7 @@ impl Greeter { opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING"); opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING"); opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING"); - opts.optopt("", "text-align", "alignment of text in the prompt container (default: 'center')", "[left|center|right]"); + opts.optopt("", "greet-align", "alignment of the greeting text in the main prompt container (default: 'center')", "[left|center|right]"); opts.optopt("", "power-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'"); opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'"); diff --git a/src/ui/prompt.rs b/src/ui/prompt.rs index f7b9080..9d57329 100644 --- a/src/ui/prompt.rs +++ b/src/ui/prompt.rs @@ -7,7 +7,11 @@ use tui::{ widgets::{Block, BorderType, Borders, Paragraph}, }; -use crate::{info::get_hostname, ui::{prompt_value, util::*, Frame}, Greeter, Mode, SecretDisplay, TextAlign}; +use crate::{ + info::get_hostname, + ui::{prompt_value, util::*, Frame}, + Greeter, Mode, SecretDisplay, GreetAlign +}; use super::common::style::Themed; @@ -23,10 +27,10 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Alignment::Center, - TextAlign::Left => Alignment::Left, - TextAlign::Right => Alignment::Right + let greeting_alignment = match greeter.greet_align() { + GreetAlign::Center => Alignment::Center, + GreetAlign::Left => Alignment::Left, + GreetAlign::Right => Alignment::Right }; let container = Rect::new(x, y, width, height); @@ -60,7 +64,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Result<(u16, u16), Box Result<(u16, u16), Box Date: Mon, 13 May 2024 13:08:49 +0100 Subject: [PATCH 3/5] Fix bug where greet align could not match; Greet align matches to exact case now, not coaxing the string into upper/lower case --- src/greeter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/greeter.rs b/src/greeter.rs index 3ccb641..c64d230 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -351,7 +351,7 @@ impl Greeter { pub fn greet_align(&self) -> GreetAlign { if let Some(value) = self.option("greet-align") { - match value.to_uppercase().as_str() { + match value.as_str() { "left" => GreetAlign::Left, "right" => GreetAlign::Right, _ => GreetAlign::Center From eed8a0bf030bccf0a0c1a83403d790853daf579a Mon Sep 17 00:00:00 2001 From: dthelegend Date: Mon, 13 May 2024 13:49:28 +0100 Subject: [PATCH 4/5] Add greet-align to the manpage --- contrib/man/tuigreet-1.scd | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contrib/man/tuigreet-1.scd b/contrib/man/tuigreet-1.scd index ca517f4..732c2ad 100644 --- a/contrib/man/tuigreet-1.scd +++ b/contrib/man/tuigreet-1.scd @@ -105,6 +105,10 @@ tuigreet - A graphical console greeter for greetd *--prompt-padding ROWS* Add spacing between form fields. +*--greet-align [left|center|right]* + Alignment of the greeting text in the main prompt container + (default: 'center') + *--power-shutdown CMD [ARGS]...* Customize the command run when instructed to shut down the machine. This must be a non-interactive command (sudo cannot prompt for a password, for example). From fbbc9c002f40006bc16818f72b4c3673b0fef893 Mon Sep 17 00:00:00 2001 From: dthelegend Date: Mon, 13 May 2024 14:55:24 +0100 Subject: [PATCH 5/5] Fix greet-align formatting for manpage --- contrib/man/tuigreet-1.scd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/man/tuigreet-1.scd b/contrib/man/tuigreet-1.scd index 732c2ad..8fde4f8 100644 --- a/contrib/man/tuigreet-1.scd +++ b/contrib/man/tuigreet-1.scd @@ -106,8 +106,8 @@ tuigreet - A graphical console greeter for greetd Add spacing between form fields. *--greet-align [left|center|right]* - Alignment of the greeting text in the main prompt container - (default: 'center') + Alignment of the greeting text in the main prompt container + (default: 'center'). *--power-shutdown CMD [ARGS]...* Customize the command run when instructed to shut down the machine. This must