Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added border type functionality inside the 'config.toml' file. #136

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ input_border = "cyan"
# prompt box border
prompt_border = "green"

# border type
border_type = "rounded"

# correctly typed words
prompt_correct = "green"
# incorrectly typed words
Expand Down Expand Up @@ -245,4 +248,15 @@ modifier = "bold"
| "underlined" ;
```

### border types

The following border types are supported in the config file.

- `plain`
- `rounded` (default)
- `double`
- `thick`
- `quadrantinside`
- `quadrantoutside`

If you're familiar with [serde](https://serde.rs), you can also read [the deserialization code](./src/config.rs).
57 changes: 56 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use ratatui::style::{Color, Modifier, Style};
use ratatui::{
style::{Color, Modifier, Style},
widgets::BorderType,
};
use serde::{
de::{self, IntoDeserializer},
Deserialize,
Expand Down Expand Up @@ -34,6 +37,9 @@ pub struct Theme {
#[serde(deserialize_with = "deserialize_style")]
pub prompt_border: Style,

#[serde(deserialize_with = "deserialize_border_type")]
pub border_type: BorderType,

#[serde(deserialize_with = "deserialize_style")]
pub prompt_correct: Style,
#[serde(deserialize_with = "deserialize_style")]
Expand Down Expand Up @@ -85,6 +91,8 @@ impl Default for Theme {
input_border: Style::default().fg(Color::Cyan),
prompt_border: Style::default().fg(Color::Green),

border_type: BorderType::Rounded,

prompt_correct: Style::default().fg(Color::Green),
prompt_incorrect: Style::default().fg(Color::Red),
prompt_untyped: Style::default().fg(Color::Gray),
Expand Down Expand Up @@ -231,6 +239,37 @@ where
deserializer.deserialize_str(ColorVisitor)
}

fn deserialize_border_type<'de, D>(deserializer: D) -> Result<BorderType, D::Error>
where
D: de::Deserializer<'de>,
{
struct BorderTypeVisitor;
impl<'de> de::Visitor<'de> for BorderTypeVisitor {
type Value = BorderType;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a border type")
}

fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
match value {
"plain" => Ok(BorderType::Plain),
"rounded" => Ok(BorderType::Rounded),
"double" => Ok(BorderType::Double),
"thick" => Ok(BorderType::Thick),
"quadrantinside" => Ok(BorderType::QuadrantInside),
"quadrantoutside" => Ok(BorderType::QuadrantOutside),
_ => Err(E::invalid_value(
de::Unexpected::Str(value),
&"a border type",
)),
}
}
}

deserializer.deserialize_str(BorderTypeVisitor)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -292,4 +331,20 @@ mod tests {
.add_modifier(Modifier::SLOW_BLINK)
);
}

#[test]
fn deserializes_border_types() {
fn border_type(string: &str) -> BorderType {
deserialize_border_type(de::IntoDeserializer::<de::value::Error>::into_deserializer(
string,
))
.expect("failed to deserialize border type")
}
assert_eq!(border_type("plain"), BorderType::Plain);
assert_eq!(border_type("rounded"), BorderType::Rounded);
assert_eq!(border_type("double"), BorderType::Double);
assert_eq!(border_type("thick"), BorderType::Thick);
assert_eq!(border_type("quadrantinside"), BorderType::QuadrantInside);
assert_eq!(border_type("quadrantoutside"), BorderType::QuadrantOutside);
}
}
10 changes: 5 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
symbols::Marker,
text::{Line, Span, Text},
widgets::{Axis, Block, BorderType, Borders, Chart, Dataset, GraphType, Paragraph, Widget},
widgets::{Axis, Block, Borders, Chart, Dataset, GraphType, Paragraph, Widget},
};
use results::Fraction;

Expand Down Expand Up @@ -89,7 +89,7 @@ impl ThemedWidget for &Test {
block: Block::default()
.title(Line::from(vec![Span::styled("Input", theme.title)]))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_type(theme.border_type)
.border_style(theme.input_border),
area: chunks[0],
};
Expand Down Expand Up @@ -126,7 +126,7 @@ impl ThemedWidget for &Test {
Block::default()
.title(Span::styled("Prompt", theme.title))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_type(theme.border_type)
.border_style(theme.prompt_border),
);
target.render(chunks[1], buf);
Expand Down Expand Up @@ -317,7 +317,7 @@ impl ThemedWidget for &results::Results {
Block::default()
.title(Span::styled("Overview", theme.title))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_type(theme.border_type)
.border_style(theme.results_overview_border),
);
overview.render(info_chunks[0], buf);
Expand Down Expand Up @@ -353,7 +353,7 @@ impl ThemedWidget for &results::Results {
Block::default()
.title(Span::styled("Worst Keys", theme.title))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_type(theme.border_type)
.border_style(theme.results_worst_keys_border),
);
worst.render(info_chunks[1], buf);
Expand Down
Loading