diff --git a/src/book/mod.rs b/src/book/mod.rs index 796c81a..a691930 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -131,6 +131,12 @@ impl Book { self.model .insert_rows(self.current_sheet, row_idx as i32, count as i32) .map_err(|e| anyhow!("Unable to insert row(s): {}", e))?; + if self.location.row >= row_idx { + self.move_to(Address { + row: self.location.row + count, + col: self.location.col, + })?; + } Ok(()) } @@ -138,6 +144,12 @@ impl Book { self.model .insert_columns(self.current_sheet, col_idx as i32, count as i32) .map_err(|e| anyhow!("Unable to insert column(s): {}", e))?; + if self.location.col >= col_idx { + self.move_to(Address { + row: self.location.row, + col: self.location.col + count, + })?; + } Ok(()) } @@ -206,8 +218,9 @@ impl Book { impl Default for Book { fn default() -> Self { - let mut book = Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap()); - book.update_entry(&Address{row: 1, col: 1}, "").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 } } diff --git a/src/book/test.rs b/src/book/test.rs index 4cc3015..a750502 100644 --- a/src/book/test.rs +++ b/src/book/test.rs @@ -63,3 +63,29 @@ fn test_book_insert_cell_new_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")); } + +#[test] +fn test_book_insert_rows() { + let mut book = Book::default(); + book.update_entry(&Address { row: 2, col: 2 }, "1") + .expect("failed to edit cell"); + book.move_to(Address { 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!("1", book.get_current_cell_rendered().expect("Failed to get rendered content")); +} + +#[test] +fn test_book_insert_columns() { + let mut book = Book::default(); + book.update_entry(&Address { row: 2, col: 2 }, "1") + .expect("failed to edit cell"); + book.move_to(Address { 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!("1", book.get_current_cell_rendered().expect("Failed to get rendered content")); +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e5067cc..9044a02 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -6,7 +6,6 @@ use crate::book::Book; use anyhow::Result; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; -use ironcalc::base::worksheet::WorksheetDimension; use ratatui::{ self, layout::{Constraint, Flex, Layout}, @@ -81,14 +80,13 @@ impl<'ws> Workspace<'ws> { } else { Book::default() }; - //book.move_to(Address { row: 0, col: 0 })?; Ok(Workspace::new(book, path.clone())) } pub fn move_down(&mut self) -> Result<()> { let mut loc = self.book.location.clone(); - let WorksheetDimension { min_row: _, max_row, min_column: _, max_column: _ } = self.book.get_dimensions()?; - if loc.row <= max_row as usize { + let (row_count, _) = self.book.get_size()?; + if loc.row < row_count { loc.row += 1; self.book.move_to(loc)?; } @@ -97,8 +95,7 @@ impl<'ws> Workspace<'ws> { pub fn move_up(&mut self) -> Result<()> { let mut loc = self.book.location.clone(); - let WorksheetDimension { min_row, max_row: _, min_column: _, max_column: _ } = self.book.get_dimensions()?; - if loc.row > min_row as usize { + if loc.row > 1 { loc.row -= 1; self.book.move_to(loc)?; } @@ -107,8 +104,7 @@ impl<'ws> Workspace<'ws> { pub fn move_left(&mut self) -> Result<()> { let mut loc = self.book.location.clone(); - let WorksheetDimension { min_row: _, max_row: _, min_column, max_column: _ } = self.book.get_dimensions()?; - if loc.col > min_column as usize { + if loc.col > 1 { loc.col -= 1; self.book.move_to(loc)?; } @@ -117,8 +113,8 @@ impl<'ws> Workspace<'ws> { pub fn move_right(&mut self) -> Result<()> { let mut loc = self.book.location.clone(); - let WorksheetDimension { min_row: _, max_row: _, min_column: _, max_column} = self.book.get_dimensions()?; - if loc.col < max_column as usize { + let (_, col_count) = self.book.get_size()?; + if loc.col < col_count { loc.col += 1; self.book.move_to(loc)?; } @@ -206,7 +202,7 @@ impl<'ws> Workspace<'ws> { } self.handle_movement_change(); } - KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => { + KeyCode::Char('t') if key.modifiers == KeyModifiers::CONTROL => { let (_, col_count) = self.book.get_size()?; self.book.update_entry(&Address {row: 1, col: col_count+1 }, "")?; }