Skip to content

Commit fb09705

Browse files
committed
feat: allow moving multiple items to top
1 parent 456b02e commit fb09705

File tree

7 files changed

+261
-32
lines changed

7 files changed

+261
-32
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ jobs:
2424
uses: dtolnay/rust-toolchain@stable
2525
- name: Build
2626
run: cargo build
27+
- name: Test
28+
run: cargo test

src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ratatui::style::Color;
22

33
pub const TITLE_FG_COLOR: Color = Color::from_u32(0x282828);
44
pub const PRIMARY_COLOR: Color = Color::from_u32(0xd3869b);
5+
pub const SELECTED_COLOR: Color = Color::from_u32(0x83a598);
56
pub const TITLE: &str = " shfl ";
67
pub const UNEXPECTED_ERROR_MESSAGE: &str =
78
"something unexpected happened, please let @dhth know via https://github.com/dhth/shfl/issues";

src/main.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ mod view;
77

88
use anyhow::Context;
99
use clap::Parser;
10-
use common::{View, UNEXPECTED_ERROR_MESSAGE};
11-
use model::{Lines, Model, RunningState, UserMessage};
10+
use common::UNEXPECTED_ERROR_MESSAGE;
11+
use model::{Model, RunningState, UserMessage};
1212
use std::fs::File;
1313
use update::{handle_event, update};
1414
use utils::read_from_file;
@@ -42,14 +42,7 @@ fn main() -> anyhow::Result<()> {
4242
let mut terminal = ratatui::init();
4343
terminal.clear().context(UNEXPECTED_ERROR_MESSAGE)?;
4444

45-
let mut model = Model {
46-
view: View::List,
47-
running_state: RunningState::Running,
48-
file_path: args.path,
49-
lines: Lines::from(&lines),
50-
message: None,
51-
save_on_exit: args.save_on_exit,
52-
};
45+
let mut model = Model::default(args.path, &lines, args.save_on_exit);
5346

5447
while model.running_state != RunningState::Done {
5548
terminal

src/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) enum Message {
99
GoToLastItem,
1010
SwitchWithNextItem,
1111
SwitchWithPreviousItem,
12-
SwitchWithFirstItem,
12+
MoveToTop,
1313
ToggleSelection,
1414
SaveSelection,
1515
ShowView(View),

src/model.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::common::{View, PRIMARY_COLOR};
1+
use crate::common::{View, SELECTED_COLOR};
22
use ratatui::{
3+
style::Style,
34
text::Line,
45
widgets::{ListItem, ListState},
56
};
@@ -10,23 +11,38 @@ pub(crate) struct Model {
1011
pub(crate) running_state: RunningState,
1112
pub(crate) file_path: String,
1213
pub(crate) lines: Lines,
14+
pub(crate) selected_count: usize,
1315
pub(crate) message: Option<UserMessage>,
1416
pub(crate) save_on_exit: bool,
1517
}
1618

19+
impl Model {
20+
pub(crate) fn default(file_path: String, lines: &Vec<String>, save_on_exit: bool) -> Self {
21+
Self {
22+
view: View::List,
23+
running_state: RunningState::Running,
24+
file_path,
25+
lines: Lines::from(lines),
26+
selected_count: 0,
27+
message: None,
28+
save_on_exit,
29+
}
30+
}
31+
}
32+
1733
#[derive(Debug)]
1834
pub(crate) struct Lines {
1935
pub(crate) items: Vec<LineItem>,
2036
pub(crate) state: ListState,
2137
}
2238

23-
#[derive(Debug)]
39+
#[derive(Debug, Clone)]
2440
pub(crate) struct LineItem {
2541
pub(crate) content: String,
2642
pub(crate) status: Selected,
2743
}
2844

29-
#[derive(Debug)]
45+
#[derive(Debug, Clone, PartialEq)]
3046
pub(crate) enum Selected {
3147
Yes,
3248
No,
@@ -74,10 +90,16 @@ impl LineItem {
7490
}
7591
}
7692

77-
pub(crate) fn toggle(&mut self) {
93+
pub(crate) fn toggle(&mut self) -> bool {
7894
match self.status {
79-
Selected::Yes => self.status = Selected::No,
80-
Selected::No => self.status = Selected::Yes,
95+
Selected::Yes => {
96+
self.status = Selected::No;
97+
false
98+
}
99+
Selected::No => {
100+
self.status = Selected::Yes;
101+
true
102+
}
81103
}
82104
}
83105
}
@@ -86,7 +108,10 @@ impl From<&LineItem> for ListItem<'_> {
86108
fn from(value: &LineItem) -> Self {
87109
let line = match value.status {
88110
Selected::No => Line::from(value.content.clone()),
89-
Selected::Yes => Line::styled(value.content.clone(), PRIMARY_COLOR),
111+
Selected::Yes => Line::styled(
112+
format!("> {}", value.content.clone()),
113+
Style::new().fg(SELECTED_COLOR),
114+
),
90115
};
91116
ListItem::new(line)
92117
}

src/static/help.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ Keymaps
66

77
J move item one position below
88
K move item one position above
9-
Enter move item to the start of the list
9+
Enter move item/selection to the start of the list
1010
j / Down go down
11-
k | Up go up
11+
k / Up go up
1212
[1-9] move current item to index in list
1313
g go to the start of the list
1414
G go to the end of the list
1515
w write to file
16+
space / s select/unselect item
1617
? show/hide help view
17-
Esc / q go back/exit
18+
Esc / q go back/reset selection/exit

0 commit comments

Comments
 (0)