Skip to content

Commit

Permalink
feat: Ask for quitting confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Nov 21, 2024
1 parent 76592fc commit b895d3a
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub fn restore_terminal() -> AppResult<()> {
pub struct App {
state: Vec<ListState>,
repository: Repository,
confirm_quit: bool,
}

impl App {
Expand All @@ -172,6 +173,7 @@ impl App {
Self {
repository,
state: vec![initial_state],
confirm_quit: false,
}
}
pub fn selected(&self) -> usize {
Expand Down Expand Up @@ -213,9 +215,25 @@ impl App {
if key.kind == KeyEventKind::Press {
use KeyCode::*;

if self.confirm_quit {
match key.code {
Char('y') | Char('Y') => return Ok(None),
_ => self.confirm_quit = false,
}
continue;
}

match key.code {
Char('q') | Esc => return Ok(None),
Char('s') => return Ok(Some(self.repository.selected.clone())),
Char('q') => self.confirm_quit = true,
Char('s') | Char('S') => return Ok(Some(self.repository.selected.clone())),
Esc => {
if self.state.len() == 1 {
self.confirm_quit = true;
} else {
self.repository.up();
self.exit_menu();
}
}
Char('h') | Left => {
self.repository.up();
self.exit_menu();
Expand Down Expand Up @@ -338,9 +356,12 @@ impl App {
}

fn render_footer(&self, area: Rect, buf: &mut Buffer) {
Paragraph::new(
"\nUse ↓↑ to move, ← to go up, → to go deeper or change the value, s/S to save and generate, ESC/q to cancel"
).centered()
.render(area, buf);
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);
}
}

0 comments on commit b895d3a

Please sign in to comment.