mirror of
https://github.com/zaphar/sheetsui.git
synced 2025-07-23 21:39:51 -04:00
merge: refactor: address refactor to include sheet
This commit is contained in:
commit
a6baa2564f
@ -37,7 +37,7 @@ impl<'book> AddressRange<'book> {
|
||||
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 });
|
||||
row.push(Address { sheet: self.start.sheet, row: *ri, col: *ci });
|
||||
}
|
||||
rows.push(row);
|
||||
}
|
||||
@ -49,7 +49,7 @@ impl<'book> AddressRange<'book> {
|
||||
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.push(Address { sheet: self.start.sheet, row: *ri, col: *ci });
|
||||
}
|
||||
}
|
||||
rows
|
||||
@ -85,7 +85,6 @@ impl<'book> AddressRange<'book> {
|
||||
/// A spreadsheet book with some internal state tracking.
|
||||
pub struct Book {
|
||||
pub(crate) model: UserModel,
|
||||
pub current_sheet: u32,
|
||||
pub location: crate::ui::Address,
|
||||
}
|
||||
|
||||
@ -94,7 +93,6 @@ impl Book {
|
||||
pub fn new(model: UserModel) -> Self {
|
||||
Self {
|
||||
model,
|
||||
current_sheet: 0,
|
||||
location: Address::default(),
|
||||
}
|
||||
}
|
||||
@ -162,7 +160,7 @@ impl Book {
|
||||
self.set_sheet_name(idx, name)?;
|
||||
}
|
||||
self.model
|
||||
.set_selected_sheet(self.current_sheet)
|
||||
.set_selected_sheet(self.location.sheet)
|
||||
.map_err(|e| anyhow!(e))?;
|
||||
Ok(())
|
||||
}
|
||||
@ -173,7 +171,7 @@ impl Book {
|
||||
}
|
||||
|
||||
/// Move to a specific sheet location in the current sheet
|
||||
pub fn move_to(&mut self, Address { row, col }: &Address) -> Result<()> {
|
||||
pub fn move_to(&mut self, Address { sheet: _, row, col }: &Address) -> Result<()> {
|
||||
// FIXME(zaphar): Check that this is safe first.
|
||||
self.location.row = *row;
|
||||
self.location.col = *col;
|
||||
@ -194,7 +192,7 @@ impl Book {
|
||||
.model
|
||||
.get_model()
|
||||
.extend_to(
|
||||
self.current_sheet,
|
||||
self.location.sheet,
|
||||
from.row as i32,
|
||||
from.col as i32,
|
||||
cell.row as i32,
|
||||
@ -203,7 +201,7 @@ impl Book {
|
||||
.map_err(|e| anyhow!(e))?;
|
||||
self.model
|
||||
.set_user_input(
|
||||
self.current_sheet,
|
||||
self.location.sheet,
|
||||
cell.row as i32,
|
||||
cell.col as i32,
|
||||
&contents,
|
||||
@ -215,14 +213,14 @@ impl Book {
|
||||
}
|
||||
|
||||
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.location.clone())
|
||||
}
|
||||
|
||||
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.location.clone())
|
||||
}
|
||||
|
||||
pub fn clear_cell_contents(&mut self, sheet: u32, Address { row, col }: Address) -> Result<()> {
|
||||
pub fn clear_cell_contents(&mut self, Address { sheet, row, col }: Address) -> Result<()> {
|
||||
Ok(self
|
||||
.model
|
||||
.range_clear_contents(&Area {
|
||||
@ -235,15 +233,15 @@ impl Book {
|
||||
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?)
|
||||
}
|
||||
|
||||
pub fn clear_cell_range(&mut self, sheet: u32, start: Address, end: Address) -> Result<()> {
|
||||
let area = calculate_area(sheet, &start, &end);
|
||||
pub fn clear_cell_range(&mut self, start: Address, end: Address) -> Result<()> {
|
||||
let area = calculate_area(start.sheet, &start, &end);
|
||||
self.model
|
||||
.range_clear_contents(&area)
|
||||
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear_cell_all(&mut self, sheet: u32, Address { row, col }: Address) -> Result<()> {
|
||||
pub fn clear_cell_all(&mut self, Address { sheet, row, col }: Address) -> Result<()> {
|
||||
Ok(self
|
||||
.model
|
||||
.range_clear_all(&Area {
|
||||
@ -256,8 +254,8 @@ impl Book {
|
||||
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?)
|
||||
}
|
||||
|
||||
pub fn clear_cell_range_all(&mut self, sheet: u32, start: Address, end: Address) -> Result<()> {
|
||||
let area = calculate_area(sheet, &start, &end);
|
||||
pub fn clear_cell_range_all(&mut self, start: Address, end: Address) -> Result<()> {
|
||||
let area = calculate_area(start.sheet, &start, &end);
|
||||
self.model
|
||||
.range_clear_all(&area)
|
||||
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?;
|
||||
@ -269,13 +267,13 @@ impl Book {
|
||||
Ok(self.get_cell_addr_rendered(&self.location)?)
|
||||
}
|
||||
|
||||
pub fn get_cell_style(&self, sheet: u32, cell: &Address) -> Option<Style> {
|
||||
pub fn get_cell_style(&self, cell: &Address) -> Option<Style> {
|
||||
// TODO(jwall): This is modeled a little weird. We should probably record
|
||||
// the error *somewhere* but for the user there is nothing to be done except
|
||||
// not use a style.
|
||||
match self
|
||||
.model
|
||||
.get_cell_style(sheet, cell.row as i32, cell.col as i32)
|
||||
.get_cell_style(cell.sheet, cell.row as i32, cell.col as i32)
|
||||
{
|
||||
Err(_) => None,
|
||||
Ok(s) => Some(s),
|
||||
@ -373,18 +371,18 @@ impl Book {
|
||||
}
|
||||
|
||||
/// Get a cells rendered content for display.
|
||||
pub fn get_cell_addr_rendered(&self, Address { row, col }: &Address) -> Result<String> {
|
||||
pub fn get_cell_addr_rendered(&self, Address { sheet, row, col }: &Address) -> Result<String> {
|
||||
Ok(self
|
||||
.model
|
||||
.get_formatted_cell_value(self.current_sheet, *row as i32, *col as i32)
|
||||
.get_formatted_cell_value(*sheet, *row as i32, *col as i32)
|
||||
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
||||
}
|
||||
|
||||
/// 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 { sheet, row, col }: &Address) -> Result<String> {
|
||||
Ok(self
|
||||
.model
|
||||
.get_cell_content(self.current_sheet, *row as i32, *col as i32)
|
||||
.get_cell_content(*sheet, *row as i32, *col as i32)
|
||||
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
||||
}
|
||||
|
||||
@ -393,7 +391,7 @@ impl Book {
|
||||
Ok(self
|
||||
.model
|
||||
.get_cell_content(
|
||||
self.current_sheet,
|
||||
self.location.sheet,
|
||||
self.location.row as i32,
|
||||
self.location.col as i32,
|
||||
)
|
||||
@ -412,7 +410,7 @@ impl Book {
|
||||
pub fn update_cell<S: AsRef<str>>(&mut self, location: &Address, value: S) -> Result<()> {
|
||||
self.model
|
||||
.set_user_input(
|
||||
self.current_sheet,
|
||||
location.sheet,
|
||||
location.row as i32,
|
||||
location.col as i32,
|
||||
// TODO(jwall): This could probably be made more efficient
|
||||
@ -426,11 +424,12 @@ impl Book {
|
||||
pub fn insert_rows(&mut self, row_idx: usize, count: usize) -> Result<()> {
|
||||
for i in 0..count {
|
||||
self.model
|
||||
.insert_row(self.current_sheet, (row_idx + i) as i32)
|
||||
.insert_row(self.location.sheet, (row_idx + i) as i32)
|
||||
.map_err(|e| anyhow!("Unable to insert row(s): {}", e))?;
|
||||
}
|
||||
if self.location.row >= row_idx {
|
||||
self.move_to(&Address {
|
||||
sheet: self.location.sheet,
|
||||
row: self.location.row + count,
|
||||
col: self.location.col,
|
||||
})?;
|
||||
@ -442,11 +441,12 @@ impl Book {
|
||||
pub fn insert_columns(&mut self, col_idx: usize, count: usize) -> Result<()> {
|
||||
for i in 0..count {
|
||||
self.model
|
||||
.insert_column(self.current_sheet, (col_idx + i) as i32)
|
||||
.insert_column(self.location.sheet, (col_idx + i) as i32)
|
||||
.map_err(|e| anyhow!("Unable to insert column(s): {}", e))?;
|
||||
}
|
||||
if self.location.col >= col_idx {
|
||||
self.move_to(&Address {
|
||||
sheet: self.location.sheet,
|
||||
row: self.location.row,
|
||||
col: self.location.col + count,
|
||||
})?;
|
||||
@ -461,7 +461,7 @@ impl Book {
|
||||
|
||||
/// Get column size
|
||||
pub fn get_col_size(&self, idx: usize) -> Result<usize> {
|
||||
self.get_column_size_for_sheet(self.current_sheet, idx)
|
||||
self.get_column_size_for_sheet(self.location.sheet, idx)
|
||||
}
|
||||
|
||||
pub fn get_column_size_for_sheet(
|
||||
@ -477,7 +477,7 @@ impl Book {
|
||||
}
|
||||
|
||||
pub fn set_col_size(&mut self, col: usize, width: usize) -> Result<()> {
|
||||
self.set_column_size_for_sheet(self.current_sheet, col, width)
|
||||
self.set_column_size_for_sheet(self.location.sheet, col, width)
|
||||
}
|
||||
|
||||
pub fn set_column_size_for_sheet(
|
||||
@ -517,7 +517,7 @@ impl Book {
|
||||
.enumerate()
|
||||
.find(|(_idx, sheet)| sheet.name == name)
|
||||
{
|
||||
self.current_sheet = idx as u32;
|
||||
self.location.sheet = idx as u32;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
@ -530,27 +530,27 @@ impl Book {
|
||||
|
||||
pub fn select_next_sheet(&mut self) {
|
||||
let len = self.model.get_model().workbook.worksheets.len() as u32;
|
||||
let mut next = self.current_sheet + 1;
|
||||
let mut next = self.location.sheet + 1;
|
||||
if next == len {
|
||||
next = 0;
|
||||
}
|
||||
self.model
|
||||
.set_selected_sheet(next)
|
||||
.expect("Unexpected error selecting sheet");
|
||||
self.current_sheet = next;
|
||||
self.location.sheet = next;
|
||||
}
|
||||
|
||||
pub fn select_prev_sheet(&mut self) {
|
||||
let len = self.model.get_model().workbook.worksheets.len() as u32;
|
||||
let next = if self.current_sheet == 0 {
|
||||
let next = if self.location.sheet == 0 {
|
||||
len - 1
|
||||
} else {
|
||||
self.current_sheet - 1
|
||||
self.location.sheet - 1
|
||||
};
|
||||
self.model
|
||||
.set_selected_sheet(next)
|
||||
.expect("Unexpected error selecting sheet");
|
||||
self.current_sheet = next;
|
||||
self.location.sheet = next;
|
||||
}
|
||||
|
||||
/// Select a sheet by id.
|
||||
@ -567,7 +567,7 @@ impl Book {
|
||||
self.model
|
||||
.set_selected_sheet(idx as u32)
|
||||
.expect("Unexpected error selecting sheet");
|
||||
self.current_sheet = idx as u32;
|
||||
self.location.sheet = idx as u32;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
@ -582,8 +582,8 @@ impl Book {
|
||||
.model
|
||||
.get_model()
|
||||
.workbook
|
||||
.worksheet(self.current_sheet)
|
||||
.map_err(|s| anyhow!("Invalid Worksheet id: {}: error: {}", self.current_sheet, s))?)
|
||||
.worksheet(self.location.sheet)
|
||||
.map_err(|s| anyhow!("Invalid Worksheet id: {}: error: {}", self.location.sheet, s))?)
|
||||
}
|
||||
|
||||
pub(crate) fn get_sheet_name_by_idx(&self, idx: usize) -> Result<&str> {
|
||||
@ -615,7 +615,7 @@ impl Default for Book {
|
||||
fn default() -> Self {
|
||||
let mut book =
|
||||
Book::new(UserModel::new_empty("default_name", "en", "America/New_York").unwrap());
|
||||
book.update_cell(&Address { row: 1, col: 1 }, "").unwrap();
|
||||
book.update_cell(&Address { sheet: 0, row: 1, col: 1 }, "").unwrap();
|
||||
book
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ fn test_book_default() {
|
||||
#[test]
|
||||
fn test_book_insert_cell_new_row() {
|
||||
let mut book = Book::default();
|
||||
book.update_cell(&Address { row: 2, col: 1 }, "1")
|
||||
book.update_cell(&Address { sheet: 0, row: 2, col: 1 }, "1")
|
||||
.expect("failed to edit cell");
|
||||
book.evaluate();
|
||||
let WorksheetDimension {
|
||||
@ -52,7 +52,7 @@ fn test_book_insert_cell_new_row() {
|
||||
#[test]
|
||||
fn test_book_insert_cell_new_column() {
|
||||
let mut book = Book::default();
|
||||
book.update_cell(&Address { row: 1, col: 2 }, "1")
|
||||
book.update_cell(&Address { sheet: 0, row: 1, col: 2 }, "1")
|
||||
.expect("failed to edit cell");
|
||||
let WorksheetDimension {
|
||||
min_row,
|
||||
@ -67,14 +67,14 @@ fn test_book_insert_cell_new_column() {
|
||||
#[test]
|
||||
fn test_book_insert_rows() {
|
||||
let mut book = Book::default();
|
||||
book.update_cell(&Address { row: 2, col: 2 }, "1")
|
||||
book.update_cell(&Address { sheet: 0, row: 2, col: 2 }, "1")
|
||||
.expect("failed to edit cell");
|
||||
book.move_to(&Address { row: 2, col: 2 })
|
||||
book.move_to(&Address { sheet: 0, row: 2, col: 2 })
|
||||
.expect("Failed to move to location");
|
||||
assert_eq!((2, 2), book.get_size().expect("Failed to get size"));
|
||||
book.insert_rows(1, 5).expect("Failed to insert rows");
|
||||
assert_eq!((7, 2), book.get_size().expect("Failed to get size"));
|
||||
assert_eq!(Address { row: 7, col: 2 }, book.location);
|
||||
assert_eq!(Address { sheet: 0, row: 7, col: 2 }, book.location);
|
||||
assert_eq!(
|
||||
"1",
|
||||
book.get_current_cell_rendered()
|
||||
@ -85,14 +85,14 @@ fn test_book_insert_rows() {
|
||||
#[test]
|
||||
fn test_book_insert_columns() {
|
||||
let mut book = Book::default();
|
||||
book.update_cell(&Address { row: 2, col: 2 }, "1")
|
||||
book.update_cell(&Address { sheet: 0, row: 2, col: 2 }, "1")
|
||||
.expect("failed to edit cell");
|
||||
book.move_to(&Address { row: 2, col: 2 })
|
||||
book.move_to(&Address { sheet: 0, row: 2, col: 2 })
|
||||
.expect("Failed to move to location");
|
||||
assert_eq!((2, 2), book.get_size().expect("Failed to get size"));
|
||||
book.insert_columns(1, 5).expect("Failed to insert rows");
|
||||
assert_eq!((2, 7), book.get_size().expect("Failed to get size"));
|
||||
assert_eq!(Address { row: 2, col: 7 }, book.location);
|
||||
assert_eq!(Address { sheet: 0, row: 2, col: 7 }, book.location);
|
||||
assert_eq!(
|
||||
"1",
|
||||
book.get_current_cell_rendered()
|
||||
@ -103,7 +103,7 @@ fn test_book_insert_columns() {
|
||||
#[test]
|
||||
fn test_book_col_size() {
|
||||
let mut book = Book::default();
|
||||
book.update_cell(&Address { row: 2, col: 2 }, "1")
|
||||
book.update_cell(&Address { sheet: 0, row: 2, col: 2 }, "1")
|
||||
.expect("failed to edit cell");
|
||||
book.set_col_size(1, 20).expect("Failed to set column size");
|
||||
assert_eq!(20, book.get_col_size(1).expect("Failed to get column size"));
|
||||
|
@ -34,8 +34,6 @@ pub enum Modality {
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RangeSelection {
|
||||
pub original_location: Option<Address>,
|
||||
pub original_sheet: Option<u32>,
|
||||
pub sheet: Option<u32>,
|
||||
pub start: Option<Address>,
|
||||
pub end: Option<Address>,
|
||||
}
|
||||
@ -45,10 +43,12 @@ impl RangeSelection {
|
||||
if let (Some(start), Some(end)) = (&self.start, &self.end) {
|
||||
return Some((
|
||||
Address {
|
||||
sheet: start.sheet,
|
||||
row: std::cmp::min(start.row, end.row),
|
||||
col: std::cmp::min(start.col, end.col),
|
||||
},
|
||||
Address {
|
||||
sheet: end.sheet,
|
||||
row: std::cmp::max(start.row, end.row),
|
||||
col: std::cmp::max(start.col, end.col),
|
||||
},
|
||||
@ -60,7 +60,6 @@ impl RangeSelection {
|
||||
pub fn reset_range_selection(&mut self) {
|
||||
self.start = None;
|
||||
self.end = None;
|
||||
self.sheet = None;
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,13 +132,14 @@ impl<'ws> AppState<'ws> {
|
||||
/// The Address in a Table.
|
||||
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
|
||||
pub struct Address {
|
||||
pub sheet: u32,
|
||||
pub row: usize,
|
||||
pub col: usize,
|
||||
}
|
||||
|
||||
impl Address {
|
||||
pub fn new(row: usize, col: usize) -> Self {
|
||||
Self { row, col }
|
||||
Self { sheet: 0, row, col }
|
||||
}
|
||||
|
||||
pub fn to_range_part(&self) -> String {
|
||||
@ -214,12 +214,12 @@ impl<'ws> Workspace<'ws> {
|
||||
start.to_range_part(),
|
||||
format!(":{}", end.to_range_part())
|
||||
);
|
||||
if let Some(range_sheet) = state.range_select.sheet {
|
||||
if range_sheet != self.book.current_sheet {
|
||||
if let Some(ref start_addr) = state.range_select.start {
|
||||
if start_addr.sheet != self.book.location.sheet {
|
||||
return format!(
|
||||
"{}!{}",
|
||||
self.book
|
||||
.get_sheet_name_by_idx(range_sheet as usize)
|
||||
.get_sheet_name_by_idx(start_addr.sheet as usize)
|
||||
.expect("No such sheet index"),
|
||||
a1
|
||||
);
|
||||
@ -243,6 +243,7 @@ impl<'ws> Workspace<'ws> {
|
||||
/// Move to the top row without changing columns
|
||||
pub fn move_to_top(&mut self) -> Result<()> {
|
||||
self.book.move_to(&Address {
|
||||
sheet: self.book.location.sheet,
|
||||
row: 1,
|
||||
col: self.book.location.col,
|
||||
})?;
|
||||
@ -411,7 +412,7 @@ impl<'ws> Workspace<'ws> {
|
||||
self.book.set_sheet_name(idx as u32, name)?;
|
||||
}
|
||||
_ => {
|
||||
self.book.set_sheet_name(self.book.current_sheet, name)?;
|
||||
self.book.set_sheet_name(self.book.location.sheet, name)?;
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
@ -431,7 +432,7 @@ impl<'ws> Workspace<'ws> {
|
||||
for r in row..(row + row_count) {
|
||||
self.book.set_row_style(
|
||||
&[("fill.bg_color", &color)],
|
||||
self.book.current_sheet,
|
||||
self.book.location.sheet,
|
||||
r,
|
||||
)?;
|
||||
}
|
||||
@ -443,14 +444,14 @@ impl<'ws> Workspace<'ws> {
|
||||
for c in col..(col + col_count) {
|
||||
self.book.set_col_style(
|
||||
&[("fill.bg_color", &color)],
|
||||
self.book.current_sheet,
|
||||
self.book.location.sheet,
|
||||
c,
|
||||
)?;
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
Ok(Some(Cmd::ColorCell(color))) => {
|
||||
let sheet = self.book.current_sheet;
|
||||
let sheet = self.book.location.sheet;
|
||||
let area = if let Some((start, end)) = self.state.range_select.get_range() {
|
||||
Area {
|
||||
sheet,
|
||||
@ -508,12 +509,8 @@ impl<'ws> Workspace<'ws> {
|
||||
self.handle_numeric_prefix(d);
|
||||
}
|
||||
KeyCode::Char('D') => {
|
||||
if let Some((start, end)) = dbg!(self.state.range_select.get_range()) {
|
||||
if let Some((start, end)) = self.state.range_select.get_range() {
|
||||
self.book.clear_cell_range_all(
|
||||
self.state
|
||||
.range_select
|
||||
.sheet
|
||||
.unwrap_or_else(|| self.book.current_sheet),
|
||||
start,
|
||||
end,
|
||||
)?;
|
||||
@ -522,10 +519,6 @@ impl<'ws> Workspace<'ws> {
|
||||
KeyCode::Char('d') => {
|
||||
if let Some((start, end)) = self.state.range_select.get_range() {
|
||||
self.book.clear_cell_range(
|
||||
self.state
|
||||
.range_select
|
||||
.sheet
|
||||
.unwrap_or_else(|| self.book.current_sheet),
|
||||
start,
|
||||
end,
|
||||
)?;
|
||||
@ -570,7 +563,6 @@ impl<'ws> Workspace<'ws> {
|
||||
ws.book.select_next_sheet();
|
||||
Ok(())
|
||||
})?;
|
||||
self.state.range_select.sheet = Some(self.book.current_sheet);
|
||||
}
|
||||
KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => {
|
||||
self.state.range_select.reset_range_selection();
|
||||
@ -578,7 +570,6 @@ impl<'ws> Workspace<'ws> {
|
||||
ws.book.select_prev_sheet();
|
||||
Ok(())
|
||||
})?;
|
||||
self.state.range_select.sheet = Some(self.book.current_sheet);
|
||||
}
|
||||
KeyCode::Char('C') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
self.copy_range(true)?;
|
||||
@ -671,12 +662,12 @@ impl<'ws> Workspace<'ws> {
|
||||
}
|
||||
KeyCode::Char('B') => {
|
||||
let address = self.book.location.clone();
|
||||
let style = self.book.get_cell_style(self.book.current_sheet, &address).map(|s| s.font.b);
|
||||
let style = self.book.get_cell_style(&address).map(|s| s.font.b);
|
||||
self.toggle_bool_style(style, "font.b", &address)?;
|
||||
}
|
||||
KeyCode::Char('I') => {
|
||||
let address = self.book.location.clone();
|
||||
let style = self.book.get_cell_style(self.book.current_sheet, &address).map(|s| s.font.i);
|
||||
let style = self.book.get_cell_style(&address).map(|s| s.font.i);
|
||||
self.toggle_bool_style(style, "font.i", &address)?;
|
||||
}
|
||||
KeyCode::Char(d) if d.is_ascii_digit() => {
|
||||
@ -751,7 +742,7 @@ impl<'ws> Workspace<'ws> {
|
||||
}
|
||||
KeyCode::Char('l') if key.modifiers == KeyModifiers::CONTROL => {
|
||||
self.run_with_prefix(|ws: &mut Workspace<'_>| -> Result<()> {
|
||||
let Address { row: _, col } = &ws.book.location;
|
||||
let Address { sheet: _, row: _, col } = &ws.book.location;
|
||||
ws.book
|
||||
.set_col_size(*col, ws.book.get_col_size(*col)? + 1)?;
|
||||
Ok(())
|
||||
@ -759,7 +750,7 @@ impl<'ws> Workspace<'ws> {
|
||||
}
|
||||
KeyCode::Char('h') if key.modifiers == KeyModifiers::CONTROL => {
|
||||
self.run_with_prefix(|ws: &mut Workspace<'_>| -> Result<()> {
|
||||
let Address { row: _, col } = &ws.book.location;
|
||||
let Address { sheet: _, row: _, col } = &ws.book.location;
|
||||
let curr_size = ws.book.get_col_size(*col)?;
|
||||
if curr_size > 1 {
|
||||
ws.book.set_col_size(*col, curr_size - 1)?;
|
||||
@ -859,7 +850,7 @@ impl<'ws> Workspace<'ws> {
|
||||
self.book.set_cell_style(
|
||||
&[(path, value)],
|
||||
&Area {
|
||||
sheet: self.book.current_sheet,
|
||||
sheet: address.sheet,
|
||||
row: address.row as i32,
|
||||
column: address.col as i32,
|
||||
width: 1,
|
||||
@ -875,7 +866,7 @@ impl<'ws> Workspace<'ws> {
|
||||
self.book.evaluate();
|
||||
}
|
||||
Some(ClipboardContents::Range(ref rows)) => {
|
||||
let Address { row, col } = self.book.location.clone();
|
||||
let Address { sheet, row, col } = self.book.location.clone();
|
||||
let row_len = rows.len();
|
||||
for ri in 0..row_len {
|
||||
let columns = &rows[ri];
|
||||
@ -883,6 +874,7 @@ impl<'ws> Workspace<'ws> {
|
||||
for ci in 0..col_len {
|
||||
self.book.update_cell(
|
||||
&Address {
|
||||
sheet,
|
||||
row: ri + row,
|
||||
col: ci + col,
|
||||
},
|
||||
@ -924,8 +916,6 @@ impl<'ws> Workspace<'ws> {
|
||||
}
|
||||
|
||||
fn enter_range_select_mode(&mut self, init_start: bool) {
|
||||
self.state.range_select.sheet = Some(self.book.current_sheet);
|
||||
self.state.range_select.original_sheet = Some(self.book.current_sheet);
|
||||
self.state.range_select.original_location = Some(self.book.location.clone());
|
||||
if init_start {
|
||||
self.state.range_select.start = Some(self.book.location.clone());
|
||||
@ -960,12 +950,6 @@ impl<'ws> Workspace<'ws> {
|
||||
}
|
||||
|
||||
fn exit_range_select_mode(&mut self) -> Result<()> {
|
||||
self.book.current_sheet = self
|
||||
.state
|
||||
.range_select
|
||||
.original_sheet
|
||||
.clone()
|
||||
.expect("Missing original sheet");
|
||||
self.book.location = self
|
||||
.state
|
||||
.range_select
|
||||
|
@ -36,7 +36,7 @@ impl<'ws> Workspace<'ws> {
|
||||
.map(|(idx, name)| format!("{} {}", name, idx))
|
||||
.collect::<Vec<String>>(),
|
||||
)
|
||||
.select(Some(ws.book.current_sheet as usize));
|
||||
.select(Some(ws.book.location.sheet as usize));
|
||||
tabs.render(rect, buf);
|
||||
}),
|
||||
Box::new(|rect: Rect, buf: &mut Buffer, ws: &mut Self| {
|
||||
|
@ -12,10 +12,10 @@ fn test_viewport_get_visible_columns() {
|
||||
Model::new_empty("test", "en", "America/New_York").expect("Failed to make model"),
|
||||
);
|
||||
let default_size = book.get_col_size(1).expect("Failed to get column size");
|
||||
let width = dbg!(dbg!(default_size) * 12 / 2);
|
||||
let width = default_size * 12 / 2;
|
||||
let app_state = AppState::default();
|
||||
let viewport = Viewport::new(&book, Some(&app_state.range_select))
|
||||
.with_selected(Address { row: 1, col: 17 });
|
||||
.with_selected(Address { sheet: 0, row: 1, col: 17 });
|
||||
let cols = viewport
|
||||
.get_visible_columns((width + 5) as u16, &mut state)
|
||||
.expect("Failed to get visible columns");
|
||||
@ -25,15 +25,15 @@ fn test_viewport_get_visible_columns() {
|
||||
|
||||
#[test]
|
||||
fn test_viewport_get_visible_rows() {
|
||||
let mut state = dbg!(ViewportState::default());
|
||||
let mut state = ViewportState::default();
|
||||
let book = Book::from_model(
|
||||
Model::new_empty("test", "en", "America/New_York").expect("Failed to make model"),
|
||||
);
|
||||
let height = 6;
|
||||
let app_state = AppState::default();
|
||||
let viewport = Viewport::new(&book, Some(&app_state.range_select))
|
||||
.with_selected(Address { row: 17, col: 1 });
|
||||
let rows = dbg!(viewport.get_visible_rows(height as u16, &mut state));
|
||||
.with_selected(Address { sheet: 0, row: 17, col: 1 });
|
||||
let rows = viewport.get_visible_rows(height as u16, &mut state);
|
||||
assert_eq!(height - 1, rows.len());
|
||||
assert_eq!(
|
||||
17 - (height - 2),
|
||||
@ -49,11 +49,11 @@ fn test_viewport_visible_columns_after_length_change() {
|
||||
Model::new_empty("test", "en", "America/New_York").expect("Failed to make model"),
|
||||
);
|
||||
let default_size = book.get_col_size(1).expect("Failed to get column size");
|
||||
let width = dbg!(dbg!(default_size) * 12 / 2);
|
||||
let width = default_size * 12 / 2;
|
||||
{
|
||||
let app_state = AppState::default();
|
||||
let viewport = Viewport::new(&book, Some(&app_state.range_select))
|
||||
.with_selected(Address { row: 1, col: 17 });
|
||||
.with_selected(Address { sheet: 0, row: 1, col: 17 });
|
||||
let cols = viewport
|
||||
.get_visible_columns((width + 5) as u16, &mut state)
|
||||
.expect("Failed to get visible columns");
|
||||
@ -66,7 +66,7 @@ fn test_viewport_visible_columns_after_length_change() {
|
||||
{
|
||||
let app_state = AppState::default();
|
||||
let viewport = Viewport::new(&book, Some(&app_state.range_select))
|
||||
.with_selected(Address { row: 1, col: 1 });
|
||||
.with_selected(Address { sheet: 0, row: 1, col: 1 });
|
||||
let cols = viewport
|
||||
.get_visible_columns((width + 5) as u16, &mut state)
|
||||
.expect("Failed to get visible columns");
|
||||
|
@ -72,8 +72,6 @@ impl<'ws> Viewport<'ws> {
|
||||
start = start + 1;
|
||||
end = row_idx;
|
||||
} else {
|
||||
//dbg!(&start);
|
||||
//dbg!(&end);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -152,7 +150,7 @@ impl<'ws> Viewport<'ws> {
|
||||
|VisibleColumn { idx: ci, length: _ }| {
|
||||
let content = self
|
||||
.book
|
||||
.get_cell_addr_rendered(&Address { row: ri, col: *ci })
|
||||
.get_cell_addr_rendered(&Address { row: ri, col: *ci, sheet: self.book.location.sheet})
|
||||
.unwrap();
|
||||
self.compute_cell_style(ri, *ci, Cell::new(Text::raw(content)))
|
||||
},
|
||||
@ -196,7 +194,7 @@ impl<'ws> Viewport<'ws> {
|
||||
// TODO(zaphar): Should probably create somekind of formatter abstraction.
|
||||
if let Some(style) = self
|
||||
.book
|
||||
.get_cell_style(self.book.current_sheet, &Address { row: ri, col: ci }) {
|
||||
.get_cell_style(&Address { sheet: self.book.location.sheet, row: ri, col: ci }) {
|
||||
cell = self.compute_cell_colors(&style, ri, ci, cell);
|
||||
cell = if style.font.b {
|
||||
cell.bold()
|
||||
|
@ -542,8 +542,9 @@ fn test_range_copy() {
|
||||
let mut ws = new_workspace();
|
||||
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
||||
|
||||
let address = Address::default();
|
||||
ws.book
|
||||
.move_to(&Address { row: 1, col: 1 })
|
||||
.move_to(&address)
|
||||
.expect("Failed to move to row");
|
||||
let original_loc = ws.book.location.clone();
|
||||
script()
|
||||
@ -564,7 +565,7 @@ fn test_range_copy() {
|
||||
.run(&mut ws)
|
||||
.expect("Failed to handle key sequence");
|
||||
assert_eq!(
|
||||
Some(Address { row: 1, col: 2 }),
|
||||
Some(Address { sheet: 0, row: 1, col: 2 }),
|
||||
ws.state.range_select.start
|
||||
);
|
||||
|
||||
@ -576,18 +577,18 @@ fn test_range_copy() {
|
||||
|
||||
assert!(ws.state.range_select.original_location.is_none());
|
||||
assert_eq!(
|
||||
Some(Address { row: 1, col: 2 }),
|
||||
Some(Address { sheet: 0, row: 1, col: 2 }),
|
||||
ws.state.range_select.start
|
||||
);
|
||||
assert_eq!(Some(Address { row: 2, col: 2 }), ws.state.range_select.end);
|
||||
assert_eq!(Some(Address { sheet: 0, row: 2, col: 2 }), ws.state.range_select.end);
|
||||
assert_eq!(original_loc, ws.book.location);
|
||||
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
||||
|
||||
ws.book
|
||||
.move_to(&Address { row: 5, col: 5 })
|
||||
.move_to(&Address { sheet: 0, row: 5, col: 5 })
|
||||
.expect("Failed to move to row");
|
||||
let original_loc_2 = ws.book.location.clone();
|
||||
assert_eq!(Address { row: 5, col: 5 }, original_loc_2);
|
||||
assert_eq!(Address { sheet: 0, row: 5, col: 5 }, original_loc_2);
|
||||
|
||||
script()
|
||||
.char('v')
|
||||
@ -607,7 +608,7 @@ fn test_range_copy() {
|
||||
.run(&mut ws)
|
||||
.expect("Failed to handle key sequence");
|
||||
assert_eq!(
|
||||
Some(Address { row: 5, col: 5 }),
|
||||
Some(Address { sheet: 0, row: 5, col: 5 }),
|
||||
ws.state.range_select.start
|
||||
);
|
||||
|
||||
@ -619,11 +620,11 @@ fn test_range_copy() {
|
||||
|
||||
assert!(ws.state.range_select.original_location.is_none());
|
||||
assert_eq!(
|
||||
Some(Address { row: 5, col: 5 }),
|
||||
Some(Address { sheet: 0, row: 5, col: 5 }),
|
||||
ws.state.range_select.start
|
||||
);
|
||||
assert_eq!(Some(Address { row: 5, col: 4 }), ws.state.range_select.end);
|
||||
assert_eq!(Address { row: 4, col: 5 }, ws.book.location);
|
||||
assert_eq!(Some(Address { sheet: 0, row: 5, col: 4 }), ws.state.range_select.end);
|
||||
assert_eq!(Address { sheet: 0, row: 4, col: 5 }, ws.book.location);
|
||||
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
||||
}
|
||||
|
||||
@ -652,14 +653,14 @@ fn test_gg_movement() {
|
||||
.char('j')
|
||||
.run(&mut ws)
|
||||
.expect("failed to handle event sequence");
|
||||
assert_eq!(ws.book.location, Address { row: 3, col: 1 });
|
||||
assert_eq!(ws.book.location, Address { sheet: 0, row: 3, col: 1 });
|
||||
script()
|
||||
.char('l')
|
||||
.char('g')
|
||||
.char('g')
|
||||
.run(&mut ws)
|
||||
.expect("failed to handle event sequence");
|
||||
assert_eq!(ws.book.location, Address { row: 1, col: 2 });
|
||||
assert_eq!(ws.book.location, Address { sheet: 0, row: 1, col: 2 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -672,14 +673,14 @@ fn test_h_j_k_l_movement() {
|
||||
.char('l')
|
||||
.run(&mut ws)
|
||||
.expect("failed to handle event sequence");
|
||||
assert_eq!(ws.book.location, Address { row: 3, col: 2 });
|
||||
assert_eq!(ws.book.location, Address { sheet: 0, row: 3, col: 2 });
|
||||
script()
|
||||
.char('h')
|
||||
.char('2')
|
||||
.char('k')
|
||||
.run(&mut ws)
|
||||
.expect("failed to handle event sequence");
|
||||
assert_eq!(ws.book.location, Address { row: 1, col: 1 });
|
||||
assert_eq!(ws.book.location, Address { sheet: 0, row: 1, col: 1 });
|
||||
}
|
||||
|
||||
macro_rules! assert_copy_paste {
|
||||
@ -929,8 +930,8 @@ fn test_command_mode_enter() {
|
||||
fn test_edit_mode_paste() {
|
||||
let mut ws = new_workspace();
|
||||
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
||||
ws.state.range_select.start = Some(Address { row: 1, col: 1 });
|
||||
ws.state.range_select.end = Some(Address { row: 2, col: 2 });
|
||||
ws.state.range_select.start = Some(Address { sheet: 0, row: 1, col: 1 });
|
||||
ws.state.range_select.end = Some(Address { sheet: 0, row: 2, col: 2 });
|
||||
script()
|
||||
.char('e')
|
||||
.ctrl('p')
|
||||
@ -978,8 +979,8 @@ macro_rules! assert_range_clear {
|
||||
($script : expr) => {{
|
||||
let mut ws = new_workspace();
|
||||
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
||||
let first_corner = Address { row: 1, col: 1 };
|
||||
let second_corner = Address { row: 2, col: 2 };
|
||||
let first_corner = Address { sheet: 0, row: 1, col: 1 };
|
||||
let second_corner = Address { sheet: 0, row: 2, col: 2 };
|
||||
ws.book
|
||||
.update_cell(&first_corner, "foo")
|
||||
.expect("Failed to update cell");
|
||||
@ -1049,18 +1050,17 @@ fn test_range_select_movement() {
|
||||
.char('k')
|
||||
.run(&mut ws)
|
||||
.expect("failed to run script");
|
||||
assert_eq!(&Address { row: 3, col: 3 }, &ws.book.location);
|
||||
assert_eq!(0, ws.book.current_sheet);
|
||||
assert_eq!(&Address { sheet: 0, row: 3, col: 3 }, &ws.book.location);
|
||||
script()
|
||||
.ctrl('n')
|
||||
.run(&mut ws)
|
||||
.expect("Unable to run script");
|
||||
assert_eq!(1, ws.book.current_sheet);
|
||||
assert_eq!(1, ws.book.location.sheet);
|
||||
script()
|
||||
.ctrl('p')
|
||||
.run(&mut ws)
|
||||
.expect("Unable to run script");
|
||||
assert_eq!(0, ws.book.current_sheet);
|
||||
assert_eq!(0, ws.book.location.sheet);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1071,8 +1071,8 @@ fn test_range_select_clear_lower_d() {
|
||||
macro_rules! assert_range_copy {
|
||||
($script: expr) => {{
|
||||
let mut ws = new_workspace();
|
||||
let top_left_addr = Address { row: 2, col: 2 };
|
||||
let bot_right_addr = Address { row: 4, col: 4 };
|
||||
let top_left_addr = Address { sheet: 0, row: 2, col: 2 };
|
||||
let bot_right_addr = Address { sheet: 0, row: 4, col: 4 };
|
||||
ws.book
|
||||
.update_cell(&top_left_addr, "top_left")
|
||||
.expect("Failed to update top left");
|
||||
@ -1111,20 +1111,13 @@ macro_rules! assert_range_copy {
|
||||
.expect("Didn't find a start of range")
|
||||
);
|
||||
assert_eq!(
|
||||
&Address { row: 1, col: 1 },
|
||||
&Address { sheet: 0, row: 1, col: 1 },
|
||||
ws.state
|
||||
.range_select
|
||||
.original_location
|
||||
.as_ref()
|
||||
.expect("Expected an original location")
|
||||
);
|
||||
assert_eq!(
|
||||
0,
|
||||
ws.state
|
||||
.range_select
|
||||
.original_sheet
|
||||
.expect("Expected an original sheet")
|
||||
);
|
||||
assert_eq!(
|
||||
Some(&Modality::RangeSelect),
|
||||
ws.state.modality_stack.iter().last()
|
||||
@ -1186,7 +1179,7 @@ fn test_extend_to_range() {
|
||||
.expect("Unable to run script");
|
||||
let extended_cell = ws
|
||||
.book
|
||||
.get_cell_addr_contents(&Address { row: 2, col: 1 })
|
||||
.get_cell_addr_contents(&Address { sheet: 0, row: 2, col: 1 })
|
||||
.expect("Failed to get cell contents");
|
||||
assert_eq!("=B2+1".to_string(), extended_cell);
|
||||
}
|
||||
@ -1206,7 +1199,7 @@ fn test_color_cells() {
|
||||
for ci in 1..=3 {
|
||||
let style = ws
|
||||
.book
|
||||
.get_cell_style(ws.book.current_sheet, &Address { row: ri, col: ci })
|
||||
.get_cell_style(&Address { sheet: ws.book.location.sheet, row: ri, col: ci })
|
||||
.expect("failed to get style");
|
||||
assert_eq!(
|
||||
"#800000",
|
||||
@ -1232,13 +1225,7 @@ fn test_color_row() {
|
||||
for ci in [1, book::LAST_COLUMN] {
|
||||
let style = ws
|
||||
.book
|
||||
.get_cell_style(
|
||||
ws.book.current_sheet,
|
||||
&Address {
|
||||
row: 1,
|
||||
col: ci as usize,
|
||||
},
|
||||
)
|
||||
.get_cell_style(&Address { sheet: ws.book.location.sheet, row: 1, col: ci as usize })
|
||||
.expect("failed to get style");
|
||||
assert_eq!(
|
||||
"#800000",
|
||||
@ -1263,13 +1250,7 @@ fn test_color_col() {
|
||||
for ri in [1, book::LAST_ROW] {
|
||||
let style = ws
|
||||
.book
|
||||
.get_cell_style(
|
||||
ws.book.current_sheet,
|
||||
&Address {
|
||||
row: ri as usize,
|
||||
col: 1,
|
||||
},
|
||||
)
|
||||
.get_cell_style(&Address { sheet: ws.book.location.sheet, row: ri as usize, col: 1 })
|
||||
.expect("failed to get style");
|
||||
assert_eq!(
|
||||
"#800000",
|
||||
@ -1287,7 +1268,7 @@ fn test_bold_text() {
|
||||
let mut ws = new_workspace();
|
||||
let before_style = ws
|
||||
.book
|
||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||
.get_cell_style(&Address { sheet: 0, row: 1, col: 1 })
|
||||
.expect("Failed to get style");
|
||||
assert!(!before_style.font.b);
|
||||
script()
|
||||
@ -1296,7 +1277,7 @@ fn test_bold_text() {
|
||||
.expect("Unable to run script");
|
||||
let style = ws
|
||||
.book
|
||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||
.get_cell_style(&Address { sheet: 0, row: 1, col: 1 })
|
||||
.expect("Failed to get style");
|
||||
assert!(style.font.b);
|
||||
script()
|
||||
@ -1311,7 +1292,7 @@ fn test_italic_text() {
|
||||
let mut ws = new_workspace();
|
||||
let before_style = ws
|
||||
.book
|
||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||
.get_cell_style(&Address { sheet: 0, row: 1, col: 1 })
|
||||
.expect("Failed to get style");
|
||||
assert!(!before_style.font.i);
|
||||
script()
|
||||
@ -1320,7 +1301,7 @@ fn test_italic_text() {
|
||||
.expect("Unable to run script");
|
||||
let style = ws
|
||||
.book
|
||||
.get_cell_style(0, &Address { row: 1, col: 1 })
|
||||
.get_cell_style(&Address { sheet: 0, row: 1, col: 1 })
|
||||
.expect("Failed to get style");
|
||||
assert!(style.font.i);
|
||||
script()
|
||||
|
Loading…
x
Reference in New Issue
Block a user