Skip to content

Commit

Permalink
Add Text Align option
Browse files Browse the repository at this point in the history
  • Loading branch information
dthelegend committed May 2, 2024
1 parent 59c4fa4 commit 9f3fb64
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]...'");
Expand Down
17 changes: 9 additions & 8 deletions src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +23,11 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

let container_padding = greeter.container_padding();
let prompt_padding = greeter.prompt_padding();
let text_alignment = match greeter.text_align() {
TextAlign::Center => 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));
Expand Down Expand Up @@ -59,15 +60,15 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

if let Some(greeting) = &greeting {
let greeting_text = greeting.trim_end();
let greeting_label = Paragraph::new(greeting_text).alignment(Alignment::Center).style(theme.of(&[Themed::Greet]));
let greeting_label = Paragraph::new(greeting_text).alignment(text_alignment).style(theme.of(&[Themed::Greet]));

f.render_widget(greeting_label, chunks[GREETING_INDEX]);
}

let username_label = if greeter.user_menu && greeter.username.value.is_empty() {
let prompt_text = Span::from(fl!("select_user"));

Paragraph::new(prompt_text).alignment(Alignment::Center)
Paragraph::new(prompt_text).alignment(text_alignment)
} else {
let username_text = prompt_value(theme, Some(fl!("username")));

Expand Down Expand Up @@ -133,7 +134,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

if let Some(message) = message {
let message_text = Text::from(message);
let message = Paragraph::new(message_text).alignment(Alignment::Center);
let message = Paragraph::new(message_text).alignment(text_alignment);

f.render_widget(message, Rect::new(x, y + height, width, message_height));
}
Expand Down

0 comments on commit 9f3fb64

Please sign in to comment.