Struct broot::command::CommandParts
source · pub struct CommandParts {
pub raw_pattern: String,
pub pattern: BeTree<PatternOperator, PatternParts>,
pub verb_invocation: Option<VerbInvocation>,
}
Expand description
An intermediate parsed representation of the raw string
Fields§
§raw_pattern: String
§pattern: BeTree<PatternOperator, PatternParts>
§verb_invocation: Option<VerbInvocation>
Implementations§
source§impl CommandParts
impl CommandParts
sourcepub fn has_not_empty_verb_invocation(&self) -> bool
pub fn has_not_empty_verb_invocation(&self) -> bool
Examples found in repository?
src/command/panel_input.rs (line 305)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
fn get_command(
&mut self,
timed_event: TimedEvent,
con: &AppContext,
sel_info: SelInfo<'_>,
app_state: &AppState,
mode: Mode,
panel_state_type: PanelStateType,
) -> Command {
match timed_event.event {
Event::Mouse(MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }) => {
if self.input_field.apply_timed_event(timed_event) {
Command::empty()
} else {
match kind {
MouseEventKind::Up(MouseButton::Left) => {
if timed_event.double_click {
Command::DoubleClick(column, row)
} else {
Command::Click(column, row)
}
}
MouseEventKind::ScrollDown => {
Command::Internal {
internal: Internal::line_down,
input_invocation: None,
}
}
MouseEventKind::ScrollUp => {
Command::Internal {
internal: Internal::line_up,
input_invocation: None,
}
}
_ => Command::None,
}
}
}
Event::Key(key) => {
// value of raw and parts before any key related change
let raw = self.input_field.get_content();
let mut parts = CommandParts::from(raw.clone());
// we first handle the cases that MUST absolutely
// not be overridden by configuration
if key == key!(esc) {
// tab cycling
self.tab_cycle_count = 0;
if let Some(raw) = self.input_before_cycle.take() {
// we cancel the tab cycling
self.input_field.set_str(&raw);
self.input_before_cycle = None;
return Command::from_raw(raw, false);
} else if con.modal && mode == Mode::Input {
// leave insertion mode
return Command::Internal {
internal: Internal::mode_command,
input_invocation: None,
};
} else {
// general back command
self.input_field.clear();
let internal = Internal::back;
return Command::Internal {
internal,
input_invocation: parts.verb_invocation,
};
}
}
// tab completion
if key == key!(tab) {
if parts.verb_invocation.is_some() {
let parts_before_cycle;
let completable_parts = if let Some(s) = &self.input_before_cycle {
parts_before_cycle = CommandParts::from(s.clone());
&parts_before_cycle
} else {
&parts
};
let completions = Completions::for_input(completable_parts, con, sel_info);
info!(" -> completions: {:?}", &completions);
let added = match completions {
Completions::None => {
debug!("nothing to complete!");
self.tab_cycle_count = 0;
self.input_before_cycle = None;
None
}
Completions::Common(completion) => {
self.tab_cycle_count = 0;
Some(completion)
}
Completions::List(mut completions) => {
let idx = self.tab_cycle_count % completions.len();
if self.tab_cycle_count == 0 {
self.input_before_cycle = Some(raw.to_string());
}
self.tab_cycle_count += 1;
Some(completions.swap_remove(idx))
}
};
if let Some(added) = added {
let mut raw = self
.input_before_cycle
.as_ref()
.map_or(raw, |s| s.to_string());
raw.push_str(&added);
self.input_field.set_str(&raw);
return Command::from_raw(raw, false);
} else {
return Command::None;
}
}
} else {
self.tab_cycle_count = 0;
self.input_before_cycle = None;
}
if key == key!(enter) && parts.has_not_empty_verb_invocation() {
return Command::from_parts(parts, true);
}
if (key == key!('?') || key == key!(shift-'?'))
&& (raw.is_empty() || parts.verb_invocation.is_some()) {
// a '?' opens the help when it's the first char
// or when it's part of the verb invocation
return Command::Internal {
internal: Internal::help,
input_invocation: parts.verb_invocation,
};
}
// we now check if the key is the trigger key of one of the verbs
if keys::is_key_allowed_for_verb(key, mode, raw.is_empty()) {
for (index, verb) in con.verb_store.verbs.iter().enumerate() {
for verb_key in &verb.keys {
if *verb_key != key {
continue;
}
if self.handle_input_related_verb(verb, con) {
return Command::from_raw(self.input_field.get_content(), false);
}
if !verb.selection_condition.is_respected_by(sel_info.common_stype()) {
continue;
}
if !verb.can_be_called_in_panel(panel_state_type) {
continue;
}
if mode != Mode::Input && verb.is_internal(Internal::mode_input) {
self.enter_input_mode_with_key(key, &parts);
}
if !verb.file_extensions.is_empty() {
let extension = sel_info.extension();
if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
continue;
}
}
if verb.auto_exec {
return Command::VerbTrigger {
index,
input_invocation: parts.verb_invocation,
};
}
if let Some(invocation_parser) = &verb.invocation_parser {
let exec_builder = ExecutionStringBuilder::without_invocation(
sel_info,
app_state,
);
let verb_invocation = exec_builder.invocation_with_default(
&invocation_parser.invocation_pattern
);
parts.verb_invocation = Some(verb_invocation);
self.set_content(&parts.to_string());
return Command::VerbEdit(parts.verb_invocation.unwrap());
}
}
}
}
// input field management
if mode == Mode::Input {
if self.input_field.apply_timed_event(timed_event) {
return Command::from_raw(self.input_field.get_content(), false);
}
}
Command::None
}
_ => Command::None,
}
}
sourcepub fn from<S: Into<String>>(raw: S) -> Self
pub fn from<S: Into<String>>(raw: S) -> Self
Examples found in repository?
More examples
src/app/panel.rs (line 158)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
pub fn clear_input_invocation(&mut self, con: &AppContext) {
let mut command_parts = CommandParts::from(self.input.get_content());
if command_parts.verb_invocation.is_some() {
command_parts.verb_invocation = None;
let new_input = format!("{}", command_parts);
self.input.set_content(&new_input);
}
self.mut_state().set_mode(initial_mode(con));
}
pub fn set_input_content(&mut self, content: &str) {
self.input.set_content(content);
}
pub fn get_input_content(&self) -> String {
self.input.get_content()
}
/// change the argument of the verb in the input, if there's one
pub fn set_input_arg(&mut self, arg: String) {
let mut command_parts = CommandParts::from(self.input.get_content());
if let Some(invocation) = &mut command_parts.verb_invocation {
invocation.args = Some(arg);
let new_input = format!("{}", command_parts);
self.input.set_content(&new_input);
}
}
src/command/sequence.rs (line 81)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
fn add_commands(
input: &str,
commands: &mut Vec<(String, Command)>,
con: &AppContext,
) -> Result<(), ProgramError> {
let raw_parts = CommandParts::from(input.to_string());
let (pattern, verb_invocation) = raw_parts.split();
if let Some(pattern) = pattern {
commands.push((input.to_string(), Command::from_parts(pattern, false)));
}
if let Some(verb_invocation) = verb_invocation {
let command = Command::from_parts(verb_invocation, true);
if let Command::VerbInvocate(invocation) = &command {
// we check that the verb exists to avoid running a sequence
// of actions with some missing
match con.verb_store.search_prefix(&invocation.name) {
PrefixSearchResult::NoMatch => {
return Err(ProgramError::UnknownVerb {
name: invocation.name.to_string(),
});
}
PrefixSearchResult::Matches(_) => {
return Err(ProgramError::AmbiguousVerbName {
name: invocation.name.to_string(),
});
}
_ => {}
}
commands.push((input.to_string(), command));
}
}
Ok(())
}
src/command/panel_input.rs (line 226)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
fn get_command(
&mut self,
timed_event: TimedEvent,
con: &AppContext,
sel_info: SelInfo<'_>,
app_state: &AppState,
mode: Mode,
panel_state_type: PanelStateType,
) -> Command {
match timed_event.event {
Event::Mouse(MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }) => {
if self.input_field.apply_timed_event(timed_event) {
Command::empty()
} else {
match kind {
MouseEventKind::Up(MouseButton::Left) => {
if timed_event.double_click {
Command::DoubleClick(column, row)
} else {
Command::Click(column, row)
}
}
MouseEventKind::ScrollDown => {
Command::Internal {
internal: Internal::line_down,
input_invocation: None,
}
}
MouseEventKind::ScrollUp => {
Command::Internal {
internal: Internal::line_up,
input_invocation: None,
}
}
_ => Command::None,
}
}
}
Event::Key(key) => {
// value of raw and parts before any key related change
let raw = self.input_field.get_content();
let mut parts = CommandParts::from(raw.clone());
// we first handle the cases that MUST absolutely
// not be overridden by configuration
if key == key!(esc) {
// tab cycling
self.tab_cycle_count = 0;
if let Some(raw) = self.input_before_cycle.take() {
// we cancel the tab cycling
self.input_field.set_str(&raw);
self.input_before_cycle = None;
return Command::from_raw(raw, false);
} else if con.modal && mode == Mode::Input {
// leave insertion mode
return Command::Internal {
internal: Internal::mode_command,
input_invocation: None,
};
} else {
// general back command
self.input_field.clear();
let internal = Internal::back;
return Command::Internal {
internal,
input_invocation: parts.verb_invocation,
};
}
}
// tab completion
if key == key!(tab) {
if parts.verb_invocation.is_some() {
let parts_before_cycle;
let completable_parts = if let Some(s) = &self.input_before_cycle {
parts_before_cycle = CommandParts::from(s.clone());
&parts_before_cycle
} else {
&parts
};
let completions = Completions::for_input(completable_parts, con, sel_info);
info!(" -> completions: {:?}", &completions);
let added = match completions {
Completions::None => {
debug!("nothing to complete!");
self.tab_cycle_count = 0;
self.input_before_cycle = None;
None
}
Completions::Common(completion) => {
self.tab_cycle_count = 0;
Some(completion)
}
Completions::List(mut completions) => {
let idx = self.tab_cycle_count % completions.len();
if self.tab_cycle_count == 0 {
self.input_before_cycle = Some(raw.to_string());
}
self.tab_cycle_count += 1;
Some(completions.swap_remove(idx))
}
};
if let Some(added) = added {
let mut raw = self
.input_before_cycle
.as_ref()
.map_or(raw, |s| s.to_string());
raw.push_str(&added);
self.input_field.set_str(&raw);
return Command::from_raw(raw, false);
} else {
return Command::None;
}
}
} else {
self.tab_cycle_count = 0;
self.input_before_cycle = None;
}
if key == key!(enter) && parts.has_not_empty_verb_invocation() {
return Command::from_parts(parts, true);
}
if (key == key!('?') || key == key!(shift-'?'))
&& (raw.is_empty() || parts.verb_invocation.is_some()) {
// a '?' opens the help when it's the first char
// or when it's part of the verb invocation
return Command::Internal {
internal: Internal::help,
input_invocation: parts.verb_invocation,
};
}
// we now check if the key is the trigger key of one of the verbs
if keys::is_key_allowed_for_verb(key, mode, raw.is_empty()) {
for (index, verb) in con.verb_store.verbs.iter().enumerate() {
for verb_key in &verb.keys {
if *verb_key != key {
continue;
}
if self.handle_input_related_verb(verb, con) {
return Command::from_raw(self.input_field.get_content(), false);
}
if !verb.selection_condition.is_respected_by(sel_info.common_stype()) {
continue;
}
if !verb.can_be_called_in_panel(panel_state_type) {
continue;
}
if mode != Mode::Input && verb.is_internal(Internal::mode_input) {
self.enter_input_mode_with_key(key, &parts);
}
if !verb.file_extensions.is_empty() {
let extension = sel_info.extension();
if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
continue;
}
}
if verb.auto_exec {
return Command::VerbTrigger {
index,
input_invocation: parts.verb_invocation,
};
}
if let Some(invocation_parser) = &verb.invocation_parser {
let exec_builder = ExecutionStringBuilder::without_invocation(
sel_info,
app_state,
);
let verb_invocation = exec_builder.invocation_with_default(
&invocation_parser.invocation_pattern
);
parts.verb_invocation = Some(verb_invocation);
self.set_content(&parts.to_string());
return Command::VerbEdit(parts.verb_invocation.unwrap());
}
}
}
}
// input field management
if mode == Mode::Input {
if self.input_field.apply_timed_event(timed_event) {
return Command::from_raw(self.input_field.get_content(), false);
}
}
Command::None
}
_ => Command::None,
}
}
sourcepub fn split(self) -> (Option<CommandParts>, Option<CommandParts>)
pub fn split(self) -> (Option<CommandParts>, Option<CommandParts>)
split an input into its two possible parts, the pattern and the verb invocation. Each part, when defined, is suitable to create a command on its own.
Examples found in repository?
src/command/sequence.rs (line 82)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
fn add_commands(
input: &str,
commands: &mut Vec<(String, Command)>,
con: &AppContext,
) -> Result<(), ProgramError> {
let raw_parts = CommandParts::from(input.to_string());
let (pattern, verb_invocation) = raw_parts.split();
if let Some(pattern) = pattern {
commands.push((input.to_string(), Command::from_parts(pattern, false)));
}
if let Some(verb_invocation) = verb_invocation {
let command = Command::from_parts(verb_invocation, true);
if let Command::VerbInvocate(invocation) = &command {
// we check that the verb exists to avoid running a sequence
// of actions with some missing
match con.verb_store.search_prefix(&invocation.name) {
PrefixSearchResult::NoMatch => {
return Err(ProgramError::UnknownVerb {
name: invocation.name.to_string(),
});
}
PrefixSearchResult::Matches(_) => {
return Err(ProgramError::AmbiguousVerbName {
name: invocation.name.to_string(),
});
}
_ => {}
}
commands.push((input.to_string(), command));
}
}
Ok(())
}
Trait Implementations§
source§impl Clone for CommandParts
impl Clone for CommandParts
source§fn clone(&self) -> CommandParts
fn clone(&self) -> CommandParts
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for CommandParts
impl Debug for CommandParts
source§impl Display for CommandParts
impl Display for CommandParts
source§impl PartialEq<CommandParts> for CommandParts
impl PartialEq<CommandParts> for CommandParts
source§fn eq(&self, other: &CommandParts) -> bool
fn eq(&self, other: &CommandParts) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.