Skip to content

Commit

Permalink
Rename modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avarel committed Dec 25, 2023
1 parent 6fe8ef6 commit 1cd0352
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 54 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ a plethora of other features that I couldn't find in any other pager.
## Built-in Keybindings
* Custom keybindings will be added in the future.

### Viewer Mode
### Normal Mode
This is the default mode. You can scroll through files.

| Keybinding | Description |
Expand All @@ -76,13 +76,14 @@ In this mode, you can enter commands to interact with the pager.
| `:findl <lit>` | Create a new filter searching for the literal. |
| `:<number>` | Go to the specific line number. |

### Selection Mode
### Visual Mode
In this mode, you can select lines to bookmark.

| Keybinding | Description |
| ------------------- | -------------------------------- |
| `Up` and `Down` | Move the select cursor. |
| `Space` and `Enter` | Toggle bookmark at current line. |
| Keybinding | Description |
| ------------------------- | ------------------------------------------------ |
| `Up` and `Down` | Move the select cursor. |
| `Shift` + `Up` and `Down` | Expand the select cursor into a selection range. |
| `Space` and `Enter` | Toggle bookmark at current line. |

### Filter Mode
In this mode, you can toggle filters from bookmarks or searches to omit or include certain lines in the viewer.
Expand All @@ -98,9 +99,9 @@ In this mode, you can toggle filters from bookmarks or searches to omit or inclu
### Mode-Independent
| Keybinding | Description |
| --------------- | ----------------------------------------- |
| `Esc` | Exit selection mode (enter viewer mode). |
| `Esc` | Exit selection mode (enter normal mode). |
| `:` | Enter command mode. |
| `i` | Enter selection mode. |
| `v` | Enter visual mode. |
| `Tab` | Enter filter mode. |
| `` ` `` and `~` | Switch selected view. |
| `1` .. `9` | Switch selected view to the `n`th buffer. |
15 changes: 10 additions & 5 deletions crates/cli/app/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pub enum Action {
Exit,
SwitchMode(InputMode),
Command(CommandAction),
Viewer(ViewerAction),
Normal(NormalAction),
Visual(VisualAction),
Filter(FilterAction),
}

Expand All @@ -17,7 +18,7 @@ pub enum Delta {
Boundary,
}

pub enum ViewerAction {
pub enum NormalAction {
PanVertical {
direction: Direction,
delta: Delta,
Expand All @@ -29,6 +30,13 @@ pub enum ViewerAction {
target_view: Option<usize>,
},
FollowOutput,
SwitchActive(Direction),
SwitchActiveIndex {
target_view: usize,
},
}

pub enum VisualAction {
Move {
direction: Direction,
select: bool,
Expand All @@ -39,9 +47,6 @@ pub enum ViewerAction {
target_view: usize,
line_number: usize,
},
SwitchActiveIndex {
target_view: usize,
},
}

pub enum FilterAction {
Expand Down
34 changes: 17 additions & 17 deletions crates/cli/app/keybinding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
actions::{Action, CommandAction, CommandJump, Delta, FilterAction, ViewerAction},
actions::{Action, CommandAction, CommandJump, Delta, FilterAction, NormalAction, VisualAction},
InputMode,
};
use crate::direction::Direction;
Expand Down Expand Up @@ -31,10 +31,10 @@ impl Keybinding {

fn mode_dependent_bind(input_mode: InputMode, event: &mut Event) -> Option<Action> {
match input_mode {
InputMode::Viewer => match event {
InputMode::Normal => match event {
Event::Key(key) => match key.code {
KeyCode::Up | KeyCode::Down => {
Some(Action::Viewer(ViewerAction::PanVertical {
Some(Action::Normal(NormalAction::PanVertical {
direction: Direction::back_if(key.code == KeyCode::Up),
delta: if key.modifiers.contains(KeyModifiers::SHIFT) {
Delta::HalfPage
Expand All @@ -45,7 +45,7 @@ impl Keybinding {
}))
}
KeyCode::Left | KeyCode::Right => {
Some(Action::Viewer(ViewerAction::PanHorizontal {
Some(Action::Normal(NormalAction::PanHorizontal {
direction: Direction::back_if(key.code == KeyCode::Left),
delta: if key.modifiers.contains(KeyModifiers::SHIFT) {
Delta::HalfPage
Expand All @@ -56,7 +56,7 @@ impl Keybinding {
}))
}
KeyCode::Home | KeyCode::End | KeyCode::Char('g') => {
Some(Action::Viewer(ViewerAction::PanVertical {
Some(Action::Normal(NormalAction::PanVertical {
direction: Direction::back_if(matches!(
key.code,
KeyCode::Home | KeyCode::Char('g')
Expand All @@ -65,16 +65,16 @@ impl Keybinding {
target_view: None,
}))
}
KeyCode::Char('G') => Some(Action::Viewer(ViewerAction::FollowOutput)),
KeyCode::Char('G') => Some(Action::Normal(NormalAction::FollowOutput)),
KeyCode::PageUp | KeyCode::PageDown | KeyCode::Char(' ') => {
Some(Action::Viewer(ViewerAction::PanVertical {
Some(Action::Normal(NormalAction::PanVertical {
direction: Direction::back_if(key.code == KeyCode::PageUp),
delta: Delta::Page,
target_view: None,
}))
}
KeyCode::Char(c @ ('u' | 'd')) => {
Some(Action::Viewer(ViewerAction::PanVertical {
Some(Action::Normal(NormalAction::PanVertical {
direction: Direction::back_if(c == 'u'),
delta: Delta::HalfPage,
target_view: None,
Expand Down Expand Up @@ -116,9 +116,9 @@ impl Keybinding {
},
_ => None,
},
InputMode::Select => match event {
InputMode::Visual => match event {
Event::Key(key) => match key.code {
KeyCode::Up | KeyCode::Down => Some(Action::Viewer(ViewerAction::Move {
KeyCode::Up | KeyCode::Down => Some(Action::Visual(VisualAction::Move {
direction: Direction::back_if(key.code == KeyCode::Up),
select: key.modifiers.contains(KeyModifiers::SHIFT),
delta: if key
Expand All @@ -130,20 +130,20 @@ impl Keybinding {
Delta::Number(1)
},
})),
KeyCode::Home | KeyCode::End => Some(Action::Viewer(ViewerAction::Move {
KeyCode::Home | KeyCode::End => Some(Action::Visual(VisualAction::Move {
direction: Direction::back_if(key.code == KeyCode::Home),
select: key.modifiers.contains(KeyModifiers::SHIFT),
delta: Delta::Boundary,
})),
KeyCode::PageUp | KeyCode::PageDown => {
Some(Action::Viewer(ViewerAction::Move {
Some(Action::Visual(VisualAction::Move {
direction: Direction::back_if(key.code == KeyCode::PageUp),
select: key.modifiers.contains(KeyModifiers::SHIFT),
delta: Delta::Page,
}))
}
KeyCode::Char(' ') | KeyCode::Enter => {
Some(Action::Viewer(ViewerAction::ToggleSelectedLine))
Some(Action::Visual(VisualAction::ToggleSelectedLine))
}
_ => None,
},
Expand Down Expand Up @@ -202,13 +202,13 @@ impl Keybinding {
Event::Key(key) => match key.code {
KeyCode::Char(':') => Some(Action::SwitchMode(InputMode::Command)),
KeyCode::Tab => Some(Action::SwitchMode(InputMode::Filter)),
KeyCode::Char(c @ ('`' | '~')) => Some(Action::Viewer(ViewerAction::SwitchActive(
KeyCode::Char(c @ ('`' | '~')) => Some(Action::Normal(NormalAction::SwitchActive(
Direction::back_if(c == '~'),
))),
KeyCode::Esc => Some(Action::SwitchMode(InputMode::Viewer)),
KeyCode::Char('i') => Some(Action::SwitchMode(InputMode::Select)),
KeyCode::Esc => Some(Action::SwitchMode(InputMode::Normal)),
KeyCode::Char('i') => Some(Action::SwitchMode(InputMode::Visual)),
KeyCode::Char(c @ '1'..='9') => {
Some(Action::Viewer(ViewerAction::SwitchActiveIndex {
Some(Action::Normal(NormalAction::SwitchActiveIndex {
target_view: c as usize - '1' as usize,
}))
}
Expand Down
33 changes: 18 additions & 15 deletions crates/cli/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod mouse;
mod widgets;

use self::{
actions::{Action, CommandAction, Delta, ViewerAction},
actions::{Action, CommandAction, Delta, NormalAction, VisualAction},
keybinding::Keybinding,
mouse::MouseHandler,
widgets::{CommandWidget, MultiplexerWidget},
Expand Down Expand Up @@ -35,8 +35,8 @@ pub type Terminal<'a> = ratatui::Terminal<Backend<'a>>;
#[derive(PartialEq, Clone, Copy)]
pub enum InputMode {
Command,
Viewer,
Select,
Normal,
Visual,
Filter,
}

Expand All @@ -51,7 +51,7 @@ pub struct App {
impl App {
pub fn new() -> Self {
Self {
mode: InputMode::Viewer,
mode: InputMode::Normal,
command: CommandApp::new(),
mux: MultiplexerApp::new(),
status: StatusApp::new(),
Expand Down Expand Up @@ -143,14 +143,14 @@ impl App {
self.command.submit();
self.mode = new_mode;

if new_mode == InputMode::Select {
if new_mode == InputMode::Visual {
if let Some(viewer) = self.mux.active_viewer_mut() {
viewer.move_selected_into_view()
}
}
}
Action::Viewer(action) => match action {
ViewerAction::PanVertical {
Action::Normal(action) => match action {
NormalAction::PanVertical {
direction,
delta,
target_view,
Expand All @@ -171,7 +171,7 @@ impl App {
viewer.viewport_mut().pan_vertical(direction, delta);
}
}
ViewerAction::PanHorizontal {
NormalAction::PanHorizontal {
direction,
delta,
target_view,
Expand All @@ -192,15 +192,18 @@ impl App {
viewer.viewport_mut().pan_horizontal(direction, delta);
}
}
ViewerAction::FollowOutput => {
NormalAction::FollowOutput => {
if let Some(viewer) = self.mux.active_viewer_mut() {
viewer.viewport_mut().follow_output();
}
}
ViewerAction::SwitchActiveIndex { target_view } => {
NormalAction::SwitchActiveIndex { target_view } => {
self.mux.move_active_index(target_view)
}
ViewerAction::Move {
NormalAction::SwitchActive(direction) => self.mux.move_active(direction),
},
Action::Visual(action) => match action {
VisualAction::Move {
direction,
select,
delta,
Expand All @@ -215,13 +218,13 @@ impl App {
viewer.move_select(direction, select, delta)
}
}
ViewerAction::ToggleSelectedLine => {
VisualAction::ToggleSelectedLine => {
if let Some(viewer) = self.mux.active_viewer_mut() {
viewer.toggle_select_bookmarks();
viewer.filterer.compute_composite();
}
}
ViewerAction::ToggleLine {
VisualAction::ToggleLine {
target_view,
line_number,
} => {
Expand Down Expand Up @@ -295,15 +298,15 @@ impl App {
CommandAction::Paste(s) => self.command.enter_str(&s),
CommandAction::Backspace => {
if !self.command.delete() {
self.mode = InputMode::Viewer;
self.mode = InputMode::Normal;
}
}
CommandAction::Submit => {
let command = self.command.submit();
if !self.process_command(command) {
return false;
}
self.mode = InputMode::Viewer;
self.mode = InputMode::Normal;
}
},
};
Expand Down
18 changes: 9 additions & 9 deletions crates/cli/app/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
actions::{Action, Delta, FilterAction, ViewerAction},
actions::{Action, Delta, FilterAction, NormalAction},
mouse::MouseHandler,
InputMode,
};
Expand All @@ -13,7 +13,7 @@ use crate::{
status::StatusApp,
viewer::{Instance, LineData, LineType},
},
direction::Direction,
direction::Direction, app::actions::VisualAction,
};
use crossterm::event::MouseEventKind;
use ratatui::{prelude::*, widgets::*};
Expand All @@ -37,8 +37,8 @@ impl<'a> Widget for StatusWidget<'a> {

let accent_color = match self.input_mode {
InputMode::Command => colors::COMMAND_ACCENT,
InputMode::Viewer => colors::VIEWER_ACCENT,
InputMode::Select => colors::SELECT_ACCENT,
InputMode::Normal => colors::VIEWER_ACCENT,
InputMode::Visual => colors::SELECT_ACCENT,
InputMode::Filter => colors::FILTER_ACCENT,
};

Expand All @@ -47,8 +47,8 @@ impl<'a> Widget for StatusWidget<'a> {
v.push(
Span::from(match self.input_mode {
InputMode::Command => " COMMAND ",
InputMode::Viewer => " VIEWER ",
InputMode::Select => " SELECT ",
InputMode::Normal => " NORMAL ",
InputMode::Visual => " VISUAL ",
InputMode::Filter => " FILTER ",
})
.fg(colors::WHITE)
Expand Down Expand Up @@ -184,7 +184,7 @@ impl ViewerWidget<'_> {

handle.on_mouse(area, |event| match event.kind {
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
Some(Action::Viewer(ViewerAction::PanVertical {
Some(Action::Normal(NormalAction::PanVertical {
direction: Direction::back_if(event.kind == MouseEventKind::ScrollUp),
delta: Delta::Number(5),
target_view: Some(self.view_index),
Expand Down Expand Up @@ -352,7 +352,7 @@ impl ViewerLineWidget<'_> {

if let Some(line) = self.line {
handle.on_mouse(area, |event| match event.kind {
MouseEventKind::Down(_) => Some(Action::Viewer(ViewerAction::ToggleLine {
MouseEventKind::Down(_) => Some(Action::Visual(VisualAction::ToggleLine {
line_number: line.line_number,
target_view: self.view_index,
})),
Expand Down Expand Up @@ -391,7 +391,7 @@ impl TabWidget<'_> {
.render(area, buf);

handle.on_mouse(area, |event| match event.kind {
MouseEventKind::Down(_) => Some(Action::Viewer(ViewerAction::SwitchActiveIndex {
MouseEventKind::Down(_) => Some(Action::Normal(NormalAction::SwitchActiveIndex {
target_view: self.view_index,
})),
_ => None,
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/components/mux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::direction::Direction;
use super::viewer::Instance;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -60,6 +61,13 @@ impl MultiplexerApp {
self.active
}

pub fn move_active(&mut self, direction: Direction) {
self.move_active_index(match direction {
Direction::Back => self.active.saturating_sub(1),
Direction::Next => self.active.saturating_add(1),
})
}

pub fn move_active_index(&mut self, index: usize) {
self.active = index.min(self.views.len().saturating_sub(1));
}
Expand Down

0 comments on commit 1cd0352

Please sign in to comment.