wip: color styling commands for rows and cols

This commit is contained in:
Jeremy Wall 2025-01-25 11:19:02 -05:00
parent 8f07e8c0e7
commit d52bf70cc3
2 changed files with 94 additions and 3 deletions

View File

@ -7,8 +7,8 @@ pub enum Cmd<'a> {
Write(Option<&'a str>),
InsertRows(usize),
InsertColumns(usize),
ColorRows(usize, &'a str),
ColorColumns(usize, &'a str),
ColorRows(Option<usize>, &'a str),
ColorColumns(Option<usize>, &'a str),
ColorCell(&'a str),
RenameSheet(Option<usize>, &'a str),
NewSheet(Option<&'a str>),
@ -54,6 +54,12 @@ pub fn parse<'cmd, 'i: 'cmd>(input: &'i str) -> Result<Option<Cmd<'cmd>>, &'stat
if let Some(cmd) = try_consume_rename_sheet(cursor.clone())? {
return Ok(Some(cmd));
}
if let Some(cmd) = try_consume_color_rows(cursor.clone())? {
return Ok(Some(cmd));
}
if let Some(cmd) = try_consume_color_columns(cursor.clone())? {
return Ok(Some(cmd));
}
if let Some(cmd) = try_consume_color_cell(cursor.clone())? {
return Ok(Some(cmd));
}
@ -306,11 +312,51 @@ fn try_consume_rename_sheet<'cmd, 'i: 'cmd>(
let (idx, rest) = try_consume_usize(input.clone());
let arg = rest.span(0..).trim();
if arg.is_empty() {
return Err("Invalid command: `rename-sheet` requires a sheet name argument?");
return Err("Invalid command: `rename-sheet` requires a sheet name argument");
}
return Ok(Some(Cmd::RenameSheet(idx, arg)));
}
fn try_consume_color_rows<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>,
) -> Result<Option<Cmd<'cmd>>, &'static str> {
const LONG: &'static str = "color-rows";
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 `color-rows [count] <color>`?");
}
let (idx, rest) = try_consume_usize(input.clone());
let arg = rest.span(0..).trim();
if arg.is_empty() {
return Err("Invalid command: `color-rows` requires a color argument");
}
return Ok(Some(Cmd::ColorRows(idx, arg)));
}
fn try_consume_color_columns<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>,
) -> Result<Option<Cmd<'cmd>>, &'static str> {
const LONG: &'static str = "color-columns";
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 `color-columns [count] <color>`?");
}
let (idx, rest) = try_consume_usize(input.clone());
let arg = rest.span(0..).trim();
if arg.is_empty() {
return Err("Invalid command: `color-columns` requires a color argument");
}
return Ok(Some(Cmd::ColorColumns(idx, arg)));
}
fn try_consume_usize<'cmd, 'i: 'cmd>(
mut input: StrCursor<'i>,
) -> (Option<usize>, StrCursor<'i>) {

View File

@ -259,6 +259,51 @@ fn test_cmd_rename_sheet_with_idx_and_name() {
assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test"));
}
#[test]
fn test_cmd_color_rows_with_color() {
let input = "color-rows red";
let result = parse(input);
assert!(result.is_ok());
let output = result.unwrap();
assert!(output.is_some());
let cmd = output.unwrap();
assert_eq!(cmd, Cmd::ColorRows(None, "red"));
}
#[test]
fn test_cmd_color_rows_with_idx_and_color() {
let input = "color-rows 1 red";
let result = parse(input);
assert!(result.is_ok());
let output = result.unwrap();
assert!(output.is_some());
let cmd = output.unwrap();
assert_eq!(cmd, Cmd::ColorRows(Some(1), "red"));
}
#[test]
fn test_cmd_color_columns_with_color() {
let input = "color-columns red";
let result = parse(input);
assert!(result.is_ok());
let output = result.unwrap();
assert!(output.is_some());
let cmd = output.unwrap();
assert_eq!(cmd, Cmd::ColorColumns(None, "red"));
}
#[test]
fn test_cmd_color_columns_with_idx_and_color() {
let input = "color-columns 1 red";
let result = parse(input);
assert!(result.is_ok());
let output = result.unwrap();
assert!(output.is_some());
let cmd = output.unwrap();
assert_eq!(cmd, Cmd::ColorColumns(Some(1), "red"));
}
#[test]
fn test_input_navitation_enter_key() {
let mut ws = new_workspace();