wip: more ui fixes

This commit is contained in:
Jeremy Wall 2024-11-16 20:25:35 -05:00
parent c55b1cdac3
commit 285e614aec
3 changed files with 25 additions and 33 deletions

View File

@ -128,17 +128,17 @@ impl Book {
} }
pub fn insert_rows(&mut self, row_idx: usize, count: usize) -> Result<()> { pub fn insert_rows(&mut self, row_idx: usize, count: usize) -> Result<()> {
Ok(self self.model
.model
.insert_rows(self.current_sheet, row_idx as i32, count as i32) .insert_rows(self.current_sheet, row_idx as i32, count as i32)
.map_err(|e| anyhow!("Unable to insert row(s): {}", e))?) .map_err(|e| anyhow!("Unable to insert row(s): {}", e))?;
Ok(())
} }
pub fn insert_columns(&mut self, col_idx: usize, count: usize) -> Result<()> { pub fn insert_columns(&mut self, col_idx: usize, count: usize) -> Result<()> {
Ok(self self.model
.model
.insert_columns(self.current_sheet, col_idx as i32, count as i32) .insert_columns(self.current_sheet, col_idx as i32, count as i32)
.map_err(|e| anyhow!("Unable to insert column(s): {}", e))?) .map_err(|e| anyhow!("Unable to insert column(s): {}", e))?;
Ok(())
} }
/// Get the current sheets dimensions. This is a somewhat expensive calculation. /// Get the current sheets dimensions. This is a somewhat expensive calculation.
@ -152,9 +152,9 @@ impl Book {
let mut row_count = 0 as i32; let mut row_count = 0 as i32;
let mut col_count = 0 as i32; let mut col_count = 0 as i32;
for (ri, cols) in sheet.iter() { for (ri, cols) in sheet.iter() {
row_count = max(*ri, row_count); row_count = max(*ri, row_count);
for (ci, _) in cols.iter() { for (ci, _) in cols.iter() {
col_count = max(*ci, col_count); col_count = max(*ci, col_count);
} }
} }
Ok((row_count as usize, col_count as usize)) Ok((row_count as usize, col_count as usize))
@ -206,6 +206,8 @@ impl Book {
impl Default for Book { impl Default for Book {
fn default() -> Self { fn default() -> Self {
Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap()) let mut book = Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap());
book.update_entry(&Address{row: 1, col: 1}, "").unwrap();
book
} }
} }

View File

