feat: ui: Show help when requested.

fixes: #6
This commit is contained in:
Jeremy Wall 2024-11-19 16:33:57 -05:00
parent 269933cae0
commit 54d026773a

View File

@ -59,6 +59,7 @@ pub struct Workspace<'ws> {
state: AppState, state: AppState,
text_area: TextArea<'ws>, text_area: TextArea<'ws>,
dirty: bool, dirty: bool,
show_help: bool,
} }
impl<'ws> Workspace<'ws> { impl<'ws> Workspace<'ws> {
@ -69,6 +70,7 @@ impl<'ws> Workspace<'ws> {
state: AppState::default(), state: AppState::default(),
text_area: reset_text_area("".to_owned()), text_area: reset_text_area("".to_owned()),
dirty: false, dirty: false,
show_help: false,
}; };
ws.handle_movement_change(); ws.handle_movement_change();
ws ws
@ -155,10 +157,15 @@ impl<'ws> Workspace<'ws> {
fn handle_edit_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> { fn handle_edit_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> {
if key.kind == KeyEventKind::Press { if key.kind == KeyEventKind::Press {
if let KeyCode::Esc = key.code { match key.code {
self.exit_edit_mode()?; KeyCode::Char('h') if key.modifiers == KeyModifiers::CONTROL => {
} else if let KeyCode::Enter = key.code { self.show_help = !self.show_help;
self.exit_edit_mode()?; }
KeyCode::Esc => self.exit_edit_mode()?,
KeyCode::Enter => self.exit_edit_mode()?,
_ => {
// NOOP
}
} }
} }
// TODO(zaphar): Some specialized editing keybinds // TODO(zaphar): Some specialized editing keybinds
@ -195,6 +202,9 @@ impl<'ws> Workspace<'ws> {
self.text_area.move_cursor(CursorMove::Bottom); self.text_area.move_cursor(CursorMove::Bottom);
self.text_area.move_cursor(CursorMove::End); self.text_area.move_cursor(CursorMove::End);
} }
KeyCode::Char('h') if key.modifiers == KeyModifiers::CONTROL => {
self.show_help = !self.show_help;
}
KeyCode::Char('s') if key.modifiers == KeyModifiers::CONTROL => { KeyCode::Char('s') if key.modifiers == KeyModifiers::CONTROL => {
self.save_file()?; self.save_file()?;
} }
@ -290,15 +300,33 @@ impl<'widget, 'ws: 'widget> Widget for &'widget mut Workspace<'ws> {
)) ))
.right_aligned(), .right_aligned(),
); );
let [edit_rect, table_rect, info_rect] = Layout::vertical(&[ let [edit_rect, table_rect] = if self.show_help {
Constraint::Fill(4), let [edit_rect, table_rect, info_rect] = Layout::vertical(&[
Constraint::Fill(30), Constraint::Fill(4),
Constraint::Fill(9), Constraint::Fill(30),
]) Constraint::Fill(9),
.vertical_margin(2) ])
.horizontal_margin(2) .vertical_margin(2)
.flex(Flex::Legacy) .horizontal_margin(2)
.areas(area.clone()); .flex(Flex::Legacy)
.areas(area.clone());
// Help panel widget display
let info_para = self.render_help_text();
info_para.render(info_rect, buf);
[edit_rect, table_rect]
} else {
let [edit_rect, table_rect] = Layout::vertical(&[
Constraint::Fill(4),
Constraint::Fill(30),
])
.vertical_margin(2)
.horizontal_margin(2)
.flex(Flex::Legacy)
.areas(area.clone());
[edit_rect, table_rect]
};
outer_block.render(area, buf); outer_block.render(area, buf);
// Input widget display // Input widget display
@ -316,9 +344,6 @@ impl<'widget, 'ws: 'widget> Widget for &'widget mut Workspace<'ws> {
use ratatui::widgets::StatefulWidget; use ratatui::widgets::StatefulWidget;
StatefulWidget::render(table, table_rect, buf, &mut self.state.table_state); StatefulWidget::render(table, table_rect, buf, &mut self.state.table_state);
// Help panel widget display
let info_para = self.render_help_text();
info_para.render(info_rect, buf);
} }
} }