refactor(tui): rename StatefulTable widget to SelectableList

This commit is contained in:
Orhun Parmaksız 2022-01-26 02:32:30 +03:00
parent 949b7ec0b8
commit bb483b9d62
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 27 additions and 27 deletions

View file

@ -1,7 +1,7 @@
use crate::command::Command;
use crate::error::Result;
use crate::options::{CopyOption, Direction, ScrollArea};
use crate::widgets::StatefulTable;
use crate::widgets::SelectableList;
#[cfg(feature = "clipboard")]
use copypasta_ext::{display::DisplayServer, prelude::ClipboardProvider};
use std::str::FromStr;
@ -29,11 +29,11 @@ pub struct App<'a> {
/// Y-scroll offset for the documentation.
pub docs_scroll_amount: u16,
/// Entries of the options menu.
pub options: Option<StatefulTable<&'a str>>,
pub options: Option<SelectableList<&'a str>>,
/// List of sysctl parameters.
pub parameter_list: StatefulTable<Parameter>,
pub parameter_list: SelectableList<Parameter>,
/// List of sysctl sections.
pub section_list: StatefulTable<String>,
pub section_list: SelectableList<String>,
#[cfg(feature = "clipboard")]
/// Clipboard context.
clipboard: Option<Box<dyn ClipboardProvider>>,
@ -52,8 +52,8 @@ impl<'a> App<'a> {
search_mode: false,
docs_scroll_amount: 0,
options: None,
parameter_list: StatefulTable::default(),
section_list: StatefulTable::with_items({
parameter_list: SelectableList::default(),
section_list: SelectableList::with_items({
let mut sections = Section::variants()
.iter()
.map(|v| v.to_string())
@ -103,7 +103,7 @@ impl<'a> App<'a> {
self.parameter_list.state.select(Some(0));
}
} else {
self.parameter_list = StatefulTable::with_items(self.sysctl.parameters.clone());
self.parameter_list = SelectableList::with_items(self.sysctl.parameters.clone());
}
self.docs_scroll_amount = 0;
}
@ -356,7 +356,7 @@ impl<'a> App<'a> {
{
copy_options.retain(|v| v != &CopyOption::Documentation)
}
self.options = Some(StatefulTable::with_items(
self.options = Some(SelectableList::with_items(
copy_options.iter().map(|v| v.as_str()).collect(),
));
} else {

View file

@ -1,5 +1,5 @@
use crate::app::App;
use crate::widgets::StatefulTable;
use crate::widgets::SelectableList;
use tui::backend::Backend;
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use tui::style::{Color, Style};
@ -206,7 +206,7 @@ fn render_section_text<B: Backend>(frame: &mut Frame<'_, B>, rect: Rect, section
fn render_options_menu<B: Backend>(
frame: &mut Frame<'_, B>,
rect: Rect,
options: &mut StatefulTable<&str>,
options: &mut SelectableList<&str>,
) {
let (length_x, length_y) = (
25,

View file

@ -1,29 +1,29 @@
use tui::widgets::TableState;
/// Table widget with TUI controlled states.
/// List widget with TUI controlled states.
#[derive(Debug)]
pub struct StatefulTable<T> {
/// Table items (states).
pub struct SelectableList<T> {
/// List items.
pub items: Vec<T>,
/// State that can be modified by TUI.
pub state: TableState,
}
impl<T> Default for StatefulTable<T> {
impl<T> Default for SelectableList<T> {
fn default() -> Self {
Self::with_items(Vec::new())
}
}
impl<T> StatefulTable<T> {
/// Constructs a new instance of `StatefulTable`.
pub fn new(items: Vec<T>, mut state: TableState) -> StatefulTable<T> {
impl<T> SelectableList<T> {
/// Constructs a new instance of `SelectableList`.
pub fn new(items: Vec<T>, mut state: TableState) -> SelectableList<T> {
state.select(Some(0));
Self { items, state }
}
/// Construct a new `StatefulTable` with given items.
pub fn with_items(items: Vec<T>) -> StatefulTable<T> {
/// Construct a new `SelectableList` with given items.
pub fn with_items(items: Vec<T>) -> SelectableList<T> {
Self::new(items, TableState::default())
}
@ -68,13 +68,13 @@ mod tests {
use super::*;
#[test]
fn test_stateful_table() {
let mut table = StatefulTable::with_items(vec!["data1", "data2", "data3"]);
table.state.select(Some(1));
assert_eq!(Some(&"data2"), table.selected());
table.next();
assert_eq!(Some(2), table.state.selected());
table.previous();
assert_eq!(Some(1), table.state.selected());
fn test_selectable_list() {
let mut list = SelectableList::with_items(vec!["data1", "data2", "data3"]);
list.state.select(Some(1));
assert_eq!(Some(&"data2"), list.selected());
list.next();
assert_eq!(Some(2), list.state.selected());
list.previous();
assert_eq!(Some(1), list.state.selected());
}
}