#![deny(unsafe_code)]
mod input;
mod motion;
pub mod keys;
mod link;
pub mod navigate;
mod view;
mod yank;
#[cfg(test)]
mod probe;
#[cfg(test)]
mod tests;
use std::time::{Duration, Instant};
use crate::dump::layout_width;
use crate::nav::Navigator;
use crate::select::{Selection, Yank};
use crate::source::{Anchor, End, Source};
use crate::term::Frame;
pub use crate::source::{collapse, search};
use search::Dir;
pub const MESSAGE_TTL: Duration = Duration::from_millis(2000);
pub const HSTEP: usize = 4;
const SEEK_SLICE: Duration = Duration::from_millis(200);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
Normal,
Outline,
Help,
Index,
Detail,
Search(Dir),
}
pub struct Pager {
pub(crate) src: Box<dyn Source>,
pub(crate) label: String,
width_override: Option<usize>,
pub(crate) cols: usize,
pub(crate) rows: usize,
pub(crate) width: usize,
pub(crate) top: usize,
pub(crate) cursor: usize,
pub(crate) hoff: usize,
pub(crate) mode: Mode,
pub(crate) query: String,
pub(crate) dir: Dir,
pub(crate) outline_sel: usize,
pub(crate) detail: Option<crate::source::Detail>,
pub(crate) detail_sel: usize,
pub(crate) help_top: usize,
pub(crate) nav: Option<Navigator>,
pub(crate) link_cursor: Option<usize>,
pub(crate) index_sel: usize,
pub(crate) index_filter: String,
pub(crate) index_typing: bool,
pub(crate) select: Option<Selection>,
pending_yank: Option<Yank>,
pending_open: Option<String>,
browser: bool,
pub(crate) message: Option<String>,
message_at: Option<Instant>,
pending: Option<char>,
search_origin: Option<Anchor>,
seek_end: bool,
dirty: bool,
quit: bool,
}
impl Pager {
pub fn new(
src: Box<dyn Source>,
label: String,
cols: usize,
rows: usize,
width_override: Option<usize>,
) -> Pager {
let mut p = Pager {
src,
label,
width_override,
cols,
rows,
width: 80,
top: 0,
cursor: 0,
hoff: 0,
mode: Mode::Normal,
query: String::new(),
dir: Dir::Forward,
outline_sel: 0,
detail: None,
detail_sel: 0,
help_top: 0,
nav: None,
link_cursor: None,
index_sel: 0,
index_filter: String::new(),
index_typing: false,
select: None,
pending_yank: None,
pending_open: None,
browser: true,
message: None,
message_at: None,
pending: None,
search_origin: None,
seek_end: false,
dirty: true,
quit: false,
};
p.relayout();
p
}
pub fn should_quit(&self) -> bool {
self.quit
}
pub fn body_rows(&self) -> usize {
self.rows.saturating_sub(1)
}
pub(crate) fn pinned(&self) -> usize {
let n = self.src.len();
self.src
.pinned()
.min(self.body_rows().saturating_sub(1))
.min(n.saturating_sub(1))
}
pub(crate) fn content_rows(&self) -> usize {
self.body_rows() - self.pinned()
}
pub(crate) fn len(&self) -> usize {
self.src.len()
}
pub(crate) fn cursor_row(&self) -> Option<usize> {
match self.cursor < self.src.len() {
true => Some(self.cursor),
false => None,
}
}
pub(crate) fn cursor_anchor(&self) -> Option<Anchor> {
self.src.anchor(self.cursor)
}
pub fn take_dirty(&mut self) -> bool {
std::mem::replace(&mut self.dirty, false)
}
pub fn paint(&mut self, frame: &mut Frame) {
view::paint(self, frame);
}
pub fn idle(&mut self) {
if self.seek_end {
return self.drive_seek();
}
if self.src.extend() {
self.dirty = true;
}
}
fn drive_seek(&mut self) {
let start = Instant::now();
while start.elapsed() < SEEK_SLICE && self.src.extend() {}
self.dirty = true;
self.goto_end();
}
pub(crate) fn goto_end(&mut self) {
match self.src.end() {
End::At(row) => {
if std::mem::take(&mut self.seek_end) {
self.clear_message();
}
self.goto(row);
}
End::Scanning(pct) => {
self.seek_end = true;
self.notify(format!(
"scanning to end of file\u{2026} {pct}% \u{b7} any key stops"
));
}
}
}
pub(crate) fn stop_seek(&mut self) -> bool {
std::mem::take(&mut self.seek_end)
}
pub fn tick(&mut self) {
if let Some(at) = self.message_at {
if at.elapsed() >= MESSAGE_TTL {
self.message = None;
self.message_at = None;
self.dirty = true;
}
}
}
pub(crate) fn selected_rows(&self) -> std::ops::Range<usize> {
let sel = match self.select {
Some(s) => s,
None => return 0..0,
};
let (lo, hi) = sel.range();
let hi = hi.min(self.src.len().saturating_sub(1));
match lo > hi {
true => 0..0,
false => lo..hi + 1,
}
}
pub fn take_yank(&mut self) -> Option<Yank> {
self.pending_yank.take()
}
pub fn set_browser(&mut self, enabled: bool) {
self.browser = enabled;
}
pub fn take_open(&mut self) -> Option<String> {
self.pending_open.take()
}
pub(crate) fn queue_open(&mut self, url: String) {
self.pending_open = Some(url);
self.dirty = true;
}
pub(crate) fn queue_yank(&mut self, yank: Yank) {
self.pending_yank = Some(yank);
self.dirty = true;
}
pub fn notify(&mut self, msg: impl Into<String>) {
self.message = Some(msg.into());
self.message_at = Some(Instant::now());
self.dirty = true;
}
pub(crate) fn clear_message(&mut self) {
self.message = None;
self.message_at = None;
self.dirty = true;
}
pub fn resize(&mut self, cols: usize, rows: usize) {
if (cols, rows) == (self.cols, self.rows) {
self.dirty = true;
return;
}
self.cols = cols;
self.rows = rows;
self.relayout();
}
pub(crate) fn relayout(&mut self) {
self.select = None;
let mark = self.src.mark(self.cursor);
self.width = match (self.width_override, self.src.full_width()) {
(Some(w), _) => layout_width(Some(w), Some(self.cols)),
(None, true) => self.cols.max(crate::render::MIN_WIDTH),
(None, false) => layout_width(None, Some(self.cols)),
};
self.src.set_width(self.width);
if self.link_cursor.map(|i| i >= self.src.links().len()).unwrap_or(false) {
self.link_cursor = None;
}
if let Some(at) = mark.and_then(|m| self.src.locate(m)) {
self.cursor = at;
}
self.clamp();
self.dirty = true;
}
pub(crate) fn folds_changed(&mut self) {
self.clamp();
}
fn open_tree(&mut self) {
match self.src.toggle_tree(self.cursor) {
Some(msg) => {
self.folds_changed();
self.notify(msg);
}
None => self.notify("nothing to open here"),
}
}
fn clamp(&mut self) {
let n = self.src.len();
self.cursor = self.cursor.min(n.saturating_sub(1));
if n == 0 {
self.cursor = 0;
self.top = 0;
self.hoff = 0;
return;
}
let pin = self.pinned();
self.cursor = self.cursor.max(pin);
let h = self.content_rows().max(1);
if self.cursor < self.top {
self.top = self.cursor;
}
if self.cursor >= self.top + h {
self.top = self.cursor + 1 - h;
}
self.top = self.top.min(n.saturating_sub(h).min(n - 1)).max(pin);
if self.hoff > 0 {
self.hoff = self.hoff.min(self.max_hoff());
}
self.dirty = true;
}
pub fn max_hoff(&mut self) -> usize {
let h = self.content_rows();
let pin = self.pinned();
let w = self.cols.max(1);
let top = self.top;
let widest = |lines: Vec<crate::render::Line>| {
lines
.iter()
.map(|l| l.width().saturating_sub(w))
.max()
.unwrap_or(0)
};
widest(self.src.lines(0..pin)).max(widest(self.src.lines(top..top.saturating_add(h))))
}
}
pub(crate) fn scrollable(line: &crate::render::Line, viewport: usize) -> bool {
line.scroll || line.width() > viewport
}