Skip to content

Commit

Permalink
Add more scroll options
Browse files Browse the repository at this point in the history
  • Loading branch information
henriklovhaug committed Mar 10, 2024
1 parent 70936a5 commit bfec9c3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "AGPL-3.0-or-later"
repository = "https://github.com/henriklovhaug/md-tui"
homepage = "https://github.com/henriklovhaug/md-tui"
keywords = ["markdown", "terminal", "viewer", "tui"]
categories = ["command-line-interface", "command-line-utilities"]

[[bin]]
name = "mdt"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Using AUR: `yay -S md-tui-bin`
| ---------- | ---------------------------------------------------------------------- |
| `j` | Scroll down |
| `k` | Scroll up |
| `l` | Scroll one page down |
| `h` | Scroll one page up |
| `r` | Reload file |
| `f` or `/` | Search |
| `s` | Enter select link mode |
Expand Down
30 changes: 30 additions & 0 deletions src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,27 @@ pub fn keyboard_mode_file_tree(
KeyCode::Char('j') => {
file_tree.next(height);
}

KeyCode::Char('k') => {
file_tree.previous(height);
}

KeyCode::Char('l') => {
file_tree.next_page(height);
}

KeyCode::Char('h') => {
file_tree.previous_page(height);
}

KeyCode::Char('g') => {
file_tree.first();
}

KeyCode::Char('G') => {
file_tree.last(height);
}

KeyCode::Enter => {
let file = if let Some(file) = file_tree.selected() {
file
Expand Down Expand Up @@ -258,6 +276,18 @@ fn keyboard_mode_view(
KeyCode::Char('u') => {
app.vertical_scroll = app.vertical_scroll.saturating_sub(height / 2);
}

KeyCode::Char('l') => {
app.vertical_scroll = cmp::min(
app.vertical_scroll + height,
markdown.height().saturating_sub(height / 2),
);
}

KeyCode::Char('h') => {
app.vertical_scroll = app.vertical_scroll.saturating_sub(height);
}

KeyCode::Char('s') => {
app.vertical_scroll = if let Ok(scroll) = markdown.select(app.select_index) {
app.selected = true;
Expand Down
44 changes: 44 additions & 0 deletions src/pages/file_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,50 @@ impl FileTree {
self.list_state.select(Some(i));
}

pub fn next_page(&mut self, height: u16) {
let partition = self.partition(height);
let i = match self.list_state.selected() {
Some(i) => {
if i + partition >= self.files.len() {
0
} else {
i + partition
}
}
None => 0,
};
self.page = (i / partition) as u32;
self.list_state.select(Some(i));
}

pub fn previous_page(&mut self, height: u16) {
let partition = self.partition(height);
let i = match self.list_state.selected() {
Some(i) => {
if i < partition {
self.files.len() - partition
} else {
i - partition
}
}
None => 0,
};
self.page = (i / partition) as u32;
self.list_state.select(Some(i));
}

pub fn first(&mut self) {
self.list_state.select(Some(0));
self.page = 0;
}

pub fn last(&mut self, height: u16) {
let partition = self.partition(height);
let i = self.files.len() - 2;
self.list_state.select(Some(i));
self.page = (i / partition) as u32;
}

pub fn unselect(&mut self) {
self.list_state.select(None);
}
Expand Down

0 comments on commit bfec9c3

Please sign in to comment.