Skip to content

Commit

Permalink
Add mockups of new widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
ifd3f committed Nov 13, 2023
1 parent 6cd58bc commit f8f4fea
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 71 deletions.
63 changes: 51 additions & 12 deletions src/ui/burn/fancy/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use futures::StreamExt;
use ratatui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::Text,
widgets::{Block, Borders, Paragraph, Wrap},
Terminal,
};
use tokio::{select, time};
use tracing::debug;

use crate::{
logging::get_bug_report_msg,
Expand All @@ -18,7 +21,7 @@ use crate::{

use super::{
state::{Quit, State},
widgets::{make_info_table, make_progress_bar},
widgets::{draw_disks_list, make_info_table, DiskListEntry, WriterStateProgressBar},
};

pub struct FancyUI<'a, B>
Expand Down Expand Up @@ -108,28 +111,40 @@ async fn get_event_child_active(
}

struct ComputedLayout {
disks: Rect,
toplevel_status: Rect,
progress: Rect,
graph: Rect,
args_display: Rect,
}

impl From<Rect> for ComputedLayout {
fn from(value: Rect) -> Self {
fn from(root: Rect) -> Self {
let root = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(10), Constraint::Length(1)])
.split(root);

let infopane_and_rightpane = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Min(16), Constraint::Percentage(75)])
.split(root[0]);

let within_rightpane = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1),
Constraint::Min(10),
Constraint::Length(10),
Constraint::Min(7),
Constraint::Length(7),
])
.split(value);

let info_pane = root[2];
.split(infopane_and_rightpane[1]);

Self {
graph: root[1],
progress: root[0],
args_display: info_pane,
toplevel_status: root[1],
disks: infopane_and_rightpane[0],
graph: within_rightpane[1],
progress: within_rightpane[0],
args_display: within_rightpane[2],
}
}
}
Expand All @@ -138,7 +153,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 = WriterStateProgressBar::from_writer_state(&state.child);

