pub struct Panel {
pub id: PanelId,
pub areas: Areas,
pub purpose: PanelPurpose,
/* private fields */
}
Expand description
A colon on screen containing a stack of states, the top one being visible
Fields§
§id: PanelId
§areas: Areas
§purpose: PanelPurpose
Implementations§
source§impl Panel
impl Panel
sourcepub fn new(
id: PanelId,
state: Box<dyn PanelState>,
areas: Areas,
con: &AppContext
) -> Self
pub fn new(
id: PanelId,
state: Box<dyn PanelState>,
areas: Areas,
con: &AppContext
) -> Self
Examples found in repository?
src/app/app.rs (lines 82-95)
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 184 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 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
pub fn new(
con: &AppContext,
) -> Result<App, ProgramError> {
let screen = Screen::new(con)?;
let panel = Panel::new(
PanelId::from(0),
Box::new(
BrowserState::new(
con.initial_root.clone(),
con.initial_tree_options.clone(),
screen,
con,
&Dam::unlimited(),
)?
),
Areas::create(&mut Vec::new(), 0, screen, false),
con,
);
let (tx_seqs, rx_seqs) = unbounded::<Sequence>();
Ok(App {
screen,
active_panel_idx: 0,
panels: panel.into(),
quitting: false,
launch_at_end: None,
created_panels_count: 1,
preview_panel: None,
stage_panel: None,
shared_root: None,
tx_seqs,
rx_seqs,
drawing_count: 0,
})
}
fn panel_ref_to_idx(&self, panel_ref: PanelReference) -> Option<usize> {
match panel_ref {
PanelReference::Active => Some(self.active_panel_idx),
PanelReference::Leftest => Some(0),
PanelReference::Rightest => Some(self.panels.len().get() - 1),
PanelReference::Id(id) => self.panel_id_to_idx(id),
PanelReference::Preview => self.preview_panel.and_then(|id| self.panel_id_to_idx(id)),
}
}
/// return the current index of the panel with given id
fn panel_id_to_idx(&self, id: PanelId) -> Option<usize> {
self.panels.iter().position(|panel| panel.id == id)
}
fn state(&self) -> &dyn PanelState {
self.panels[self.active_panel_idx].state()
}
fn mut_state(&mut self) -> &mut dyn PanelState {
self.panels[self.active_panel_idx].mut_state()
}
fn panel(&self) -> &Panel {
&self.panels[self.active_panel_idx]
}
fn mut_panel(&mut self) -> &mut Panel {
unsafe {
self.panels
.as_mut_slice()
.get_unchecked_mut(self.active_panel_idx)
}
}
/// close the panel if it's not the last one
///
/// Return true when the panel has been removed (ie it wasn't the last one)
fn close_panel(&mut self, panel_idx: usize) -> bool {
let active_panel_id = self.panels[self.active_panel_idx].id;
if let Some(preview_id) = self.preview_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != preview_id {
// we don't want to stay with just the preview
return false;
}
}
if let Some(stage_id) = self.stage_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != stage_id {
// we don't want to stay with just the stage
return false;
}
}
if let Ok(removed_panel) = self.panels.remove(panel_idx) {
if self.preview_panel == Some(removed_panel.id) {
self.preview_panel = None;
}
if self.stage_panel == Some(removed_panel.id) {
self.stage_panel = None;
}
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
self.active_panel_idx = self
.panels
.iter()
.position(|p| p.id == active_panel_id)
.unwrap_or(self.panels.len().get() - 1);
true
} else {
false // there's no other panel to go to
}
}
/// remove the top state of the current panel
///
/// Close the panel too if that was its only state.
/// Close nothing and return false if there's not
/// at least two states in the app.
fn remove_state(&mut self) -> bool {
self.panels[self.active_panel_idx].remove_state()
|| self.close_panel(self.active_panel_idx)
}
/// redraw the whole screen. All drawing
/// are supposed to happen here, and only here.
fn display_panels(
&mut self,
w: &mut W,
skin: &AppSkin,
app_state: &AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
self.drawing_count += 1;
for (idx, panel) in self.panels.as_mut_slice().iter_mut().enumerate() {
let active = idx == self.active_panel_idx;
let panel_skin = if active { &skin.focused } else { &skin.unfocused };
let disc = DisplayContext {
count: self.drawing_count,
active,
screen: self.screen,
panel_skin,
state_area: panel.areas.state.clone(),
app_state,
con,
};
time!(
"display panel",
panel.display(w, &disc)?,
);
}
kitty::manager().lock().unwrap().erase_images_before(w, self.drawing_count)?;
w.flush()?;
Ok(())
}
/// if there are exactly two non preview panels, return the selection
/// in the non focused panel
fn get_other_panel_path(&self) -> Option<PathBuf> {
let len = self.panels.len().get();
if len == 3 {
if let Some(preview_id) = self.preview_panel {
for (idx, panel) in self.panels.iter().enumerate() {
if self.active_panel_idx != idx && panel.id != preview_id {
return panel.state().selected_path()
.map(|p| p.to_path_buf());
}
}
}
None
} else if self.panels.len().get() == 2 && self.preview_panel.is_none() {
let non_focused_panel_idx = if self.active_panel_idx == 0 { 1 } else { 0 };
self.panels[non_focused_panel_idx].state().selected_path()
.map(|p| p.to_path_buf())
} else {
None
}
}
/// apply a command. Change the states but don't redraw on screen.
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(())
}
/// update the state of the preview, if there's some
fn update_preview(&mut self, con: &AppContext, refresh: bool) {
let preview_idx = self.preview_panel.and_then(|id| self.panel_id_to_idx(id));
if let Some(preview_idx) = preview_idx {
if let Some(path) = self.state().selected_path() {
let old_path = self.panels[preview_idx].state().selected_path();
if (refresh || Some(path) != old_path) && path.is_file() {
let path = path.to_path_buf();
self.panels[preview_idx].mut_state().set_selected_path(path, con);
}
}
}
}
/// get the index of the panel at x
fn clicked_panel_index(&self, x: u16, _y: u16) -> usize {
let len = self.panels.len().get();
(len * x as usize) / (self.screen.width as usize + 1)
}
/// handle CmdResult::NewPanel
fn new_panel(
&mut self,
state: Box<dyn PanelState>,
purpose: PanelPurpose,
direction: HDir,
is_input_invocation: bool, // if true we clean the input
con: &AppContext,
) -> Result<(), String> {
match state.get_type() {
PanelStateType::Preview if self.preview_panel.is_some() => {
return Err("There can be only one preview panel".to_owned());
// todo replace instead ?
}
PanelStateType::Stage if self.stage_panel.is_some() => {
return Err("There can be only one stage panel".to_owned());
// todo replace instead ?
}
_ => {}
}
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let insertion_idx = if purpose.is_preview() {
self.panels.len().get()
} else if direction == HDir::Right {
self.active_panel_idx + 1
} else {
self.active_panel_idx
};
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
insertion_idx,
self.screen,
with_preview,
);
let panel_id = self.created_panels_count.into();
match state.get_type() {
PanelStateType::Preview => {
self.preview_panel = Some(panel_id);
}
PanelStateType::Stage => {
self.stage_panel = Some(panel_id);
}
_ => {
self.active_panel_idx = insertion_idx;
}
}
let mut panel = Panel::new(panel_id, state, areas, con);
panel.purpose = purpose;
self.created_panels_count += 1;
self.panels.insert(insertion_idx, panel);
Ok(())
}
sourcepub fn set_error(&mut self, text: String)
pub fn set_error(&mut self, text: String)
Examples found in repository?
src/app/app.rs (line 519)
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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
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(())
}
/// update the state of the preview, if there's some
fn update_preview(&mut self, con: &AppContext, refresh: bool) {
let preview_idx = self.preview_panel.and_then(|id| self.panel_id_to_idx(id));
if let Some(preview_idx) = preview_idx {
if let Some(path) = self.state().selected_path() {
let old_path = self.panels[preview_idx].state().selected_path();
if (refresh || Some(path) != old_path) && path.is_file() {
let path = path.to_path_buf();
self.panels[preview_idx].mut_state().set_selected_path(path, con);
}
}
}
}
/// get the index of the panel at x
fn clicked_panel_index(&self, x: u16, _y: u16) -> usize {
let len = self.panels.len().get();
(len * x as usize) / (self.screen.width as usize + 1)
}
/// handle CmdResult::NewPanel
fn new_panel(
&mut self,
state: Box<dyn PanelState>,
purpose: PanelPurpose,
direction: HDir,
is_input_invocation: bool, // if true we clean the input
con: &AppContext,
) -> Result<(), String> {
match state.get_type() {
PanelStateType::Preview if self.preview_panel.is_some() => {
return Err("There can be only one preview panel".to_owned());
// todo replace instead ?
}
PanelStateType::Stage if self.stage_panel.is_some() => {
return Err("There can be only one stage panel".to_owned());
// todo replace instead ?
}
_ => {}
}
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let insertion_idx = if purpose.is_preview() {
self.panels.len().get()
} else if direction == HDir::Right {
self.active_panel_idx + 1
} else {
self.active_panel_idx
};
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
insertion_idx,
self.screen,
with_preview,
);
let panel_id = self.created_panels_count.into();
match state.get_type() {
PanelStateType::Preview => {
self.preview_panel = Some(panel_id);
}
PanelStateType::Stage => {
self.stage_panel = Some(panel_id);
}
_ => {
self.active_panel_idx = insertion_idx;
}
}
let mut panel = Panel::new(panel_id, state, areas, con);
panel.purpose = purpose;
self.created_panels_count += 1;
self.panels.insert(insertion_idx, panel);
Ok(())
}
/// do the pending tasks, if any, and refresh the screen accordingly
fn do_pending_tasks(
&mut self,
w: &mut W,
skin: &AppSkin,
dam: &mut Dam,
app_state: &mut AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
while self.has_pending_task() && !dam.has_event() {
let error = self.do_pending_task(app_state, con, dam).err();
self.update_preview(con, false); // the selection may have changed
if let Some(error) = &error {
self.mut_panel().set_error(error.to_string());
//} else {
// // the refresh here removes the toggle status message. Not
// // sure there's a case it's useful
// let app_cmd_context = AppCmdContext {
// panel_skin: &skin.focused,
// 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);
}
self.display_panels(w, skin, app_state, con)?;
if error.is_some() {
return Ok(()); // breaking pending tasks chain on first error/interruption
}
}
Ok(())
}
sourcepub fn set_message<S: Into<String>>(&mut self, md: S)
pub fn set_message<S: Into<String>>(&mut self, md: S)
Examples found in repository?
src/app/app.rs (line 469)
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 apply_command<'c>(
&mut self,
w: &'c mut W,
cmd: &'c Command,
app_state: &mut AppState,
app_cmd_context: &'c AppCmdContext<'c>
) -> Result<CmdResult, ProgramError>
pub fn apply_command<'c>(
&mut self,
w: &'c mut W,
cmd: &'c Command,
app_state: &mut AppState,
app_cmd_context: &'c AppCmdContext<'c>
) -> Result<CmdResult, ProgramError>
apply a command on the current state, with no effect on screen
Examples found in repository?
src/app/app.rs (line 269)
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 refresh_input_status<'c>(
&mut self,
app_state: &AppState,
app_cmd_context: &'c AppCmdContext<'c>
)
pub fn refresh_input_status<'c>(
&mut self,
app_state: &AppState,
app_cmd_context: &'c AppCmdContext<'c>
)
called on focusing the panel and before the display, this updates the status from the command read in the input
Examples found in repository?
src/app/app.rs (line 370)
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 do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam
) -> Result<(), ProgramError>
pub fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam
) -> Result<(), ProgramError>
do the next pending task stopping as soon as there’s an event in the dam
Examples found in repository?
src/app/app.rs (line 659)
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
fn do_pending_task(
&mut self,
app_state: &mut AppState,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
let screen = self.screen;
// we start with the focused panel
if self.panel().has_pending_task() {
return self.mut_panel().do_pending_task(app_state, screen, con, dam);
}
// then the other ones
for idx in 0..self.panels.len().get() {
if idx != self.active_panel_idx {
let panel = &mut self.panels[idx];
if panel.has_pending_task() {
return panel.do_pending_task(app_state, screen, con, dam);
}
}
}
warn!("unexpected lack of pending task");
Ok(())
}
sourcepub fn has_pending_task(&self) -> bool
pub fn has_pending_task(&self) -> bool
Examples found in repository?
src/app/app.rs (line 658)
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
fn do_pending_task(
&mut self,
app_state: &mut AppState,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
let screen = self.screen;
// we start with the focused panel
if self.panel().has_pending_task() {
return self.mut_panel().do_pending_task(app_state, screen, con, dam);
}
// then the other ones
for idx in 0..self.panels.len().get() {
if idx != self.active_panel_idx {
let panel = &mut self.panels[idx];
if panel.has_pending_task() {
return panel.do_pending_task(app_state, screen, con, dam);
}
}
}
warn!("unexpected lack of pending task");
Ok(())
}
fn has_pending_task(&mut self) -> bool {
self.panels.iter().any(|p| p.has_pending_task())
}
sourcepub fn add_event(
&mut self,
w: &mut W,
event: TimedEvent,
app_state: &AppState,
con: &AppContext
) -> Result<Command, ProgramError>
pub fn add_event(
&mut self,
w: &mut W,
event: TimedEvent,
app_state: &AppState,
con: &AppContext
) -> Result<Command, ProgramError>
return a new command Update the input field
Examples found in repository?
src/app/app.rs (line 770)
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
pub fn run(
mut self,
w: &mut W,
con: &mut AppContext,
conf: &Conf,
) -> Result<Option<Launchable>, ProgramError> {
#[cfg(feature = "clipboard")]
{
// different systems have different clipboard capabilities
// and it may be useful to know which one we have
debug!("Clipboard backend: {:?}", terminal_clipboard::get_type());
}
// we listen for events in a separate thread so that we can go on listening
// when a long search is running, and interrupt it if needed
let event_source = EventSource::new()?;
let rx_events = event_source.receiver();
let mut dam = Dam::from(rx_events);
let skin = AppSkin::new(conf, con.launch_args.color == TriBool::No);
let mut app_state = AppState {
stage: Stage::default(),
root: con.initial_root.clone(),
other_panel_path: None,
};
self.screen.clear_bottom_right_char(w, &skin.focused)?;
if let Some(raw_sequence) = &con.launch_args.cmd {
self.tx_seqs
.send(Sequence::new_local(raw_sequence.to_string()))
.unwrap();
}
#[cfg(unix)]
let _server = con.launch_args.listen.as_ref()
.map(|server_name| {
let shared_root = Arc::new(Mutex::new(app_state.root.clone()));
let server = crate::net::Server::new(
server_name,
self.tx_seqs.clone(),
Arc::clone(&shared_root),
);
self.shared_root = Some(shared_root);
server
})
.transpose()?;
loop {
if !self.quitting {
self.display_panels(w, &skin, &app_state, con)?;
time!(
Info,
"pending_tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
#[allow(unused_mut)]
match dam.next(&self.rx_seqs) {
Either::First(Some(event)) => {
info!("event: {:?}", &event);
let mut handled = false;
// app level handling
if let Some((x, y)) = event.as_click() {
if self.clicked_panel_index(x, y) != self.active_panel_idx {
// panel activation click
self.active_panel_idx = self.clicked_panel_index(x, y);
handled = true;
}
} else if let Event::Resize(mut width, mut height) = event.event {
// I don't know why but Crossterm seems to always report an
// understimated size on Windows
#[cfg(windows)]
{
width += 1;
height += 1;
}
self.screen.set_terminal_size(width, height, con);
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
for panel in &mut self.panels {
panel.mut_state().refresh(self.screen, con);
}
handled = true;
}
// event handled by the panel
if !handled {
let cmd = self.mut_panel().add_event(w, event, &app_state, con)?;
debug!("command after add_event: {:?}", &cmd);
self.apply_command(w, cmd, &skin.focused, &mut app_state, con)?;
}
event_source.unblock(self.quitting);
}
Either::First(None) => {
// this is how we quit the application,
// when the input thread is properly closed
break;
}
Either::Second(Some(raw_sequence)) => {
debug!("got command sequence: {:?}", &raw_sequence);
for (input, arg_cmd) in raw_sequence.parse(con)? {
self.mut_panel().set_input_content(&input);
self.apply_command(w, arg_cmd, &skin.focused, &mut app_state, con)?;
if self.quitting {
// is that a 100% safe way of quitting ?
return Ok(self.launch_at_end.take());
} else {
self.display_panels(w, &skin, &app_state, con)?;
time!(
"sequence pending tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
}
}
Either::Second(None) => {
warn!("I didn't expect a None to occur here");
}
}
}
Ok(self.launch_at_end.take())
}
sourcepub fn push_state(&mut self, new_state: Box<dyn PanelState>)
pub fn push_state(&mut self, new_state: Box<dyn PanelState>)
Examples found in repository?
src/app/app.rs (line 467)
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 mut_state(&mut self) -> &mut dyn PanelState
pub fn mut_state(&mut self) -> &mut dyn PanelState
Examples found in repository?
src/app/app.rs (line 132)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 184 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 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
fn mut_state(&mut self) -> &mut dyn PanelState {
self.panels[self.active_panel_idx].mut_state()
}
fn panel(&self) -> &Panel {
&self.panels[self.active_panel_idx]
}
fn mut_panel(&mut self) -> &mut Panel {
unsafe {
self.panels
.as_mut_slice()
.get_unchecked_mut(self.active_panel_idx)
}
}
/// close the panel if it's not the last one
///
/// Return true when the panel has been removed (ie it wasn't the last one)
fn close_panel(&mut self, panel_idx: usize) -> bool {
let active_panel_id = self.panels[self.active_panel_idx].id;
if let Some(preview_id) = self.preview_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != preview_id {
// we don't want to stay with just the preview
return false;
}
}
if let Some(stage_id) = self.stage_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != stage_id {
// we don't want to stay with just the stage
return false;
}
}
if let Ok(removed_panel) = self.panels.remove(panel_idx) {
if self.preview_panel == Some(removed_panel.id) {
self.preview_panel = None;
}
if self.stage_panel == Some(removed_panel.id) {
self.stage_panel = None;
}
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
self.active_panel_idx = self
.panels
.iter()
.position(|p| p.id == active_panel_id)
.unwrap_or(self.panels.len().get() - 1);
true
} else {
false // there's no other panel to go to
}
}
/// remove the top state of the current panel
///
/// Close the panel too if that was its only state.
/// Close nothing and return false if there's not
/// at least two states in the app.
fn remove_state(&mut self) -> bool {
self.panels[self.active_panel_idx].remove_state()
|| self.close_panel(self.active_panel_idx)
}
/// redraw the whole screen. All drawing
/// are supposed to happen here, and only here.
fn display_panels(
&mut self,
w: &mut W,
skin: &AppSkin,
app_state: &AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
self.drawing_count += 1;
for (idx, panel) in self.panels.as_mut_slice().iter_mut().enumerate() {
let active = idx == self.active_panel_idx;
let panel_skin = if active { &skin.focused } else { &skin.unfocused };
let disc = DisplayContext {
count: self.drawing_count,
active,
screen: self.screen,
panel_skin,
state_area: panel.areas.state.clone(),
app_state,
con,
};
time!(
"display panel",
panel.display(w, &disc)?,
);
}
kitty::manager().lock().unwrap().erase_images_before(w, self.drawing_count)?;
w.flush()?;
Ok(())
}
/// if there are exactly two non preview panels, return the selection
/// in the non focused panel
fn get_other_panel_path(&self) -> Option<PathBuf> {
let len = self.panels.len().get();
if len == 3 {
if let Some(preview_id) = self.preview_panel {
for (idx, panel) in self.panels.iter().enumerate() {
if self.active_panel_idx != idx && panel.id != preview_id {
return panel.state().selected_path()
.map(|p| p.to_path_buf());
}
}
}
None
} else if self.panels.len().get() == 2 && self.preview_panel.is_none() {
let non_focused_panel_idx = if self.active_panel_idx == 0 { 1 } else { 0 };
self.panels[non_focused_panel_idx].state().selected_path()
.map(|p| p.to_path_buf())
} else {
None
}
}
/// apply a command. Change the states but don't redraw on screen.
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(())
}
/// update the state of the preview, if there's some
fn update_preview(&mut self, con: &AppContext, refresh: bool) {
let preview_idx = self.preview_panel.and_then(|id| self.panel_id_to_idx(id));
if let Some(preview_idx) = preview_idx {
if let Some(path) = self.state().selected_path() {
let old_path = self.panels[preview_idx].state().selected_path();
if (refresh || Some(path) != old_path) && path.is_file() {
let path = path.to_path_buf();
self.panels[preview_idx].mut_state().set_selected_path(path, con);
}
}
}
}
/// get the index of the panel at x
fn clicked_panel_index(&self, x: u16, _y: u16) -> usize {
let len = self.panels.len().get();
(len * x as usize) / (self.screen.width as usize + 1)
}
/// handle CmdResult::NewPanel
fn new_panel(
&mut self,
state: Box<dyn PanelState>,
purpose: PanelPurpose,
direction: HDir,
is_input_invocation: bool, // if true we clean the input
con: &AppContext,
) -> Result<(), String> {
match state.get_type() {
PanelStateType::Preview if self.preview_panel.is_some() => {
return Err("There can be only one preview panel".to_owned());
// todo replace instead ?
}
PanelStateType::Stage if self.stage_panel.is_some() => {
return Err("There can be only one stage panel".to_owned());
// todo replace instead ?
}
_ => {}
}
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let insertion_idx = if purpose.is_preview() {
self.panels.len().get()
} else if direction == HDir::Right {
self.active_panel_idx + 1
} else {
self.active_panel_idx
};
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
insertion_idx,
self.screen,
with_preview,
);
let panel_id = self.created_panels_count.into();
match state.get_type() {
PanelStateType::Preview => {
self.preview_panel = Some(panel_id);
}
PanelStateType::Stage => {
self.stage_panel = Some(panel_id);
}
_ => {
self.active_panel_idx = insertion_idx;
}
}
let mut panel = Panel::new(panel_id, state, areas, con);
panel.purpose = purpose;
self.created_panels_count += 1;
self.panels.insert(insertion_idx, panel);
Ok(())
}
/// do the pending tasks, if any, and refresh the screen accordingly
fn do_pending_tasks(
&mut self,
w: &mut W,
skin: &AppSkin,
dam: &mut Dam,
app_state: &mut AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
while self.has_pending_task() && !dam.has_event() {
let error = self.do_pending_task(app_state, con, dam).err();
self.update_preview(con, false); // the selection may have changed
if let Some(error) = &error {
self.mut_panel().set_error(error.to_string());
//} else {
// // the refresh here removes the toggle status message. Not
// // sure there's a case it's useful
// let app_cmd_context = AppCmdContext {
// panel_skin: &skin.focused,
// 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);
}
self.display_panels(w, skin, app_state, con)?;
if error.is_some() {
return Ok(()); // breaking pending tasks chain on first error/interruption
}
}
Ok(())
}
/// Do the next pending task
fn do_pending_task(
&mut self,
app_state: &mut AppState,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
let screen = self.screen;
// we start with the focused panel
if self.panel().has_pending_task() {
return self.mut_panel().do_pending_task(app_state, screen, con, dam);
}
// then the other ones
for idx in 0..self.panels.len().get() {
if idx != self.active_panel_idx {
let panel = &mut self.panels[idx];
if panel.has_pending_task() {
return panel.do_pending_task(app_state, screen, con, dam);
}
}
}
warn!("unexpected lack of pending task");
Ok(())
}
fn has_pending_task(&mut self) -> bool {
self.panels.iter().any(|p| p.has_pending_task())
}
/// This is the main loop of the application
pub fn run(
mut self,
w: &mut W,
con: &mut AppContext,
conf: &Conf,
) -> Result<Option<Launchable>, ProgramError> {
#[cfg(feature = "clipboard")]
{
// different systems have different clipboard capabilities
// and it may be useful to know which one we have
debug!("Clipboard backend: {:?}", terminal_clipboard::get_type());
}
// we listen for events in a separate thread so that we can go on listening
// when a long search is running, and interrupt it if needed
let event_source = EventSource::new()?;
let rx_events = event_source.receiver();
let mut dam = Dam::from(rx_events);
let skin = AppSkin::new(conf, con.launch_args.color == TriBool::No);
let mut app_state = AppState {
stage: Stage::default(),
root: con.initial_root.clone(),
other_panel_path: None,
};
self.screen.clear_bottom_right_char(w, &skin.focused)?;
if let Some(raw_sequence) = &con.launch_args.cmd {
self.tx_seqs
.send(Sequence::new_local(raw_sequence.to_string()))
.unwrap();
}
#[cfg(unix)]
let _server = con.launch_args.listen.as_ref()
.map(|server_name| {
let shared_root = Arc::new(Mutex::new(app_state.root.clone()));
let server = crate::net::Server::new(
server_name,
self.tx_seqs.clone(),
Arc::clone(&shared_root),
);
self.shared_root = Some(shared_root);
server
})
.transpose()?;
loop {
if !self.quitting {
self.display_panels(w, &skin, &app_state, con)?;
time!(
Info,
"pending_tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
#[allow(unused_mut)]
match dam.next(&self.rx_seqs) {
Either::First(Some(event)) => {
info!("event: {:?}", &event);
let mut handled = false;
// app level handling
if let Some((x, y)) = event.as_click() {
if self.clicked_panel_index(x, y) != self.active_panel_idx {
// panel activation click
self.active_panel_idx = self.clicked_panel_index(x, y);
handled = true;
}
} else if let Event::Resize(mut width, mut height) = event.event {
// I don't know why but Crossterm seems to always report an
// understimated size on Windows
#[cfg(windows)]
{
width += 1;
height += 1;
}
self.screen.set_terminal_size(width, height, con);
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
for panel in &mut self.panels {
panel.mut_state().refresh(self.screen, con);
}
handled = true;
}
// event handled by the panel
if !handled {
let cmd = self.mut_panel().add_event(w, event, &app_state, con)?;
debug!("command after add_event: {:?}", &cmd);
self.apply_command(w, cmd, &skin.focused, &mut app_state, con)?;
}
event_source.unblock(self.quitting);
}
Either::First(None) => {
// this is how we quit the application,
// when the input thread is properly closed
break;
}
Either::Second(Some(raw_sequence)) => {
debug!("got command sequence: {:?}", &raw_sequence);
for (input, arg_cmd) in raw_sequence.parse(con)? {
self.mut_panel().set_input_content(&input);
self.apply_command(w, arg_cmd, &skin.focused, &mut app_state, con)?;
if self.quitting {
// is that a 100% safe way of quitting ?
return Ok(self.launch_at_end.take());
} else {
self.display_panels(w, &skin, &app_state, con)?;
time!(
"sequence pending tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
}
}
Either::Second(None) => {
warn!("I didn't expect a None to occur here");
}
}
}
Ok(self.launch_at_end.take())
}
More examples
src/app/panel.rs (line 119)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 184 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
pub fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
self.mut_state().do_pending_task(app_state, screen, con, dam)
}
pub fn has_pending_task(&self) -> bool {
self.state().get_pending_task().is_some()
}
/// return a new command
/// Update the input field
pub fn add_event(
&mut self,
w: &mut W,
event: TimedEvent,
app_state: &AppState,
con: &AppContext,
) -> Result<Command, ProgramError> {
let sel_info = self.states[self.states.len() - 1].sel_info(app_state);
let mode = self.state().get_mode();
let panel_state_type = self.state().get_type();
self.input.on_event(w, event, con, sel_info, app_state, mode, panel_state_type)
}
pub fn push_state(&mut self, new_state: Box<dyn PanelState>) {
self.input.set_content(&new_state.get_starting_input());
self.states.push(new_state);
}
pub fn mut_state(&mut self) -> &mut dyn PanelState {
self.states.last_mut().unwrap().as_mut()
}
pub fn state(&self) -> &dyn PanelState {
self.states.last().unwrap().as_ref()
}
pub fn clear_input(&mut self) {
self.input.set_content("");
}
/// remove the verb invocation from the input but keep
/// the filter if there's one
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);
}
}
/// return true when the element has been removed
pub fn remove_state(&mut self) -> bool {
if self.states.len() > 1 {
self.states.pop();
self.input.set_content(&self.state().get_starting_input());
true
} else {
false
}
}
/// render the whole panel (state, status, purpose, input, flags)
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext,
) -> Result<(), ProgramError> {
self.mut_state().display(w, disc)?;
if disc.active || !WIDE_STATUS {
self.write_status(w, disc.panel_skin, disc.screen)?;
}
let mut input_area = self.areas.input.clone();
if disc.active {
self.write_purpose(w, disc.panel_skin, disc.screen, disc.con)?;
let flags = self.state().get_flags();
let input_content_len = self.input.get_content().len() as u16;
let flags_len = flags_display::visible_width(&flags);
if input_area.width > input_content_len + 1 + flags_len {
input_area.width -= flags_len + 1;
disc.screen.goto(w, input_area.left + input_area.width, input_area.top)?;
flags_display::write(w, &flags, disc.panel_skin)?;
}
}
self.input.display(w, disc.active, self.state().get_mode(), input_area, disc.panel_skin)?;
Ok(())
}
sourcepub fn state(&self) -> &dyn PanelState
pub fn state(&self) -> &dyn PanelState
Examples found in repository?
src/app/app.rs (line 129)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 184 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 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551
fn state(&self) -> &dyn PanelState {
self.panels[self.active_panel_idx].state()
}
fn mut_state(&mut self) -> &mut dyn PanelState {
self.panels[self.active_panel_idx].mut_state()
}
fn panel(&self) -> &Panel {
&self.panels[self.active_panel_idx]
}
fn mut_panel(&mut self) -> &mut Panel {
unsafe {
self.panels
.as_mut_slice()
.get_unchecked_mut(self.active_panel_idx)
}
}
/// close the panel if it's not the last one
///
/// Return true when the panel has been removed (ie it wasn't the last one)
fn close_panel(&mut self, panel_idx: usize) -> bool {
let active_panel_id = self.panels[self.active_panel_idx].id;
if let Some(preview_id) = self.preview_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != preview_id {
// we don't want to stay with just the preview
return false;
}
}
if let Some(stage_id) = self.stage_panel {
if self.panels.has_len(2) && self.panels[panel_idx].id != stage_id {
// we don't want to stay with just the stage
return false;
}
}
if let Ok(removed_panel) = self.panels.remove(panel_idx) {
if self.preview_panel == Some(removed_panel.id) {
self.preview_panel = None;
}
if self.stage_panel == Some(removed_panel.id) {
self.stage_panel = None;
}
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
self.active_panel_idx = self
.panels
.iter()
.position(|p| p.id == active_panel_id)
.unwrap_or(self.panels.len().get() - 1);
true
} else {
false // there's no other panel to go to
}
}
/// remove the top state of the current panel
///
/// Close the panel too if that was its only state.
/// Close nothing and return false if there's not
/// at least two states in the app.
fn remove_state(&mut self) -> bool {
self.panels[self.active_panel_idx].remove_state()
|| self.close_panel(self.active_panel_idx)
}
/// redraw the whole screen. All drawing
/// are supposed to happen here, and only here.
fn display_panels(
&mut self,
w: &mut W,
skin: &AppSkin,
app_state: &AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
self.drawing_count += 1;
for (idx, panel) in self.panels.as_mut_slice().iter_mut().enumerate() {
let active = idx == self.active_panel_idx;
let panel_skin = if active { &skin.focused } else { &skin.unfocused };
let disc = DisplayContext {
count: self.drawing_count,
active,
screen: self.screen,
panel_skin,
state_area: panel.areas.state.clone(),
app_state,
con,
};
time!(
"display panel",
panel.display(w, &disc)?,
);
}
kitty::manager().lock().unwrap().erase_images_before(w, self.drawing_count)?;
w.flush()?;
Ok(())
}
/// if there are exactly two non preview panels, return the selection
/// in the non focused panel
fn get_other_panel_path(&self) -> Option<PathBuf> {
let len = self.panels.len().get();
if len == 3 {
if let Some(preview_id) = self.preview_panel {
for (idx, panel) in self.panels.iter().enumerate() {
if self.active_panel_idx != idx && panel.id != preview_id {
return panel.state().selected_path()
.map(|p| p.to_path_buf());
}
}
}
None
} else if self.panels.len().get() == 2 && self.preview_panel.is_none() {
let non_focused_panel_idx = if self.active_panel_idx == 0 { 1 } else { 0 };
self.panels[non_focused_panel_idx].state().selected_path()
.map(|p| p.to_path_buf())
} else {
None
}
}
/// apply a command. Change the states but don't redraw on screen.
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(())
}
/// update the state of the preview, if there's some
fn update_preview(&mut self, con: &AppContext, refresh: bool) {
let preview_idx = self.preview_panel.and_then(|id| self.panel_id_to_idx(id));
if let Some(preview_idx) = preview_idx {
if let Some(path) = self.state().selected_path() {
let old_path = self.panels[preview_idx].state().selected_path();
if (refresh || Some(path) != old_path) && path.is_file() {
let path = path.to_path_buf();
self.panels[preview_idx].mut_state().set_selected_path(path, con);
}
}
}
}
More examples
src/app/panel.rs (line 85)
67 68 69 70 71 72 73 74 75 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 184 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
pub fn apply_command<'c>(
&mut self,
w: &'c mut W,
cmd: &'c Command,
app_state: &mut AppState,
app_cmd_context: &'c AppCmdContext<'c>,
) -> Result<CmdResult, ProgramError> {
let state_idx = self.states.len() - 1;
let cc = CmdContext {
cmd,
app: app_cmd_context,
panel: PanelCmdContext {
areas: &self.areas,
purpose: self.purpose,
},
};
let result = self.states[state_idx].on_command(w, app_state, &cc);
let has_previous_state = self.states.len() > 1;
self.status = self.state().get_status(app_state, &cc, has_previous_state);
result
}
/// called on focusing the panel and before the display,
/// this updates the status from the command read in the input
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);
}
/// do the next pending task stopping as soon as there's an event
/// in the dam
pub fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
self.mut_state().do_pending_task(app_state, screen, con, dam)
}
pub fn has_pending_task(&self) -> bool {
self.state().get_pending_task().is_some()
}
/// return a new command
/// Update the input field
pub fn add_event(
&mut self,
w: &mut W,
event: TimedEvent,
app_state: &AppState,
con: &AppContext,
) -> Result<Command, ProgramError> {
let sel_info = self.states[self.states.len() - 1].sel_info(app_state);
let mode = self.state().get_mode();
let panel_state_type = self.state().get_type();
self.input.on_event(w, event, con, sel_info, app_state, mode, panel_state_type)
}
pub fn push_state(&mut self, new_state: Box<dyn PanelState>) {
self.input.set_content(&new_state.get_starting_input());
self.states.push(new_state);
}
pub fn mut_state(&mut self) -> &mut dyn PanelState {
self.states.last_mut().unwrap().as_mut()
}
pub fn state(&self) -> &dyn PanelState {
self.states.last().unwrap().as_ref()
}
pub fn clear_input(&mut self) {
self.input.set_content("");
}
/// remove the verb invocation from the input but keep
/// the filter if there's one
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);
}
}
/// return true when the element has been removed
pub fn remove_state(&mut self) -> bool {
if self.states.len() > 1 {
self.states.pop();
self.input.set_content(&self.state().get_starting_input());
true
} else {
false
}
}
/// render the whole panel (state, status, purpose, input, flags)
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext,
) -> Result<(), ProgramError> {
self.mut_state().display(w, disc)?;
if disc.active || !WIDE_STATUS {
self.write_status(w, disc.panel_skin, disc.screen)?;
}
let mut input_area = self.areas.input.clone();
if disc.active {
self.write_purpose(w, disc.panel_skin, disc.screen, disc.con)?;
let flags = self.state().get_flags();
let input_content_len = self.input.get_content().len() as u16;
let flags_len = flags_display::visible_width(&flags);
if input_area.width > input_content_len + 1 + flags_len {
input_area.width -= flags_len + 1;
disc.screen.goto(w, input_area.left + input_area.width, input_area.top)?;
flags_display::write(w, &flags, disc.panel_skin)?;
}
}
self.input.display(w, disc.active, self.state().get_mode(), input_area, disc.panel_skin)?;
Ok(())
}
fn write_status(
&self,
w: &mut W,
panel_skin: &PanelSkin,
screen: Screen,
) -> Result<(), ProgramError> {
let task = self.state().get_pending_task();
status_line::write(
w,
task,
&self.status,
&self.areas.status,
panel_skin,
screen,
)
}
sourcepub fn clear_input(&mut self)
pub fn clear_input(&mut self)
Examples found in repository?
src/app/app.rs (line 281)
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 clear_input_invocation(&mut self, con: &AppContext)
pub fn clear_input_invocation(&mut self, con: &AppContext)
remove the verb invocation from the input but keep the filter if there’s one
Examples found in repository?
src/app/app.rs (line 289)
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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
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(())
}
/// update the state of the preview, if there's some
fn update_preview(&mut self, con: &AppContext, refresh: bool) {
let preview_idx = self.preview_panel.and_then(|id| self.panel_id_to_idx(id));
if let Some(preview_idx) = preview_idx {
if let Some(path) = self.state().selected_path() {
let old_path = self.panels[preview_idx].state().selected_path();
if (refresh || Some(path) != old_path) && path.is_file() {
let path = path.to_path_buf();
self.panels[preview_idx].mut_state().set_selected_path(path, con);
}
}
}
}
/// get the index of the panel at x
fn clicked_panel_index(&self, x: u16, _y: u16) -> usize {
let len = self.panels.len().get();
(len * x as usize) / (self.screen.width as usize + 1)
}
/// handle CmdResult::NewPanel
fn new_panel(
&mut self,
state: Box<dyn PanelState>,
purpose: PanelPurpose,
direction: HDir,
is_input_invocation: bool, // if true we clean the input
con: &AppContext,
) -> Result<(), String> {
match state.get_type() {
PanelStateType::Preview if self.preview_panel.is_some() => {
return Err("There can be only one preview panel".to_owned());
// todo replace instead ?
}
PanelStateType::Stage if self.stage_panel.is_some() => {
return Err("There can be only one stage panel".to_owned());
// todo replace instead ?
}
_ => {}
}
if is_input_invocation {
self.mut_panel().clear_input_invocation(con);
}
let insertion_idx = if purpose.is_preview() {
self.panels.len().get()
} else if direction == HDir::Right {
self.active_panel_idx + 1
} else {
self.active_panel_idx
};
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
insertion_idx,
self.screen,
with_preview,
);
let panel_id = self.created_panels_count.into();
match state.get_type() {
PanelStateType::Preview => {
self.preview_panel = Some(panel_id);
}
PanelStateType::Stage => {
self.stage_panel = Some(panel_id);
}
_ => {
self.active_panel_idx = insertion_idx;
}
}
let mut panel = Panel::new(panel_id, state, areas, con);
panel.purpose = purpose;
self.created_panels_count += 1;
self.panels.insert(insertion_idx, panel);
Ok(())
}
sourcepub fn set_input_content(&mut self, content: &str)
pub fn set_input_content(&mut self, content: &str)
Examples found in repository?
src/app/app.rs (line 786)
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
pub fn run(
mut self,
w: &mut W,
con: &mut AppContext,
conf: &Conf,
) -> Result<Option<Launchable>, ProgramError> {
#[cfg(feature = "clipboard")]
{
// different systems have different clipboard capabilities
// and it may be useful to know which one we have
debug!("Clipboard backend: {:?}", terminal_clipboard::get_type());
}
// we listen for events in a separate thread so that we can go on listening
// when a long search is running, and interrupt it if needed
let event_source = EventSource::new()?;
let rx_events = event_source.receiver();
let mut dam = Dam::from(rx_events);
let skin = AppSkin::new(conf, con.launch_args.color == TriBool::No);
let mut app_state = AppState {
stage: Stage::default(),
root: con.initial_root.clone(),
other_panel_path: None,
};
self.screen.clear_bottom_right_char(w, &skin.focused)?;
if let Some(raw_sequence) = &con.launch_args.cmd {
self.tx_seqs
.send(Sequence::new_local(raw_sequence.to_string()))
.unwrap();
}
#[cfg(unix)]
let _server = con.launch_args.listen.as_ref()
.map(|server_name| {
let shared_root = Arc::new(Mutex::new(app_state.root.clone()));
let server = crate::net::Server::new(
server_name,
self.tx_seqs.clone(),
Arc::clone(&shared_root),
);
self.shared_root = Some(shared_root);
server
})
.transpose()?;
loop {
if !self.quitting {
self.display_panels(w, &skin, &app_state, con)?;
time!(
Info,
"pending_tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
#[allow(unused_mut)]
match dam.next(&self.rx_seqs) {
Either::First(Some(event)) => {
info!("event: {:?}", &event);
let mut handled = false;
// app level handling
if let Some((x, y)) = event.as_click() {
if self.clicked_panel_index(x, y) != self.active_panel_idx {
// panel activation click
self.active_panel_idx = self.clicked_panel_index(x, y);
handled = true;
}
} else if let Event::Resize(mut width, mut height) = event.event {
// I don't know why but Crossterm seems to always report an
// understimated size on Windows
#[cfg(windows)]
{
width += 1;
height += 1;
}
self.screen.set_terminal_size(width, height, con);
Areas::resize_all(
self.panels.as_mut_slice(),
self.screen,
self.preview_panel.is_some(),
);
for panel in &mut self.panels {
panel.mut_state().refresh(self.screen, con);
}
handled = true;
}
// event handled by the panel
if !handled {
let cmd = self.mut_panel().add_event(w, event, &app_state, con)?;
debug!("command after add_event: {:?}", &cmd);
self.apply_command(w, cmd, &skin.focused, &mut app_state, con)?;
}
event_source.unblock(self.quitting);
}
Either::First(None) => {
// this is how we quit the application,
// when the input thread is properly closed
break;
}
Either::Second(Some(raw_sequence)) => {
debug!("got command sequence: {:?}", &raw_sequence);
for (input, arg_cmd) in raw_sequence.parse(con)? {
self.mut_panel().set_input_content(&input);
self.apply_command(w, arg_cmd, &skin.focused, &mut app_state, con)?;
if self.quitting {
// is that a 100% safe way of quitting ?
return Ok(self.launch_at_end.take());
} else {
self.display_panels(w, &skin, &app_state, con)?;
time!(
"sequence pending tasks",
self.do_pending_tasks(w, &skin, &mut dam, &mut app_state, con)?,
);
}
}
}
Either::Second(None) => {
warn!("I didn't expect a None to occur here");
}
}
}
Ok(self.launch_at_end.take())
}
sourcepub fn get_input_content(&self) -> String
pub fn get_input_content(&self) -> String
Examples found in repository?
src/app/app.rs (line 315)
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 set_input_arg(&mut self, arg: String)
pub fn set_input_arg(&mut self, arg: String)
change the argument of the verb in the input, if there’s one
Examples found in repository?
src/app/app.rs (line 314)
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 remove_state(&mut self) -> bool
pub fn remove_state(&mut self) -> bool
return true when the element has been removed
sourcepub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext<'_>
) -> Result<(), ProgramError>
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext<'_>
) -> Result<(), ProgramError>
render the whole panel (state, status, purpose, input, flags)
Examples found in repository?
src/app/app.rs (line 219)
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
fn display_panels(
&mut self,
w: &mut W,
skin: &AppSkin,
app_state: &AppState,
con: &AppContext,
) -> Result<(), ProgramError> {
self.drawing_count += 1;
for (idx, panel) in self.panels.as_mut_slice().iter_mut().enumerate() {
let active = idx == self.active_panel_idx;
let panel_skin = if active { &skin.focused } else { &skin.unfocused };
let disc = DisplayContext {
count: self.drawing_count,
active,
screen: self.screen,
panel_skin,
state_area: panel.areas.state.clone(),
app_state,
con,
};
time!(
"display panel",
panel.display(w, &disc)?,
);
}
kitty::manager().lock().unwrap().erase_images_before(w, self.drawing_count)?;
w.flush()?;
Ok(())
}