diff --git a/src/book/mod.rs b/src/book/mod.rs index e2c2103..13b03da 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -251,6 +251,12 @@ impl Book { } } + pub fn set_cell_style(&mut self, style: &Style, sheet: u32, cell: &Address) -> Result<()> { + self.model.set_cell_style(sheet, cell.row as i32, cell.col as i32, style) + .map_err(|s| anyhow!("Unable to format cell {}", s))?; + Ok(()) + } + /// Get a cells rendered content for display. pub fn get_cell_addr_rendered(&self, Address { row, col }: &Address) -> Result { Ok(self diff --git a/src/ui/cmd.rs b/src/ui/cmd.rs index edaeed3..225878f 100644 --- a/src/ui/cmd.rs +++ b/src/ui/cmd.rs @@ -7,6 +7,9 @@ pub enum Cmd<'a> { Write(Option<&'a str>), InsertRows(usize), InsertColumns(usize), + ColorRows(usize, &'a str), + ColorColumns(usize, &'a str), + ColorCell(&'a str), RenameSheet(Option, &'a str), NewSheet(Option<&'a str>), SelectSheet(&'a str), @@ -51,6 +54,9 @@ pub fn parse<'cmd, 'i: 'cmd>(input: &'i str) -> Result>, &'stat if let Some(cmd) = try_consume_rename_sheet(cursor.clone())? { return Ok(Some(cmd)); } + if let Some(cmd) = try_consume_color_cell(cursor.clone())? { + return Ok(Some(cmd)); + } Ok(None) } @@ -138,6 +144,28 @@ fn try_consume_select_sheet<'cmd, 'i: 'cmd>( return Ok(Some(Cmd::SelectSheet(arg))); } +fn try_consume_color_cell<'cmd, 'i: 'cmd>( + mut input: StrCursor<'i>, +) -> Result>, &'static str> { + const SHORT: &'static str = "cc"; + const LONG: &'static str = "color-cell"; + if compare(input.clone(), LONG) { + input.seek(LONG.len()); + } else if compare(input.clone(), SHORT) { + input.seek(SHORT.len()); + } else { + return Ok(None); + }; + if input.remaining() > 0 && !is_ws(&mut input) { + return Err("Invalid command: Did you mean to type `color-cell `?"); + } + let arg = input.span(0..).trim(); + if arg.len() == 0 { + return Err("Invalid command: Did you mean to type `color-cell `?"); + } + return Ok(Some(Cmd::ColorCell(arg))); +} + fn try_consume_insert_row<'cmd, 'i: 'cmd>( mut input: StrCursor<'i>, ) -> Result>, &'static str> { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 71bfb4b..4967ef5 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -465,6 +465,21 @@ impl<'ws> Workspace<'ws> { Ok(Some(Cmd::Quit)) => { Ok(Some(ExitCode::SUCCESS)) } + Ok(Some(Cmd::ColorRows(_count, _color))) => { + Ok(Some(ExitCode::FAILURE)) + } + Ok(Some(Cmd::ColorColumns(_count, _color))) => { + Ok(Some(ExitCode::FAILURE)) + } + Ok(Some(Cmd::ColorCell(color))) => { + let address = self.book.location.clone(); + let sheet = self.book.current_sheet; + let mut style = self.book.get_cell_style(sheet, &address) + .expect("I think this should be impossible.").clone(); + style.fill.bg_color = Some(color.to_string()); + self.book.set_cell_style(&style, sheet, &address)?; + Ok(None) + } Ok(None) => { self.enter_dialog_mode(vec![format!("Unrecognized commmand {}", cmd_text)]); Ok(None)