#![deny(unsafe_code)]
use super::Pager;
use crate::source::Anchor;
impl Pager {
pub(super) fn frame_block(&mut self, row: usize) {
let n = self.len();
if n == 0 {
return;
}
self.cursor = row.min(n - 1);
let h = self.content_rows().max(1);
let end = match self.src.block_at(self.cursor) {
Some(r) => r.end.max(self.cursor + 1),
None => self.cursor + 1,
};
let last = (end - 1).min(n - 1).max(self.cursor);
match last - self.cursor >= h {
true => self.top = self.cursor,
false => {
if self.cursor < self.top {
self.top = self.cursor;
}
if last >= self.top + h {
self.top = last + 1 - h;
}
}
}
self.clamp();
}
pub(super) fn move_cursor(&mut self, delta: isize) {
let n = self.len();
if n == 0 {
return;
}
let max = n as isize - 1;
let next = (self.cursor as isize).saturating_add(delta).clamp(0, max);
self.cursor = next as usize;
self.clamp();
}
pub(super) fn goto(&mut self, row: usize) {
if self.len() == 0 {
return;
}
self.cursor = row.min(self.len() - 1);
self.clamp();
}
pub(super) fn scroll_h(&mut self, delta: isize) {
let max = self.max_hoff();
let hoff = self.hoff;
let view = self.cols.max(1);
let next = match self.src.hscroll(hoff, delta.signum(), view) {
Some(off) => off as isize,
None => (hoff as isize).saturating_add(delta),
};
self.hoff = next.clamp(0, max as isize) as usize;
}
pub(super) fn widen(&mut self) {
match self.src.widen() {
Some(msg) => {
self.notify(msg);
self.clamp();
}
None => self.notify("nothing to widen here"),
}
}
pub(super) fn jump_to_row(&mut self, row: usize) {
self.cursor = row.min(self.len().saturating_sub(1));
let h = self.content_rows().max(1);
if self.cursor < self.top || self.cursor >= self.top + h {
self.top = self.cursor.saturating_sub(h / 3);
}
self.clamp();
}
pub(super) fn jump_to(&mut self, anchor: Anchor) {
match self.src.reveal(anchor) {
Some(row) => self.jump_to_row(row),
None => self.clamp(),
}
}
pub(super) fn jump_heading(&mut self, forward: bool) {
match self.src.next_landmark(self.cursor, forward) {
Some(row) => match self.src.blocks() {
true => self.frame_block(row),
false => self.goto(row),
},
None => self.notify(match (self.src.blocks(), forward) {
(true, true) => "no further block",
(true, false) => "no previous block",
(false, true) => "no further heading",
(false, false) => "no previous heading",
}),
}
}
}