Skip to content

Commit

Permalink
Simplify keys rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Dec 22, 2024
1 parent b9f21f1 commit eddaea0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "0.8"
csv = "1.3"
bitflags = "2"
env_logger = { version = "0.11", optional = true }
directories = { version = "3", optional = true }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "charset"] }
Expand Down
19 changes: 12 additions & 7 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub enum State {
impl State {
/// Create a new state in the `Processing` state
pub const fn new() -> Self {
Self::Processing(Processing::new())
Self::Processing(Processing::empty())
}

pub fn success(&mut self, title: impl Into<String>, message: impl Into<String>) {
Expand Down Expand Up @@ -219,25 +219,30 @@ pub struct Provision {
/// The state of the model when processing a sub-task
#[derive(Debug)]
pub struct Processing {
/// The title of the processing (e.g. "Preparing bundle", etc.)
pub title: String,
/// The status of the processing (e.g. "Loading", etc.)
pub status: String,
/// A counter helper for displaying a processing progress
pub counter: Wrapping<usize>,
}

impl Processing {
/// Create a new `Preparing` state with empty status
pub const fn new() -> Self {
const fn empty() -> Self {
Self {
title: String::new(),
status: String::new(),
counter: Wrapping(0),
}
}
}

impl Default for Processing {
fn default() -> Self {
Self::new()
/// Create a new `Preparing` state with empty status
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
status: String::new(),
counter: Wrapping(0),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ where
input: &Input<'_>,
) -> anyhow::Result<(), TaskError> {
self.model
.modify(|state| *state = State::Processing(Processing::new()));
.modify(|state| *state = State::Processing(Processing::new(" Read eFuse IDs ")));

Self::process(&self.model.clone(), self.prep_efuse_readouts(), input).await
}
Expand Down Expand Up @@ -355,7 +355,7 @@ where
});

self.model
.modify(|state| *state = State::Processing(Processing::new()));
.modify(|state| *state = State::Processing(Processing::new(" Preparing bundle ")));

let bundle_id = match self.conf.bundle_identification {
BundleIdentification::None => None,
Expand Down
93 changes: 65 additions & 28 deletions src/view.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::cmp::Ordering;

use bitflags::bitflags;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Direction, Layout, Margin, Rect};
use ratatui::style::Stylize;
Expand Down Expand Up @@ -55,12 +56,7 @@ impl Widget for &Readout {
fn render(self, area: Rect, buf: &mut Buffer) {
let area = render_main(
Some(" Readouts ".bold()),
Some(Line::from(vec![
"Readout ".into(),
"<chars> + <Enter> ".yellow().bold(),
"Reset ".into(),
"<Esc> ".yellow().bold(),
])),
Keys::INPUT | Keys::RESET | Keys::QUIT,
area,
buf,
);
Expand Down Expand Up @@ -188,14 +184,7 @@ impl Widget for &Provision {
self.bundle.name.as_str().bold(),
" ".into(),
])),
(!self.provisioning).then(|| {
Line::from(vec![
" Provision ".into(),
"<Enter> ".yellow().bold(),
"Quit ".into(),
"<Esc> ".yellow().bold(),
])
}),
Keys::CONFIRM | Keys::BACK | Keys::QUIT,
area,
buf,
);
Expand Down Expand Up @@ -312,8 +301,8 @@ impl Widget for &Provision {
impl Widget for &Processing {
fn render(self, area: Rect, buf: &mut Buffer) {
let area = render_main(
Some(Span::from(" Bundle Preparation ").bold()),
Some(Line::from(vec![" Quit ".into(), "<Esc> ".yellow().bold()])),
Some(self.title.clone().bold()),
Keys::BACK | Keys::QUIT,
area,
buf,
);
Expand Down Expand Up @@ -341,16 +330,11 @@ impl Widget for &Status {
fn render(self, area: Rect, buf: &mut Buffer) {
let area = render_main(
Some(self.title.clone().bold()),
Some(if self.error {
Line::from(vec![
" Re-try ".into(),
"<Enter> ".yellow().bold(),
"Quit ".into(),
"<Esc> ".yellow().bold(),
])
if self.error {
Keys::RETRY | Keys::BACK | Keys::QUIT
} else {
Line::from(vec![" Continue ".into(), "<Enter> ".yellow().bold()])
}),
Keys::CONFIRM | Keys::QUIT
},
area,
buf,
);
Expand All @@ -367,7 +351,7 @@ impl Widget for &Status {

fn render_main<'a>(
title: Option<impl Into<Line<'a>>>,
instructions: Option<impl Into<Line<'a>>>,
keys: Keys,
area: Rect,
buf: &mut Buffer,
) -> Rect {
Expand All @@ -384,8 +368,8 @@ fn render_main<'a>(
block = block.title_top(title.into().bold().centered().green());
}

if let Some(instructions) = instructions {
block = block.title_bottom(instructions.into().right_aligned().yellow());
if let Some(instructions) = keys.instructions() {
block = block.title_bottom(instructions.right_aligned().yellow());
}

block.on_blue().white().render(layout[0], buf);
Expand Down Expand Up @@ -416,3 +400,56 @@ fn render_main<'a>(

layout[0]
}

bitflags! {
struct Keys: u8 {
const QUIT = 0b00000;
const RETRY = 0b00001;
const CONFIRM = 0b00010;
const BACK = 0b00100;
const RESET = 0b01000;
const INPUT = 0b10000;
}
}

impl Keys {
fn instructions(&self) -> Option<Line<'static>> {
(!self.is_empty()).then(|| {
let mut instructions = Vec::new();

if self.contains(Self::INPUT) {
instructions.push(" Readout ".into());
instructions.push("<chars> + <Enter>".yellow().bold());
}

if self.contains(Self::CONFIRM) {
instructions.push(" Continue ".into());
instructions.push("<Enter>".yellow().bold());
}

if self.contains(Self::RETRY) {
instructions.push(" Re-try ".into());
instructions.push("<Enter>".yellow().bold());
}

if self.contains(Self::BACK) {
instructions.push(" Back ".into());
instructions.push("<Esc>".yellow().bold());
}

if self.contains(Self::RESET) {
instructions.push(" Reset ".into());
instructions.push("<Esc>".yellow().bold());
}

if self.contains(Self::QUIT) {
instructions.push(" Quit ".into());
instructions.push("<Alt-Q>".yellow().bold());
}

instructions.push(" ".into());

Line::from(instructions)
})
}
}

0 comments on commit eddaea0

Please sign in to comment.