chore: refator InputScript api for ergonomics

This commit is contained in:
Jeremy Wall 2024-12-29 19:09:42 -05:00
parent e984d7324c
commit 247674530b

View File

@ -200,19 +200,31 @@ pub struct InputScript{
} }
impl InputScript { impl InputScript {
pub fn add_char(self, c: char) -> Self { pub fn char(self, c: char) -> Self {
self.add_event(construct_key_event(KeyCode::Char(c))) self.event(construct_key_event(KeyCode::Char(c)))
} }
pub fn add_modified_char(self, c: char, mods: KeyModifiers) -> Self { pub fn ctrl(self, c: char) -> Self {
self.add_event(construct_modified_key_event(KeyCode::Char(c), mods)) self.modified_char(c, KeyModifiers::CONTROL)
} }
pub fn add_event(mut self, evt: Event) -> Self { pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self {
self.event(construct_modified_key_event(KeyCode::Char(c), mods))
}
pub fn event(mut self, evt: Event) -> Self {
self.events.push(evt); self.events.push(evt);
self self
} }
pub fn enter(self) -> Self {
self.event(construct_key_event(KeyCode::Enter))
}
pub fn esc(self) -> Self {
self.event(construct_key_event(KeyCode::Esc))
}
pub fn run(self, ws: &mut Workspace) -> anyhow::Result<Option<ExitCode>> { pub fn run(self, ws: &mut Workspace) -> anyhow::Result<Option<ExitCode>> {
for evt in self.events { for evt in self.events {
if let Some(e) = ws.handle_input(evt)? { if let Some(e) = ws.handle_input(evt)? {
@ -354,9 +366,9 @@ fn test_navigation_numeric_prefix()
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2");
ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3");
InputScript::default() InputScript::default()
.add_char('2') .char('2')
.add_char('3') .char('3')
.add_char('9') .char('9')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run script"); .expect("Failed to run script");
assert_eq!(239, ws.state.get_n_prefix()); assert_eq!(239, ws.state.get_n_prefix());
@ -371,10 +383,10 @@ fn test_navigation_numeric_prefix_cancel()
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2");
ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3");
InputScript::default() InputScript::default()
.add_char('2') .char('2')
.add_char('3') .char('3')
.add_char('9') .char('9')
.add_event(construct_key_event(KeyCode::Esc)) .esc()
.run(&mut ws) .run(&mut ws)
.expect("Failed to run script"); .expect("Failed to run script");
assert_eq!(1, ws.state.get_n_prefix()); assert_eq!(1, ws.state.get_n_prefix());
@ -436,18 +448,18 @@ fn test_range_copy() {
let original_loc_2 = ws.book.location.clone(); let original_loc_2 = ws.book.location.clone();
assert_eq!(Address { row: 5, col: 5 }, original_loc_2); assert_eq!(Address { row: 5, col: 5 }, original_loc_2);
ws.handle_input(construct_modified_key_event(KeyCode::Char('r'), KeyModifiers::CONTROL)) InputScript::default().char('v').run(&mut ws)
.expect("Failed to handle 'Ctrl-r' key event"); .expect("Failed to handle 'v' key event");
assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last());
assert_eq!(Some(original_loc_2.clone()), ws.state.range_select.original_location); assert_eq!(Some(original_loc_2.clone()), ws.state.range_select.original_location);
assert!(ws.state.range_select.start.is_none()); assert!(ws.state.range_select.start.is_some());
assert!(ws.state.range_select.end.is_none()); assert!(ws.state.range_select.end.is_none());
ws.handle_input(construct_key_event(KeyCode::Char('h'))) ws.handle_input(construct_key_event(KeyCode::Char('h')))
.expect("Failed to handle 'h' key event"); .expect("Failed to handle 'h' key event");
ws.handle_input(construct_key_event(KeyCode::Char(' '))) ws.handle_input(construct_key_event(KeyCode::Char(' ')))
.expect("Failed to handle ' ' key event"); .expect("Failed to handle ' ' key event");
assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.start); assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start);
ws.handle_input(construct_key_event(KeyCode::Char('k'))) ws.handle_input(construct_key_event(KeyCode::Char('k')))
.expect("Failed to handle 'k' key event"); .expect("Failed to handle 'k' key event");
@ -455,9 +467,9 @@ fn test_range_copy() {
.expect("Failed to handle ' ' key event"); .expect("Failed to handle ' ' key event");
assert!(ws.state.range_select.original_location.is_none()); assert!(ws.state.range_select.original_location.is_none());
assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.start); assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start);
assert_eq!(Some(Address {row:4, col:4, }), ws.state.range_select.end); assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.end);
assert_eq!(original_loc_2, ws.book.location); assert_eq!(Address {row:4, col:5, }, ws.book.location);
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
} }
@ -480,14 +492,14 @@ fn test_gg_movement() {
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
InputScript::default() InputScript::default()
.add_char('j') .char('j')
.add_char('j').run(&mut ws) .char('j').run(&mut ws)
.expect("failed to handle event sequence"); .expect("failed to handle event sequence");
assert_eq!(ws.book.location, Address { row: 3, col: 1 }); assert_eq!(ws.book.location, Address { row: 3, col: 1 });
InputScript::default() InputScript::default()
.add_char('l') .char('l')
.add_char('g') .char('g')
.add_char('g') .char('g')
.run(&mut ws) .run(&mut ws)
.expect("failed to handle event sequence"); .expect("failed to handle event sequence");
assert_eq!(ws.book.location, Address { row: 1, col: 2 }); assert_eq!(ws.book.location, Address { row: 1, col: 2 });
@ -507,17 +519,17 @@ macro_rules! assert_copy_paste {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
InputScript::default() InputScript::default()
.add_char('j') .char('j')
.add_char('l') .char('l')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run script"); .expect("Failed to run script");
ws.book.edit_current_cell($source).expect("Failed to edit cell"); ws.book.edit_current_cell($source).expect("Failed to edit cell");
ws.book.evaluate(); ws.book.evaluate();
InputScript::default() InputScript::default()
.add_event($c) .event($c)
.add_char('l') .char('l')
.add_char('j') .char('j')
.add_event($p) .event($p)
.run(&mut ws) .run(&mut ws)
.expect("Failed to run script"); .expect("Failed to run script");
let copy = ws.book.get_current_cell_contents() let copy = ws.book.get_current_cell_contents()
@ -573,7 +585,7 @@ fn test_clear_cell() {
ws.book.evaluate(); ws.book.evaluate();
assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents"));
InputScript::default() InputScript::default()
.add_char('d') .char('d')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents"));
@ -588,7 +600,7 @@ fn test_clear_cell_all() {
ws.book.evaluate(); ws.book.evaluate();
assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents"));
InputScript::default() InputScript::default()
.add_char('D') .char('D')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents"));
@ -602,13 +614,13 @@ fn test_sheet_navigation() {
ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name"); ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name");
ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name"); ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name");
InputScript::default() InputScript::default()
.add_modified_char('n', KeyModifiers::CONTROL) .ctrl('n')
.add_modified_char('n', KeyModifiers::CONTROL) .ctrl('n')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!("sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name")); assert_eq!("sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name"));
InputScript::default() InputScript::default()
.add_modified_char('p', KeyModifiers::CONTROL) .ctrl('p')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!("sheet 2", ws.book.get_sheet_name().expect("Failed to get sheet name")); assert_eq!("sheet 2", ws.book.get_sheet_name().expect("Failed to get sheet name"));
@ -619,14 +631,14 @@ fn test_sheet_column_sizing() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
InputScript::default() InputScript::default()
.add_char('3') .char('3')
.add_modified_char('l', KeyModifiers::CONTROL) .ctrl('l')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!(28, ws.book.get_col_size(1).expect("Failed to get column size")); assert_eq!(28, ws.book.get_col_size(1).expect("Failed to get column size"));
InputScript::default() InputScript::default()
.add_char('1') .char('1')
.add_modified_char('h', KeyModifiers::CONTROL) .ctrl('h')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!(27, ws.book.get_col_size(1).expect("Failed to get column size")); assert_eq!(27, ws.book.get_col_size(1).expect("Failed to get column size"));
@ -637,7 +649,7 @@ fn test_quit() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
let result = InputScript::default() let result = InputScript::default()
.add_char('q') .char('q')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert!(result.is_some()); assert!(result.is_some());