1 use { 2 super::*, 3 crate::{ 4 pattern::*, 5 verb::{Internal, VerbInvocation}, 6 }, 7 bet::BeTree, 8 }; 9 10 /// a command which may result in a change in the application state. 11 /// 12 /// It may come from a shortcut, from the parsed input, from an argument 13 /// given on launch. 14 #[derive(Debug, Clone)] 15 pub enum Command { 16 17 /// no command 18 None, 19 20 /// a verb invocation, unfinished 21 /// (user didn't hit enter) 22 VerbEdit(VerbInvocation), 23 24 /// verb invocation, finished 25 /// (coming from --cmd, or after the user hit enter) 26 VerbInvocate(VerbInvocation), 27 28 /// call of an internal done without the input 29 /// (using a trigger key for example) 30 Internal { 31 internal: Internal, 32 input_invocation: Option<VerbInvocation>, 33 }, 34 35 /// call of a verb done without the input 36 /// (using a trigger key for example) 37 VerbTrigger { 38 index: usize, 39 input_invocation: Option<VerbInvocation>, 40 }, 41 42 /// a pattern being edited 43 PatternEdit { 44 raw: String, 45 expr: BeTree<PatternOperator, PatternParts>, 46 }, 47 48 /// a mouse click 49 Click(u16, u16), 50 51 /// a mouse double-click 52 /// Always come after a simple click at same position 53 DoubleClick(u16, u16), 54 } 55 56 impl Command { 57 empty() -> Command58 pub fn empty() -> Command { 59 Command::None 60 } 61 62 /// build a command from the parsed string representation 63 /// 64 /// The command being finished is the difference between 65 /// a command being edited and a command launched (which 66 /// happens on enter in the input). from_parts(mut cp: CommandParts, finished: bool) -> Self67 pub fn from_parts(mut cp: CommandParts, finished: bool) -> Self { 68 if let Some(verb_invocation) = cp.verb_invocation.take() { 69 if finished { 70 Self::VerbInvocate(verb_invocation) 71 } else { 72 Self::VerbEdit(verb_invocation) 73 } 74 } else if finished { 75 Self::Internal { 76 internal: Internal::open_stay, 77 input_invocation: None, 78 } 79 } else { 80 Self::PatternEdit { 81 raw: cp.raw_pattern, 82 expr: cp.pattern, 83 } 84 } 85 } 86 87 /// tells whether this action is a verb being invocated on enter 88 /// in the input field is_verb_invocated_from_input(&self) -> bool89 pub fn is_verb_invocated_from_input(&self) -> bool { 90 matches!(self, Self::VerbInvocate(_)) 91 } 92 93 /// create a command from a raw input. 94 /// 95 /// `finished` makes the command an executed form, 96 /// it's equivalent to using the Enter key in the Gui. from_raw(raw: String, finished: bool) -> Self97 pub fn from_raw(raw: String, finished: bool) -> Self { 98 let parts = CommandParts::from(raw); 99 Self::from_parts(parts, finished) 100 } 101 102 /// build a non executed command from a pattern from_pattern(pattern: &InputPattern) -> Self103 pub fn from_pattern(pattern: &InputPattern) -> Self { 104 Command::from_raw(pattern.raw.clone(), false) 105 } 106 } 107 108 impl Default for Command { default() -> Command109 fn default() -> Command { 110 Command::empty() 111 } 112 } 113