@ -14,7 +14,7 @@ fn test_book_default() {
max_column, max_column,
} = book.get_dimensions().expect("couldn't get dimensions"); } = book.get_dimensions().expect("couldn't get dimensions");
assert_eq!((1, 1, 1, 1), (min_row, max_row, min_column, max_column)); assert_eq!((1, 1, 1, 1), (min_row, max_row, min_column, max_column));
assert_eq!((0, 0), book.get_size().expect("Failed to get size")); assert_eq!((1, 1), book.get_size().expect("Failed to get size"));
let cell = book let cell = book
.get_current_cell_contents() .get_current_cell_contents()
.expect("couldn't get contents"); .expect("couldn't get contents");
@ -30,36 +30,28 @@ fn test_book_default() {
.expect("couldn't get contents"); .expect("couldn't get contents");
assert_eq!("1", cell); assert_eq!("1", cell);
let sheets = book.get_all_sheets_identifiers(); let sheets = book.get_all_sheets_identifiers();
dbg!(&sheets);
assert_eq!(1, sheets.len()); assert_eq!(1, sheets.len());
} }
#[test]
fn test_insert_rows() {
let mut book = Book::default();
}
#[test] #[test]
fn test_book_insert_cell_new_row() { fn test_book_insert_cell_new_row() {
let mut book = Book::default(); let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 1 }, "1") book.update_entry(&Address { row: 2, col: 1 }, "1")
.expect("failed to edit cell"); .expect("failed to edit cell");
book.evaluate(); book.evaluate();
dbg!(book.get_sheet().expect("Failed to get sheet"));
let WorksheetDimension { let WorksheetDimension {
min_row, min_row,
max_row, max_row,
min_column, min_column,
max_column, max_column,
} = book.get_dimensions().expect("couldn't get dimensions"); } = book.get_dimensions().expect("couldn't get dimensions");
assert_eq!((2, 2, 1, 1), (min_row, max_row, min_column, max_column)); assert_eq!((1, 2, 1, 1), (min_row, max_row, min_column, max_column));
assert_eq!((2, 1), book.get_size().expect("Failed to get size")); assert_eq!((2, 1), book.get_size().expect("Failed to get size"));
} }
#[test] #[test]
fn test_book_insert_cell_new_column() { fn test_book_insert_cell_new_column() {
let mut book = Book::default(); let mut book = Book::default();
book.insert_columns(1, 1).expect("couldn't insert rows");
book.update_entry(&Address { row: 1, col: 2 }, "1") book.update_entry(&Address { row: 1, col: 2 }, "1")
.expect("failed to edit cell"); .expect("failed to edit cell");
let WorksheetDimension { let WorksheetDimension {
@ -68,6 +60,6 @@ fn test_book_insert_cell_new_column() {
min_column, min_column,
max_column, max_column,
} = book.get_dimensions().expect("couldn't get dimensions"); } = book.get_dimensions().expect("couldn't get dimensions");
assert_eq!((1, 1, 2, 2), (min_row, max_row, min_column, max_column)); assert_eq!((1, 1, 1, 2), (min_row, max_row, min_column, max_column));
assert_eq!((1, 2), book.get_size().expect("Failed to get size")); assert_eq!((1, 2), book.get_size().expect("Failed to get size"));
} }

View File

@ -196,19 +196,19 @@ impl<'ws> Workspace<'ws> {
self.save_file()?; self.save_file()?;
} }
KeyCode::Char('r') if key.modifiers == KeyModifiers::CONTROL => { KeyCode::Char('r') if key.modifiers == KeyModifiers::CONTROL => {
let WorksheetDimension { min_row: _, max_row, min_column: _, max_column: _ } = self.book.get_dimensions()?; let (row_count, _) = self.book.get_size()?;
self.book.insert_rows(max_row as usize, 1)?; self.book.update_entry(&Address {row: row_count+1, col: 1 }, "")?;
let WorksheetDimension { min_row: _, max_row, min_column: _, max_column: _ } = self.book.get_dimensions()?; let (row, _) = self.book.get_size()?;
let mut loc = self.book.location.clone(); let mut loc = self.book.location.clone();
if loc.row < max_row as usize { if loc.row < row as usize {
loc.row = (max_row - 1) as usize; loc.row = row as usize;
self.book.move_to(loc)?; self.book.move_to(loc)?;
} }
self.handle_movement_change(); self.handle_movement_change();
} }
KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => { KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => {
let WorksheetDimension { min_row: _, max_row: _, min_column: _, max_column} = self.book.get_dimensions()?; let (_, col_count) = self.book.get_size()?;
self.book.insert_columns(max_column as usize, 1)?; self.book.update_entry(&Address {row: 1, col: col_count+1 }, "")?;
} }
KeyCode::Char('q') => { KeyCode::Char('q') => {
return Ok(Some(ExitCode::SUCCESS)); return Ok(Some(ExitCode::SUCCESS));
@ -318,17 +318,16 @@ const COLNAMES: [&'static str; 26] = [
"T", "U", "V", "W", "X", "Y", "Z", "T", "U", "V", "W", "X", "Y", "Z",
]; ];
// TODO(jwall): Maybe this should be TryFrom?
impl<'t, 'book: 't> TryFrom<&'book Book> for Table<'t> { impl<'t, 'book: 't> TryFrom<&'book Book> for Table<'t> {
fn try_from(value: &'book Book) -> std::result::Result<Self, Self::Error> { fn try_from(value: &'book Book) -> std::result::Result<Self, Self::Error> {
// TODO(zaphar): This is apparently expensive. Maybe we can cache it somehow? // TODO(zaphar): This is apparently expensive. Maybe we can cache it somehow?
// We should do the correct thing here if this fails // We should do the correct thing here if this fails
let WorksheetDimension { min_row, max_row, min_column, max_column } = value.get_dimensions()?; let (row_count, col_count) = value.get_size()?;
let (row_count, col_count) = ((max_row - min_row) as usize, (max_column - min_column) as usize);
let rows: Vec<Row> = (1..=row_count) let rows: Vec<Row> = (1..=row_count)
.into_iter() .into_iter()
.map(|ri| { .map(|ri| {
let cells: Vec<Cell> = (1..=col_count) let mut cells = vec![Cell::new(Text::from(ri.to_string()))];
cells.extend((1..=col_count)
.into_iter() .into_iter()
.map(|ci| { .map(|ci| {
// TODO(zaphar): Is this safe? // TODO(zaphar): Is this safe?
@ -349,8 +348,7 @@ impl<'t, 'book: 't> TryFrom<&'book Book> for Table<'t> {
}), }),
} }
.bold() .bold()
}) }));
.collect();
Row::new(cells) Row::new(cells)
}) })
.collect(); .collect();