diff --git a/docs/index.md b/docs/index.md index c4fcc75..dc9762d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -44,6 +44,8 @@ table and between the sheets using the following keybinds: * `l` and, ➡️ will move one cell to the right. * `j`, ⬇️, and `Enter` will move one cell down. * `k` ⬆️, will move one cell up. +* `d` will delete the contents of the selected cell leaving style untouched +* `D` will delete the contents of the selected cell including any style **Sheet Navigation** @@ -129,12 +131,14 @@ will be discarded if you have not saved first. ### Range Select Mode -Range Select mode copies a range reference for use later. You can enter range +Range Select mode copies a range reference for use later or delete a range's contents. You can enter range select mode from CellEdit mode with `CTRL-r`. * `h`, `j`, `k`, `l` will navigate around the sheet. * `Ctrl-n`, `Ctrl-p` will navigate between sheets. * `The spacebar will select the start and end of the range respectively. +* `d` will delete the contents of the range leaving any style untouched +* `D` will delete the contents of the range including any style When you have selected the end of the range you will exit range select mode and the range reference will be placed into the cell contents you are editing. diff --git a/src/book/mod.rs b/src/book/mod.rs index 8602ea2..6c00a4f 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -104,6 +104,48 @@ impl Book { Ok(()) } + pub fn clear_current_cell(&mut self) -> Result<()> { + self.clear_cell_contents(self.current_sheet as u32, self.location.clone()) + } + + pub fn clear_current_cell_all(&mut self) -> Result<()> { + 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<()> { + Ok(self + .model + .cell_clear_contents(sheet, row as i32, col as i32) + .map_err(|s| anyhow!("Unable to clear cell contents {}", s))?) + } + + pub fn clear_cell_range(&mut self, sheet: u32, start: Address, end: Address) -> Result<()> { + for row in start.row..=end.row { + for col in start.col..=end.col { + self.clear_cell_contents(sheet, Address { row, col })?; + } + } + Ok(()) + } + + pub fn clear_cell_all(&mut self, sheet: u32, Address { row, col, }: Address) -> Result<()> { + Ok(self + .model + .cell_clear_all(sheet, row as i32, col as i32) + .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<()> { + for row in start.row..=end.row { + for col in start.col..=end.col { + self.clear_cell_all(sheet, Address { row, col })?; + } + } + Ok(()) + } + + /// Get a cells formatted content. pub fn get_current_cell_rendered(&self) -> Result { Ok(self.get_cell_addr_rendered(&self.location)?) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 4536687..9975c94 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -201,7 +201,11 @@ impl<'ws> Workspace<'ws> { pub fn selected_range_to_string(&self) -> String { let state = &self.state; if let Some((start, end)) = state.range_select.get_range() { - let a1 = format!("{}{}", start.to_range_part(), format!(":{}", end.to_range_part())); + let a1 = format!( + "{}{}", + 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 { return format!( @@ -215,7 +219,7 @@ impl<'ws> Workspace<'ws> { } return a1; } - return String::new() + return String::new(); } /// Move a row down in the current sheet. @@ -281,6 +285,8 @@ impl<'ws> Workspace<'ws> { "* ENTER/RETURN: Go down one cell".to_string(), "* TAB: Go over one cell".to_string(), "* h,j,k,l: vim style navigation".to_string(), + "* d: clear cell contents leaving style untouched".to_string(), + "* D: clear cell contents including style".to_string(), "* CTRl-r: Add a row".to_string(), "* CTRl-c: Add a column".to_string(), "* CTRl-l: Grow column width by 1".to_string(), @@ -308,6 +314,8 @@ impl<'ws> Workspace<'ws> { "Range Selection Mode:".to_string(), "* ESC: Exit command mode".to_string(), "* h,j,k,l: vim style navigation".to_string(), + "* d: delete the contents of the range leaving style untouched".to_string(), + "* D: clear cell contents including style".to_string(), "* Spacebar: Select start and end of range".to_string(), "* CTRl-n: Next sheet. Starts over at beginning if at end.".to_string(), "* CTRl-p: Previous sheet. Starts over at end if at beginning.".to_string(), @@ -471,6 +479,30 @@ impl<'ws> Workspace<'ws> { KeyCode::Char(d) if d.is_ascii_digit() => { self.handle_numeric_prefix(d); } + KeyCode::Char('D') => { + 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, + )?; + } + } + 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, + )?; + } + } KeyCode::Char('h') => { self.run_with_prefix(|ws: &mut Workspace<'_>| -> Result<()> { ws.move_left()?; @@ -567,6 +599,12 @@ impl<'ws> Workspace<'ws> { Ok(()) })?; } + KeyCode::Char('d') => { + self.book.clear_current_cell()?; + } + KeyCode::Char('D') => { + self.book.clear_current_cell_all()?; + } KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => { self.run_with_prefix(|ws: &mut Workspace<'_>| -> Result<()> { ws.book.select_prev_sheet();