nushell/crates/nu-table/src/table_theme.rs

256 lines
6.9 KiB
Rust
Raw Normal View History

use tabled::settings::style::{HorizontalLine, Line, RawStyle, Style};
2022-05-16 17:35:57 +02:00
#[derive(Debug, Clone)]
pub struct TableTheme {
theme: RawStyle,
full_theme: RawStyle,
has_inner: bool,
2022-05-16 17:35:57 +02:00
}
impl TableTheme {
pub fn basic() -> TableTheme {
Self {
theme: Style::ascii().into(),
full_theme: Style::ascii().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn thin() -> TableTheme {
Self {
theme: Style::modern().into(),
full_theme: Style::modern().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn light() -> TableTheme {
let theme = Style::blank()
.horizontals([HorizontalLine::new(
1,
Line::new(Some('─'), Some('─'), None, None),
)])
.into();
Self {
theme,
full_theme: Style::modern().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
add 6 more table themes (#10279) # Description After looking at a users terminal that didn't support UTF-8, I wanted to add some themes that may help them. Here's what they look like. ## psql ![image](https://github.com/nushell/nushell/assets/343840/67ac003a-72f1-4e2b-8bb0-244b70385d59) ## markdown ![image](https://github.com/nushell/nushell/assets/343840/a8f4a439-013b-48ee-b9e0-284ec47d1eef) ## dots please excuse the different theme ![image](https://github.com/nushell/nushell/assets/343840/fb931650-cc64-4f0a-bf3d-ec736e0374ad) ## restructured ![image](https://github.com/nushell/nushell/assets/343840/80595a8e-f2b3-49dc-ad02-81e94bde5253) ## ascii_rounded ![image](https://github.com/nushell/nushell/assets/343840/42f0b8b2-1fd2-4ae5-b28c-477e83ded354) ## basic_compact ![image](https://github.com/nushell/nushell/assets/343840/5888b6b2-b9b8-48bc-963e-5a76ef246adc) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-08 23:34:36 +02:00
pub fn psql() -> TableTheme {
Self {
theme: Style::psql().into(),
full_theme: Style::psql().into(),
has_inner: true,
}
}
pub fn markdown() -> TableTheme {
Self {
theme: Style::markdown().into(),
full_theme: Style::markdown().into(),
has_inner: true,
}
}
pub fn dots() -> TableTheme {
let theme = Style::dots().remove_horizontal().into();
Self {
theme,
full_theme: Style::dots().into(),
has_inner: true,
}
}
pub fn restructured() -> TableTheme {
add 6 more table themes (#10279) # Description After looking at a users terminal that didn't support UTF-8, I wanted to add some themes that may help them. Here's what they look like. ## psql ![image](https://github.com/nushell/nushell/assets/343840/67ac003a-72f1-4e2b-8bb0-244b70385d59) ## markdown ![image](https://github.com/nushell/nushell/assets/343840/a8f4a439-013b-48ee-b9e0-284ec47d1eef) ## dots please excuse the different theme ![image](https://github.com/nushell/nushell/assets/343840/fb931650-cc64-4f0a-bf3d-ec736e0374ad) ## restructured ![image](https://github.com/nushell/nushell/assets/343840/80595a8e-f2b3-49dc-ad02-81e94bde5253) ## ascii_rounded ![image](https://github.com/nushell/nushell/assets/343840/42f0b8b2-1fd2-4ae5-b28c-477e83ded354) ## basic_compact ![image](https://github.com/nushell/nushell/assets/343840/5888b6b2-b9b8-48bc-963e-5a76ef246adc) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-09-08 23:34:36 +02:00
Self {
theme: Style::re_structured_text().into(),
full_theme: Style::re_structured_text().into(),
has_inner: true,
}
}
pub fn ascii_rounded() -> TableTheme {
Self {
theme: Style::ascii_rounded().into(),
full_theme: Style::ascii_rounded().into(),
has_inner: true,
}
}
pub fn basic_compact() -> TableTheme {
let theme = Style::ascii().remove_horizontal().into();
Self {
theme,
full_theme: Style::ascii().into(),
has_inner: true,
}
}
2022-05-16 17:35:57 +02:00
pub fn compact() -> TableTheme {
let theme = Style::modern()
.remove_left()
.remove_right()
.remove_horizontal()
.horizontals([HorizontalLine::new(1, Style::modern().get_horizontal())
.left(None)
.right(None)])
.into();
Self {
theme,
full_theme: Style::modern().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn with_love() -> TableTheme {
let theme = Style::empty()
.top('❤')
.bottom('❤')
.vertical('❤')
.horizontals([HorizontalLine::new(
1,
Line::new(Some('❤'), Some('❤'), None, None),
)]);
let full_theme = Style::empty()
.top('❤')
.bottom('❤')
.vertical('❤')
.horizontal('❤')
.left('❤')
.right('❤')
.intersection_top('❤')
.corner_top_left('❤')
.corner_top_right('❤')
.intersection_bottom('❤')
.corner_bottom_left('❤')
.corner_bottom_right('❤')
.intersection_right('❤')
.intersection_left('❤')
.intersection('❤');
Self {
theme: theme.into(),
full_theme: full_theme.into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn compact_double() -> TableTheme {
let theme = Style::extended()
.remove_left()
.remove_right()
.remove_horizontal()
.horizontals([HorizontalLine::new(1, Style::extended().get_horizontal())
.left(None)
.right(None)])
.into();
Self {
theme,
full_theme: Style::extended().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn rounded() -> TableTheme {
Self {
theme: Style::rounded().into(),
full_theme: Style::modern()
.corner_top_left('╭')
.corner_top_right('╮')
.corner_bottom_left('╰')
.corner_bottom_right('╯')
.into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn reinforced() -> TableTheme {
let full_theme = Style::modern()
.corner_top_left('┏')
.corner_top_right('┓')
.corner_bottom_left('┗')
.corner_bottom_right('┛');
Self {
theme: full_theme.clone().remove_horizontal().into(),
full_theme: full_theme.into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn heavy() -> TableTheme {
let theme = Style::empty()
.top('━')
.bottom('━')
.vertical('┃')
.left('┃')
.right('┃')
.intersection_top('┳')
.intersection_bottom('┻')
.corner_top_left('┏')
.corner_top_right('┓')
.corner_bottom_left('┗')
.corner_bottom_right('┛')
.horizontals([HorizontalLine::new(1, Line::full('━', '╋', '┣', '┫'))]);
let full_theme = theme
.clone()
.remove_horizontals()
.horizontal('━')
.intersection_left('┣')
.intersection_right('┫')
.intersection('╋');
Self {
theme: theme.into(),
full_theme: full_theme.into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn none() -> TableTheme {
Self {
theme: Style::blank().into(),
full_theme: Style::blank().into(),
has_inner: true,
2022-05-16 17:35:57 +02:00
}
}
pub fn has_top_line(&self) -> bool {
self.theme.get_top().is_some()
|| self.theme.get_top_intersection().is_some()
|| self.theme.get_top_left().is_some()
|| self.theme.get_top_right().is_some()
}
pub fn has_left(&self) -> bool {
self.theme.get_left().is_some()
|| self.theme.get_left_intersection().is_some()
|| self.theme.get_top_left().is_some()
|| self.theme.get_bottom_left().is_some()
}
pub fn has_right(&self) -> bool {
self.theme.get_right().is_some()
|| self.theme.get_right_intersection().is_some()
|| self.theme.get_top_right().is_some()
|| self.theme.get_bottom_right().is_some()
}
pub fn has_inner(&self) -> bool {
self.has_inner
}
pub fn has_horizontals(&self) -> bool {
self.full_theme.get_borders().has_horizontal()
}
pub fn get_theme_full(&self) -> RawStyle {
self.full_theme.clone()
}
pub fn get_theme(&self) -> RawStyle {
self.theme.clone()
}
2022-05-16 17:35:57 +02:00
}