Skip to content

Commit

Permalink
Display help text (#100)
Browse files Browse the repository at this point in the history
* Wrap footer

* Display help text for items

* Changelog

* Don't display empty line
  • Loading branch information
bugadani authored Feb 10, 2025
1 parent 6351109 commit 7e32ee9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- After generating the project the tool now checks the rust version, espflash version and probe-rs version (#88)
- Be more helpful in case of common linker errors (#94)
- Support for `ELIF` conditions (#96)
- Display help text (#100)

### Changed
- Update `probe-rs run` arguments (#90)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ crossterm = "0.28.1"
env_logger = "0.11.6"
esp-metadata = { version = "0.5.0", features = ["clap"] }
log = "0.4.25"
ratatui = { version = "0.29.0", features = ["crossterm"] }
ratatui = { version = "0.29.0", features = ["crossterm", "unstable"] }
rhai = "1.20.1"
taplo = "0.13.2"
update-informer = "1.1.0"
Expand Down
27 changes: 24 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod tui;
pub struct GeneratorOption {
name: &'static str,
display_name: &'static str,
help: &'static str,
enables: &'static [&'static str],
disables: &'static [&'static str],
chips: &'static [Chip],
Expand All @@ -36,6 +37,7 @@ impl GeneratorOption {
pub struct GeneratorOptionCategory {
name: &'static str,
display_name: &'static str,
help: &'static str,
options: &'static [GeneratorOptionItem],
}

Expand Down Expand Up @@ -94,19 +96,28 @@ impl GeneratorOptionItem {
GeneratorOptionItem::Option(option) => option.enables,
}
}

fn help(&self) -> &str {
match self {
GeneratorOptionItem::Category(category) => category.help,
GeneratorOptionItem::Option(option) => option.help,
}
}
}

static OPTIONS: &[GeneratorOptionItem] = &[
GeneratorOptionItem::Option(GeneratorOption {
name: "alloc",
display_name: "Enable allocations via the `esp-alloc` crate.",
help: "",
enables: &[],
disables: &[],
chips: &[],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "wifi",
display_name: "Enable Wi-Fi via the `esp-wifi` crate. Requires `alloc`.",
display_name: "Enable Wi-Fi via the `esp-wifi` crate.",
help: "Requires `alloc`. Not available on ESP32-H2.",
enables: &["alloc"],
disables: &[],
chips: &[
Expand All @@ -120,7 +131,8 @@ static OPTIONS: &[GeneratorOptionItem] = &[
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "ble",
display_name: "Enable BLE via the `esp-wifi` crate. Requires `alloc`.",
display_name: "Enable BLE via the `esp-wifi` crate.",
help: "Requires `alloc`. Not available on ESP32-S2.",
enables: &["alloc"],
disables: &[],
chips: &[
Expand All @@ -135,24 +147,28 @@ static OPTIONS: &[GeneratorOptionItem] = &[
GeneratorOptionItem::Option(GeneratorOption {
name: "embassy",
display_name: "Add `embassy` framework support.",
help: "",
enables: &[],
disables: &[],
chips: &[],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "probe-rs",
display_name: "Enables `defmt` and flashes using `probe-rs` instead of `espflash`.",
display_name: "Enable `defmt` and flashes using `probe-rs` instead of `espflash`.",
help: "",
enables: &[],
disables: &[],
chips: &[],
}),
GeneratorOptionItem::Category(GeneratorOptionCategory {
name: "optional",
display_name: "Options",
help: "",
options: &[
GeneratorOptionItem::Option(GeneratorOption {
name: "wokwi",
display_name: "Add support for Wokwi simulation using VS Code Wokwi extension.",
help: "",
enables: &[],
disables: &[],
chips: &[
Expand All @@ -167,13 +183,15 @@ static OPTIONS: &[GeneratorOptionItem] = &[
GeneratorOptionItem::Option(GeneratorOption {
name: "dev-container",
display_name: "Add support for VS Code Dev Containers and GitHub Codespaces.",
help: "",
enables: &[],
disables: &[],
chips: &[],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "ci",
display_name: "Add GitHub Actions support with some basic checks.",
help: "",
enables: &[],
disables: &[],
chips: &[],
Expand All @@ -183,17 +201,20 @@ static OPTIONS: &[GeneratorOptionItem] = &[
GeneratorOptionItem::Category(GeneratorOptionCategory {
name: "editor",
display_name: "Optional editor config files for rust-analyzer",
help: "",
options: &[
GeneratorOptionItem::Option(GeneratorOption {
name: "helix",
display_name: "Add rust-analyzer settings for Helix Editor",
help: "",
enables: &[],
disables: &[],
chips: &[],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "vscode",
display_name: "Add rust-analyzer settings for Visual Studio Code",
help: "",
enables: &[],
disables: &[],
chips: &[],
Expand Down
70 changes: 57 additions & 13 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::{GeneratorOption, GeneratorOptionItem};

const TODO_HEADER_BG: Color = tailwind::BLUE.c950;
const NORMAL_ROW_COLOR: Color = tailwind::SLATE.c950;
const HELP_ROW_COLOR: Color = tailwind::SLATE.c800;
const SELECTED_STYLE_FG: Color = tailwind::BLUE.c300;
const DISABLED_STYLE_FG: Color = tailwind::GRAY.c600;
const TEXT_COLOR: Color = tailwind::SLATE.c200;
Expand All @@ -35,7 +36,7 @@ impl Repository {
}
}

fn current_level(&self) -> Vec<GeneratorOptionItem> {
fn current_level(&self) -> &[GeneratorOptionItem] {
let mut current = self.options;

for &index in &self.path {
Expand All @@ -45,7 +46,7 @@ impl Repository {
}
}

Vec::from(current)
current
}

fn select(&mut self, index: usize) {
Expand Down Expand Up @@ -274,21 +275,17 @@ impl App {

impl Widget for &mut App {
fn render(self, area: Rect, buf: &mut Buffer) {
// Create a space for header, todo list and the footer.
let vertical = Layout::vertical([
Constraint::Length(2),
Constraint::Fill(1),
Constraint::Length(2),
Constraint::Length(self.help_lines(area)),
Constraint::Length(self.footer_lines(area)),
]);
let [header_area, rest_area, footer_area] = vertical.areas(area);

// Create two chunks with equal vertical screen space. One for the list and the
// other for the info block.
let vertical = Layout::vertical([Constraint::Percentage(100)]);
let [upper_item_list_area] = vertical.areas(rest_area);
let [header_area, rest_area, help_area, footer_area] = vertical.areas(area);

self.render_title(header_area, buf);
self.render_item(upper_item_list_area, buf);
self.render_item(rest_area, buf);
self.render_help(help_area, buf);
self.render_footer(footer_area, buf);
}
}
Expand Down Expand Up @@ -358,13 +355,60 @@ impl App {
}
}

fn render_footer(&self, area: Rect, buf: &mut Buffer) {
fn help_paragraph(&self) -> Option<Paragraph<'_>> {
let selected = self
.selected()
.min(self.repository.current_level().len() - 1);
let option = &self.repository.current_level()[selected];
// Create a space for header, list, help text and the footer.
let help_text = option.help();

if help_text.is_empty() {
return None;
}

let help_block = Block::default()
.borders(Borders::NONE)
.fg(TEXT_COLOR)
.bg(HELP_ROW_COLOR);

Some(
Paragraph::new(help_text)
.centered()
.wrap(Wrap { trim: false })
.block(help_block),
)
}

fn help_lines(&self, area: Rect) -> u16 {
if let Some(paragraph) = self.help_paragraph() {
paragraph.line_count(area.width) as u16
} else {
0
}
}

fn render_help(&self, area: Rect, buf: &mut Buffer) {
if let Some(paragraph) = self.help_paragraph() {
paragraph.render(area, buf);
}
}

fn footer_paragraph(&self) -> Paragraph<'_> {
let text = if self.confirm_quit {
"Are you sure you want to quit? (y/N)"
} else {
"Use ↓↑ to move, ESC/← to go up, → to go deeper or change the value, s/S to save and generate, ESC/q to cancel"
};

Paragraph::new(text).centered().render(area, buf);
Paragraph::new(text).centered().wrap(Wrap { trim: false })
}

fn footer_lines(&self, area: Rect) -> u16 {
self.footer_paragraph().line_count(area.width) as u16
}

fn render_footer(&self, area: Rect, buf: &mut Buffer) {
self.footer_paragraph().render(area, buf);
}
}

0 comments on commit 7e32ee9

Please sign in to comment.