mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-26 23:24:20 +02:00
Move over to datarow
This commit is contained in:
parent
519ea23b21
commit
3e5dbb75fb
@ -238,7 +238,7 @@ impl Application for AppState {
|
||||
self.terminator.load(SeqCst)
|
||||
}
|
||||
|
||||
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<'static, Self::Message> {
|
||||
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message> {
|
||||
use crate::tuine::FlexElement;
|
||||
use crate::tuine::TempTable;
|
||||
use crate::tuine::TextTable;
|
||||
|
@ -21,7 +21,7 @@ pub trait Application: Sized {
|
||||
/// always returning false.
|
||||
fn is_terminated(&self) -> bool;
|
||||
|
||||
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<'static, Self::Message>;
|
||||
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message>;
|
||||
|
||||
/// To run upon stopping the application.
|
||||
fn destructor(&mut self) {}
|
||||
|
@ -8,16 +8,16 @@ use crate::tuine::{
|
||||
///
|
||||
/// Inspired by Flutter's [Container class](https://api.flutter.dev/flutter/widgets/Container-class.html).
|
||||
#[derive(Default)]
|
||||
pub struct Container<'a, Message> {
|
||||
pub struct Container<Message> {
|
||||
width: Option<u16>,
|
||||
height: Option<u16>,
|
||||
child: Option<Box<Element<'a, Message>>>,
|
||||
child: Option<Box<Element<Message>>>,
|
||||
}
|
||||
|
||||
impl<'a, Message> Container<'a, Message> {
|
||||
impl<Message> Container<Message> {
|
||||
pub fn with_child<C>(child: C) -> Self
|
||||
where
|
||||
C: Into<Element<'a, Message>>,
|
||||
C: Into<Element<Message>>,
|
||||
{
|
||||
Self {
|
||||
width: None,
|
||||
@ -28,7 +28,7 @@ impl<'a, Message> Container<'a, Message> {
|
||||
|
||||
pub fn child<C>(mut self, child: Option<C>) -> Self
|
||||
where
|
||||
C: Into<Element<'a, Message>>,
|
||||
C: Into<Element<Message>>,
|
||||
{
|
||||
self.child = child.map(|c| Box::new(c.into()));
|
||||
self
|
||||
@ -45,7 +45,7 @@ impl<'a, Message> Container<'a, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> TmpComponent<Message> for Container<'a, Message> {
|
||||
impl<Message> TmpComponent<Message> for Container<Message> {
|
||||
fn draw<B>(
|
||||
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
|
||||
frame: &mut Frame<'_, B>,
|
||||
|
@ -6,28 +6,28 @@ use crate::tuine::{
|
||||
|
||||
use super::Axis;
|
||||
|
||||
pub struct FlexElement<'a, Message> {
|
||||
pub struct FlexElement<Message> {
|
||||
/// Represents a ratio with other [`FlexElement`]s on how far to expand.
|
||||
pub flex: u16,
|
||||
element: Element<'a, Message>,
|
||||
element: Element<Message>,
|
||||
}
|
||||
|
||||
impl<'a, Message> FlexElement<'a, Message> {
|
||||
pub fn new<I: Into<Element<'a, Message>>>(element: I) -> Self {
|
||||
impl<Message> FlexElement<Message> {
|
||||
pub fn new<I: Into<Element<Message>>>(element: I) -> Self {
|
||||
Self {
|
||||
flex: 1,
|
||||
element: element.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_flex<I: Into<Element<'a, Message>>>(element: I, flex: u16) -> Self {
|
||||
pub fn with_flex<I: Into<Element<Message>>>(element: I, flex: u16) -> Self {
|
||||
Self {
|
||||
flex,
|
||||
element: element.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_no_flex<I: Into<Element<'a, Message>>>(element: I) -> Self {
|
||||
pub fn with_no_flex<I: Into<Element<Message>>>(element: I) -> Self {
|
||||
Self {
|
||||
flex: 0,
|
||||
element: element.into(),
|
||||
@ -86,8 +86,8 @@ impl<'a, Message> FlexElement<'a, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> From<Element<'a, Message>> for FlexElement<'a, Message> {
|
||||
fn from(element: Element<'a, Message>) -> Self {
|
||||
impl<Message> From<Element<Message>> for FlexElement<Message> {
|
||||
fn from(element: Element<Message>) -> Self {
|
||||
Self { flex: 0, element }
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ pub enum Axis {
|
||||
Vertical,
|
||||
}
|
||||
|
||||
pub struct Flex<'a, Message> {
|
||||
children: Vec<FlexElement<'a, Message>>,
|
||||
pub struct Flex<Message> {
|
||||
children: Vec<FlexElement<Message>>,
|
||||
alignment: Axis,
|
||||
}
|
||||
|
||||
impl<'a, Message> Flex<'a, Message> {
|
||||
impl<Message> Flex<Message> {
|
||||
pub fn new(alignment: Axis) -> Self {
|
||||
Self {
|
||||
children: vec![],
|
||||
@ -40,7 +40,7 @@ impl<'a, Message> Flex<'a, Message> {
|
||||
/// Creates a new [`Flex`] with a horizontal alignment with the given children.
|
||||
pub fn row_with_children<C>(children: Vec<C>) -> Self
|
||||
where
|
||||
C: Into<FlexElement<'a, Message>>,
|
||||
C: Into<FlexElement<Message>>,
|
||||
{
|
||||
Self {
|
||||
children: children.into_iter().map(Into::into).collect(),
|
||||
@ -59,7 +59,7 @@ impl<'a, Message> Flex<'a, Message> {
|
||||
/// Creates a new [`Flex`] with a vertical alignment with the given children.
|
||||
pub fn column_with_children<C>(children: Vec<C>) -> Self
|
||||
where
|
||||
C: Into<FlexElement<'a, Message>>,
|
||||
C: Into<FlexElement<Message>>,
|
||||
{
|
||||
Self {
|
||||
children: children.into_iter().map(Into::into).collect(),
|
||||
@ -69,7 +69,7 @@ impl<'a, Message> Flex<'a, Message> {
|
||||
|
||||
pub fn with_child<E>(mut self, child: E) -> Self
|
||||
where
|
||||
E: Into<Element<'a, Message>>,
|
||||
E: Into<Element<Message>>,
|
||||
{
|
||||
self.children.push(FlexElement::with_no_flex(child.into()));
|
||||
self
|
||||
@ -77,7 +77,7 @@ impl<'a, Message> Flex<'a, Message> {
|
||||
|
||||
pub fn with_flex_child<E>(mut self, child: E, flex: u16) -> Self
|
||||
where
|
||||
E: Into<Element<'a, Message>>,
|
||||
E: Into<Element<Message>>,
|
||||
{
|
||||
self.children
|
||||
.push(FlexElement::with_flex(child.into(), flex));
|
||||
@ -85,7 +85,7 @@ impl<'a, Message> Flex<'a, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> TmpComponent<Message> for Flex<'a, Message> {
|
||||
impl<Message> TmpComponent<Message> for Flex<Message> {
|
||||
fn draw<B>(
|
||||
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
|
||||
frame: &mut Frame<'_, B>,
|
||||
|
@ -19,6 +19,7 @@ impl Numeric for u8 {}
|
||||
impl Numeric for usize {}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone, Copy)]
|
||||
#[enum_dispatch(Numeric)]
|
||||
pub enum Number {
|
||||
f64,
|
||||
@ -54,6 +55,7 @@ impl Display for Number {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum DataCell {
|
||||
NumberCell(Number),
|
||||
String(Cow<'static, str>),
|
||||
|
@ -1,15 +1,30 @@
|
||||
use tui::style::Style;
|
||||
use tui::{style::Style, widgets::Row};
|
||||
|
||||
use super::DataCell;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DataRow {
|
||||
cells: Vec<DataCell>,
|
||||
style: Option<Style>,
|
||||
pub cells: Vec<DataCell>,
|
||||
pub style: Option<Style>,
|
||||
}
|
||||
|
||||
impl DataRow {
|
||||
pub fn new(cells: Vec<DataCell>) -> Self {
|
||||
Self { cells, style: None }
|
||||
}
|
||||
|
||||
pub fn style(mut self, style: Option<Style>) -> Self {
|
||||
self.style = style;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DataRow> for Row<'_> {
|
||||
fn from(row: DataRow) -> Self {
|
||||
if let Some(style) = row.style {
|
||||
Row::new(row.cells).style(style)
|
||||
} else {
|
||||
Row::new(row.cells)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,13 +34,13 @@ pub struct StyleSheet {
|
||||
}
|
||||
|
||||
/// A sortable, scrollable table for text data.
|
||||
pub struct TextTable<'a, Message> {
|
||||
pub struct TextTable<Message> {
|
||||
key: Key,
|
||||
column_widths: Vec<u16>,
|
||||
columns: Vec<TextColumn>,
|
||||
show_gap: bool,
|
||||
show_selected_entry: bool,
|
||||
rows: Vec<Row<'a>>,
|
||||
rows: Vec<DataRow>,
|
||||
style_sheet: StyleSheet,
|
||||
sortable: bool,
|
||||
table_gap: u16,
|
||||
@ -48,7 +48,7 @@ pub struct TextTable<'a, Message> {
|
||||
on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
|
||||
}
|
||||
|
||||
impl<'a, Message> TextTable<'a, Message> {
|
||||
impl<Message> TextTable<Message> {
|
||||
#[track_caller]
|
||||
pub fn new<S: Into<Cow<'static, str>>>(ctx: &mut ViewContext<'_>, columns: Vec<S>) -> Self {
|
||||
Self {
|
||||
@ -72,7 +72,13 @@ impl<'a, Message> TextTable<'a, Message> {
|
||||
/// Sets the row to display in the table.
|
||||
///
|
||||
/// Defaults to displaying no data if not set.
|
||||
pub fn rows(mut self, rows: Vec<Row<'a>>) -> Self {
|
||||
pub fn rows(mut self, rows: Vec<DataRow>) -> Self {
|
||||
self.rows = rows;
|
||||
|
||||
if self.sortable {
|
||||
self.sort_data();
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
@ -98,6 +104,11 @@ impl<'a, Message> TextTable<'a, Message> {
|
||||
/// Defaults to `false` if not set.
|
||||
pub fn sortable(mut self, sortable: bool) -> Self {
|
||||
self.sortable = sortable;
|
||||
|
||||
if self.sortable {
|
||||
self.sort_data();
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
@ -120,6 +131,10 @@ impl<'a, Message> TextTable<'a, Message> {
|
||||
self
|
||||
}
|
||||
|
||||
fn sort_data(&mut self) {
|
||||
self.rows.sort_by(|a, b| todo!());
|
||||
}
|
||||
|
||||
fn update_column_widths(&mut self, bounds: Rect) {
|
||||
let total_width = bounds.width;
|
||||
let mut width_remaining = bounds.width;
|
||||
@ -169,7 +184,7 @@ impl<'a, Message> TextTable<'a, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> TmpComponent<Message> for TextTable<'a, Message> {
|
||||
impl<Message> TmpComponent<Message> for TextTable<Message> {
|
||||
fn draw<B>(
|
||||
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
|
||||
frame: &mut Frame<'_, B>,
|
||||
@ -205,7 +220,10 @@ impl<'a, Message> TmpComponent<Message> for TextTable<'a, Message> {
|
||||
let start = state.display_start_index(rect, scrollable_height as usize);
|
||||
let end = min(state.num_items(), start + scrollable_height as usize);
|
||||
|
||||
self.rows[start..end].to_vec()
|
||||
self.rows.drain(start..end).into_iter().map(|row| {
|
||||
let r: Row<'_> = row.into();
|
||||
r
|
||||
})
|
||||
};
|
||||
|
||||
// Now build up our headers...
|
||||
|
@ -1,11 +1,11 @@
|
||||
use crate::tuine::{Shortcut, TextTable, TmpComponent, ViewContext};
|
||||
|
||||
/// A [`TempTable`] is a text table that is meant to display temperature data.
|
||||
pub struct TempTable<'a, Message> {
|
||||
inner: Shortcut<Message, TextTable<'a, Message>>,
|
||||
pub struct TempTable<Message> {
|
||||
inner: Shortcut<Message, TextTable<Message>>,
|
||||
}
|
||||
|
||||
impl<'a, Message> TempTable<'a, Message> {
|
||||
impl<Message> TempTable<Message> {
|
||||
#[track_caller]
|
||||
pub fn new(ctx: &mut ViewContext<'_>) -> Self {
|
||||
Self {
|
||||
@ -14,7 +14,7 @@ impl<'a, Message> TempTable<'a, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> TmpComponent<Message> for TempTable<'a, Message> {
|
||||
impl<Message> TmpComponent<Message> for TempTable<Message> {
|
||||
fn draw<Backend>(
|
||||
&mut self, state_ctx: &mut crate::tuine::StateContext<'_>,
|
||||
draw_ctx: &crate::tuine::DrawContext<'_>, frame: &mut tui::Frame<'_, Backend>,
|
||||
|
@ -8,16 +8,16 @@ use super::{
|
||||
|
||||
/// An [`Element`] is an instantiated [`Component`].
|
||||
#[enum_dispatch(TmpComponent<Message>)]
|
||||
pub enum Element<'a, Message, C = Empty>
|
||||
pub enum Element<Message, C = Empty>
|
||||
where
|
||||
C: TmpComponent<Message>,
|
||||
{
|
||||
Block,
|
||||
Carousel,
|
||||
Container(Container<'a, Message>),
|
||||
Flex(Flex<'a, Message>),
|
||||
Container(Container<Message>),
|
||||
Flex(Flex<Message>),
|
||||
Shortcut(Shortcut<Message, C>),
|
||||
TextTable(TextTable<'a, Message>),
|
||||
TextTable(TextTable<Message>),
|
||||
Empty,
|
||||
TempTable(TempTable<'a, Message>),
|
||||
TempTable(TempTable<Message>),
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use tui::layout::Rect;
|
||||
|
||||
use crate::tuine::{Bounds, Element, LayoutNode, TmpComponent};
|
||||
|
||||
pub fn build_layout_tree<Message>(rect: Rect, root: &Element<'_, Message>) -> LayoutNode {
|
||||
pub fn build_layout_tree<Message>(rect: Rect, root: &Element<Message>) -> LayoutNode {
|
||||
let mut root_layout_node = LayoutNode::from_rect(rect);
|
||||
let bounds = Bounds {
|
||||
min_width: 0,
|
||||
|
@ -75,7 +75,7 @@ where
|
||||
|
||||
/// Handles a [`Event`].
|
||||
fn on_event<A>(
|
||||
application: &mut A, user_interface: &mut Element<'_, A::Message>, app_data: &mut AppData,
|
||||
application: &mut A, user_interface: &mut Element<A::Message>, app_data: &mut AppData,
|
||||
layout: &mut LayoutNode, event: Event,
|
||||
) where
|
||||
A: Application + 'static,
|
||||
@ -100,9 +100,7 @@ fn on_event<A>(
|
||||
}
|
||||
|
||||
/// Creates a new [`Element`] representing the root of the user interface.
|
||||
fn new_user_interface<A>(
|
||||
application: &mut A, app_data: &mut AppData,
|
||||
) -> Element<'static, A::Message>
|
||||
fn new_user_interface<A>(application: &mut A, app_data: &mut AppData) -> Element<A::Message>
|
||||
where
|
||||
A: Application + 'static,
|
||||
{
|
||||
@ -112,7 +110,7 @@ where
|
||||
|
||||
/// Updates the layout, and draws the given user interface.
|
||||
fn draw<M, B>(
|
||||
user_interface: &mut Element<'_, M>, terminal: &mut Terminal<B>, app_data: &mut AppData,
|
||||
user_interface: &mut Element<M>, terminal: &mut Terminal<B>, app_data: &mut AppData,
|
||||
layout: &mut LayoutNode,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
|
Loading…
x
Reference in New Issue
Block a user