jarq 0.8.3

An interactive jq-like JSON query tool with a TUI
Documentation
/// Viewport scroll/display state
pub(super) struct Viewport {
    pub(super) scroll_offset: usize,
    pub(super) horizontal_offset: usize,
    pub(super) height: usize,
    width: usize,
}

impl Viewport {
    pub(super) fn new() -> Self {
        Self {
            scroll_offset: 0,
            horizontal_offset: 0,
            height: 0,
            width: 0,
        }
    }

    pub(super) fn set_size(&mut self, width: usize, height: usize) {
        self.width = width;
        self.height = height;
    }

    pub(super) fn scroll_down(&mut self, lines: usize, total_lines: usize) {
        self.scroll_offset = self.scroll_offset.saturating_add(lines);
        self.clamp_scroll(total_lines);
    }

    pub(super) fn scroll_up(&mut self, lines: usize) {
        self.scroll_offset = self.scroll_offset.saturating_sub(lines);
    }

    pub(super) fn scroll_left(&mut self, cols: usize) {
        self.horizontal_offset = self.horizontal_offset.saturating_sub(cols);
    }

    pub(super) fn scroll_right(&mut self, cols: usize) {
        self.horizontal_offset = self.horizontal_offset.saturating_add(cols);
    }

    pub(super) fn scroll_to_top(&mut self) {
        self.scroll_offset = 0;
    }

    pub(super) fn scroll_to_bottom(&mut self, total_lines: usize) {
        if total_lines > self.height {
            self.scroll_offset = total_lines - self.height;
        }
    }

    pub(super) fn clamp_scroll(&mut self, total_lines: usize) {
        let max_scroll = total_lines.saturating_sub(self.height);
        self.scroll_offset = self.scroll_offset.min(max_scroll);
    }

    pub(super) fn reset_scroll(&mut self) {
        self.scroll_offset = 0;
        self.horizontal_offset = 0;
    }
}