let final_time = match state.child {
WriterState::Finished { finish_time, .. } => finish_time,
Expand All @@ -155,7 +170,10 @@ pub fn draw(
terminal.draw(|f| {
let layout = ComputedLayout::from(f.size());

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

state
.ui_state
Expand All @@ -175,6 +193,27 @@ pub fn draw(
} else {
f.render_widget(info_table, layout.args_display);
}

f.render_widget(
Paragraph::new(Text::raw("↑/↓ (select disk) n (new disk)"))
.style(Style::new().bg(Color::LightBlue)),
layout.toplevel_status,
);

debug!(?layout.disks, "test");
let disks_block = Block::default().borders(Borders::RIGHT);
let actual_disks = disks_block.inner(layout.disks);
f.render_widget(disks_block, layout.disks);

draw_disks_list(
f,
actual_disks,
&vec![DiskListEntry {
name: "/dev/whatever",
state: &state.child,
}],
);
})?;

Ok(())
}
158 changes: 99 additions & 59 deletions src/ui/burn/fancy/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use ratatui::{
style::{Color, Style},
symbols,
text::Span,
widgets::{Axis, Block, Borders, Cell, Chart, Dataset, Gauge, GraphType, Row, Table},
widgets::{
Axis, Block, Borders, Cell, Chart, Dataset, Gauge, GraphType, LineGauge, Paragraph, Row,
Table,
},
Frame,
};

Expand All @@ -17,43 +20,6 @@ pub struct UIState {
graph_max_speed: f64,
}

pub fn make_progress_bar(state: &WriterState) -> StateProgressBar {
match state {
WriterState::Writing(st) => StateProgressBar {
bytes_written: st.write_hist.bytes_encountered(),
label_state: "Burning...",
style: Style::default().fg(Color::Yellow),
ratio: st.approximate_ratio(),
display_total_bytes: st.total_raw_bytes,
},
WriterState::Verifying {
verify_hist,
total_write_bytes,
..
} => StateProgressBar::from_simple(
verify_hist.bytes_encountered(),
*total_write_bytes,
"Verifying...",
Style::default().fg(Color::Blue).bg(Color::Yellow),
),
WriterState::Finished {
write_hist,
error,
total_write_bytes,
..
} => StateProgressBar::from_simple(
write_hist.bytes_encountered(),
*total_write_bytes,
if error.is_some() { "Error!" } else { "Done!" },
if error.is_some() {
Style::default().fg(Color::White).bg(Color::Red)
} else {
Style::default().fg(Color::Green).bg(Color::Black)
},
),
}
}

impl UIState {
pub fn draw_speed_chart(
&mut self,
Expand Down Expand Up @@ -146,16 +112,53 @@ impl UIState {
}
}

pub struct StateProgressBar {
pub struct WriterStateProgressBar {
bytes_written: u64,
display_total_bytes: Option<u64>,
ratio: f64,
label_state: &'static str,
style: Style,
}

impl StateProgressBar {
fn from_simple(bytes_written: u64, max: u64, label_state: &'static str, style: Style) -> Self {
impl WriterStateProgressBar {
pub fn from_writer_state(state: &WriterState) -> WriterStateProgressBar {
match state {
WriterState::Writing(st) => WriterStateProgressBar {
bytes_written: st.write_hist.bytes_encountered(),
label_state: "Burning...",
style: Style::default().fg(Color::Yellow),
ratio: st.approximate_ratio(),
display_total_bytes: st.total_raw_bytes,
},
WriterState::Verifying {
verify_hist,
total_write_bytes,
..
} => WriterStateProgressBar::new(
verify_hist.bytes_encountered(),
*total_write_bytes,
"Verifying...",
Style::default().fg(Color::Blue).bg(Color::Yellow),
),
WriterState::Finished {
write_hist,
error,
total_write_bytes,
..
} => WriterStateProgressBar::new(
write_hist.bytes_encountered(),
*total_write_bytes,
if error.is_some() { "Error!" } else { "Done!" },
if error.is_some() {
Style::default().fg(Color::White).bg(Color::Red)
} else {
Style::default().fg(Color::Green).bg(Color::Black)
},
),
}
}

fn new(bytes_written: u64, max: u64, label_state: &'static str, style: Style) -> Self {
Self {
bytes_written,
display_total_bytes: Some(max),
Expand All @@ -165,28 +168,32 @@ impl StateProgressBar {
}
}

pub fn render(&self) -> Gauge {
pub fn label(&self) -> String {
if let Some(max) = self.display_total_bytes {
Gauge::default()
.label(format!(
"{} {} / {}",
self.label_state,
ByteSize::b(self.bytes_written),
ByteSize::b(max)
))
.ratio(self.ratio)
.gauge_style(self.style)
format!(
"{} {} / {}",
self.label_state,
ByteSize::b(self.bytes_written),
ByteSize::b(max)
)
} else {
Gauge::default()
.label(format!(
"{} {} / ???",
self.label_state,
ByteSize::b(self.bytes_written),
))
.ratio(self.ratio)
.gauge_style(self.style)
format!(
"{} {} / ???",
self.label_state,
ByteSize::b(self.bytes_written),
)
}
}

pub fn as_gauge(&self) -> Gauge<'_> {
Gauge::default().ratio(self.ratio).gauge_style(self.style)
}

pub fn as_line_gauge(&self) -> LineGauge<'_> {
LineGauge::default()
.ratio(self.ratio)
.gauge_style(self.style)
}
}

impl Default for UIState {
Expand Down Expand Up @@ -251,3 +258,36 @@ pub fn make_info_table<'a>(
.widths(&[Constraint::Length(16), Constraint::Percentage(100)])
.block(Block::default().title("Stats").borders(Borders::ALL))
}

pub struct DiskListEntry<'a> {
pub name: &'a str,
pub state: &'a WriterState,
}

pub fn draw_disks_list<'a, 'b>(
frame: &mut Frame<'a>,
area: Rect,
entries: &Vec<DiskListEntry<'b>>,
) {
// TODO use list selection state to offset correctly

let selected_index = 0;

for (i, e) in entries.iter().enumerate() {
let pb = WriterStateProgressBar::from_writer_state(e.state);
let status = pb
.as_line_gauge()
.label(e.name) // TODO truncate name to max chars
.line_set(symbols::line::THICK);

let bar_area = Rect::new(area.x + 2, area.y + i as u16, area.width - 2, 1);
frame.render_widget(status, bar_area);

// TODO implement marker selection
if i == selected_index {
let marker = Paragraph::new("> ");
let marker_area = Rect::new(area.x, area.y + i as u16, 2, 1);
frame.render_widget(marker, marker_area);
}
}
}

0 comments on commit f8f4fea

Please sign in to comment.