diff --git a/src/app.rs b/src/app.rs index fdf7db0f..d361cdb1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -29,7 +29,7 @@ use frozen_state::FrozenState; use crate::{ canvas::Painter, constants, - tuine::{Application, Element, Flex, ViewContext}, + tuine::{Application, Element, Flex, Status, ViewContext}, units::data_units::DataUnit, Pid, }; @@ -267,7 +267,7 @@ impl Application for AppState { fn global_event_handler( &mut self, event: crate::tuine::Event, messages: &mut Vec, - ) { + ) -> Status { use crate::tuine::Event; use crossterm::event::{KeyCode, KeyModifiers}; @@ -277,22 +277,27 @@ impl Application for AppState { match event.code { KeyCode::Char('q') | KeyCode::Char('Q') => { messages.push(AppMessages::Quit); + Status::Captured } - _ => {} + _ => Status::Ignored, } } else if let KeyModifiers::CONTROL = event.modifiers { match event.code { KeyCode::Char('c') | KeyCode::Char('C') => { messages.push(AppMessages::Quit); + Status::Captured } KeyCode::Char('r') | KeyCode::Char('R') => { messages.push(AppMessages::Reset); + Status::Captured } - _ => {} + _ => Status::Ignored, } + } else { + Status::Ignored } } - Event::Mouse(_event) => {} + Event::Mouse(_event) => Status::Ignored, } } } diff --git a/src/tuine/application.rs b/src/tuine/application.rs index 6390abf0..97d5c78a 100644 --- a/src/tuine/application.rs +++ b/src/tuine/application.rs @@ -4,7 +4,7 @@ use tui::Terminal; use super::{ runtime::{self, RuntimeEvent}, - Element, Event, ViewContext, + Element, Event, Status, ViewContext, }; /// An alias to the [`tui::backend::CrosstermBackend`] writing to [`std::io::Stdout`]. @@ -31,7 +31,9 @@ pub trait Application: Sized { /// *only* if it is not handled at all by it. /// /// Defaults to not doing anything. - fn global_event_handler(&mut self, event: Event, messages: &mut Vec) {} + fn global_event_handler(&mut self, event: Event, messages: &mut Vec) -> Status { + Status::Ignored + } } /// Launches some application with tuine. Note this will take over the calling thread. diff --git a/src/tuine/component/base/text_table/data_cell.rs b/src/tuine/component/base/text_table/data_cell.rs index f52cabae..2b3a32c7 100644 --- a/src/tuine/component/base/text_table/data_cell.rs +++ b/src/tuine/component/base/text_table/data_cell.rs @@ -1,73 +1,61 @@ use std::{borrow::Cow, fmt::Display}; use enum_dispatch::enum_dispatch; +use float_ord::FloatOrd; use tui::widgets::Cell; #[enum_dispatch] -pub trait Numeric {} -impl Numeric for f64 {} -impl Numeric for f32 {} -impl Numeric for i64 {} -impl Numeric for i32 {} -impl Numeric for i16 {} -impl Numeric for i8 {} -impl Numeric for isize {} -impl Numeric for u64 {} -impl Numeric for u32 {} -impl Numeric for u16 {} -impl Numeric for u8 {} -impl Numeric for usize {} +pub trait DataCellValue {} + +impl DataCellValue for FloatOrd {} +impl DataCellValue for FloatOrd {} +impl DataCellValue for i64 {} +impl DataCellValue for i32 {} +impl DataCellValue for i16 {} +impl DataCellValue for i8 {} +impl DataCellValue for isize {} +impl DataCellValue for u64 {} +impl DataCellValue for u32 {} +impl DataCellValue for u16 {} +impl DataCellValue for u8 {} +impl DataCellValue for usize {} +impl DataCellValue for Cow<'static, str> {} #[allow(non_camel_case_types)] -#[derive(Clone, Copy)] -#[enum_dispatch(Numeric)] -pub enum Number { - f64, - f32, - i64, - i32, - i16, - i8, - isize, - u64, - u32, - u16, - u8, - usize, -} - -impl Display for Number { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match &self { - Number::f64(val) => write!(f, "{}", val), - Number::f32(val) => write!(f, "{}", val), - Number::i64(val) => write!(f, "{}", val), - Number::i32(val) => write!(f, "{}", val), - Number::i16(val) => write!(f, "{}", val), - Number::i8(val) => write!(f, "{}", val), - Number::isize(val) => write!(f, "{}", val), - Number::u64(val) => write!(f, "{}", val), - Number::u32(val) => write!(f, "{}", val), - Number::u16(val) => write!(f, "{}", val), - Number::u8(val) => write!(f, "{}", val), - Number::usize(val) => write!(f, "{}", val), - } - } -} - -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] +#[enum_dispatch(DataCellValue)] pub enum DataCell { - NumberCell(Number), - String(Cow<'static, str>), + f64(FloatOrd), + f32(FloatOrd), + i64(i64), + i32(i32), + i16(i16), + i8(i8), + isize(isize), + u64(u64), + u32(u32), + u16(u16), + u8(u8), + usize(usize), + Cow(Cow<'static, str>), } -impl DataCell {} - impl Display for DataCell { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - DataCell::NumberCell(n) => n.fmt(f), - DataCell::String(d) => d.fmt(f), + DataCell::f64(val) => val.0.fmt(f), + DataCell::f32(val) => val.0.fmt(f), + DataCell::i64(val) => val.fmt(f), + DataCell::i32(val) => val.fmt(f), + DataCell::i16(val) => val.fmt(f), + DataCell::i8(val) => val.fmt(f), + DataCell::isize(val) => val.fmt(f), + DataCell::u64(val) => val.fmt(f), + DataCell::u32(val) => val.fmt(f), + DataCell::u16(val) => val.fmt(f), + DataCell::u8(val) => val.fmt(f), + DataCell::usize(val) => val.fmt(f), + DataCell::Cow(val) => val.fmt(f), } } } @@ -78,20 +66,26 @@ impl From for Cell<'_> { } } -impl From for DataCell { - fn from(num: Number) -> Self { - DataCell::NumberCell(num) +impl From for DataCell { + fn from(num: f64) -> Self { + DataCell::f64(FloatOrd(num)) + } +} + +impl From for DataCell { + fn from(num: f32) -> Self { + DataCell::f32(FloatOrd(num)) } } impl From for DataCell { fn from(s: String) -> Self { - DataCell::String(s.into()) + DataCell::Cow(Cow::from(s)) } } impl From<&'static str> for DataCell { fn from(s: &'static str) -> Self { - DataCell::String(s.into()) + DataCell::Cow(Cow::from(s)) } } diff --git a/src/tuine/component/base/text_table/data_row.rs b/src/tuine/component/base/text_table/data_row.rs index 478d3c7e..52610bdf 100644 --- a/src/tuine/component/base/text_table/data_row.rs +++ b/src/tuine/component/base/text_table/data_row.rs @@ -2,21 +2,33 @@ use tui::{style::Style, widgets::Row}; use super::DataCell; -#[derive(Clone)] +#[derive(Default, Clone)] pub struct DataRow { - pub cells: Vec, - pub style: Option