Skip to content

Commit

Permalink
Refactor TUI components to implement Widget trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ifd3f committed Nov 13, 2023
1 parent 109015f commit 7bfa3ab
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 162 deletions.
1 change: 1 addition & 0 deletions src/byteseries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl ByteSeries {
(b1 - b0) / window
}

/// Returns a series of points representing a timeseries, aggregated by the given window size.
pub fn speeds(&self, window: f64) -> impl Iterator<Item = (f64, f64)> + '_ {
let bins = (self.last_datapoint().0 / window).ceil() as usize;
(0..bins).map(move |i| {
Expand Down
20 changes: 13 additions & 7 deletions src/ui/burn/fancy/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

use super::{
state::{Quit, State},
widgets::{make_info_table, make_progress_bar},
widgets::{WritingInfoTable, SpeedChart, WriterProgressBar},
};

pub struct FancyUI<'a, B>
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn draw(
state: &mut State,
terminal: &mut Terminal<impl ratatui::backend::Backend>,
) -> anyhow::Result<()> {
let progress_bar = make_progress_bar(&state.child);
let progress_bar = WriterProgressBar::from_writer(&state.child);

let final_time = match state.child {
WriterState::Finished { finish_time, .. } => finish_time,
Expand All @@ -150,16 +150,22 @@ pub fn draw(
_ => None,
};

let info_table = make_info_table(&state.input_filename, &state.target_filename, &state.child);
let info_table = WritingInfoTable {
input_filename: &state.input_filename,
target_filename: &state.target_filename,
state: &state.child,
};

let speed_chart = SpeedChart {
state: &state.child,
final_time,
};

terminal.draw(|f| {
let layout = ComputedLayout::from(f.size());

f.render_widget(progress_bar.render(), layout.progress);

state
.ui_state
.draw_speed_chart(&state.child, f, layout.graph, final_time);
f.render_stateful_widget(speed_chart, layout.graph, &mut state.graph_state);

if let Some(error) = error {
f.render_widget(
Expand Down
6 changes: 3 additions & 3 deletions src/ui/burn/fancy/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
writer_process::{ipc::StatusMessage, state_tracking::WriterState},
};

use super::widgets::UIState;
use super::widgets::SpeedChartState;

#[derive(Debug, PartialEq, Clone)]
pub enum UIEvent {
Expand All @@ -22,16 +22,16 @@ pub struct State {
pub input_filename: String,
pub target_filename: String,
pub child: WriterState,
pub ui_state: UIState,
pub graph_state: SpeedChartState,
}

impl State {
pub fn initial(now: Instant, params: &BeginParams, input_file_bytes: u64) -> Self {
State {
input_filename: params.input_file.to_string_lossy().to_string(),
target_filename: params.target.devnode.to_string_lossy().to_string(),
ui_state: UIState::default(),
child: WriterState::initial(now, !params.compression.is_identity(), input_file_bytes),
graph_state: SpeedChartState::default(),
}
}

Expand Down
Loading

0 comments on commit 7bfa3ab

Please sign in to comment.