-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: finish tidying up table (#942)
- Loading branch information
Showing
2 changed files
with
80 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,12 @@ | ||
use strum::{Display, EnumString}; | ||
|
||
mod cell; | ||
mod highlight_spacing; | ||
mod row; | ||
#[allow(clippy::module_inception)] | ||
mod table; | ||
mod table_state; | ||
|
||
pub use cell::Cell; | ||
pub use row::Row; | ||
pub use table::Table; | ||
pub use table_state::TableState; | ||
|
||
/// This option allows the user to configure the "highlight symbol" column width spacing | ||
#[derive(Debug, Display, EnumString, PartialEq, Eq, Clone, Default, Hash)] | ||
pub enum HighlightSpacing { | ||
/// Always add spacing for the selection symbol column | ||
/// | ||
/// With this variant, the column for the selection symbol will always be allocated, and so the | ||
/// table will never change size, regardless of if a row is selected or not | ||
Always, | ||
|
||
/// Only add spacing for the selection symbol column if a row is selected | ||
/// | ||
/// With this variant, the column for the selection symbol will only be allocated if there is a | ||
/// selection, causing the table to shift if selected / unselected | ||
#[default] | ||
WhenSelected, | ||
|
||
/// Never add spacing to the selection symbol column, regardless of whether something is | ||
/// selected or not | ||
/// | ||
/// This means that the highlight symbol will never be drawn | ||
Never, | ||
} | ||
|
||
impl HighlightSpacing { | ||
/// Determine if a selection column should be displayed | ||
/// | ||
/// has_selection: true if a row is selected in the table | ||
/// | ||
/// Returns true if a selection column should be displayed | ||
pub(crate) fn should_add(&self, has_selection: bool) -> bool { | ||
match self { | ||
HighlightSpacing::Always => true, | ||
HighlightSpacing::WhenSelected => has_selection, | ||
HighlightSpacing::Never => false, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn highlight_spacing_to_string() { | ||
assert_eq!(HighlightSpacing::Always.to_string(), "Always".to_string()); | ||
assert_eq!( | ||
HighlightSpacing::WhenSelected.to_string(), | ||
"WhenSelected".to_string() | ||
); | ||
assert_eq!(HighlightSpacing::Never.to_string(), "Never".to_string()); | ||
} | ||
|
||
#[test] | ||
fn highlight_spacing_from_str() { | ||
assert_eq!( | ||
"Always".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::Always) | ||
); | ||
assert_eq!( | ||
"WhenSelected".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::WhenSelected) | ||
); | ||
assert_eq!( | ||
"Never".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::Never) | ||
); | ||
assert_eq!( | ||
"".parse::<HighlightSpacing>(), | ||
Err(strum::ParseError::VariantNotFound) | ||
); | ||
} | ||
} | ||
pub use cell::*; | ||
pub use highlight_spacing::*; | ||
pub use row::*; | ||
pub use table::*; | ||
pub use table_state::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use strum::{Display, EnumString}; | ||
|
||
/// This option allows the user to configure the "highlight symbol" column width spacing | ||
#[derive(Debug, Display, EnumString, PartialEq, Eq, Clone, Default, Hash)] | ||
pub enum HighlightSpacing { | ||
/// Always add spacing for the selection symbol column | ||
/// | ||
/// With this variant, the column for the selection symbol will always be allocated, and so the | ||
/// table will never change size, regardless of if a row is selected or not | ||
Always, | ||
|
||
/// Only add spacing for the selection symbol column if a row is selected | ||
/// | ||
/// With this variant, the column for the selection symbol will only be allocated if there is a | ||
/// selection, causing the table to shift if selected / unselected | ||
#[default] | ||
WhenSelected, | ||
|
||
/// Never add spacing to the selection symbol column, regardless of whether something is | ||
/// selected or not | ||
/// | ||
/// This means that the highlight symbol will never be drawn | ||
Never, | ||
} | ||
|
||
impl HighlightSpacing { | ||
/// Determine if a selection column should be displayed | ||
/// | ||
/// has_selection: true if a row is selected in the table | ||
/// | ||
/// Returns true if a selection column should be displayed | ||
pub(crate) fn should_add(&self, has_selection: bool) -> bool { | ||
match self { | ||
HighlightSpacing::Always => true, | ||
HighlightSpacing::WhenSelected => has_selection, | ||
HighlightSpacing::Never => false, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn to_string() { | ||
assert_eq!(HighlightSpacing::Always.to_string(), "Always".to_string()); | ||
assert_eq!( | ||
HighlightSpacing::WhenSelected.to_string(), | ||
"WhenSelected".to_string() | ||
); | ||
assert_eq!(HighlightSpacing::Never.to_string(), "Never".to_string()); | ||
} | ||
|
||
#[test] | ||
fn from_str() { | ||
assert_eq!( | ||
"Always".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::Always) | ||
); | ||
assert_eq!( | ||
"WhenSelected".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::WhenSelected) | ||
); | ||
assert_eq!( | ||
"Never".parse::<HighlightSpacing>(), | ||
Ok(HighlightSpacing::Never) | ||
); | ||
assert_eq!( | ||
"".parse::<HighlightSpacing>(), | ||
Err(strum::ParseError::VariantNotFound) | ||
); | ||
} | ||
} |