Skip to content

Commit

Permalink
Update tui-widget list to 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Reiter committed Aug 10, 2024
1 parent e315ad1 commit 99811fe
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wireman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ core = { package = "wireman-core", path = "../wireman-core", version = "0.1.0" }
config = { package = "wireman-config", path = "../wireman-config", version = "0.1.0" }
logger = { package = "wireman-logger", path = "../wireman-logger", version = "0.1.0" }
theme = { package = "wireman-theme", path = "../wireman-theme", version = "0.1.0" }
tui-widget-list = { version = "0.10.0" }
tui-widget-list = { version = "0.11.0" }
# tui-widget-list = { git = "https://github.com/preiter93/tui-widget-list.git", branch = "v0.9" }
# tui-widget-list = { path = "../../tui-widget-list", version = "0.9.0"}
tui-key-event-handler = { package = "tui-key-event-handler", path = "../tui-key-event-handler", version = "0.1.0" }
Expand Down
11 changes: 9 additions & 2 deletions wireman/src/view/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::{
widgets::{Block, StatefulWidget, Tabs, Widget},
};
use theme::Theme;
use tui_widget_list::{List, ListState};
use tui_widget_list::{ListBuilder, ListState, ListView};

/// The request and response tab
pub struct HeadersPage<'a> {
Expand Down Expand Up @@ -210,7 +210,14 @@ impl Widget for Metadata {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut state = ListState::default();
state.selected = self.selected_row;
let list = List::new(self.content);
let item_count = self.content.len();
let list = ListView::new(
ListBuilder::new(move |context| {
let item = self.content[context.index].clone();
(item, 3)
}),
item_count,
);
list.render(area, buf, &mut state);
}
}
57 changes: 40 additions & 17 deletions wireman/src/view/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ratatui::layout::Rect;
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Padding, Paragraph, StatefulWidget, Widget};
use theme::Theme;
use tui_widget_list::List;
use tui_widget_list::{ListBuilder, ListView};

/// The page where to select services and methods.
pub struct SelectionPage<'a> {
Expand Down Expand Up @@ -57,11 +57,7 @@ impl Widget for SelectionPage<'_> {
.padding(Padding::new(1, 1, 1, 1));

// Services
let services = self
.model
.services()
.into_iter()
.map(|service| ListItem::new(service.clone()));
let services = self.model.services();
let services_state = &mut self.model.services_state;
let mut services_block = block
.clone()
Expand All @@ -75,11 +71,25 @@ impl Widget for SelectionPage<'_> {
.border_style(theme.border.border.1)
.border_type(theme.border.border_type.1);
}
List::new(services.collect()).block(services_block).render(
svc_content,
buf,
services_state,
);

let item_count = services.len();
let builder = ListBuilder::new(move |context| {
let theme = Theme::global();
let title = &services[context.index];
let mut widget = ListItem::new(title.to_string());

if context.is_selected {
widget.prefix = Some(">");
widget.style = theme.list.focused;
} else {
widget.style = theme.list.text;
}
(widget, 1)
});

ListView::new(builder, item_count)
.block(services_block)
.render(svc_content, buf, services_state);

// Search line for services
if show_services_search == 1 {
Expand All @@ -88,25 +98,38 @@ impl Widget for SelectionPage<'_> {
}

// Methods
let methods = self
.model
.methods()
.into_iter()
.map(|method| ListItem::new(method.clone()));
let methods = self.model.methods();
let methods_state = &mut self.model.methods_state;
let mut methods_block = block
.clone()
.title("Methods")
.title_style(theme.border.text.0)
.border_style(theme.border.border.0)
.border_type(theme.border.border_type.0);

if [SelectionTab::Methods, SelectionTab::SearchMethods].contains(&self.tab) {
methods_block = methods_block
.title_style(theme.border.text.1)
.border_style(theme.border.border.1)
.border_type(theme.border.border_type.1);
}
List::new(methods.collect())

let item_count = methods.len();
let builder = ListBuilder::new(move |context| {
let theme = Theme::global();
let title = &methods[context.index];
let mut widget = ListItem::new(title.to_string());

if context.is_selected {
widget.prefix = Some(">>");
widget.style = theme.list.focused;
} else {
widget.style = theme.list.text;
}
(widget, 1)
});

ListView::new(builder, item_count)
.block(methods_block)
.render(mtd_content, buf, methods_state);

Expand Down
7 changes: 0 additions & 7 deletions wireman/src/widgets/kv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::widgets::editor::{view_single_selected, view_single_unselected};
use edtui::EditorState;
use ratatui::{prelude::*, widgets::Widget};
use tui_widget_list::{PreRender, PreRenderContext};

#[derive(Clone)]
pub(crate) struct KV {
Expand All @@ -22,12 +21,6 @@ impl KV {
}
}

impl PreRender for KV {
fn pre_render(&mut self, _context: &PreRenderContext) -> u16 {
3
}
}

impl Widget for KV {
fn render(mut self, area: Rect, buf: &mut Buffer) {
use ratatui::layout::Constraint::Percentage;
Expand Down
20 changes: 0 additions & 20 deletions wireman/src/widgets/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use ratatui::prelude::*;
use theme::Theme;
use tui_widget_list::{PreRender, PreRenderContext};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListItem<'a> {
Expand All @@ -26,24 +24,6 @@ impl<'a> ListItem<'a> {
}
}
}
impl PreRender for ListItem<'_> {
fn pre_render(&mut self, context: &PreRenderContext) -> u16
where
Self: Sized,
{
let theme = Theme::global();
let main_axis_size = 1;

if context.is_selected {
self.prefix = Some(">>");
self.style = theme.list.focused;
} else {
self.style = theme.list.text;
}

main_axis_size
}
}

impl Widget for ListItem<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
Expand Down

0 comments on commit 99811fe

Please sign in to comment.