2024-11-25 15:52:10 -05:00
|
|
|
use std::cmp::min;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use ratatui::{
|
2024-11-25 18:16:10 -05:00
|
|
|
buffer::Buffer,
|
|
|
|
layout::{Constraint, Flex, Rect},
|
2024-11-25 15:52:10 -05:00
|
|
|
style::{Color, Stylize},
|
|
|
|
text::Text,
|
2024-11-25 18:16:10 -05:00
|
|
|
widgets::{Block, Cell, Row, StatefulWidget, Table, Widget},
|
2024-11-25 15:52:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
use super::{Address, Book};
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
// TODO(zaphar): Move this to the book module.
|
|
|
|
// NOTE(zaphar): This is stolen from ironcalc but ironcalc doesn't expose it
|
2024-11-25 15:52:10 -05:00
|
|
|
// publically.
|
|
|
|
pub(crate) const LAST_COLUMN: usize = 16_384;
|
2024-11-25 18:16:10 -05:00
|
|
|
pub(crate) const LAST_ROW: usize = 1_048_576;
|
2024-11-25 15:52:10 -05:00
|
|
|
|
|
|
|
/// A visible column to show in our Viewport.
|
2024-11-25 18:16:10 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2024-11-25 15:52:10 -05:00
|
|
|
pub struct VisibleColumn {
|
|
|
|
pub idx: usize,
|
|
|
|
pub length: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a VisibleColumn> for Constraint {
|
|
|
|
fn from(value: &'a VisibleColumn) -> Self {
|
|
|
|
Constraint::Length(value.length as u16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct ViewportState {
|
|
|
|
prev_corner: Address,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewportState {
|
|
|
|
pub fn new(location: Address) -> Self {
|
|
|
|
Self {
|
|
|
|
prev_corner: location,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:52:10 -05:00
|
|
|
/// A renderable viewport over a book.
|
|
|
|
pub struct Viewport<'book> {
|
2024-11-25 18:16:10 -05:00
|
|
|
pub(crate) selected: Address,
|
2024-11-25 15:52:10 -05:00
|
|
|
book: &'book Book,
|
|
|
|
block: Option<Block<'book>>,
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
pub(crate) const COLNAMES: [&'static str; 26] = [
|
2024-11-25 15:52:10 -05:00
|
|
|
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
|
|
|
|
"T", "U", "V", "W", "X", "Y", "Z",
|
|
|
|
];
|
|
|
|
|
|
|
|
impl<'book> Viewport<'book> {
|
|
|
|
pub fn new(book: &'book Book) -> Self {
|
|
|
|
Self {
|
|
|
|
book,
|
2024-11-25 18:16:10 -05:00
|
|
|
selected: Default::default(),
|
2024-11-25 15:52:10 -05:00
|
|
|
block: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
pub fn with_selected(mut self, location: Address) -> Self {
|
|
|
|
self.selected = location;
|
2024-11-25 15:52:10 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
pub(crate) fn get_visible_columns(
|
|
|
|
&self,
|
|
|
|
width: u16,
|
|
|
|
state: &ViewportState,
|
|
|
|
) -> Result<Vec<VisibleColumn>> {
|
2024-11-25 15:52:10 -05:00
|
|
|
let mut visible = Vec::new();
|
2024-11-25 18:16:10 -05:00
|
|
|
// TODO(zaphar): This should be a shared constant with our first column.
|
|
|
|
// We start out with a length of 5 already researved
|
|
|
|
let mut length = 5;
|
|
|
|
let start_idx = std::cmp::min(self.selected.col, state.prev_corner.col);
|
|
|
|
for idx in start_idx..=LAST_COLUMN {
|
2024-11-25 15:52:10 -05:00
|
|
|
let size = self.book.get_col_size(idx)? as u16;
|
|
|
|
let updated_length = length + size;
|
2024-11-25 18:16:10 -05:00
|
|
|
let col = VisibleColumn { idx, length: size };
|
|
|
|
if updated_length < width {
|
2024-11-25 15:52:10 -05:00
|
|
|
length = updated_length;
|
2024-11-25 18:16:10 -05:00
|
|
|
visible.push(col);
|
|
|
|
} else if self.selected.col >= col.idx {
|
|
|
|
// We need a sliding window now
|
|
|
|
if let Some(first) = visible.first() {
|
|
|
|
// subtract the first columns size.
|
|
|
|
length = length - first.length;
|
|
|
|
// remove the first column.
|
|
|
|
visible = visible.into_iter().skip(1).collect();
|
|
|
|
}
|
|
|
|
// Add this col to the visible.
|
|
|
|
length += size;
|
|
|
|
visible.push(col);
|
|
|
|
// What if the length is still too long?
|
|
|
|
if length > width {
|
|
|
|
if let Some(first) = visible.first() {
|
|
|
|
// subtract the first columns size.
|
|
|
|
length = length - first.length;
|
|
|
|
}
|
|
|
|
visible = visible.into_iter().skip(1).collect();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
2024-11-25 15:52:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block(mut self, block: Block<'book>) -> Self {
|
|
|
|
self.block = Some(block);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
pub(crate) fn to_table<'widget>(
|
|
|
|
&self,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
state: &mut ViewportState,
|
|
|
|
) -> Result<Table<'widget>> {
|
|
|
|
let visible_columns = self.get_visible_columns(width, state)?;
|
|
|
|
if let Some(vc) = visible_columns.first() {
|
|
|
|
state.prev_corner.col = vc.idx
|
|
|
|
}
|
|
|
|
let max_row = min(state.prev_corner.row + height as usize, LAST_COLUMN);
|
|
|
|
let rows: Vec<Row> =
|
|
|
|
(state.prev_corner.row..=max_row)
|
|
|
|
.into_iter()
|
|
|
|
.map(|ri| {
|
|
|
|
let mut cells = vec![Cell::new(Text::from(ri.to_string()))];
|
|
|
|
cells.extend(visible_columns.iter().map(
|
|
|
|
|VisibleColumn { idx: ci, length: _ }| {
|
|
|
|
// TODO(zaphar): Is this safe?
|
|
|
|
let content = self
|
|
|
|
.book
|
|
|
|
.get_cell_addr_rendered(&Address { row: ri, col: *ci })
|
|
|
|
.unwrap();
|
|
|
|
let cell = Cell::new(Text::raw(content));
|
|
|
|
match (self.book.location.row == ri, self.book.location.col == *ci) {
|
|
|
|
(true, true) => cell.fg(Color::White).underlined(),
|
|
|
|
_ => cell
|
|
|
|
.bg(if ri % 2 == 0 {
|
|
|
|
Color::Rgb(57, 61, 71)
|
|
|
|
} else {
|
|
|
|
Color::Rgb(165, 169, 160)
|
|
|
|
})
|
|
|
|
.fg(if ri % 2 == 0 {
|
|
|
|
Color::White
|
|
|
|
} else {
|
|
|
|
Color::Rgb(31, 32, 34)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
.bold()
|
|
|
|
},
|
|
|
|
));
|
|
|
|
Row::new(cells)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let constraints: Vec<Constraint> = visible_columns
|
|
|
|
.iter()
|
2024-11-25 15:52:10 -05:00
|
|
|
.map(|vc| Constraint::from(vc))
|
|
|
|
.collect();
|
2024-11-25 18:16:10 -05:00
|
|
|
let end_idx = visible_columns.last().unwrap().idx;
|
2024-11-25 15:52:10 -05:00
|
|
|
let mut header = Vec::with_capacity(constraints.len());
|
|
|
|
header.push(Cell::new(""));
|
2024-11-25 18:16:10 -05:00
|
|
|
header.extend((state.prev_corner.col..end_idx).map(|i| {
|
2024-11-25 15:52:10 -05:00
|
|
|
let count = (i / 26) + 1;
|
2024-11-25 18:16:10 -05:00
|
|
|
Cell::new(COLNAMES[(i - 1) % 26].repeat(count))
|
2024-11-25 15:52:10 -05:00
|
|
|
}));
|
|
|
|
// TODO(zaphar): We should calculate the length from the length of the stringified version of the
|
|
|
|
// row indexes.
|
|
|
|
let mut col_constraints = vec![Constraint::Length(5)];
|
|
|
|
col_constraints.extend(constraints.into_iter());
|
|
|
|
Ok(Table::new(rows, col_constraints)
|
|
|
|
.header(Row::new(header).underlined())
|
|
|
|
.column_spacing(1)
|
|
|
|
.flex(Flex::Start))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 18:16:10 -05:00
|
|
|
impl<'book> StatefulWidget for Viewport<'book> {
|
|
|
|
type State = ViewportState;
|
|
|
|
|
|
|
|
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
|
|
|
let mut table = self
|
|
|
|
.to_table(area.width, area.height, state)
|
|
|
|
.expect("Failed to turn viewport into a table.");
|
2024-11-25 15:52:10 -05:00
|
|
|
if let Some(block) = self.block {
|
|
|
|
table = table.block(block);
|
|
|
|
}
|
2024-11-25 18:16:10 -05:00
|
|
|
Widget::render(table, area, buf);
|
2024-11-25 15:52:10 -05:00
|
|
|
}
|
|
|
|
}
|