Some refactoring

This commit is contained in:
ClementTsang 2021-12-24 23:12:58 -05:00
parent f56d70b9ee
commit 79c0bedd51
8 changed files with 78 additions and 39 deletions

View File

@ -200,7 +200,7 @@ impl AppState {
fn set_current_screen(&mut self, screen_type: CurrentScreen) {
if self.current_screen == screen_type {
self.current_screen = screen_type;
// TODO: Redraw
// FIXME: Redraw with new screen, save old screen state if main
}
}
}

View File

@ -8,7 +8,6 @@ use bottom::{app::AppMessages, options::*, tuine::RuntimeEvent, *};
use std::{
boxed::Box,
io::stdout,
panic,
sync::{mpsc, Arc, Condvar, Mutex},
thread,
@ -16,12 +15,6 @@ use std::{
};
use anyhow::{Context, Result};
use crossterm::{
event::EnableMouseCapture,
execute,
terminal::{enable_raw_mode, EnterAlternateScreen},
};
use tui::{backend::CrosstermBackend, Terminal};
fn main() -> Result<()> {
let matches = clap::get_matches();
@ -90,13 +83,7 @@ fn main() -> Result<()> {
);
// Set up up tui and crossterm
let mut stdout_val = stdout();
execute!(stdout_val, EnterAlternateScreen, EnableMouseCapture)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?;
terminal.clear()?;
terminal.hide_cursor()?;
let mut terminal = init_terminal()?;
// Set panic hook
// TODO: [Threads, Panic] Make this close all the child threads too!
@ -109,7 +96,7 @@ fn main() -> Result<()> {
thread_termination_cvar.notify_all();
let _ = input_thread.join();
cleanup_terminal(&mut terminal)?;
cleanup_terminal(&mut terminal);
Ok(())
}

View File

@ -9,7 +9,7 @@ extern crate log;
use std::{
boxed::Box,
fs,
io::{stdout, Write},
io::{stdout, Stdout, Write},
panic::PanicInfo,
path::PathBuf,
sync::Arc,
@ -20,15 +20,19 @@ use std::{
};
use crossterm::{
event::{poll, read, DisableMouseCapture, MouseEventKind},
event::{poll, read, DisableMouseCapture, EnableMouseCapture, MouseEventKind},
execute,
style::Print,
terminal::{disable_raw_mode, LeaveAlternateScreen},
terminal::{
disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen,
LeaveAlternateScreen,
},
};
use app::{data_harvester, AppMessages, UsedWidgets};
use constants::*;
use options::*;
use tui::{backend::CrosstermBackend, terminal::Terminal};
use tuine::{Event, RuntimeEvent};
use utils::error;
@ -116,18 +120,28 @@ pub fn create_or_get_config(config_path: &Option<PathBuf>) -> error::Result<Conf
}
}
pub fn cleanup_terminal(
terminal: &mut tui::terminal::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>,
) -> error::Result<()> {
disable_raw_mode()?;
pub fn init_terminal() -> anyhow::Result<Terminal<CrosstermBackend<Stdout>>> {
let mut stdout_val = stdout();
execute!(stdout_val, EnterAlternateScreen, EnableMouseCapture)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?;
terminal.clear()?;
terminal.hide_cursor()?;
Ok(terminal)
}
pub fn cleanup_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) {
terminal.clear().unwrap();
disable_raw_mode().unwrap();
execute!(
terminal.backend_mut(),
DisableMouseCapture,
LeaveAlternateScreen
)?;
terminal.show_cursor()?;
Ok(())
)
.unwrap();
terminal.show_cursor().unwrap();
}
/// Based on https://github.com/Rigellute/spotify-tui/blob/master/src/main.rs
@ -146,7 +160,13 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
let stacktrace: String = format!("{:?}", backtrace::Backtrace::new());
disable_raw_mode().unwrap();
execute!(stdout, DisableMouseCapture, LeaveAlternateScreen).unwrap();
execute!(
stdout,
Clear(ClearType::All),
DisableMouseCapture,
LeaveAlternateScreen
)
.unwrap();
// Print stack trace. Must be done after!
execute!(

View File

@ -0,0 +1,10 @@
use std::borrow::Cow;
use tui::style::Style;
pub enum DataCell {
Display,
Numeric,
}
impl DataCell {}

View File

@ -0,0 +1,15 @@
use tui::style::Style;
use super::DataCell;
pub struct DataRow {
cells: Vec<DataCell>,
style: Option<Style>,
}
impl DataRow {
pub fn style(mut self, style: Option<Style>) -> Self {
self.style = style;
self
}
}

View File

@ -1,5 +1,14 @@
pub mod table_column;
pub use self::table_column::{TextColumn, TextColumnConstraint};
mod table_scroll_state;
use self::table_scroll_state::ScrollState as TextTableState;
pub mod data_row;
pub use data_row::DataRow;
pub mod data_cell;
pub use data_cell::DataCell;
use std::{borrow::Cow, cmp::min, panic::Location};
@ -17,9 +26,6 @@ use crate::{
tuine::{DrawContext, Event, Key, StateContext, Status, TmpComponent, ViewContext},
};
pub use self::table_column::{TextColumn, TextColumnConstraint};
use self::table_scroll_state::ScrollState as TextTableState;
#[derive(Clone, Debug, Default)]
pub struct StyleSheet {
text: Style,
@ -67,7 +73,6 @@ impl<'a, Message> TextTable<'a, Message> {
///
/// Defaults to displaying no data if not set.
pub fn rows(mut self, rows: Vec<Row<'a>>) -> Self {
self.rows = rows;
self
}

View File

@ -20,7 +20,7 @@ pub trait TmpComponent<Message> {
) where
Backend: tui::backend::Backend;
/// How a component should react to an [`Event`].
/// How a component should react to an [`Event`](super::Event).
///
/// Defaults to just ignoring the event.
fn on_event(

View File

@ -1,12 +1,14 @@
//! tuine is a wrapper around tui-rs that expands upon state management and
//! tuine is a wrapper around tui-rs that expands on it with state management and
//! event handling.
//!
//! tuine is inspired by a **ton** of other libraries and frameworks, like:
//! - Iced
//! - [crochet](https://github.com/raphlinus/crochet)
//! - Flutter
//! - React
//! - Yew
//!
//! - [Crochet](https://github.com/raphlinus/crochet)
//! - [Druid](https://github.com/linebender/druid)
//! - [Flutter](https://flutter.dev/)
//! - [Iced](https://github.com/iced-rs/iced)
//! - [React](https://reactjs.org/)
//! - [Yew](https://yew.rs/)
mod tui_rs;