Skip to content

Commit

Permalink
Support more options in configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Oct 1, 2024
1 parent 777e054 commit aa2f264
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
10 changes: 6 additions & 4 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct Remember {
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Ui {
pub theme: Option<String>,
pub greeting: Option<String>,
#[serde(default)]
pub use_issue: bool,
Expand All @@ -65,10 +66,11 @@ pub struct Ui {
#[serde(default)]
pub show_asterisks: bool,
pub asterisks_char: Option<char>,
pub width: Option<u64>,
pub window_padding: Option<u64>,
pub container_padding: Option<u64>,
pub prompt_padding: Option<u64>,
pub width: Option<u16>,
pub window_padding: Option<u16>,
pub container_padding: Option<u16>,
pub prompt_padding: Option<u16>,
pub greet_align: Option<String>,
pub command_f_key: Option<u8>,
pub sessions_f_key: Option<u8>,
pub power_f_key: Option<u8>,
Expand Down
10 changes: 9 additions & 1 deletion src/config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
info::{get_issue, get_min_max_uids, get_users},
power::PowerOption,
ui::{
common::menu::Menu,
common::{menu::Menu, style::Theme},
power::Power,
sessions::{SessionSource, SessionType},
},
Expand All @@ -33,6 +33,14 @@ impl Greeter {
}
}

pub fn parse_theme(&mut self) -> Result<(), Box<dyn Error>> {
if let Some(spec) = self.config().opt_str("theme").or_else(|| self.config.ui.theme.clone()) {
self.theme = Theme::parse(spec.as_str());
}

Ok(())
}

pub fn parse_greeting(&mut self) -> Result<(), Box<dyn Error>> {
let has_greeting = self.config().opt_present("greeting") || self.config.ui.greeting.is_some();
let has_issue = self.config().opt_present("issue") || self.config.ui.use_issue;
Expand Down
27 changes: 10 additions & 17 deletions src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,17 @@ impl Greeter {
// Returns the width of the main window where content is displayed from the
// provided arguments.
pub fn width(&self) -> u16 {
if let Some(value) = self.option("width") {
if let Ok(width) = value.parse::<u16>() {
return width;
}
if let Some(width) = self.option("width").and_then(|value| value.parse::<u16>().ok()).or(self.config.ui.width) {
return width;
}

80
}

// Returns the padding of the screen from the provided arguments.
pub fn window_padding(&self) -> u16 {
if let Some(value) = self.option("window-padding") {
if let Ok(padding) = value.parse::<u16>() {
return padding;
}
if let Some(padding) = self.option("window-padding").and_then(|value| value.parse::<u16>().ok()).or(self.config.ui.window_padding) {
return padding;
}

0
Expand All @@ -396,28 +392,24 @@ impl Greeter {
// Returns the padding of the main window where content is displayed from the
// provided arguments.
pub fn container_padding(&self) -> u16 {
if let Some(value) = self.option("container-padding") {
if let Ok(padding) = value.parse::<u16>() {
return padding + 1;
}
if let Some(padding) = self.option("container-padding").and_then(|value| value.parse::<u16>().ok()).or(self.config.ui.container_padding) {
return padding + 1;
}

2
}

// Returns the spacing between each prompt from the provided arguments.
pub fn prompt_padding(&self) -> u16 {
if let Some(value) = self.option("prompt-padding") {
if let Ok(padding) = value.parse::<u16>() {
return padding;
}
if let Some(padding) = self.option("prompt-padding").and_then(|value| value.parse::<u16>().ok()).or(self.config.ui.prompt_padding) {
return padding;
}

1
}

pub fn greet_align(&self) -> GreetAlign {
if let Some(value) = self.option("greet-align") {
if let Some(value) = self.option("greet-align").or_else(|| self.config.ui.greet_align.clone()) {
match value.as_str() {
"left" => GreetAlign::Left,
"right" => GreetAlign::Right,
Expand Down Expand Up @@ -526,6 +518,7 @@ impl Greeter {
self.parse_remembers()?;
self.parse_power();
self.parse_keybinds()?;
self.parse_theme()?;

Ok(())
}
Expand Down

0 comments on commit aa2f264

Please sign in to comment.