Skip to content

Commit

Permalink
Revert "refactor: Create a prototype time graph element"
Browse files Browse the repository at this point in the history
This reverts commit bd95ca8.
  • Loading branch information
ClementTsang committed Jul 13, 2021
1 parent 1bcd0eb commit 0eefac5
Show file tree
Hide file tree
Showing 28 changed files with 132 additions and 548 deletions.
16 changes: 8 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ pub struct AppConfigFields {
#[derive(TypedBuilder)]
pub struct AppState {
#[builder(default = false, setter(skip))]
awaiting_second_char: bool, // TODO: Move out to input
awaiting_second_char: bool,

#[builder(default, setter(skip))]
second_char: Option<char>, // TODO: Move out to input
second_char: Option<char>,

#[builder(default, setter(skip))]
pub dd_err: Option<String>,
Expand All @@ -83,7 +83,7 @@ pub struct AppState {
pub is_frozen: bool,

#[builder(default = Instant::now(), setter(skip))]
last_key_press: Instant, // TODO: Move out to input
last_key_press: Instant,

#[builder(default, setter(skip))]
pub canvas_data: canvas::DisplayableData,
Expand Down Expand Up @@ -129,11 +129,11 @@ pub struct AppState {
}

#[cfg(target_os = "windows")]
const MAX_KILL_SIGNAL: usize = 1;
const MAX_SIGNAL: usize = 1;
#[cfg(target_os = "linux")]
const MAX_KILL_SIGNAL: usize = 64;
const MAX_SIGNAL: usize = 64;
#[cfg(target_os = "macos")]
const MAX_KILL_SIGNAL: usize = 31;
const MAX_SIGNAL: usize = 31;

impl AppState {
pub fn reset(&mut self) {
Expand Down Expand Up @@ -967,7 +967,7 @@ impl AppState {
if self.delete_dialog_state.is_showing_dd {
let mut new_signal = match self.delete_dialog_state.selected_signal {
KillSignal::Cancel => 8,
KillSignal::Kill(signal) => min(signal + 8, MAX_KILL_SIGNAL),
KillSignal::Kill(signal) => min(signal + 8, MAX_SIGNAL),
};
if new_signal > 31 && new_signal < 42 {
new_signal += 2;
Expand Down Expand Up @@ -2133,7 +2133,7 @@ impl AppState {
.max_scroll_index
.saturating_sub(1);
} else if self.delete_dialog_state.is_showing_dd {
self.delete_dialog_state.selected_signal = KillSignal::Kill(MAX_KILL_SIGNAL);
self.delete_dialog_state.selected_signal = KillSignal::Kill(MAX_SIGNAL);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/app/layout_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,25 @@ impl std::str::FromStr for BottomWidgetType {
"empty" => Ok(BottomWidgetType::Empty),
"battery" | "batt" => Ok(BottomWidgetType::Battery),
_ => Err(BottomError::ConfigError(format!(
"\"{}\" is an invalid widget name.",
"\"{}\" is an invalid widget name.
Supported widget names:
+--------------------------+
| cpu |
+--------------------------+
| mem, memory |
+--------------------------+
| net, network |
+--------------------------+
| proc, process, processes |
+--------------------------+
| temp, temperature |
+--------------------------+
| disk |
+--------------------------+
| batt, battery |
+--------------------------+
",
s
))),
}
Expand Down
1 change: 1 addition & 0 deletions src/app/widget_states/graph_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! States for a graph widget.
4 changes: 2 additions & 2 deletions src/app/widget_states/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum CursorDirection {
}

/// AppScrollWidgetState deals with fields for a scrollable app's current state.
#[derive(Debug, Default)]
#[derive(Default)]
pub struct AppScrollWidgetState {
pub current_scroll_position: usize,
pub previous_scroll_position: usize,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Default for AppHelpDialogState {
}

/// Meant for canvas operations involving table column widths.
#[derive(Debug, Default)]
#[derive(Default)]
pub struct CanvasTableWidthState {
pub desired_column_widths: Vec<u16>,
pub calculated_column_widths: Vec<u16>,
Expand Down
1 change: 1 addition & 0 deletions src/app/widget_states/table_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! States for a table widget.
78 changes: 31 additions & 47 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[macro_use]
extern crate log;

use bottom::{canvas, data_conversion::*, options::*, *};
use bottom::{canvas, constants::*, data_conversion::*, options::*, *};

use std::{
boxed::Box,
Expand Down Expand Up @@ -61,30 +61,6 @@ fn main() -> Result<()> {
get_color_scheme(&matches, &config)?,
)?;

// 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()?;

// Set panic hook
panic::set_hook(Box::new(|info| panic_hook(info)));

// Set termination hook
let is_terminated = Arc::new(AtomicBool::new(false));
{
let is_terminated = is_terminated.clone();
ctrlc::set_handler(move || {
is_terminated.store(true, Ordering::SeqCst);
})?;
}
let mut first_pass = true;

// ===== Start of actual thread creation and loop =====

// Create termination mutex and cvar
#[allow(clippy::mutex_atomic)]
let thread_termination_lock = Arc::new(Mutex::new(false));
Expand Down Expand Up @@ -131,34 +107,46 @@ fn main() -> Result<()> {
app.used_widgets.clone(),
);

// 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()?;

// Set panic hook
panic::set_hook(Box::new(|info| panic_hook(info)));

// Set termination hook
let is_terminated = Arc::new(AtomicBool::new(false));
let ist_clone = is_terminated.clone();
ctrlc::set_handler(move || {
ist_clone.store(true, Ordering::SeqCst);
})?;
let mut first_run = true;

while !is_terminated.load(Ordering::SeqCst) {
if let Ok(recv) = receiver.recv() {
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
match recv {
BottomEvent::KeyInput(event) => {
if handle_key_event_or_break(event, &mut app, &collection_thread_ctrl_sender) {
break;
}
handle_force_redraws(&mut app);

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
BottomEvent::MouseInput(event) => {
handle_mouse_event(event, &mut app);
handle_force_redraws(&mut app);

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
BottomEvent::Update(data) => {
app.data_collection.eat_data(data);

// This thing is required as otherwise, some widgets can't draw correctly w/o
// some data (or they need to be re-drawn).
if first_pass {
first_pass = false;
if first_run {
first_run = false;
app.is_force_redraw = true;
}

Expand Down Expand Up @@ -233,29 +221,25 @@ fn main() -> Result<()> {
convert_battery_harvest(&app.data_collection);
}
}

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
BottomEvent::Clean => {
app.data_collection
.clean_data(constants::STALE_MAX_MILLISECONDS);
}
BottomEvent::RequestRedraw => {
if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
}
}

// TODO: [OPT] Should not draw if no change (ie: scroll max)
try_drawing(&mut terminal, &mut app, &mut painter)?;
}

// I think doing it in this order is safe...

*thread_termination_lock.lock().unwrap() = true;

thread_termination_cvar.notify_all();
cleanup_terminal(&mut terminal)?;

// ===== End of actual thread creation and loop =====
cleanup_terminal(&mut terminal)?;

Ok(())
}
34 changes: 18 additions & 16 deletions src/canvas/mod.rs → src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use std::{collections::HashMap, str::FromStr};

use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout as tuiLayout, Rect},
layout::{Constraint, Direction, Layout, Rect},
text::{Span, Spans},
widgets::Paragraph,
Frame, Terminal,
};

// use ordered_float::OrderedFloat;

use canvas_colours::*;
use dialogs::*;
use elements::*;
use widgets::*;

use crate::{
app::{
Expand All @@ -30,7 +32,7 @@ use crate::{
mod canvas_colours;
mod dialogs;
mod drawing_utils;
mod elements;
mod widgets;

/// Point is of time, data
type Point = (f64, f64);
Expand Down Expand Up @@ -287,7 +289,7 @@ impl Painter {
"Frozen, press 'f' to unfreeze",
self.colours.currently_selected_text_style,
)),
tuiLayout::default()
Layout::default()
.horizontal_margin(1)
.constraints([Constraint::Length(1)])
.split(draw_loc)[0],
Expand All @@ -301,7 +303,7 @@ impl Painter {

terminal.draw(|mut f| {
let (terminal_size, frozen_draw_loc) = if app_state.is_frozen {
let split_loc = tuiLayout::default()
let split_loc = Layout::default()
.constraints([Constraint::Min(0), Constraint::Length(1)])
.split(f.size());
(split_loc[0], Some(split_loc[1]))
Expand Down Expand Up @@ -344,7 +346,7 @@ impl Painter {
if app_state.help_dialog_state.is_showing_help {
let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3;
let border_len = terminal_height.saturating_sub(gen_help_len) / 2;
let vertical_dialog_chunk = tuiLayout::default()
let vertical_dialog_chunk = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(border_len),
Expand All @@ -353,7 +355,7 @@ impl Painter {
])
.split(terminal_size);

let middle_dialog_chunk = tuiLayout::default()
let middle_dialog_chunk = Layout::default()
.direction(Direction::Horizontal)
.constraints(if terminal_width < 100 {
// TODO: [REFACTOR] The point we start changing size at currently hard-coded in.
Expand Down Expand Up @@ -427,7 +429,7 @@ impl Painter {
// };

let vertical_bordering = terminal_height.saturating_sub(text_height) / 2;
let vertical_dialog_chunk = tuiLayout::default()
let vertical_dialog_chunk = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(vertical_bordering),
Expand All @@ -437,7 +439,7 @@ impl Painter {
.split(terminal_size);

let horizontal_bordering = terminal_width.saturating_sub(text_width) / 2;
let middle_dialog_chunk = tuiLayout::default()
let middle_dialog_chunk = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Length(horizontal_bordering),
Expand All @@ -454,7 +456,7 @@ impl Painter {
self.draw_frozen_indicator(&mut f, frozen_draw_loc);
}

let rect = tuiLayout::default()
let rect = Layout::default()
.margin(0)
.constraints([Constraint::Percentage(100)])
.split(terminal_size);
Expand Down Expand Up @@ -538,7 +540,7 @@ impl Painter {
}
};

let vertical_chunks = tuiLayout::default()
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(0)
.constraints([
Expand All @@ -549,7 +551,7 @@ impl Painter {
])
.split(terminal_size);

let middle_chunks = tuiLayout::default()
let middle_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(vertical_chunks[1]);
Expand Down Expand Up @@ -612,7 +614,7 @@ impl Painter {
}

if self.derived_widget_draw_locs.is_empty() || app_state.is_force_redraw {
let draw_locs = tuiLayout::default()
let draw_locs = Layout::default()
.margin(0)
.constraints(self.row_constraints.as_ref())
.direction(Direction::Vertical)
Expand All @@ -634,7 +636,7 @@ impl Painter {
cols,
)| {
izip!(
tuiLayout::default()
Layout::default()
.constraints(col_constraint.as_ref())
.direction(Direction::Horizontal)
.split(draw_loc)
Expand All @@ -645,7 +647,7 @@ impl Painter {
)
.map(|(split_loc, constraint, col_constraint_vec, col_rows)| {
izip!(
tuiLayout::default()
Layout::default()
.constraints(constraint.as_ref())
.direction(Direction::Vertical)
.split(split_loc)
Expand All @@ -655,7 +657,7 @@ impl Painter {
)
.map(|(draw_loc, col_row_constraint_vec, widgets)| {
// Note that col_row_constraint_vec CONTAINS the widget constraints
let widget_draw_locs = tuiLayout::default()
let widget_draw_locs = Layout::default()
.constraints(col_row_constraint_vec.as_ref())
.direction(Direction::Horizontal)
.split(draw_loc);
Expand Down
Loading

0 comments on commit 0eefac5

Please sign in to comment.