diff --git a/src/ui/cmd.rs b/src/ui/cmd.rs index fed68d1..e5c7103 100644 --- a/src/ui/cmd.rs +++ b/src/ui/cmd.rs @@ -15,6 +15,7 @@ pub enum Cmd<'a> { SelectSheet(&'a str), Edit(&'a str), Help(Option<&'a str>), + Export(&'a str), Quit, } @@ -35,10 +36,14 @@ pub fn parse<'cmd, 'i: 'cmd>(input: &'i str) -> Result>, &'stat if let Some(cmd) = try_consume_insert_row(cursor.clone())? { return Ok(Some(cmd)); } - //// try consume insert-col command. + // try consume insert-col command. if let Some(cmd) = try_consume_insert_column(cursor.clone())? { return Ok(Some(cmd)); } + // Try consume export + if let Some(cmd) = try_consume_export(cursor.clone())? { + return Ok(Some(cmd)); + } // try consume edit command. if let Some(cmd) = try_consume_edit(cursor.clone())? { return Ok(Some(cmd)); @@ -99,7 +104,7 @@ fn try_consume_write<'cmd, 'i: 'cmd>( return Ok(None); } if input.remaining() > 0 && !is_ws(&mut input) { - return Err("Invalid command: Did you mean to type `write `?"); + return Err("Invalid command: Did you mean to type `write `?"); } let arg = input.span(0..).trim(); return Ok(Some(Cmd::Write(if arg.is_empty() { @@ -109,6 +114,27 @@ fn try_consume_write<'cmd, 'i: 'cmd>( }))); } +fn try_consume_export<'cmd, 'i: 'cmd>( + mut input: StrCursor<'i>, +) -> Result>, &'static str> { + const SHORT: &'static str = "ex"; + const LONG: &'static str = "export"; + + if compare(input.clone(), LONG) { + input.seek(LONG.len()); + } else if compare(input.clone(), SHORT) { + input.seek(SHORT.len()); + // Should we check for whitespace? + } else { + return Ok(None); + } + if input.remaining() == 0 || !is_ws(&mut input) { + return Err("Invalid command: Did you mean to type `export `?"); + } + let arg = input.span(0..).trim(); + return Ok(Some(Cmd::Export(arg))); +} + fn try_consume_new_sheet<'cmd, 'i: 'cmd>( mut input: StrCursor<'i>, ) -> Result>, &'static str> { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e18da11..ccfda4a 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -425,6 +425,10 @@ impl<'ws> Workspace<'ws> { } Ok(None) } + Ok(Some(Cmd::Export(path))) => { + self.book.save_sheet_to_csv(self.book.location.sheet, path)?; + Ok(None) + } Ok(Some(Cmd::InsertColumns(count))) => { self.book.insert_columns(self.book.location.col, count)?; self.book.evaluate(); diff --git a/src/ui/test.rs b/src/ui/test.rs index b45166a..b86e524 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -217,6 +217,17 @@ fn test_cmd_new_sheet_with_name() { assert_eq!(cmd, Cmd::NewSheet(Some("test"))); } +#[test] +fn test_cmd_export() { + let input = "export test.csv"; + let result = parse(input); + assert!(result.is_ok()); + let output = result.unwrap(); + assert!(output.is_some()); + let cmd = output.unwrap(); + assert_eq!(cmd, Cmd::Export("test.csv")); +} + #[test] fn test_cmd_new_sheet_no_name() { let input = "new-sheet";