fix: Changed the type constraints to use the ToText Ratatui trait

This commit is contained in:
2024-11-14 16:38:38 -07:00
parent 37d7b77f90
commit 8a91f662dd
6 changed files with 86 additions and 87 deletions
+27 -27
View File
@@ -7,64 +7,64 @@ use ratatui::widgets::StatefulWidget;
use managarr_tree_widget::{Tree, TreeItem, TreeState};
#[must_use]
fn example_items() -> Vec<TreeItem<&'static str, String>> {
fn example_items() -> Vec<TreeItem<&'static str, &'static str>> {
vec![
TreeItem::new_leaf("a", "Alfa".to_owned()),
TreeItem::new_leaf("a", "Alfa"),
TreeItem::new(
"b",
"Bravo".to_owned(),
"Bravo",
vec![
TreeItem::new_leaf("c", "Charlie".to_owned()),
TreeItem::new_leaf("c", "Charlie"),
TreeItem::new(
"d",
"Delta".to_owned(),
"Delta",
vec![
TreeItem::new_leaf("e", "Echo".to_owned()),
TreeItem::new_leaf("f", "Foxtrot".to_owned()),
TreeItem::new_leaf("e", "Echo"),
TreeItem::new_leaf("f", "Foxtrot"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("g", "Golf".to_owned()),
TreeItem::new_leaf("g", "Golf"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("h", "Hotel".to_owned()),
TreeItem::new_leaf("h", "Hotel"),
TreeItem::new(
"i",
"India".to_owned(),
"India",
vec![
TreeItem::new_leaf("j", "Juliett".to_owned()),
TreeItem::new_leaf("k", "Kilo".to_owned()),
TreeItem::new_leaf("l", "Lima".to_owned()),
TreeItem::new_leaf("m", "Mike".to_owned()),
TreeItem::new_leaf("n", "November".to_owned()),
TreeItem::new_leaf("j", "Juliett"),
TreeItem::new_leaf("k", "Kilo"),
TreeItem::new_leaf("l", "Lima"),
TreeItem::new_leaf("m", "Mike"),
TreeItem::new_leaf("n", "November"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("o", "Oscar".to_owned()),
TreeItem::new_leaf("o", "Oscar"),
TreeItem::new(
"p",
"Papa".to_owned(),
"Papa",
vec![
TreeItem::new_leaf("q", "Quebec".to_owned()),
TreeItem::new_leaf("r", "Romeo".to_owned()),
TreeItem::new_leaf("s", "Sierra".to_owned()),
TreeItem::new_leaf("t", "Tango".to_owned()),
TreeItem::new_leaf("u", "Uniform".to_owned()),
TreeItem::new_leaf("q", "Quebec"),
TreeItem::new_leaf("r", "Romeo"),
TreeItem::new_leaf("s", "Sierra"),
TreeItem::new_leaf("t", "Tango"),
TreeItem::new_leaf("u", "Uniform"),
TreeItem::new(
"v",
"Victor".to_owned(),
"Victor",
vec![
TreeItem::new_leaf("w", "Whiskey".to_owned()),
TreeItem::new_leaf("x", "Xray".to_owned()),
TreeItem::new_leaf("y", "Yankee".to_owned()),
TreeItem::new_leaf("w", "Whiskey"),
TreeItem::new_leaf("x", "Xray"),
TreeItem::new_leaf("y", "Yankee"),
],
)
.expect("all item identifiers are unique"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("z", "Zulu".to_owned()),
TreeItem::new_leaf("z", "Zulu"),
]
}
+27 -27
View File
@@ -12,7 +12,7 @@ use managarr_tree_widget::{Tree, TreeItem, TreeState};
#[must_use]
struct App {
state: TreeState<&'static str>,
items: Vec<TreeItem<&'static str, String>>,
items: Vec<TreeItem<&'static str, &'static str>>,
}
impl App {
@@ -20,62 +20,62 @@ impl App {
Self {
state: TreeState::default(),
items: vec![
TreeItem::new_leaf("a", "Alfa".to_owned()),
TreeItem::new_leaf("a", "Alfa"),
TreeItem::new(
"b",
"Bravo".to_owned(),
"Bravo",
vec![
TreeItem::new_leaf("c", "Charlie".to_owned()),
TreeItem::new_leaf("c", "Charlie"),
TreeItem::new(
"d",
"Delta".to_owned(),
"Delta",
vec![
TreeItem::new_leaf("e", "Echo".to_owned()),
TreeItem::new_leaf("f", "Foxtrot".to_owned()),
TreeItem::new_leaf("e", "Echo"),
TreeItem::new_leaf("f", "Foxtrot"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("g", "Golf".to_owned()),
TreeItem::new_leaf("g", "Golf"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("h", "Hotel".to_owned()),
TreeItem::new_leaf("h", "Hotel"),
TreeItem::new(
"i",
"India".to_owned(),
"India",
vec![
TreeItem::new_leaf("j", "Juliett".to_owned()),
TreeItem::new_leaf("k", "Kilo".to_owned()),
TreeItem::new_leaf("l", "Lima".to_owned()),
TreeItem::new_leaf("m", "Mike".to_owned()),
TreeItem::new_leaf("n", "November".to_owned()),
TreeItem::new_leaf("j", "Juliett"),
TreeItem::new_leaf("k", "Kilo"),
TreeItem::new_leaf("l", "Lima"),
TreeItem::new_leaf("m", "Mike"),
TreeItem::new_leaf("n", "November"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("o", "Oscar".to_owned()),
TreeItem::new_leaf("o", "Oscar"),
TreeItem::new(
"p",
"Papa".to_owned(),
"Papa",
vec![
TreeItem::new_leaf("q", "Quebec".to_owned()),
TreeItem::new_leaf("r", "Romeo".to_owned()),
TreeItem::new_leaf("s", "Sierra".to_owned()),
TreeItem::new_leaf("t", "Tango".to_owned()),
TreeItem::new_leaf("u", "Uniform".to_owned()),
TreeItem::new_leaf("q", "Quebec"),
TreeItem::new_leaf("r", "Romeo"),
TreeItem::new_leaf("s", "Sierra"),
TreeItem::new_leaf("t", "Tango"),
TreeItem::new_leaf("u", "Uniform"),
TreeItem::new(
"v",
"Victor".to_owned(),
"Victor",
vec![
TreeItem::new_leaf("w", "Whiskey".to_owned()),
TreeItem::new_leaf("x", "Xray".to_owned()),
TreeItem::new_leaf("y", "Yankee".to_owned()),
TreeItem::new_leaf("w", "Whiskey"),
TreeItem::new_leaf("x", "Xray"),
TreeItem::new_leaf("y", "Yankee"),
],
)
.expect("all item identifiers are unique"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("z", "Zulu".to_owned()),
TreeItem::new_leaf("z", "Zulu"),
],
}
}
+4 -5
View File
@@ -1,8 +1,7 @@
use std::collections::HashSet;
use ratatui::text::Text;
use crate::tree_item::TreeItem;
use ratatui::text::ToText;
/// A flattened item of all visible [`TreeItem`]s.
///
@@ -11,7 +10,7 @@ use crate::tree_item::TreeItem;
pub struct Flattened<'a, Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
pub identifier: Vec<Identifier>,
pub item: &'a TreeItem<Identifier, T>,
@@ -20,7 +19,7 @@ where
impl<'a, Identifier, T> Flattened<'a, Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
/// Zero based depth. Depth 0 means top level with 0 indentation.
#[must_use]
@@ -40,7 +39,7 @@ pub fn flatten<'a, Identifier, T>(
) -> Vec<Flattened<'a, Identifier, T>>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
let mut result = Vec::new();
for item in items {
+7 -7
View File
@@ -10,7 +10,7 @@ use std::collections::HashSet;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::Text;
use ratatui::text::ToText;
use ratatui::widgets::{Block, Scrollbar, ScrollbarState, StatefulWidget, Widget};
use unicode_width::UnicodeWidthStr;
@@ -37,7 +37,7 @@ mod tree_state;
/// # let mut terminal = Terminal::new(TestBackend::new(32, 32)).unwrap();
/// let mut state = TreeState::default();
///
/// let item = TreeItem::new_leaf("l".to_owned(), "leaf".to_owned());
/// let item = TreeItem::new_leaf("l", "leaf");
/// let items = vec![item];
///
/// terminal.draw(|frame| {
@@ -56,7 +56,7 @@ mod tree_state;
pub struct Tree<'a, Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
items: &'a [TreeItem<Identifier, T>],
@@ -81,7 +81,7 @@ where
impl<'a, Identifier, T> Tree<'a, Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
/// Create a new `Tree`.
///
@@ -172,7 +172,7 @@ fn tree_new_errors_with_duplicate_identifiers() {
impl<'a, Identifier, T> StatefulWidget for Tree<'a, Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
type State = TreeState<Identifier>;
@@ -279,7 +279,7 @@ where
height,
};
let text = item.content.clone().into();
let text = item.content.to_text();
let item_style = text.style;
let is_selected = state.selected == *identifier;
@@ -342,7 +342,7 @@ where
impl<'a, Identifier, T> Widget for Tree<'a, Identifier, T>
where
Identifier: Clone + Default + Eq + core::hash::Hash,
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
fn render(self, area: Rect, buf: &mut Buffer) {
let mut state = TreeState::default();
+19 -19
View File
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use ratatui::text::Text;
use ratatui::text::ToText;
/// One item inside a [`Tree`](crate::Tree).
///
@@ -29,15 +29,15 @@ use ratatui::text::Text;
///
/// ```
/// # use managarr_tree_widget::TreeItem;
/// let a = TreeItem::new_leaf("l".to_owned(), "Leaf".to_owned());
/// let b = TreeItem::new("r".to_owned(), "Root".to_owned(), vec![a])?;
/// let a = TreeItem::new_leaf("l", "Leaf");
/// let b = TreeItem::new("r", "Root", vec![a])?;
/// # Ok::<(), std::io::Error>(())
/// ```
#[derive(Debug, Clone)]
pub struct TreeItem<Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'a> Into<Text<'a>> + Clone + Default,
T: ToText + Clone + Default,
{
pub(super) identifier: Identifier,
pub(super) content: T,
@@ -47,7 +47,7 @@ where
impl<Identifier, T> TreeItem<Identifier, T>
where
Identifier: Clone + PartialEq + Eq + core::hash::Hash,
T: for<'a> Into<Text<'a>> + Clone + Default,
T: ToText + Clone + Default,
{
/// Create a new `TreeItem` without children.
#[must_use]
@@ -116,7 +116,7 @@ where
#[must_use]
pub fn height(&self) -> usize {
self.content.clone().into().height()
self.content.clone().to_text().height()
}
/// Add a child to the `TreeItem`.
@@ -142,31 +142,31 @@ where
}
}
impl TreeItem<&'static str, String> {
impl TreeItem<&'static str, &'static str> {
#[cfg(test)]
#[must_use]
pub(crate) fn example() -> Vec<Self> {
vec![
Self::new_leaf("a", "Alfa".to_owned()),
Self::new_leaf("a", "Alfa"),
Self::new(
"b",
"Bravo".to_owned(),
"Bravo",
vec![
Self::new_leaf("c", "Charlie".to_owned()),
Self::new_leaf("c", "Charlie"),
Self::new(
"d",
"Delta".to_owned(),
"Delta",
vec![
Self::new_leaf("e", "Echo".to_owned()),
Self::new_leaf("f", "Foxtrot".to_owned()),
Self::new_leaf("e", "Echo"),
Self::new_leaf("f", "Foxtrot"),
],
)
.expect("all item identifiers are unique"),
Self::new_leaf("g", "Golf".to_owned()),
Self::new_leaf("g", "Golf"),
],
)
.expect("all item identifiers are unique"),
Self::new_leaf("h", "Hotel".to_owned()),
Self::new_leaf("h", "Hotel"),
]
}
}
@@ -174,16 +174,16 @@ impl TreeItem<&'static str, String> {
#[test]
#[should_panic = "duplicate identifiers"]
fn tree_item_new_errors_with_duplicate_identifiers() {
let item = TreeItem::new_leaf("same".to_owned(), "text".to_owned());
let item = TreeItem::new_leaf("same", "text");
let another = item.clone();
TreeItem::new("root".to_owned(), "Root".to_owned(), vec![item, another]).unwrap();
TreeItem::new("root", "Root", vec![item, another]).unwrap();
}
#[test]
#[should_panic = "identifier already exists"]
fn tree_item_add_child_errors_with_duplicate_identifiers() {
let item = TreeItem::new_leaf("same".to_owned(), "text".to_owned());
let item = TreeItem::new_leaf("same", "text");
let another = item.clone();
let mut root = TreeItem::new("root".to_owned(), "Root".to_owned(), vec![item]).unwrap();
let mut root = TreeItem::new("root", "Root", vec![item]).unwrap();
root.add_child(another).unwrap();
}
+2 -2
View File
@@ -1,7 +1,7 @@
use std::collections::HashSet;
use ratatui::layout::{Position, Rect};
use ratatui::text::Text;
use ratatui::text::ToText;
use crate::flatten::{flatten, Flattened};
use crate::tree_item::TreeItem;
@@ -67,7 +67,7 @@ where
items: &'a [TreeItem<Identifier, T>],
) -> Vec<Flattened<'a, Identifier, T>>
where
T: for<'b> Into<Text<'b>> + Clone + Default,
T: ToText + Clone + Default,
{
flatten(&self.opened, items, &[])
}