pub enum Command {
None,
VerbEdit(VerbInvocation),
VerbInvocate(VerbInvocation),
Internal {
internal: Internal,
input_invocation: Option<VerbInvocation>,
},
VerbTrigger {
index: usize,
input_invocation: Option<VerbInvocation>,
},
PatternEdit {
raw: String,
expr: BeTree<PatternOperator, PatternParts>,
},
Click(u16, u16),
DoubleClick(u16, u16),
}
Expand description
a command which may result in a change in the application state.
It may come from a shortcut, from the parsed input, from an argument given on launch.
Variants§
None
no command
VerbEdit(VerbInvocation)
a verb invocation, unfinished (user didn’t hit enter)
VerbInvocate(VerbInvocation)
verb invocation, finished (coming from –cmd, or after the user hit enter)
Internal
call of an internal done without the input (using a trigger key for example)
VerbTrigger
call of a verb done without the input (using a trigger key for example)
PatternEdit
a pattern being edited
Click(u16, u16)
a mouse click
DoubleClick(u16, u16)
a mouse double-click Always come after a simple click at same position
Implementations§
source§impl Command
impl Command
sourcepub fn empty() -> Command
pub fn empty() -> Command
Examples found in repository?
More examples
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 as_verb_invocation(&self) -> Option<&VerbInvocation>
pub fn as_verb_invocation(&self) -> Option<&VerbInvocation>
Examples found in repository?
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
fn apply_command(
&mut self,
w: &mut W,
cmd: Command,
panel_skin: &PanelSkin,
app_state: &mut AppState,
con: &mut AppContext,
) -> Result<(), ProgramError> {
use CmdResult::*;
let mut error: Option<String> = None;
let is_input_invocation = cmd.is_verb_invocated_from_input();
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen, // it can't change in this function
con,
};
let cmd_result = self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
debug!("cmd_result: {:?}", &cmd_result);
match cmd_result {
ApplyOnPanel { id } => {
if let Some(idx) = self.panel_id_to_idx(id) {
if let DisplayError(txt) = self.panels[idx].apply_command(
w, &cmd, app_state, &app_cmd_context
)? {
// we should probably handle other results
// which implies the possibility of a recursion
error = Some(txt);
} else if is_input_invocation {
self.mut_panel().clear_input();
}
} else {
warn!("no panel found for ApplyOnPanel");
}
}
ClosePanel { validate_purpose, panel_ref } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let close_idx = self.panel_ref_to_idx(panel_ref)
.unwrap_or_else(||
// when there's a preview panel, we close it rather than the app
if self.panels.len().get()==2 && self.preview_panel.is_some() {
1
} else {
self.active_panel_idx
}
);
let mut new_arg = None;
if validate_purpose {
let purpose = &self.panels[close_idx].purpose;
if let PanelPurpose::ArgEdition { .. } = purpose {
new_arg = self.panels[close_idx]
.state()
.selected_path()
.map(|p| p.to_string_lossy().to_string());
}
}
if self.close_panel(close_idx) {
let screen = self.screen;
self.mut_state().refresh(screen, con);
if let Some(new_arg) = new_arg {
self.mut_panel().set_input_arg(new_arg);
let new_input = self.panel().get_input_content();
let cmd = Command::from_raw(new_input, false);
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
}
} else {
self.quitting = true;
}
}
DisplayError(txt) => {
error = Some(txt);
}
ExecuteSequence { sequence } => {
self.tx_seqs.send(sequence).unwrap();
}
HandleInApp(internal) => {
debug!("handling internal {internal:?} at app level");
match internal {
Internal::panel_left_no_open | Internal::panel_right_no_open => {
let new_active_panel_idx = if internal == Internal::panel_left_no_open {
// we're here because the state wants us to either move to the panel
// to the left, or close the rightest one
if self.active_panel_idx == 0 {
self.close_panel(self.panels.len().get()-1);
None
} else {
Some(self.active_panel_idx - 1)
}
} else { // panel_right
// we either move to the right or close the leftest panel
if self.active_panel_idx + 1 == self.panels.len().get() {
self.close_panel(0);
None
} else {
Some(self.active_panel_idx + 1)
}
};
if let Some(idx) = new_active_panel_idx {
if is_input_invocation {
self.mut_panel().clear_input();
}
self.active_panel_idx = idx;
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
Internal::toggle_second_tree => {
let panels_count = self.panels.len().get();
let trees_count = self.panels.iter()
.filter(|p| p.state().get_type() == PanelStateType::Tree)
.count();
if trees_count < 2 {
// we open a tree, closing a (non tree) panel if necessary
if panels_count >= con.max_panels_count {
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() != PanelStateType::Tree {
self.close_panel(i);
break;
}
}
}
if let Some(selected_path) = self.state().selected_path() {
let dir = closest_dir(selected_path);
if let Ok(new_state) = BrowserState::new(
dir,
self.state().tree_options().without_pattern(),
self.screen,
con,
&Dam::unlimited(),
) {
if let Err(s) = self.new_panel(
Box::new(new_state),
PanelPurpose::None,
HDir::Right,
is_input_invocation,
con,
) {
error = Some(s);
}
}
}
} else {
// we close the rightest inactive tree
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() == PanelStateType::Tree {
if i == self.active_panel_idx {
continue;
}
self.close_panel(i);
break;
}
}
}
}
Internal::set_syntax_theme => {
let arg = cmd
.as_verb_invocation()
.and_then(|vi| vi.args.as_ref());
match arg {
Some(arg) => {
match SyntaxTheme::from_str(arg) {
Ok(theme) => {
con.syntax_theme = Some(theme);
self.update_preview(con, true);
}
Err(e) => {
error = Some(e.to_string());
}
}
}
None => {
error = Some("no theme provided".to_string());
}
}
}
_ => {
info!("unhandled propagated internal. cmd={:?}", &cmd);
}
}
}
Keep => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
}
Launch(launchable) => {
self.launch_at_end = Some(*launchable);
self.quitting = true;
}
NewPanel {
state,
purpose,
direction,
} => {
if let Err(s) = self.new_panel(state, purpose, direction, is_input_invocation, con) {
error = Some(s);
}
}
NewState { state, message } => {
self.mut_panel().clear_input();
self.mut_panel().push_state(state);
if let Some(md) = message {
self.mut_panel().set_message(md);
} else {
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
PopState => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
self.mut_state().refresh(app_cmd_context.screen, con);
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
PopStateAndReapply => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
Quit => {
self.quitting = true;
}
RefreshState { clear_cache } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
if clear_cache {
clear_caches();
}
app_state.stage.refresh();
for i in 0..self.panels.len().get() {
self.panels[i].mut_state().refresh(self.screen, con);
}
}
}
if let Some(text) = error {
self.mut_panel().set_error(text);
}
app_state.other_panel_path = self.get_other_panel_path();
if let Some(path) = self.state().tree_root() {
app_state.root = path.to_path_buf();
}
if let Some(shared_root) = &mut self.shared_root {
if let Ok(mut root) = shared_root.lock() {
*root = app_state.root.clone();
}
}
self.update_preview(con, false);
Ok(())
}
sourcepub fn from_parts(cp: CommandParts, finished: bool) -> Self
pub fn from_parts(cp: CommandParts, finished: bool) -> Self
build a command from the parsed string representation
The command being finished is the difference between a command being edited and a command launched (which happens on enter in the input).
Examples found in repository?
More examples
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(())
}
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 is_verb_invocated_from_input(&self) -> bool
pub fn is_verb_invocated_from_input(&self) -> bool
tells whether this action is a verb being invocated on enter in the input field
Examples found in repository?
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
fn apply_command(
&mut self,
w: &mut W,
cmd: Command,
panel_skin: &PanelSkin,
app_state: &mut AppState,
con: &mut AppContext,
) -> Result<(), ProgramError> {
use CmdResult::*;
let mut error: Option<String> = None;
let is_input_invocation = cmd.is_verb_invocated_from_input();
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen, // it can't change in this function
con,
};
let cmd_result = self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
debug!("cmd_result: {:?}", &cmd_result);
match cmd_result {
ApplyOnPanel { id } => {
if let Some(idx) = self.panel_id_to_idx(id) {
if let DisplayError(txt) = self.panels[idx].apply_command(
w, &cmd, app_state, &app_cmd_context
)? {
// we should probably handle other results
// which implies the possibility of a recursion
error = Some(txt);
} else if is_input_invocation {
self.mut_panel().clear_input();
}
} else {
warn!("no panel found for ApplyOnPanel");
}
}
ClosePanel { validate_purpose, panel_ref } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let close_idx = self.panel_ref_to_idx(panel_ref)
.unwrap_or_else(||
// when there's a preview panel, we close it rather than the app
if self.panels.len().get()==2 && self.preview_panel.is_some() {
1
} else {
self.active_panel_idx
}
);
let mut new_arg = None;
if validate_purpose {
let purpose = &self.panels[close_idx].purpose;
if let PanelPurpose::ArgEdition { .. } = purpose {
new_arg = self.panels[close_idx]
.state()
.selected_path()
.map(|p| p.to_string_lossy().to_string());
}
}
if self.close_panel(close_idx) {
let screen = self.screen;
self.mut_state().refresh(screen, con);
if let Some(new_arg) = new_arg {
self.mut_panel().set_input_arg(new_arg);
let new_input = self.panel().get_input_content();
let cmd = Command::from_raw(new_input, false);
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
}
} else {
self.quitting = true;
}
}
DisplayError(txt) => {
error = Some(txt);
}
ExecuteSequence { sequence } => {
self.tx_seqs.send(sequence).unwrap();
}
HandleInApp(internal) => {
debug!("handling internal {internal:?} at app level");
match internal {
Internal::panel_left_no_open | Internal::panel_right_no_open => {
let new_active_panel_idx = if internal == Internal::panel_left_no_open {
// we're here because the state wants us to either move to the panel
// to the left, or close the rightest one
if self.active_panel_idx == 0 {
self.close_panel(self.panels.len().get()-1);
None
} else {
Some(self.active_panel_idx - 1)
}
} else { // panel_right
// we either move to the right or close the leftest panel
if self.active_panel_idx + 1 == self.panels.len().get() {
self.close_panel(0);
None
} else {
Some(self.active_panel_idx + 1)
}
};
if let Some(idx) = new_active_panel_idx {
if is_input_invocation {
self.mut_panel().clear_input();
}
self.active_panel_idx = idx;
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
Internal::toggle_second_tree => {
let panels_count = self.panels.len().get();
let trees_count = self.panels.iter()
.filter(|p| p.state().get_type() == PanelStateType::Tree)
.count();
if trees_count < 2 {
// we open a tree, closing a (non tree) panel if necessary
if panels_count >= con.max_panels_count {
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() != PanelStateType::Tree {
self.close_panel(i);
break;
}
}
}
if let Some(selected_path) = self.state().selected_path() {
let dir = closest_dir(selected_path);
if let Ok(new_state) = BrowserState::new(
dir,
self.state().tree_options().without_pattern(),
self.screen,
con,
&Dam::unlimited(),
) {
if let Err(s) = self.new_panel(
Box::new(new_state),
PanelPurpose::None,
HDir::Right,
is_input_invocation,
con,
) {
error = Some(s);
}
}
}
} else {
// we close the rightest inactive tree
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() == PanelStateType::Tree {
if i == self.active_panel_idx {
continue;
}
self.close_panel(i);
break;
}
}
}
}
Internal::set_syntax_theme => {
let arg = cmd
.as_verb_invocation()
.and_then(|vi| vi.args.as_ref());
match arg {
Some(arg) => {
match SyntaxTheme::from_str(arg) {
Ok(theme) => {
con.syntax_theme = Some(theme);
self.update_preview(con, true);
}
Err(e) => {
error = Some(e.to_string());
}
}
}
None => {
error = Some("no theme provided".to_string());
}
}
}
_ => {
info!("unhandled propagated internal. cmd={:?}", &cmd);
}
}
}
Keep => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
}
Launch(launchable) => {
self.launch_at_end = Some(*launchable);
self.quitting = true;
}
NewPanel {
state,
purpose,
direction,
} => {
if let Err(s) = self.new_panel(state, purpose, direction, is_input_invocation, con) {
error = Some(s);
}
}
NewState { state, message } => {
self.mut_panel().clear_input();
self.mut_panel().push_state(state);
if let Some(md) = message {
self.mut_panel().set_message(md);
} else {
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
PopState => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
self.mut_state().refresh(app_cmd_context.screen, con);
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
PopStateAndReapply => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
Quit => {
self.quitting = true;
}
RefreshState { clear_cache } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
if clear_cache {
clear_caches();
}
app_state.stage.refresh();
for i in 0..self.panels.len().get() {
self.panels[i].mut_state().refresh(self.screen, con);
}
}
}
if let Some(text) = error {
self.mut_panel().set_error(text);
}
app_state.other_panel_path = self.get_other_panel_path();
if let Some(path) = self.state().tree_root() {
app_state.root = path.to_path_buf();
}
if let Some(shared_root) = &mut self.shared_root {
if let Ok(mut root) = shared_root.lock() {
*root = app_state.root.clone();
}
}
self.update_preview(con, false);
Ok(())
}
sourcepub fn from_raw(raw: String, finished: bool) -> Self
pub fn from_raw(raw: String, finished: bool) -> Self
create a command from a raw input.
finished
makes the command an executed form,
it’s equivalent to using the Enter key in the Gui.
Examples found in repository?
More examples
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
pub fn refresh_input_status<'c>(
&mut self,
app_state: &AppState,
app_cmd_context: &'c AppCmdContext<'c>,
) {
let cmd = Command::from_raw(self.input.get_content(), false);
let cc = CmdContext {
cmd: &cmd,
app: app_cmd_context,
panel: PanelCmdContext {
areas: &self.areas,
purpose: self.purpose,
},
};
let has_previous_state = self.states.len() > 1;
self.status = self.state().get_status(app_state, &cc, has_previous_state);
}
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,
}
}
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
fn apply_command(
&mut self,
w: &mut W,
cmd: Command,
panel_skin: &PanelSkin,
app_state: &mut AppState,
con: &mut AppContext,
) -> Result<(), ProgramError> {
use CmdResult::*;
let mut error: Option<String> = None;
let is_input_invocation = cmd.is_verb_invocated_from_input();
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen, // it can't change in this function
con,
};
let cmd_result = self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
debug!("cmd_result: {:?}", &cmd_result);
match cmd_result {
ApplyOnPanel { id } => {
if let Some(idx) = self.panel_id_to_idx(id) {
if let DisplayError(txt) = self.panels[idx].apply_command(
w, &cmd, app_state, &app_cmd_context
)? {
// we should probably handle other results
// which implies the possibility of a recursion
error = Some(txt);
} else if is_input_invocation {
self.mut_panel().clear_input();
}
} else {
warn!("no panel found for ApplyOnPanel");
}
}
ClosePanel { validate_purpose, panel_ref } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let close_idx = self.panel_ref_to_idx(panel_ref)
.unwrap_or_else(||
// when there's a preview panel, we close it rather than the app
if self.panels.len().get()==2 && self.preview_panel.is_some() {
1
} else {
self.active_panel_idx
}
);
let mut new_arg = None;
if validate_purpose {
let purpose = &self.panels[close_idx].purpose;
if let PanelPurpose::ArgEdition { .. } = purpose {
new_arg = self.panels[close_idx]
.state()
.selected_path()
.map(|p| p.to_string_lossy().to_string());
}
}
if self.close_panel(close_idx) {
let screen = self.screen;
self.mut_state().refresh(screen, con);
if let Some(new_arg) = new_arg {
self.mut_panel().set_input_arg(new_arg);
let new_input = self.panel().get_input_content();
let cmd = Command::from_raw(new_input, false);
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
}
} else {
self.quitting = true;
}
}
DisplayError(txt) => {
error = Some(txt);
}
ExecuteSequence { sequence } => {
self.tx_seqs.send(sequence).unwrap();
}
HandleInApp(internal) => {
debug!("handling internal {internal:?} at app level");
match internal {
Internal::panel_left_no_open | Internal::panel_right_no_open => {
let new_active_panel_idx = if internal == Internal::panel_left_no_open {
// we're here because the state wants us to either move to the panel
// to the left, or close the rightest one
if self.active_panel_idx == 0 {
self.close_panel(self.panels.len().get()-1);
None
} else {
Some(self.active_panel_idx - 1)
}
} else { // panel_right
// we either move to the right or close the leftest panel
if self.active_panel_idx + 1 == self.panels.len().get() {
self.close_panel(0);
None
} else {
Some(self.active_panel_idx + 1)
}
};
if let Some(idx) = new_active_panel_idx {
if is_input_invocation {
self.mut_panel().clear_input();
}
self.active_panel_idx = idx;
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
Internal::toggle_second_tree => {
let panels_count = self.panels.len().get();
let trees_count = self.panels.iter()
.filter(|p| p.state().get_type() == PanelStateType::Tree)
.count();
if trees_count < 2 {
// we open a tree, closing a (non tree) panel if necessary
if panels_count >= con.max_panels_count {
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() != PanelStateType::Tree {
self.close_panel(i);
break;
}
}
}
if let Some(selected_path) = self.state().selected_path() {
let dir = closest_dir(selected_path);
if let Ok(new_state) = BrowserState::new(
dir,
self.state().tree_options().without_pattern(),
self.screen,
con,
&Dam::unlimited(),
) {
if let Err(s) = self.new_panel(
Box::new(new_state),
PanelPurpose::None,
HDir::Right,
is_input_invocation,
con,
) {
error = Some(s);
}
}
}
} else {
// we close the rightest inactive tree
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() == PanelStateType::Tree {
if i == self.active_panel_idx {
continue;
}
self.close_panel(i);
break;
}
}
}
}
Internal::set_syntax_theme => {
let arg = cmd
.as_verb_invocation()
.and_then(|vi| vi.args.as_ref());
match arg {
Some(arg) => {
match SyntaxTheme::from_str(arg) {
Ok(theme) => {
con.syntax_theme = Some(theme);
self.update_preview(con, true);
}
Err(e) => {
error = Some(e.to_string());
}
}
}
None => {
error = Some("no theme provided".to_string());
}
}
}
_ => {
info!("unhandled propagated internal. cmd={:?}", &cmd);
}
}
}
Keep => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
}
Launch(launchable) => {
self.launch_at_end = Some(*launchable);
self.quitting = true;
}
NewPanel {
state,
purpose,
direction,
} => {
if let Err(s) = self.new_panel(state, purpose, direction, is_input_invocation, con) {
error = Some(s);
}
}
NewState { state, message } => {
self.mut_panel().clear_input();
self.mut_panel().push_state(state);
if let Some(md) = message {
self.mut_panel().set_message(md);
} else {
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
}
}
PopState => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
self.mut_state().refresh(app_cmd_context.screen, con);
self.mut_panel().refresh_input_status(app_state, &app_cmd_context);
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
PopStateAndReapply => {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
stage_panel: self.stage_panel,
screen: self.screen,
con,
};
self.mut_panel().apply_command(w, &cmd, app_state, &app_cmd_context)?;
} else if con.quit_on_last_cancel {
self.quitting = true;
}
}
Quit => {
self.quitting = true;
}
RefreshState { clear_cache } => {
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
if clear_cache {
clear_caches();
}
app_state.stage.refresh();
for i in 0..self.panels.len().get() {
self.panels[i].mut_state().refresh(self.screen, con);
}
}
}
if let Some(text) = error {
self.mut_panel().set_error(text);
}
app_state.other_panel_path = self.get_other_panel_path();
if let Some(path) = self.state().tree_root() {
app_state.root = path.to_path_buf();
}
if let Some(shared_root) = &mut self.shared_root {
if let Ok(mut root) = shared_root.lock() {
*root = app_state.root.clone();
}
}
self.update_preview(con, false);
Ok(())
}
sourcepub fn from_pattern(pattern: &InputPattern) -> Self
pub fn from_pattern(pattern: &InputPattern) -> Self
build a non executed command from a pattern
Examples found in repository?
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
fn refresh(&mut self, screen: Screen, con: &AppContext) -> Command {
let page_height = BrowserState::page_height(screen) as usize;
// refresh the base tree
if let Err(e) = self.tree.refresh(page_height, con) {
warn!("refreshing base tree failed : {:?}", e);
}
// refresh the filtered tree, if any
Command::from_pattern(match self.filtered_tree {
Some(ref mut tree) => {
if let Err(e) = tree.refresh(page_height, con) {
warn!("refreshing filtered tree failed : {:?}", e);
}
&tree.options.pattern
}
None => &self.tree.options.pattern,
})
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl UnwindSafe for Command
Blanket Implementations§
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.