Skip to content

Commit df246e7

Browse files
committed
feat: add help view
1 parent a1c1741 commit df246e7

File tree

6 files changed

+100
-27
lines changed

6 files changed

+100
-27
lines changed

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod common;
2+
mod message;
23
mod model;
34
mod update;
45
mod utils;
@@ -7,7 +8,7 @@ mod view;
78
use anyhow::Context;
89
use clap::Parser;
910
use common::UNEXPECTED_ERROR_MESSAGE;
10-
use model::{Lines, Model, RunningState, UserMessage};
11+
use model::{Lines, Model, RunningState, UserMessage, View};
1112
use std::fs::File;
1213
use update::{handle_event, update};
1314
use utils::read_from_file;
@@ -42,6 +43,7 @@ fn main() -> anyhow::Result<()> {
4243
terminal.clear().context(UNEXPECTED_ERROR_MESSAGE)?;
4344

4445
let mut model = Model {
46+
view: View::List,
4547
running_state: RunningState::Running,
4648
file_path: args.path,
4749
lines: Lines::from(&lines),

src/message.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::model::View;
2+
3+
#[derive(PartialEq)]
4+
pub(crate) enum Message {
5+
MoveToIndex(usize),
6+
GoToNextItem,
7+
GoToPreviousPreview,
8+
GoToFirstItem,
9+
GoToLastItem,
10+
SwitchWithNextItem,
11+
SwitchWithPreviousItem,
12+
SwitchWithFirstItem,
13+
ToggleSelection,
14+
SaveSelection,
15+
ShowView(View),
16+
Quit,
17+
}

src/model.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ratatui::{
66

77
#[derive(Debug)]
88
pub(crate) struct Model {
9+
pub(crate) view: View,
910
pub(crate) running_state: RunningState,
1011
pub(crate) file_path: String,
1112
pub(crate) lines: Lines,
@@ -38,27 +39,18 @@ pub(crate) enum RunningState {
3839
Done,
3940
}
4041

41-
#[derive(PartialEq)]
42-
pub(crate) enum Message {
43-
MoveToIndex(usize),
44-
GoToNextItem,
45-
GoToPreviousPreview,
46-
GoToFirstItem,
47-
GoToLastItem,
48-
SwitchWithNextItem,
49-
SwitchWithPreviousItem,
50-
SwitchWithFirstItem,
51-
ToggleSelection,
52-
SaveSelection,
53-
Quit,
54-
}
55-
5642
#[derive(Debug)]
5743
pub(crate) enum UserMessage {
5844
Success(String),
5945
Error(String),
6046
}
6147

48+
#[derive(PartialEq, Debug)]
49+
pub(crate) enum View {
50+
List,
51+
Help,
52+
}
53+
6254
impl UserMessage {
6355
pub(crate) fn value(&self) -> String {
6456
match self {
@@ -196,10 +188,22 @@ impl Model {
196188
}
197189
}
198190

191+
pub(crate) fn show_view(&mut self, view: View) {
192+
self.view = match self.view {
193+
View::Help => View::List,
194+
_ => view,
195+
}
196+
}
197+
199198
pub(crate) fn go_back_or_quit(&mut self) {
200-
if self.save_on_exit {
201-
self.save_selection();
199+
match self.view {
200+
View::List => {
201+
if self.save_on_exit {
202+
self.save_selection();
203+
}
204+
self.running_state = RunningState::Done;
205+
}
206+
View::Help => self.view = View::List,
202207
}
203-
self.running_state = RunningState::Done;
204208
}
205209
}

src/static/help.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Help
2+
===
3+
4+
Keymaps
5+
---
6+
7+
J move item one position below
8+
K move item one position above
9+
Enter move item to the start of the list
10+
j / Down go down
11+
k | Up go up
12+
[1-9] move current item to index in list
13+
g go to the start of the list
14+
G go to the end of the list
15+
w write to file
16+
? show/hide help view
17+
Esc / q go back/exit

src/update.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::model::{Message, Model};
1+
use crate::message::Message;
2+
use crate::model::{Model, View};
23
use ratatui::crossterm::event::{self, Event, KeyCode};
34
use std::time::Duration;
45

@@ -15,6 +16,7 @@ pub(crate) fn update(model: &mut Model, msg: Message) -> Option<Message> {
1516
Message::SwitchWithFirstItem => model.switch_with_first(),
1617
Message::ToggleSelection => model.toggle_current(),
1718
Message::SaveSelection => model.save_selection(),
19+
Message::ShowView(v) => model.show_view(v),
1820
Message::Quit => model.go_back_or_quit(),
1921
};
2022
None
@@ -51,6 +53,7 @@ pub(crate) fn handle_key(key: event::KeyEvent) -> Option<Message> {
5153
KeyCode::Enter => Some(Message::SwitchWithFirstItem),
5254
KeyCode::Char('s') | KeyCode::Char(' ') => Some(Message::ToggleSelection),
5355
KeyCode::Esc | KeyCode::Char('q') => Some(Message::Quit),
56+
KeyCode::Char('?') => Some(Message::ShowView(View::Help)),
5457
KeyCode::Char('w') => Some(Message::SaveSelection),
5558
_ => None,
5659
}

src/view.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
use crate::common::{PRIMARY_COLOR, TITLE, TITLE_FG_COLOR};
22
use crate::model::Model;
3+
use crate::model::View;
34
use ratatui::{
5+
layout::Alignment,
46
style::{Style, Stylize},
5-
widgets::{Block, List, ListDirection, ListItem, Padding},
7+
text::Line,
8+
widgets::{Block, List, ListDirection, ListItem, Padding, Paragraph},
69
Frame,
710
};
811

12+
const HELP_CONTENTS: &str = include_str!("static/help.txt");
13+
914
pub(crate) fn view(model: &mut Model, frame: &mut Frame) {
15+
match model.view {
16+
View::List => render_list_view(model, frame),
17+
View::Help => render_help_view(frame),
18+
}
19+
}
20+
21+
fn render_list_view(model: &mut Model, frame: &mut Frame) {
1022
let items: Vec<ListItem> = model.lines.items.iter().map(ListItem::from).collect();
1123

1224
let title = model
@@ -20,13 +32,13 @@ pub(crate) fn view(model: &mut Model, frame: &mut Frame) {
2032
Some(_) => base_title_style,
2133
None => base_title_style.bg(PRIMARY_COLOR).fg(TITLE_FG_COLOR),
2234
};
35+
36+
let block = Block::default()
37+
.title_bottom(title)
38+
.title_style(title_style);
39+
2340
let list = List::new(items)
24-
.block(
25-
Block::default()
26-
.title_bottom(title)
27-
.padding(Padding::bottom(1))
28-
.title_style(title_style),
29-
)
41+
.block(block)
3042
.style(Style::new().white())
3143
.repeat_highlight_symbol(true)
3244
.highlight_symbol(">> ")
@@ -35,3 +47,21 @@ pub(crate) fn view(model: &mut Model, frame: &mut Frame) {
3547

3648
frame.render_stateful_widget(list, frame.area(), &mut model.lines.state)
3749
}
50+
51+
fn render_help_view(frame: &mut Frame) {
52+
let title_style = Style::new().bold().bg(PRIMARY_COLOR).fg(TITLE_FG_COLOR);
53+
54+
let block = Block::default()
55+
.title_bottom(TITLE)
56+
.padding(Padding::left(1))
57+
.title_style(title_style);
58+
59+
let lines: Vec<Line<'_>> = HELP_CONTENTS.lines().map(Line::from).collect();
60+
61+
let p = Paragraph::new(lines)
62+
.block(block)
63+
.style(Style::new().white())
64+
.alignment(Alignment::Left);
65+
66+
frame.render_widget(p, frame.area())
67+
}

0 commit comments

Comments
 (0)