Skip to content

Commit

Permalink
Add a special notation to jump to a random page
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed May 1, 2020
1 parent c0f5e8e commit 2b67dec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/NAVIGATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ Page names can also be used instead of page numbers:
```

For the page names to be resolved, you'll need to name the first page of each category.

## Special Notations

`-` or `+` can be prepended to a page number to jump to a relative page.

You can jump to a random page by specifying `_` as the page number.
7 changes: 6 additions & 1 deletion src/view/home/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Child, Stdio};
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use rand::Rng;
use regex::Regex;
use serde_json::Value as JsonValue;
use anyhow::{Error, format_err};
Expand Down Expand Up @@ -1313,7 +1314,11 @@ impl View for Home {
true
},
Event::Submit(ViewId::GoToPageInput, ref text) => {
if let Ok(index) = text.parse::<usize>() {
if text == "_" {
let mut rng = rand::thread_rng();
let index = rng.gen::<usize>() % self.pages_count;
self.go_to_page(index, hub, context);
} else if let Ok(index) = text.parse::<usize>() {
self.go_to_page(index.saturating_sub(1), hub, context);
}
true
Expand Down
7 changes: 6 additions & 1 deletion src/view/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use chrono::Local;
use regex::Regex;
use septem::prelude::*;
use septem::{Roman, Digit};
use rand::Rng;
use crate::input::{DeviceEvent, FingerStatus, ButtonCode, ButtonStatus};
use crate::framebuffer::{Framebuffer, UpdateMode, Pixmap};
use crate::view::{View, Event, AppCmd, Hub, Bus, ViewId, EntryKind, EntryId, SliderId};
Expand Down Expand Up @@ -2953,7 +2954,11 @@ impl View for Reader {
self.go_to_page(location, true, hub, context);
}
} else {
if let Ok(number) = caps[2].parse::<f64>() {
if text == "_" {
let mut rng = rand::thread_rng();
let location = rng.gen::<usize>() % self.pages_count;
self.go_to_page(location, true, hub, context);
} else if let Ok(number) = caps[2].parse::<f64>() {
let location = if !self.synthetic {
let mut index = number.max(0.0) as usize;
match prefix {
Expand Down

0 comments on commit 2b67dec

Please sign in to comment.