Compare commits

...

2 Commits

2 changed files with 102 additions and 36 deletions

View File

@ -18,6 +18,64 @@ mod test;
const COL_PIXELS: f64 = 5.0; const COL_PIXELS: f64 = 5.0;
#[derive(Debug, Clone)]
pub struct AddressRange<'book> {
pub start: &'book Address,
pub end: &'book Address,
}
impl<'book> AddressRange<'book> {
pub fn as_rows(&self) -> Vec<Vec<Address>> {
let (row_range, col_range) = self.get_ranges();
let mut rows = Vec::with_capacity(row_range.len());
for ri in row_range.iter() {
let mut row = Vec::with_capacity(col_range.len());
for ci in col_range.iter() {
row.push(Address { row: *ri, col: *ci });
}
rows.push(row);
}
rows
}
pub fn as_series(&self) -> Vec<Address> {
let (row_range, col_range) = self.get_ranges();
let mut rows = Vec::with_capacity(row_range.len() * col_range.len());
for ri in row_range.iter() {
for ci in col_range.iter() {
rows.push(Address { row: *ri, col: *ci });
}
}
rows
}
fn get_ranges(&self) -> (Vec<usize>, Vec<usize>) {
let row_range = if self.start.row <= self.end.row {
(self.start.row..=self.end.row)
.into_iter()
.collect::<Vec<usize>>()
} else {
let mut v = (self.start.row..=self.end.row)
.into_iter()
.collect::<Vec<usize>>();
v.reverse();
v
};
let col_range = if self.start.col <= self.end.col {
(self.start.col..=self.end.col)
.into_iter()
.collect::<Vec<usize>>()
} else {
let mut v = (self.start.col..=self.end.col)
.into_iter()
.collect::<Vec<usize>>();
v.reverse();
v
};
(row_range, col_range)
}
}
/// A spreadsheet book with some internal state tracking. /// A spreadsheet book with some internal state tracking.
pub struct Book { pub struct Book {
pub(crate) model: Model, pub(crate) model: Model,
@ -103,18 +161,35 @@ impl Book {
self.location.col = *col; self.location.col = *col;
Ok(()) Ok(())
} }
/// Extend a cell to the rest of the range. /// Extend a cell to the rest of the range.
pub fn extend_to(&mut self, from: &Address, to: &Address) -> Result<()> { pub fn extend_to(&mut self, from: &Address, to: &Address) -> Result<()> {
for ri in from.row..=to.row { for cell in (AddressRange {
for ci in from.col..=to.col { start: from,
if ri == from.row && ci == from.col { end: to,
continue; })
} .as_series()
let contents = self.model.extend_to(self.current_sheet, from.row as i32, from.col as i32, ri as i32, ci as i32).map_err(|e| anyhow!(e))?; .iter()
self.model.set_user_input(self.current_sheet, ri as i32, ci as i32, contents) .skip(1)
.map_err(|e| anyhow!(e))?; {
} let contents = self
.model
.extend_to(
self.current_sheet,
from.row as i32,
from.col as i32,
cell.row as i32,
cell.col as i32,
)
.map_err(|e| anyhow!(e))?;
self.model
.set_user_input(
self.current_sheet,
cell.row as i32,
cell.col as i32,
contents,
)
.map_err(|e| anyhow!(e))?;
} }
self.evaluate(); self.evaluate();
Ok(()) Ok(())
@ -123,13 +198,12 @@ impl Book {
pub fn clear_current_cell(&mut self) -> Result<()> { pub fn clear_current_cell(&mut self) -> Result<()> {
self.clear_cell_contents(self.current_sheet as u32, self.location.clone()) self.clear_cell_contents(self.current_sheet as u32, self.location.clone())
} }
pub fn clear_current_cell_all(&mut self) -> Result<()> { pub fn clear_current_cell_all(&mut self) -> Result<()> {
self.clear_cell_all(self.current_sheet as u32, self.location.clone()) self.clear_cell_all(self.current_sheet as u32, self.location.clone())
} }
pub fn clear_cell_contents(&mut self, sheet: u32, Address { row, col }: Address) -> Result<()> {
pub fn clear_cell_contents(&mut self, sheet: u32, Address { row, col, }: Address) -> Result<()> {
Ok(self Ok(self
.model .model
.cell_clear_contents(sheet, row as i32, col as i32) .cell_clear_contents(sheet, row as i32, col as i32)
@ -144,8 +218,8 @@ impl Book {
} }
Ok(()) Ok(())
} }
pub fn clear_cell_all(&mut self, sheet: u32, Address { row, col, }: Address) -> Result<()> { pub fn clear_cell_all(&mut self, sheet: u32, Address { row, col }: Address) -> Result<()> {
Ok(self Ok(self
.model .model
.cell_clear_all(sheet, row as i32, col as i32) .cell_clear_all(sheet, row as i32, col as i32)
@ -161,7 +235,6 @@ impl Book {
Ok(()) Ok(())
} }
/// Get a cells formatted content. /// Get a cells formatted content.
pub fn get_current_cell_rendered(&self) -> Result<String> { pub fn get_current_cell_rendered(&self) -> Result<String> {
Ok(self.get_cell_addr_rendered(&self.location)?) Ok(self.get_cell_addr_rendered(&self.location)?)
@ -174,7 +247,7 @@ impl Book {
.get_formatted_cell_value(self.current_sheet, *row as i32, *col as i32) .get_formatted_cell_value(self.current_sheet, *row as i32, *col as i32)
.map_err(|s| anyhow!("Unable to format cell {}", s))?) .map_err(|s| anyhow!("Unable to format cell {}", s))?)
} }
/// Get a cells actual content unformatted as a string. /// Get a cells actual content unformatted as a string.
pub fn get_cell_addr_contents(&self, Address { row, col }: &Address) -> Result<String> { pub fn get_cell_addr_contents(&self, Address { row, col }: &Address) -> Result<String> {
Ok(self Ok(self
@ -183,7 +256,6 @@ impl Book {
.map_err(|s| anyhow!("Unable to format cell {}", s))?) .map_err(|s| anyhow!("Unable to format cell {}", s))?)
} }
/// Get a cells actual content as a string. /// Get a cells actual content as a string.
pub fn get_current_cell_contents(&self) -> Result<String> { pub fn get_current_cell_contents(&self) -> Result<String> {
Ok(self Ok(self
@ -290,7 +362,7 @@ impl Book {
.enumerate() .enumerate()
.find(|(_idx, sheet)| sheet.name == name) .find(|(_idx, sheet)| sheet.name == name)
{ {
self.current_sheet =idx as u32; self.current_sheet = idx as u32;
return true; return true;
} }
false false
@ -309,7 +381,7 @@ impl Book {
} }
self.current_sheet = next; self.current_sheet = next;
} }
pub fn select_prev_sheet(&mut self) { pub fn select_prev_sheet(&mut self) {
let len = self.model.workbook.worksheets.len() as u32; let len = self.model.workbook.worksheets.len() as u32;
let next = if self.current_sheet == 0 { let next = if self.current_sheet == 0 {
@ -320,7 +392,6 @@ impl Book {
self.current_sheet = next; self.current_sheet = next;
} }
/// Select a sheet by id. /// Select a sheet by id.
pub fn select_sheet_by_id(&mut self, id: u32) -> bool { pub fn select_sheet_by_id(&mut self, id: u32) -> bool {
if let Some((idx, _sheet)) = self if let Some((idx, _sheet)) = self
@ -353,13 +424,14 @@ impl Book {
.worksheet_mut(self.current_sheet) .worksheet_mut(self.current_sheet)
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?) .map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
} }
pub(crate) fn get_sheet_name_by_idx(&self, idx: usize) -> Result<&str> { pub(crate) fn get_sheet_name_by_idx(&self, idx: usize) -> Result<&str> {
Ok(&self Ok(&self
.model .model
.workbook .workbook
.worksheet(idx as u32) .worksheet(idx as u32)
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?.name) .map_err(|s| anyhow!("Invalid Worksheet: {}", s))?
.name)
} }
pub(crate) fn get_sheet_by_idx_mut(&mut self, idx: usize) -> Result<&mut Worksheet> { pub(crate) fn get_sheet_by_idx_mut(&mut self, idx: usize) -> Result<&mut Worksheet> {
Ok(self Ok(self

View File

@ -1,7 +1,7 @@
//! Ui rendering logic //! Ui rendering logic
use std::{path::PathBuf, process::ExitCode}; use std::{path::PathBuf, process::ExitCode};
use crate::book::Book; use crate::book::{AddressRange, Book};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
@ -598,25 +598,19 @@ impl<'ws> Workspace<'ws> {
self.update_range_selection()?; self.update_range_selection()?;
match &self.state.range_select.get_range() { match &self.state.range_select.get_range() {
Some(( Some((
Address { start,
row: row_start, end,
col: col_start,
},
Address {
row: row_end,
col: col_end,
},
)) => { )) => {
let mut rows = Vec::new(); let mut rows = Vec::new();
for ri in (*row_start)..=(*row_end) { for row in (AddressRange { start, end, }).as_rows() {
let mut cols = Vec::new(); let mut cols = Vec::new();
for ci in (*col_start)..=(*col_end) { for cell in row {
cols.push(if formatted { cols.push(if formatted {
self.book self.book
.get_cell_addr_rendered(&Address { row: ri, col: ci })? .get_cell_addr_rendered(&cell)?
} else { } else {
self.book self.book
.get_cell_addr_contents(&Address { row: ri, col: ci })? .get_cell_addr_contents(&cell)?
}); });
} }
rows.push(cols); rows.push(cols);