Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flesh out history #269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inlyne.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ code-highlighter = "github"
#
# Possible Modifiers: ["Alt", "Ctrl", "Os", "Shift"]
# Possible Actions: [
# "HistoryNext", "HistoryPrevious",
# "ToTop", "ToBottom",
# "ScrollUp", "ScrollDown",
# "PageUp", "PageDown",
Expand Down
73 changes: 51 additions & 22 deletions src/history.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,76 @@
use smart_debug::SmartDebug;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

#[derive(SmartDebug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct History {
history: Vec<PathBuf>,
index: usize,
}

impl History {
pub fn new(path_buf: PathBuf) -> Self {
History {
Self {
history: vec![path_buf],
index: 0,
}
}
pub fn truncate(&mut self) {
if self.index + 1 < self.history.len() {
self.history.truncate(self.index + 1);
}

pub fn get_path(&self) -> &Path {
self.history
.get(self.index)
.expect("History should always be in bounds")
.as_path()
}
pub fn append(&mut self, file_path: PathBuf) {

pub fn make_next(&mut self, file_path: PathBuf) {
self.history.truncate(self.index + 1);
self.history.push(file_path);
self.index += 1;
}
pub fn get_path(&self) -> &PathBuf {
self.history
.get(self.index)
.expect("History should be bound checked for all possible indexes.")
}

#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<&PathBuf> {
pub fn next(&mut self) -> Option<&Path> {
if self.index + 1 == self.history.len() {
return None;
None
} else {
self.index += 1;
Some(self.get_path())
}
self.index += 1;
Some(self.get_path())
}
pub fn previous(&mut self) -> Option<&PathBuf> {

pub fn previous(&mut self) -> Option<&Path> {
if self.index == 0 {
return None;
None
} else {
self.index -= 1;
Some(self.get_path())
}
self.index -= 1;
Some(self.get_path())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn sanity() {
let root = PathBuf::from("a");
let fork1 = PathBuf::from("b");
let fork2 = PathBuf::from("c");

let mut hist = History::new(root.clone());
assert_eq!(hist.get_path(), root);
assert_eq!(hist.previous(), None);

hist.make_next(fork1.clone());
assert_eq!(hist.get_path(), fork1);

assert_eq!(hist.previous().unwrap(), root);
hist.make_next(fork2.clone());
assert_eq!(hist.get_path(), fork2);

assert_eq!(hist.previous().unwrap(), root);
assert_eq!(hist.previous(), None);
assert_eq!(hist.next().unwrap(), fork2);
assert_eq!(hist.next(), None);
}
}
14 changes: 7 additions & 7 deletions src/keybindings/action.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
Navigate(Navigation),
History(HistDirection),
ToEdge(VertDirection),
Scroll(VertDirection),
Page(VertDirection),
Expand All @@ -9,6 +9,12 @@ pub enum Action {
Quit,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HistDirection {
Next,
Prev,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VertDirection {
Up,
Expand All @@ -21,9 +27,3 @@ pub enum Zoom {
Out,
Reset,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Navigation {
Previous,
Next,
}
20 changes: 11 additions & 9 deletions src/keybindings/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::action::{Action, Navigation, VertDirection, Zoom};
use crate::keybindings::action::HistDirection;

use super::action::{Action, VertDirection, Zoom};
use super::{Key, KeyCombo, ModifiedKey};

use winit::event::{ModifiersState, VirtualKeyCode as VirtKey};
Expand Down Expand Up @@ -34,17 +36,17 @@ pub fn defaults() -> Vec<(Action, KeyCombo)> {
ctrl_or_command,
)]),
),
// Navigate to next file: Ctrl+Right
// Navigate to next file: Alt+Right
(
Action::Navigate(Navigation::Next),
Action::History(HistDirection::Next),
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Right),
ModifiersState::ALT,
)]),
),
// Navigate to previous file: Ctrl+Left
// Navigate to previous file: Alt+Left
(
Action::Navigate(Navigation::Previous),
Action::History(HistDirection::Prev),
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Left),
ModifiersState::ALT,
Expand Down Expand Up @@ -127,17 +129,17 @@ pub fn defaults() -> Vec<(Action, KeyCombo)> {
ModifiedKey(Key::from(VirtKey::Q), ModifiersState::SHIFT),
]),
),
// Navigate to next file: Ctrl+Right
// Navigate to next file: bn
(
Action::Navigate(Navigation::Next),
Action::History(HistDirection::Next),
KeyCombo(vec![
ModifiedKey::from(VirtKey::B),
ModifiedKey::from(VirtKey::N),
]),
),
// Navigate to previous file: Ctrl+Left
// Navigate to previous file: bp
(
Action::Navigate(Navigation::Previous),
Action::History(HistDirection::Prev),
KeyCombo(vec![
ModifiedKey::from(VirtKey::B),
ModifiedKey::from(VirtKey::P),
Expand Down
6 changes: 6 additions & 0 deletions src/keybindings/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::str::FromStr;

use crate::keybindings::action::HistDirection;

use super::action::{Action, VertDirection, Zoom};
use super::{Key, KeyCombo, ModifiedKey};

Expand All @@ -13,6 +15,8 @@ impl<'de> Deserialize<'de> for Action {
{
#[derive(Deserialize)]
enum FlatAction {
HistoryNext,
HistoryPrevious,
ToTop,
ToBottom,
ScrollUp,
Expand All @@ -27,6 +31,8 @@ impl<'de> Deserialize<'de> for Action {
}

let action = match FlatAction::deserialize(deserializer)? {
FlatAction::HistoryNext => Action::History(HistDirection::Next),
FlatAction::HistoryPrevious => Action::History(HistDirection::Prev),
FlatAction::ToTop => Action::ToEdge(VertDirection::Up),
FlatAction::ToBottom => Action::ToEdge(VertDirection::Down),
FlatAction::ScrollUp => Action::Scroll(VertDirection::Up),
Expand Down
19 changes: 9 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::sync::{Arc, Mutex};
use file_watcher::Watcher;
use image::{Image, ImageData};
use interpreter::HtmlInterpreter;
use keybindings::action::{Action, Navigation, VertDirection, Zoom};
use keybindings::action::{Action, HistDirection, VertDirection, Zoom};
use keybindings::{Key, KeyCombos, ModifiedKey};
use opts::{Args, Config, Opts};
use positioner::{Positioned, Row, Section, Spacer, DEFAULT_MARGIN, DEFAULT_PADDING};
Expand Down Expand Up @@ -180,11 +180,11 @@ impl Inlyne {
pub fn new(opts: Opts) -> anyhow::Result<Self> {
let keycombos = KeyCombos::new(opts.keybindings.clone())?;

let file_path = opts.history.get_path();
let file_path = opts.history.get_path().to_owned();

let event_loop = EventLoopBuilder::<InlyneEvent>::with_user_event().build();
let window = Arc::new(Window::new(&event_loop).unwrap());
match root_filepath_to_vcs_dir(file_path) {
match root_filepath_to_vcs_dir(&file_path) {
Some(path) => window.set_title(&format!("Inlyne - {}", path.to_string_lossy())),
None => window.set_title("Inlyne"),
}
Expand All @@ -198,7 +198,7 @@ impl Inlyne {

let element_queue = Arc::new(Mutex::new(VecDeque::new()));
let image_cache = Arc::new(Mutex::new(HashMap::new()));
let md_string = read_to_string(file_path)
let md_string = read_to_string(&file_path)
.with_context(|| format!("Could not read file at '{}'", file_path.display()))?;

let interpreter = HtmlInterpreter::new(
Expand Down Expand Up @@ -522,8 +522,7 @@ impl Inlyne {
&path,
contents,
);
self.opts.history.truncate();
self.opts.history.append(path);
self.opts.history.make_next(path);
self.renderer.set_scroll_y(0.);
}
Err(err) => {
Expand Down Expand Up @@ -629,10 +628,10 @@ impl Inlyne {
Action::Copy => clipboard
.set_contents(selection_cache.trim().to_owned()),
Action::Quit => *control_flow = ControlFlow::Exit,
Action::Navigate(navigation_action) => {
let changed_path = match navigation_action {
Navigation::Next => self.opts.history.next(),
Navigation::Previous => self.opts.history.previous(),
Action::History(hist_dir) => {
let changed_path = match hist_dir {
HistDirection::Next => self.opts.history.next(),
HistDirection::Prev => self.opts.history.previous(),
};
let Some(file_path) = changed_path else {
return;
Expand Down