wip: update the command syntax to be more future proof

This commit is contained in:
Jeremy Wall 2025-03-04 21:04:29 -05:00
parent 4dd0365bdd
commit 7e4109e734
5 changed files with 10 additions and 12 deletions

View File

@ -12,6 +12,7 @@ The currently supported commands are:
* `new-sheet [name]` Creates a new sheet. If the name is provided then uses that. If omitted then uses a default sheet name. * `new-sheet [name]` Creates a new sheet. If the name is provided then uses that. If omitted then uses a default sheet name.
* `select-sheet <name>` Select a sheet by name. * `select-sheet <name>` Select a sheet by name.
* `edit <path>` Edit a new spreadsheet at the current path. `e` is a shorthand alias for this command. * `edit <path>` Edit a new spreadsheet at the current path. `e` is a shorthand alias for this command.
* `export-csv <path>` Export the current sheet to a csv file at `<path>`.
* `quit` Quits the application. `q` is a shorthand alias for this command. * `quit` Quits the application. `q` is a shorthand alias for this command.
<aside>Note that in the case of `quit` and `edit` that we do not currently <aside>Note that in the case of `quit` and `edit` that we do not currently

View File

@ -120,7 +120,8 @@ impl Book {
} }
pub fn csv_for_sheet<W>(&self, sheet: u32, sink: W) -> Result<()> pub fn csv_for_sheet<W>(&self, sheet: u32, sink: W) -> Result<()>
where W: std::io::Write, where
W: std::io::Write,
{ {
let rows = self.get_export_rows_for_sheet(sheet)?; let rows = self.get_export_rows_for_sheet(sheet)?;
let mut writer = csv::Writer::from_writer(sink); let mut writer = csv::Writer::from_writer(sink);

View File

@ -15,7 +15,7 @@ pub enum Cmd<'a> {
SelectSheet(&'a str), SelectSheet(&'a str),
Edit(&'a str), Edit(&'a str),
Help(Option<&'a str>), Help(Option<&'a str>),
Export(&'a str), ExportCsv(&'a str),
Quit, Quit,
} }
@ -41,7 +41,7 @@ pub fn parse<'cmd, 'i: 'cmd>(input: &'i str) -> Result<Option<Cmd<'cmd>>, &'stat
return Ok(Some(cmd)); return Ok(Some(cmd));
} }
// Try consume export // Try consume export
if let Some(cmd) = try_consume_export(cursor.clone())? { if let Some(cmd) = try_consume_export_csv(cursor.clone())? {
return Ok(Some(cmd)); return Ok(Some(cmd));
} }
// try consume edit command. // try consume edit command.
@ -114,17 +114,13 @@ fn try_consume_write<'cmd, 'i: 'cmd>(
}))); })));
} }
fn try_consume_export<'cmd, 'i: 'cmd>( fn try_consume_export_csv<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>, mut input: StrCursor<'i>,
) -> Result<Option<Cmd<'cmd>>, &'static str> { ) -> Result<Option<Cmd<'cmd>>, &'static str> {
const SHORT: &'static str = "ex"; const LONG: &'static str = "export-csv";
const LONG: &'static str = "export";
if compare(input.clone(), LONG) { if compare(input.clone(), LONG) {
input.seek(LONG.len()); input.seek(LONG.len());
} else if compare(input.clone(), SHORT) {
input.seek(SHORT.len());
// Should we check for whitespace?
} else { } else {
return Ok(None); return Ok(None);
} }
@ -132,7 +128,7 @@ fn try_consume_export<'cmd, 'i: 'cmd>(
return Err("Invalid command: Did you mean to type `export <path>`?"); return Err("Invalid command: Did you mean to type `export <path>`?");
} }
let arg = input.span(0..).trim(); let arg = input.span(0..).trim();
return Ok(Some(Cmd::Export(arg))); return Ok(Some(Cmd::ExportCsv(arg)));
} }
fn try_consume_new_sheet<'cmd, 'i: 'cmd>( fn try_consume_new_sheet<'cmd, 'i: 'cmd>(

View File

@ -425,7 +425,7 @@ impl<'ws> Workspace<'ws> {
} }
Ok(None) Ok(None)
} }
Ok(Some(Cmd::Export(path))) => { Ok(Some(Cmd::ExportCsv(path))) => {
self.book.save_sheet_to_csv(self.book.location.sheet, path)?; self.book.save_sheet_to_csv(self.book.location.sheet, path)?;
Ok(None) Ok(None)
} }

View File

@ -219,7 +219,7 @@ fn test_cmd_new_sheet_with_name() {
#[test] #[test]
fn test_cmd_export() { fn test_cmd_export() {
let input = "export test.csv"; let input = "export-csv test.csv";
let result = parse(input); let result = parse(input);
assert!(result.is_ok()); assert!(result.is_ok());
let output = result.unwrap(); let output = result.unwrap();