Skip to content

Commit 8aab4fa

Browse files
committed
✨ can now hide/display help block
1 parent 7641679 commit 8aab4fa

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/app.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct App {
2020
pub should_spawn_ssh: bool,
2121
pub config_paragraph_offset: u16,
2222
pub db: FileDatabase,
23+
pub show_help: bool,
2324
}
2425

2526
impl App {
@@ -36,6 +37,7 @@ impl App {
3637
should_spawn_ssh: false,
3738
config_display_mode: ConfigDisplayMode::Selected,
3839
db,
40+
show_help: false,
3941
})
4042
}
4143

src/input_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub fn handle_inputs(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
4949
ConfigDisplayMode::Selected => ConfigDisplayMode::Global,
5050
};
5151
}
52+
KeyCode::Char('h') => {
53+
app.show_help = !app.show_help;
54+
}
5255
KeyCode::Char('q') => app.should_quit = true,
5356
KeyCode::Enter => app.should_spawn_ssh = true,
5457
_ => {}

src/main.rs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use std::process::Command;
22

3-
use tui::layout::{Constraint, Direction, Layout};
3+
use tui::{
4+
layout::{Constraint, Direction, Layout},
5+
style::{Color, Style},
6+
text::Spans,
7+
widgets::{Block, Borders, Paragraph},
8+
};
49

510
mod app;
611
mod database;
@@ -43,26 +48,66 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4348
.constraints([Constraint::Length(3), Constraint::Percentage(90)].as_ref())
4449
.split(frame.size());
4550

46-
let chunk_b = Layout::default()
51+
let chunk_t = Layout::default()
4752
.direction(Direction::Horizontal)
48-
.margin(1)
49-
.horizontal_margin(0)
53+
.margin(0)
5054
.constraints(
5155
[
56+
Constraint::Percentage(80),
57+
Constraint::Length(2),
58+
Constraint::Length(10),
59+
]
60+
.as_ref(),
61+
)
62+
.split(chunks[0]);
63+
64+
let constraints = match app.show_help {
65+
false => {
66+
vec![
67+
Constraint::Percentage(50),
68+
Constraint::Length(2),
69+
Constraint::Percentage(50),
70+
]
71+
}
72+
true => {
73+
vec![
5274
Constraint::Percentage(40),
5375
Constraint::Length(2),
5476
Constraint::Percentage(30),
5577
Constraint::Length(2),
5678
Constraint::Percentage(30),
5779
]
58-
.as_ref(),
59-
)
80+
}
81+
};
82+
83+
let chunk_b = Layout::default()
84+
.direction(Direction::Horizontal)
85+
.margin(1)
86+
.horizontal_margin(0)
87+
.constraints(constraints.as_ref())
6088
.split(chunks[1]);
6189

62-
render_group_tabs(&app, chunks[0], frame);
90+
let block = Block::default()
91+
.borders(Borders::ALL)
92+
.border_type(tui::widgets::BorderType::Rounded)
93+
.border_style(Style::default().fg(Color::LightMagenta))
94+
.title_alignment(tui::layout::Alignment::Center);
95+
96+
let help_span = Spans::from("'h' Show help");
97+
frame.render_widget(
98+
Paragraph::new(help_span)
99+
.block(block)
100+
.alignment(tui::layout::Alignment::Center),
101+
chunk_t[2],
102+
);
103+
104+
render_group_tabs(&app, chunk_t[0], frame);
63105
render_host_table(&mut app, chunk_b[0], frame);
64106
render_config(&mut app, chunk_b[2], frame);
65-
render_shortcuts(&app, chunk_b[4], frame);
107+
108+
if app.show_help {
109+
render_shortcuts(&app, chunk_b[4], frame);
110+
}
66111
})?;
67112

68113
handle_inputs(&mut app)?;

src/render_shortcuts.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ pub fn render_shortcuts(_app: &App, area: Rect, frame: &mut Frame<CrosstermBacke
1515
.border_type(tui::widgets::BorderType::Rounded)
1616
.border_style(Style::default().fg(Color::LightMagenta))
1717
.title_alignment(tui::layout::Alignment::Center)
18-
.title(Span::styled(
19-
" Shortcuts ",
20-
Style::default().fg(Color::White),
21-
));
18+
.title(Span::styled(" Help ", Style::default().fg(Color::White)));
2219

2320
let text = vec![
2421
Spans::from("'Enter': Validate"),
@@ -34,5 +31,5 @@ pub fn render_shortcuts(_app: &App, area: Rect, frame: &mut Frame<CrosstermBacke
3431
.block(block)
3532
.wrap(Wrap { trim: true });
3633

37-
frame.render_widget(paragraph, area)
34+
frame.render_widget(paragraph, area);
3835
}

0 commit comments

Comments
 (0)