Skip to content

Commit

Permalink
Outfile selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ifd3f committed Nov 13, 2023
1 parent 3faf253 commit 475842e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/ui/burn/fancy/ask_outfile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use inquire::Select;
use ratatui::{
prelude::{Buffer, Constraint, Layout, Rect},
widgets::{Block, Borders, Cell, Paragraph, Row, Table, Widget},
};
use tracing::debug;

use crate::{
device::{enumerate_devices, Removable, WriteTarget},
ui::cli::BurnArgs,
};

pub struct OutfileSelector {
targets: Vec<WriteTarget>,
show_removables_only: bool,
}

impl OutfileSelector {
fn make_table(&self) -> Table<'_> {
let header = Row::new(["Disk", "Model", "Size", "Removable", "Type"]);

let rows = self.displayed_devices().map(|t| {
Row::new([
Cell::from(t.name),
Cell::from(t.model.to_string()),
Cell::from(t.size.to_string()),
Cell::from(match t.removable {
Removable::Yes => "✅️",
Removable::No => "❌",
Removable::Unknown => "?",
}),
Cell::from(t.target_type.to_string()),
])
});

Table::new([header].into_iter().chain(rows))
}

fn help_widget(&self) -> Paragraph<'_> {
let removables_only_section = if self.show_removables_only {
"show all devices: [h]"
} else {
"only show removable: [h]"
};

Paragraph::new(format!("refresh: [r] | {removables_only_section}"))
}

pub fn displayed_devices(&self) -> impl Iterator<Item = &WriteTarget> {
self.targets
.iter()
.filter(|d| !self.show_removables_only || d.removable == Removable::Yes)
}
}

impl Widget for OutfileSelector {
fn render(self, area: Rect, buf: &mut Buffer) {
let block = Block::new().borders(Borders::all()).title("Pick a target");
let inner = block.inner(area);
let layout = Layout::default()
.constraints([Constraint::Percentage(100), Constraint::Length(1)])
.split(area);
let table_area = layout[0];
let help_area = layout[1];

block.render(area, buf);
self.make_table().render(table_area, buf);
self.help_widget().render(help_area, buf);
}
}

#[tracing::instrument(skip_all)]
pub fn ask_outfile(args: &BurnArgs) -> anyhow::Result<WriteTarget> {
let mut show_all_disks = args.show_all_disks;

loop {
debug!(show_all_disks, "Beginning loop");

let targets = enumerate_options(show_all_disks)?;

let ans = Select::new("Select target disk", targets)
.with_help_message(if show_all_disks {
"Showing all disks. Proceed with caution!"
} else {
"Only displaying removable disks."
})
.prompt()?;

let dev = match ans {
ListOption::Device(dev) => dev,
ListOption::RetryWithShowAll(sa) => {
show_all_disks = sa;
continue;
}
ListOption::Refresh => {
continue;
}
};
return Ok(dev);
}
}

#[tracing::instrument]
fn enumerate_options(show_all_disks: bool) -> anyhow::Result<Vec<ListOption>> {
let mut burn_targets: Vec<WriteTarget> = enumerate_devices()
.filter(|d| show_all_disks || d.removable == Removable::Yes)
.collect();

burn_targets.sort();

let options = burn_targets.into_iter().map(ListOption::Device).chain([
ListOption::Refresh,
ListOption::RetryWithShowAll(!show_all_disks),
]);

Ok(options.collect())
}
1 change: 1 addition & 0 deletions src/ui/burn/fancy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod display;
mod state;
mod widgets;
mod ask_outfile;

pub use display::FancyUI;

0 comments on commit 475842e

Please sign in to comment.