From be194c80c1cff38b2f6af9c79f7ac0fcc08fad2e Mon Sep 17 00:00:00 2001 From: Bastien Dejean Date: Wed, 17 Nov 2021 10:41:52 +0100 Subject: [PATCH] Interpret percentages in the *go to page* field --- doc/NAVIGATION.md | 2 ++ src/view/reader/mod.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/doc/NAVIGATION.md b/doc/NAVIGATION.md index 72e476a..90b577a 100644 --- a/doc/NAVIGATION.md +++ b/doc/NAVIGATION.md @@ -53,3 +53,5 @@ For the page names to be resolved, you'll need to name the first page of each ca Instead of the page number, you can specify one of the following characters: - `(` and `)` to jump to the first and last page. - `_` to jump to a random page. + +If a number ending with `%` is given it will be interpreted as a percentage of the book's pages count. diff --git a/src/view/reader/mod.rs b/src/view/reader/mod.rs index 334e903..121859d 100644 --- a/src/view/reader/mod.rs +++ b/src/view/reader/mod.rs @@ -3144,6 +3144,11 @@ impl View for Reader { self.go_to_page(0, true, hub, rq, context); } else if text == ")" { self.go_to_page(self.pages_count.saturating_sub(1), true, hub, rq, context); + } else if let Some(percent) = text.strip_suffix('%') { + if let Ok(number) = percent.parse::() { + let location = (number.max(0.0).min(100.0) / 100.0 * self.pages_count as f64).round() as usize; + self.go_to_page(location, true, hub, rq, context); + } } else if let Ok(number) = caps[2].parse::() { let location = if !self.synthetic { let mut index = number.max(0.0) as usize; -- 2.51.2