feat: new-sheet command

This commit is contained in:
Jeremy Wall 2024-11-29 09:44:53 -05:00
parent f0646d1d63
commit b2034e7f2b
3 changed files with 38 additions and 0 deletions

View File

@ -83,6 +83,14 @@ impl Book {
Ok(())
}
pub fn new_sheet(&mut self, sheet_name: Option<&str>) -> Result<()> {
let (_, idx) = self.model.new_sheet();
if let Some(name) = sheet_name {
self.set_sheet_name(idx as usize, name)?;
}
Ok(())
}
/// Get the sheet data for the current worksheet.
pub fn get_sheet_data(&self) -> Result<&SheetData> {
Ok(&self.get_sheet()?.sheet_data)

View File

@ -8,6 +8,7 @@ pub enum Cmd<'a> {
InsertRow(usize),
InsertColumns(usize),
RenameSheet(Option<usize>, &'a str),
NewSheet(Option<&'a str>),
Edit(&'a str),
Help(Option<&'a str>),
Quit,
@ -20,6 +21,9 @@ pub fn parse<'cmd, 'i: 'cmd>(input: &'i str) -> Result<Option<Cmd<'cmd>>, &'stat
if let Some(cmd) = try_consume_write(cursor.clone())? {
return Ok(Some(cmd));
}
if let Some(cmd) = try_consume_new_sheet(cursor.clone())? {
return Ok(Some(cmd));
}
// try consume insert-row command.
if let Some(cmd) = try_consume_insert_row(cursor.clone())? {
return Ok(Some(cmd));
@ -89,6 +93,28 @@ fn try_consume_write<'cmd, 'i: 'cmd>(
})));
}
fn try_consume_new_sheet<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>,
) -> Result<Option<Cmd<'cmd>>, &'static str> {
const LONG: &'static str = "new-sheet";
if compare(input.clone(), LONG) {
input.seek(LONG.len());
} else {
return Ok(None);
}
if input.remaining() > 0 && !is_ws(&mut input) {
return Err("Invalid command: Did you mean to type `write <arg>`?");
}
let arg = input.span(0..).trim();
return Ok(Some(Cmd::NewSheet(if arg.is_empty() {
None
} else {
Some(arg)
})));
}
fn try_consume_insert_row<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>,
) -> Result<Option<Cmd<'cmd>>, &'static str> {

View File

@ -289,6 +289,10 @@ impl<'ws> Workspace<'ws> {
}
Ok(true)
}
Ok(Some(Cmd::NewSheet(name))) => {
self.book.new_sheet(name)?;
Ok(true)
}
Ok(Some(Cmd::Quit)) => {
// TODO(zaphar): We probably need to do better than this
std::process::exit(0);