wip: cmd: Add a command for epxorting csv

This commit is contained in:
Jeremy Wall 2025-03-04 19:33:33 -05:00
parent 8cd93cb6b0
commit 4dd0365bdd
3 changed files with 43 additions and 2 deletions

View File

@ -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<Option<Cmd<'cmd>>, &'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 <arg>`?");
return Err("Invalid command: Did you mean to type `write <path>`?");
}
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<Option<Cmd<'cmd>>, &'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 <path>`?");
}
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<Option<Cmd<'cmd>>, &'static str> {

View File

@ -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();

View File

@ -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";