pub struct Tree {
pub lines: Box<[TreeLine]>,
pub selection: usize,
pub options: TreeOptions,
pub scroll: usize,
pub total_search: bool,
pub git_status: ComputationResult<TreeGitStatus>,
pub build_report: BuildReport,
}
Expand description
The tree which may be displayed, with onle line per visible line of the panel.
In the tree structure, every “node” is just a line, there’s no link from a child to its parent or from a parent to its children.
Fields§
§lines: Box<[TreeLine]>
§selection: usize
§options: TreeOptions
§scroll: usize
§total_search: bool
§git_status: ComputationResult<TreeGitStatus>
§build_report: BuildReport
Implementations§
source§impl Tree
impl Tree
sourcepub fn refresh(
&mut self,
page_height: usize,
con: &AppContext
) -> Result<(), TreeBuildError>
pub fn refresh(
&mut self,
page_height: usize,
con: &AppContext
) -> Result<(), TreeBuildError>
rebuild the tree with the same root, height, and options
Examples found in repository?
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
fn refresh(&mut self, screen: Screen, con: &AppContext) -> Command {
let page_height = BrowserState::page_height(screen) as usize;
// refresh the base tree
if let Err(e) = self.tree.refresh(page_height, con) {
warn!("refreshing base tree failed : {:?}", e);
}
// refresh the filtered tree, if any
Command::from_pattern(match self.filtered_tree {
Some(ref mut tree) => {
if let Err(e) = tree.refresh(page_height, con) {
warn!("refreshing filtered tree failed : {:?}", e);
}
&tree.options.pattern
}
None => &self.tree.options.pattern,
})
}
sourcepub fn after_lines_changed(&mut self)
pub fn after_lines_changed(&mut self)
do what must be done after line additions or removals:
- sort the lines
- compute left branches
Examples found in repository?
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
fn take_as_tree(mut self, out_blines: &[BId]) -> Tree {
let mut lines: Vec<TreeLine> = Vec::new();
for id in out_blines.iter() {
if self.blines[*id].has_match {
// we need to count the children, so we load them
if self.blines[*id].can_enter() && self.blines[*id].children.is_none() {
self.load_children(*id);
}
if let Ok(tree_line) = self.blines[*id].to_tree_line(*id, self.con) {
lines.push(tree_line);
} else {
// I guess the file went missing during tree computation
warn!(
"Error while builind treeline for {:?}",
self.blines[*id].path,
);
}
}
}
let mut tree = Tree {
lines: lines.into_boxed_slice(),
selection: 0,
options: self.options.clone(),
scroll: 0,
total_search: self.total_search,
git_status: ComputationResult::None,
build_report: self.report,
};
tree.after_lines_changed();
if let Some(computer) = self.line_status_computer {
// tree git status is slow to compute, we just mark it should be
// done (later on)
tree.git_status = ComputationResult::NotComputed;
// it would make no sense to keep only files having a git status and
// not display that type
for mut line in tree.lines.iter_mut() {
line.git_status = computer.line_status(&line.path);
}
}
tree
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Examples found in repository?
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
fn no_verb_status(
&self,
has_previous_state: bool,
con: &AppContext,
) -> Status {
let tree = self.displayed_tree();
if tree.is_empty() {
if tree.build_report.hidden_count > 0 {
let mut parts = Vec::new();
if let Some(md) = con.standard_status.all_files_hidden.clone() {
parts.push(md);
}
if let Some(md) = con.standard_status.all_files_git_ignored.clone() {
parts.push(md);
}
if !parts.is_empty() {
return Status::from_error(parts.join(". "));
}
}
}
let mut ssb = con.standard_status.builder(
PanelStateType::Tree,
tree.selected_line().as_selection(),
);
ssb.has_previous_state = has_previous_state;
ssb.is_filtered = self.filtered_tree.is_some();
ssb.has_removed_pattern = false;
ssb.on_tree_root = tree.selection == 0;
ssb.status()
}
sourcepub fn has_branch(&self, line_index: usize, depth: usize) -> bool
pub fn has_branch(&self, line_index: usize, depth: usize) -> bool
Examples found in repository?
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
fn write_branch<'w, W: Write>(
&self,
cw: &mut CropWriter<'w, W>,
line_index: usize,
line: &TreeLine,
selected: bool,
staged: bool,
) -> Result<usize, ProgramError> {
cond_bg!(branch_style, self, selected, self.skin.tree);
let mut branch = String::new();
for depth in 0..line.depth {
branch.push_str(
if line.left_branchs[depth as usize] {
if self.tree.has_branch(line_index + 1, depth as usize) {
// TODO: If a theme is on, remove the horizontal lines
if depth == line.depth - 1 {
if staged {
"├◍─"
} else {
"├──"
}
} else {
"│ "
}
} else {
if staged {
"└◍─"
} else {
"└──"
}
}
} else {
" "
},
);
}
if !branch.is_empty() {
cw.queue_g_string(branch_style, branch)?;
}
Ok(0)
}
sourcepub fn move_selection(&mut self, dy: i32, page_height: usize, cycle: bool)
pub fn move_selection(&mut self, dy: i32, page_height: usize, cycle: bool)
select another line
For example the following one if dy is 1.
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_scroll(&mut self, dy: i32, page_height: usize) -> bool
pub fn try_scroll(&mut self, dy: i32, page_height: usize) -> bool
Scroll the desired amount and return true, or return false if it’s already at end or the tree fits the page
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_y(&mut self, y: usize) -> bool
pub fn try_select_y(&mut self, y: usize) -> bool
try to select a line by index of visible line (works if y+scroll falls on a selectable line)
sourcepub fn make_selection_visible(&mut self, page_height: usize)
pub fn make_selection_visible(&mut self, page_height: usize)
Examples found in repository?
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
pub fn on_path(
path: PathBuf,
tree: &mut Tree,
screen: Screen,
in_new_panel: bool,
) -> CmdResult {
info!("executing :select on path {:?}", &path);
if in_new_panel {
warn!("bang in :select isn't supported yet");
}
if tree.try_select_path(&path) {
tree.make_selection_visible(BrowserState::page_height(screen) as usize);
}
CmdResult::Keep
}
More examples
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 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
pub fn refresh(
&mut self,
page_height: usize,
con: &AppContext,
) -> Result<(), errors::TreeBuildError> {
let builder = TreeBuilder::from(
self.root().to_path_buf(),
self.options.clone(),
page_height,
con,
)?;
let mut tree = builder
.build_tree(
false, // on refresh we always do a non total search
&Dam::unlimited(),
)
.unwrap(); // should not fail
// we save the old selection to try restore it
let selected_path = self.selected_line().path.to_path_buf();
mem::swap(&mut self.lines, &mut tree.lines);
self.scroll = 0;
if !self.try_select_path(&selected_path) {
if self.selection >= self.lines.len() {
self.selection = 0;
}
}
self.make_selection_visible(page_height);
Ok(())
}
/// do what must be done after line additions or removals:
/// - sort the lines
/// - compute left branches
pub fn after_lines_changed(&mut self) {
// we need to order the lines to build the tree.
// It's a little complicated because
// - we want a case insensitive sort
// - we still don't want to confuse the children of AA and Aa
// - a node can come from a not parent node, when we followed a link
let mut bid_parents: FnvHashMap<BId, BId> = FnvHashMap::default();
let mut bid_lines: FnvHashMap<BId, &TreeLine> = FnvHashMap::default();
for line in self.lines[..].iter() {
if let Some(parent_bid) = line.parent_bid {
bid_parents.insert(line.bid, parent_bid);
}
bid_lines.insert(line.bid, line);
}
let mut sort_paths: FnvHashMap<BId, String> = FnvHashMap::default();
for line in self.lines[1..].iter() {
let mut sort_path = String::new();
let mut bid = line.bid;
while let Some(l) = bid_lines.get(&bid) {
let lower_name = l.path.file_name().map_or(
"".to_string(),
|name| name.to_string_lossy().to_lowercase(),
);
let sort_prefix = match self.options.sort {
Sort::TypeDirsFirst => {
if l.is_dir() {
" "
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
Sort::TypeDirsLast => {
if l.is_dir() {
"~~~~~~~~~~~~~~"
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
_ => { "" }
};
sort_path = format!(
"{}{}-{}/{}",
sort_prefix,
lower_name,
bid.index(), // to be sure to separate paths having the same lowercase
sort_path,
);
if let Some(&parent_bid) = bid_parents.get(&bid) {
bid = parent_bid;
} else {
break;
}
}
sort_paths.insert(line.bid, sort_path);
}
self.lines[1..].sort_by_key(|line| sort_paths.get(&line.bid).unwrap());
let mut best_index = 0; // index of the line with the best score
for i in 1..self.lines.len() {
if self.lines[i].score > self.lines[best_index].score {
best_index = i;
}
for d in 0..self.lines[i].left_branchs.len() {
self.lines[i].left_branchs[d] = false;
}
}
// then we discover the branches (for the drawing)
// and we mark the last children as pruning, if they have unlisted brothers
let mut last_parent_index: usize = self.lines.len() + 1;
for end_index in (1..self.lines.len()).rev() {
let depth = (self.lines[end_index].depth - 1) as usize;
let start_index = {
let parent_index = match self.lines[end_index].parent_bid {
Some(parent_bid) => {
let mut index = end_index;
loop {
index -= 1;
if self.lines[index].bid == parent_bid {
break;
}
if index == 0 {
break;
}
}
index
}
None => end_index, // Should not happen
};
if parent_index != last_parent_index {
// the line at end_index is the last listed child of the line at parent_index
let unlisted = self.lines[parent_index].unlisted;
if unlisted > 0 && self.lines[end_index].nb_kept_children == 0 {
if best_index == end_index {
//debug!("Avoiding to prune the line with best score");
} else {
//debug!("turning {:?} into Pruning", self.lines[end_index].path);
self.lines[end_index].line_type = TreeLineType::Pruning;
self.lines[end_index].unlisted = unlisted + 1;
self.lines[end_index].name = format!("{} unlisted", unlisted + 1);
self.lines[parent_index].unlisted = 0;
}
}
last_parent_index = parent_index;
}
parent_index + 1
};
for i in start_index..=end_index {
self.lines[i].left_branchs[depth] = true;
}
}
if self.options.needs_sum() {
time!("fetch_file_sum", self.fetch_regular_file_sums()); // not the dirs, only simple files
self.sort_siblings(); // does nothing when sort mode is None
}
}
pub fn is_empty(&self) -> bool {
self.lines.len() == 1
}
pub fn has_branch(&self, line_index: usize, depth: usize) -> bool {
if line_index >= self.lines.len() {
return false;
}
let line = &self.lines[line_index];
depth < usize::from(line.depth) && line.left_branchs[depth]
}
/// select another line
///
/// For example the following one if dy is 1.
pub fn move_selection(&mut self, dy: i32, page_height: usize, cycle: bool) {
let l = self.lines.len();
// we find the new line to select
loop {
if dy < 0 {
let ady = (-dy) as usize;
if !cycle && self.selection < ady {
break;
}
self.selection = (self.selection + l - ady) % l;
} else {
let dy = dy as usize;
if !cycle && self.selection + dy >= l {
break;
}
self.selection = (self.selection + dy) % l;
}
if self.lines[self.selection].is_selectable() {
break;
}
}
// we adjust the scroll
if l > page_height {
if self.selection < 3 {
self.scroll = 0;
} else if self.selection < self.scroll + 3 {
self.scroll = self.selection - 3;
} else if self.selection + 3 > l {
self.scroll = l - page_height;
} else if self.selection + 3 > self.scroll + page_height {
self.scroll = self.selection + 3 - page_height;
}
}
}
/// Scroll the desired amount and return true, or return false if it's
/// already at end or the tree fits the page
pub fn try_scroll(&mut self, dy: i32, page_height: usize) -> bool {
if self.lines.len() <= page_height {
return false;
}
if dy < 0 { // scroll up
if self.scroll == 0 {
return false;
} else {
let ady = -dy as usize;
if ady < self.scroll {
self.scroll -= ady;
} else {
self.scroll = 0;
}
}
} else { // scroll down
let max = self.lines.len() - page_height;
if self.scroll >= max {
return false;
}
self.scroll = (self.scroll + dy as usize).min(max);
}
self.select_visible_line(page_height);
true
}
/// try to select a line by index of visible line
/// (works if y+scroll falls on a selectable line)
pub fn try_select_y(&mut self, y: usize) -> bool {
let y = y + self.scroll;
if y < self.lines.len() {
if self.lines[y].is_selectable() {
self.selection = y;
return true;
}
}
false
}
/// fix the selection so that it's a selectable visible line
fn select_visible_line(&mut self, page_height: usize) {
if self.selection < self.scroll || self.selection >= self.scroll + page_height {
self.selection = self.scroll;
let l = self.lines.len();
loop {
self.selection = (self.selection + l + 1) % l;
if self.lines[self.selection].is_selectable() {
break;
}
}
}
}
pub fn make_selection_visible(&mut self, page_height: usize) {
if page_height >= self.lines.len() || self.selection < 3 {
self.scroll = 0;
} else if self.selection <= self.scroll {
self.scroll = self.selection - 2;
} else if self.selection > self.lines.len() - 2 {
self.scroll = self.lines.len() - page_height;
} else if self.selection >= self.scroll + page_height {
self.scroll = self.selection + 2 - page_height;
}
}
pub fn selected_line(&self) -> &TreeLine {
&self.lines[self.selection]
}
pub fn root(&self) -> &PathBuf {
&self.lines[0].path
}
/// select the line with the best matching score
pub fn try_select_best_match(&mut self) {
let mut best_score = 0;
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if best_score > line.score {
continue;
}
if line.score == best_score {
// in case of equal scores, we prefer the shortest path
if self.lines[idx].depth >= self.lines[self.selection].depth {
continue;
}
}
best_score = line.score;
self.selection = idx;
}
}
/// return true when we could select the given path
pub fn try_select_path(&mut self, path: &Path) -> bool {
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if path == line.path {
self.selection = idx;
return true;
}
}
false
}
pub fn try_select_first(&mut self) -> bool {
for idx in 0..self.lines.len() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.scroll = 0;
return true;
}
}
false
}
pub fn try_select_last(&mut self, page_height: usize) -> bool {
for idx in (0..self.lines.len()).rev() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_previous_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_next_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_previous_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_next_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
fn no_verb_status(
&self,
has_previous_state: bool,
con: &AppContext,
) -> Status {
let tree = self.displayed_tree();
if tree.is_empty() {
if tree.build_report.hidden_count > 0 {
let mut parts = Vec::new();
if let Some(md) = con.standard_status.all_files_hidden.clone() {
parts.push(md);
}
if let Some(md) = con.standard_status.all_files_git_ignored.clone() {
parts.push(md);
}
if !parts.is_empty() {
return Status::from_error(parts.join(". "));
}
}
}
let mut ssb = con.standard_status.builder(
PanelStateType::Tree,
tree.selected_line().as_selection(),
);
ssb.has_previous_state = has_previous_state;
ssb.is_filtered = self.filtered_tree.is_some();
ssb.has_removed_pattern = false;
ssb.on_tree_root = tree.selection == 0;
ssb.status()
}
/// do some work, totally or partially, if there's some to do.
/// Stop as soon as the dam asks for interruption
fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
if let Some(pending_task) = self.pending_task.take() {
match pending_task {
BrowserTask::Search { pattern, total } => {
let pattern_str = pattern.raw.clone();
let mut options = self.tree.options.clone();
options.pattern = pattern;
let root = self.tree.root().clone();
let page_height = BrowserState::page_height(screen) as usize;
let builder = TreeBuilder::from(root, options, page_height, con)?;
let filtered_tree = time!(
Info,
"tree filtering",
&pattern_str,
builder.build_tree(total, dam),
);
if let Ok(mut ft) = filtered_tree {
ft.try_select_best_match();
ft.make_selection_visible(BrowserState::page_height(screen));
self.filtered_tree = Some(ft);
}
}
BrowserTask::StageAll(pattern) => {
let tree = self.displayed_tree();
let root = tree.root().clone();
let mut options = tree.options.clone();
let total_search = true;
options.pattern = pattern; // should be the same
let builder = TreeBuilder::from(root, options, con.max_staged_count, con);
let mut paths = builder
.and_then(|mut builder| {
builder.matches_max = Some(con.max_staged_count);
time!(builder.build_paths(
total_search,
dam,
|line| line.file_type.is_file(),
))
})?;
for path in paths.drain(..) {
app_state.stage.add(path);
}
}
}
} else if self.displayed_tree().is_missing_git_status_computation() {
let root_path = self.displayed_tree().root();
let git_status = git::get_tree_status(root_path, dam);
self.displayed_tree_mut().git_status = git_status;
} else {
self.displayed_tree_mut().fetch_some_missing_dir_sum(dam, con);
}
Ok(())
}
sourcepub fn selected_line(&self) -> &TreeLine
pub fn selected_line(&self) -> &TreeLine
Examples found in repository?
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 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
fn modified(
&self,
screen: Screen,
root: PathBuf,
options: TreeOptions,
message: Option<&'static str>,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut new_state = BrowserState::new(root, options, screen, con, &Dam::unlimited());
if let Ok(bs) = &mut new_state {
if tree.selection != 0 {
bs.displayed_tree_mut().try_select_path(&tree.selected_line().path);
}
}
CmdResult::from_optional_state(
new_state,
message,
in_new_panel,
)
}
pub fn root(&self) -> &Path {
self.tree.root()
}
pub fn page_height(screen: Screen) -> usize {
screen.height as usize - 2 // br shouldn't be displayed when the screen is smaller
}
/// return a reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree(&self) -> &Tree {
self.filtered_tree.as_ref().unwrap_or(&self.tree)
}
/// return a mutable reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree_mut(&mut self) -> &mut Tree {
self.filtered_tree.as_mut().unwrap_or(&mut self.tree)
}
pub fn open_selection_stay_in_broot(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
keep_pattern: bool,
) -> Result<CmdResult, ProgramError> {
let tree = self.displayed_tree();
let line = tree.selected_line();
let mut target = line.target().to_path_buf();
if line.is_dir() {
if tree.selection == 0 {
// opening the root would be going to where we already are.
// We go up one level instead
if let Some(parent) = target.parent() {
target = PathBuf::from(parent);
}
}
let dam = Dam::unlimited();
Ok(CmdResult::from_optional_state(
BrowserState::new(
target,
if keep_pattern {
tree.options.clone()
} else {
tree.options.without_pattern()
},
screen,
con,
&dam,
),
None,
in_new_panel,
))
} else {
match opener::open(&target) {
Ok(exit_status) => {
info!("open returned with exit_status {:?}", exit_status);
Ok(CmdResult::Keep)
}
Err(e) => Ok(CmdResult::error(format!("{:?}", e))),
}
}
}
pub fn go_to_parent(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
) -> CmdResult {
match &self.displayed_tree().selected_line().path.parent() {
Some(path) => CmdResult::from_optional_state(
BrowserState::new(
path.to_path_buf(),
self.displayed_tree().options.without_pattern(),
screen,
con,
&Dam::unlimited(),
),
None,
in_new_panel,
),
None => CmdResult::error("no parent found"),
}
}
}
impl PanelState for BrowserState {
fn tree_root(&self) -> Option<&Path> {
Some(self.root())
}
fn get_type(&self) -> PanelStateType {
PanelStateType::Tree
}
fn set_mode(&mut self, mode: Mode) {
self.mode = mode;
}
fn get_mode(&self) -> Mode {
self.mode
}
fn get_pending_task(&self) -> Option<&'static str> {
if self.displayed_tree().has_dir_missing_sum() {
Some("computing stats")
} else if self.displayed_tree().is_missing_git_status_computation() {
Some("computing git status")
} else {
self
.pending_task.as_ref().map(|task| match task {
BrowserTask::Search{ .. } => "searching",
BrowserTask::StageAll(_) => "staging",
})
}
}
fn selected_path(&self) -> Option<&Path> {
Some(&self.displayed_tree().selected_line().path)
}
fn selection(&self) -> Option<Selection<'_>> {
let tree = self.displayed_tree();
let mut selection = tree.selected_line().as_selection();
selection.line = tree.options.pattern.pattern
.get_match_line_count(selection.path)
.unwrap_or(0);
Some(selection)
}
fn tree_options(&self) -> TreeOptions {
self.displayed_tree().options.clone()
}
/// build a cmdResult asking for the addition of a new state
/// being a browser state similar to the current one but with
/// different options
fn with_new_options(
&mut self,
screen: Screen,
change_options: &dyn Fn(&mut TreeOptions) -> &'static str,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut options = tree.options.clone();
let message = change_options(&mut options);
let message = Some(message);
self.modified(
screen,
tree.root().clone(),
options,
message,
in_new_panel,
con,
)
}
fn clear_pending(&mut self) {
self.pending_task = None;
}
fn on_click(
&mut self,
_x: u16,
y: u16,
_screen: Screen,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
self.displayed_tree_mut().try_select_y(y as usize);
Ok(CmdResult::Keep)
}
fn on_double_click(
&mut self,
_x: u16,
y: u16,
screen: Screen,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if self.displayed_tree().selection == y as usize {
self.open_selection_stay_in_broot(screen, con, false, false)
} else {
// A double click always come after a simple click at
// same position. If it's not the selected line, it means
// the click wasn't on a selectable/openable tree line
Ok(CmdResult::Keep)
}
}
fn on_pattern(
&mut self,
pat: InputPattern,
_app_state: &AppState,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if pat.is_none() {
self.filtered_tree = None;
}
if let Some(filtered_tree) = &self.filtered_tree {
if pat != filtered_tree.options.pattern {
self.search(pat, false);
}
} else {
self.search(pat, false);
}
Ok(CmdResult::Keep)
}
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
fn no_verb_status(
&self,
has_previous_state: bool,
con: &AppContext,
) -> Status {
let tree = self.displayed_tree();
if tree.is_empty() {
if tree.build_report.hidden_count > 0 {
let mut parts = Vec::new();
if let Some(md) = con.standard_status.all_files_hidden.clone() {
parts.push(md);
}
if let Some(md) = con.standard_status.all_files_git_ignored.clone() {
parts.push(md);
}
if !parts.is_empty() {
return Status::from_error(parts.join(". "));
}
}
}
let mut ssb = con.standard_status.builder(
PanelStateType::Tree,
tree.selected_line().as_selection(),
);
ssb.has_previous_state = has_previous_state;
ssb.is_filtered = self.filtered_tree.is_some();
ssb.has_removed_pattern = false;
ssb.on_tree_root = tree.selection == 0;
ssb.status()
}
More examples
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 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
pub fn refresh(
&mut self,
page_height: usize,
con: &AppContext,
) -> Result<(), errors::TreeBuildError> {
let builder = TreeBuilder::from(
self.root().to_path_buf(),
self.options.clone(),
page_height,
con,
)?;
let mut tree = builder
.build_tree(
false, // on refresh we always do a non total search
&Dam::unlimited(),
)
.unwrap(); // should not fail
// we save the old selection to try restore it
let selected_path = self.selected_line().path.to_path_buf();
mem::swap(&mut self.lines, &mut tree.lines);
self.scroll = 0;
if !self.try_select_path(&selected_path) {
if self.selection >= self.lines.len() {
self.selection = 0;
}
}
self.make_selection_visible(page_height);
Ok(())
}
/// do what must be done after line additions or removals:
/// - sort the lines
/// - compute left branches
pub fn after_lines_changed(&mut self) {
// we need to order the lines to build the tree.
// It's a little complicated because
// - we want a case insensitive sort
// - we still don't want to confuse the children of AA and Aa
// - a node can come from a not parent node, when we followed a link
let mut bid_parents: FnvHashMap<BId, BId> = FnvHashMap::default();
let mut bid_lines: FnvHashMap<BId, &TreeLine> = FnvHashMap::default();
for line in self.lines[..].iter() {
if let Some(parent_bid) = line.parent_bid {
bid_parents.insert(line.bid, parent_bid);
}
bid_lines.insert(line.bid, line);
}
let mut sort_paths: FnvHashMap<BId, String> = FnvHashMap::default();
for line in self.lines[1..].iter() {
let mut sort_path = String::new();
let mut bid = line.bid;
while let Some(l) = bid_lines.get(&bid) {
let lower_name = l.path.file_name().map_or(
"".to_string(),
|name| name.to_string_lossy().to_lowercase(),
);
let sort_prefix = match self.options.sort {
Sort::TypeDirsFirst => {
if l.is_dir() {
" "
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
Sort::TypeDirsLast => {
if l.is_dir() {
"~~~~~~~~~~~~~~"
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
_ => { "" }
};
sort_path = format!(
"{}{}-{}/{}",
sort_prefix,
lower_name,
bid.index(), // to be sure to separate paths having the same lowercase
sort_path,
);
if let Some(&parent_bid) = bid_parents.get(&bid) {
bid = parent_bid;
} else {
break;
}
}
sort_paths.insert(line.bid, sort_path);
}
self.lines[1..].sort_by_key(|line| sort_paths.get(&line.bid).unwrap());
let mut best_index = 0; // index of the line with the best score
for i in 1..self.lines.len() {
if self.lines[i].score > self.lines[best_index].score {
best_index = i;
}
for d in 0..self.lines[i].left_branchs.len() {
self.lines[i].left_branchs[d] = false;
}
}
// then we discover the branches (for the drawing)
// and we mark the last children as pruning, if they have unlisted brothers
let mut last_parent_index: usize = self.lines.len() + 1;
for end_index in (1..self.lines.len()).rev() {
let depth = (self.lines[end_index].depth - 1) as usize;
let start_index = {
let parent_index = match self.lines[end_index].parent_bid {
Some(parent_bid) => {
let mut index = end_index;
loop {
index -= 1;
if self.lines[index].bid == parent_bid {
break;
}
if index == 0 {
break;
}
}
index
}
None => end_index, // Should not happen
};
if parent_index != last_parent_index {
// the line at end_index is the last listed child of the line at parent_index
let unlisted = self.lines[parent_index].unlisted;
if unlisted > 0 && self.lines[end_index].nb_kept_children == 0 {
if best_index == end_index {
//debug!("Avoiding to prune the line with best score");
} else {
//debug!("turning {:?} into Pruning", self.lines[end_index].path);
self.lines[end_index].line_type = TreeLineType::Pruning;
self.lines[end_index].unlisted = unlisted + 1;
self.lines[end_index].name = format!("{} unlisted", unlisted + 1);
self.lines[parent_index].unlisted = 0;
}
}
last_parent_index = parent_index;
}
parent_index + 1
};
for i in start_index..=end_index {
self.lines[i].left_branchs[depth] = true;
}
}
if self.options.needs_sum() {
time!("fetch_file_sum", self.fetch_regular_file_sums()); // not the dirs, only simple files
self.sort_siblings(); // does nothing when sort mode is None
}
}
pub fn is_empty(&self) -> bool {
self.lines.len() == 1
}
pub fn has_branch(&self, line_index: usize, depth: usize) -> bool {
if line_index >= self.lines.len() {
return false;
}
let line = &self.lines[line_index];
depth < usize::from(line.depth) && line.left_branchs[depth]
}
/// select another line
///
/// For example the following one if dy is 1.
pub fn move_selection(&mut self, dy: i32, page_height: usize, cycle: bool) {
let l = self.lines.len();
// we find the new line to select
loop {
if dy < 0 {
let ady = (-dy) as usize;
if !cycle && self.selection < ady {
break;
}
self.selection = (self.selection + l - ady) % l;
} else {
let dy = dy as usize;
if !cycle && self.selection + dy >= l {
break;
}
self.selection = (self.selection + dy) % l;
}
if self.lines[self.selection].is_selectable() {
break;
}
}
// we adjust the scroll
if l > page_height {
if self.selection < 3 {
self.scroll = 0;
} else if self.selection < self.scroll + 3 {
self.scroll = self.selection - 3;
} else if self.selection + 3 > l {
self.scroll = l - page_height;
} else if self.selection + 3 > self.scroll + page_height {
self.scroll = self.selection + 3 - page_height;
}
}
}
/// Scroll the desired amount and return true, or return false if it's
/// already at end or the tree fits the page
pub fn try_scroll(&mut self, dy: i32, page_height: usize) -> bool {
if self.lines.len() <= page_height {
return false;
}
if dy < 0 { // scroll up
if self.scroll == 0 {
return false;
} else {
let ady = -dy as usize;
if ady < self.scroll {
self.scroll -= ady;
} else {
self.scroll = 0;
}
}
} else { // scroll down
let max = self.lines.len() - page_height;
if self.scroll >= max {
return false;
}
self.scroll = (self.scroll + dy as usize).min(max);
}
self.select_visible_line(page_height);
true
}
/// try to select a line by index of visible line
/// (works if y+scroll falls on a selectable line)
pub fn try_select_y(&mut self, y: usize) -> bool {
let y = y + self.scroll;
if y < self.lines.len() {
if self.lines[y].is_selectable() {
self.selection = y;
return true;
}
}
false
}
/// fix the selection so that it's a selectable visible line
fn select_visible_line(&mut self, page_height: usize) {
if self.selection < self.scroll || self.selection >= self.scroll + page_height {
self.selection = self.scroll;
let l = self.lines.len();
loop {
self.selection = (self.selection + l + 1) % l;
if self.lines[self.selection].is_selectable() {
break;
}
}
}
}
pub fn make_selection_visible(&mut self, page_height: usize) {
if page_height >= self.lines.len() || self.selection < 3 {
self.scroll = 0;
} else if self.selection <= self.scroll {
self.scroll = self.selection - 2;
} else if self.selection > self.lines.len() - 2 {
self.scroll = self.lines.len() - page_height;
} else if self.selection >= self.scroll + page_height {
self.scroll = self.selection + 2 - page_height;
}
}
pub fn selected_line(&self) -> &TreeLine {
&self.lines[self.selection]
}
pub fn root(&self) -> &PathBuf {
&self.lines[0].path
}
/// select the line with the best matching score
pub fn try_select_best_match(&mut self) {
let mut best_score = 0;
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if best_score > line.score {
continue;
}
if line.score == best_score {
// in case of equal scores, we prefer the shortest path
if self.lines[idx].depth >= self.lines[self.selection].depth {
continue;
}
}
best_score = line.score;
self.selection = idx;
}
}
/// return true when we could select the given path
pub fn try_select_path(&mut self, path: &Path) -> bool {
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if path == line.path {
self.selection = idx;
return true;
}
}
false
}
pub fn try_select_first(&mut self) -> bool {
for idx in 0..self.lines.len() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.scroll = 0;
return true;
}
}
false
}
pub fn try_select_last(&mut self, page_height: usize) -> bool {
for idx in (0..self.lines.len()).rev() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_previous_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_next_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_previous_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_next_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn has_dir_missing_sum(&self) -> bool {
self.options.needs_sum()
&& self
.lines
.iter()
.any(|line| line.line_type == TreeLineType::Dir && line.sum.is_none())
}
pub fn is_missing_git_status_computation(&self) -> bool {
self.git_status.is_not_computed()
}
/// fetch the file_sums of regular files (thus avoiding the
/// long computation which is needed for directories)
pub fn fetch_regular_file_sums(&mut self) {
for i in 1..self.lines.len() {
match self.lines[i].line_type {
TreeLineType::Dir | TreeLineType::Pruning => {}
_ => {
self.lines[i].sum = Some(FileSum::from_file(&self.lines[i].path));
}
}
}
self.sort_siblings();
}
/// compute the file_sum of one directory
///
/// To compute the size of all of them, this should be called until
/// has_dir_missing_sum returns false
pub fn fetch_some_missing_dir_sum(&mut self, dam: &Dam, con: &AppContext) {
// we prefer to compute the root directory last: its computation
// is faster when its first level children are already computed
for i in (0..self.lines.len()).rev() {
if self.lines[i].sum.is_none() && self.lines[i].line_type == TreeLineType::Dir {
self.lines[i].sum = FileSum::from_dir(&self.lines[i].path, dam, con);
self.sort_siblings();
return;
}
}
}
/// Sort files according to the sort option
///
/// (does nothing if it's None)
fn sort_siblings(&mut self) {
match self.options.sort {
Sort::Count => {
// we'll try to keep the same path selected
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let acount = a.sum.map_or(0, |s| s.to_count());
let bcount = b.sum.map_or(0, |s| s.to_count());
bcount.cmp(&acount)
});
self.try_select_path(&selected_path);
}
Sort::Date => {
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let adate = a.sum.map_or(0, |s| s.to_seconds());
let bdate = b.sum.map_or(0, |s| s.to_seconds());
bdate.cmp(&adate)
});
self.try_select_path(&selected_path);
}
Sort::Size => {
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let asize = a.sum.map_or(0, |s| s.to_size());
let bsize = b.sum.map_or(0, |s| s.to_size());
bsize.cmp(&asize)
});
self.try_select_path(&selected_path);
}
_ => {}
}
}
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
pub fn on_internal(
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
tree: &mut Tree,
app_state: & AppState,
cc: &CmdContext,
) -> CmdResult {
let screen = cc.app.screen;
info!(
"internal_select.on_internal internal_exec={:?} input_invocation={:?} trygger_type={:?}",
internal_exec,
input_invocation,
trigger_type,
);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
let input_arg = input_invocation.as_ref()
.and_then(|invocation| invocation.args.as_ref());
match trigger_type {
TriggerType::Input(verb) => {
let path = path_from_input(
verb,
internal_exec,
&tree.selected_line().path,
input_arg,
app_state,
);
on_path(path, tree, screen, bang)
}
_ => {
// the :select internal was triggered by a key
if let Some(arg) = &internal_exec.arg {
// the internal_execution specifies the path to use
// (it may come from a configured verb whose execution is
// `:select some/path`).
// The given path may be relative hence the need for the
// state's selection
let path = path::path_from(
&tree.selected_line().path,
PathAnchor::Unspecified,
arg,
);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
on_path(path, tree, screen, bang)
} else {
// there's nothing really to do here
CmdResult::Keep
}
}
}
}
sourcepub fn root(&self) -> &PathBuf
pub fn root(&self) -> &PathBuf
Examples found in repository?
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 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
pub fn root(&self) -> &Path {
self.tree.root()
}
pub fn page_height(screen: Screen) -> usize {
screen.height as usize - 2 // br shouldn't be displayed when the screen is smaller
}
/// return a reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree(&self) -> &Tree {
self.filtered_tree.as_ref().unwrap_or(&self.tree)
}
/// return a mutable reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree_mut(&mut self) -> &mut Tree {
self.filtered_tree.as_mut().unwrap_or(&mut self.tree)
}
pub fn open_selection_stay_in_broot(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
keep_pattern: bool,
) -> Result<CmdResult, ProgramError> {
let tree = self.displayed_tree();
let line = tree.selected_line();
let mut target = line.target().to_path_buf();
if line.is_dir() {
if tree.selection == 0 {
// opening the root would be going to where we already are.
// We go up one level instead
if let Some(parent) = target.parent() {
target = PathBuf::from(parent);
}
}
let dam = Dam::unlimited();
Ok(CmdResult::from_optional_state(
BrowserState::new(
target,
if keep_pattern {
tree.options.clone()
} else {
tree.options.without_pattern()
},
screen,
con,
&dam,
),
None,
in_new_panel,
))
} else {
match opener::open(&target) {
Ok(exit_status) => {
info!("open returned with exit_status {:?}", exit_status);
Ok(CmdResult::Keep)
}
Err(e) => Ok(CmdResult::error(format!("{:?}", e))),
}
}
}
pub fn go_to_parent(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
) -> CmdResult {
match &self.displayed_tree().selected_line().path.parent() {
Some(path) => CmdResult::from_optional_state(
BrowserState::new(
path.to_path_buf(),
self.displayed_tree().options.without_pattern(),
screen,
con,
&Dam::unlimited(),
),
None,
in_new_panel,
),
None => CmdResult::error("no parent found"),
}
}
}
impl PanelState for BrowserState {
fn tree_root(&self) -> Option<&Path> {
Some(self.root())
}
fn get_type(&self) -> PanelStateType {
PanelStateType::Tree
}
fn set_mode(&mut self, mode: Mode) {
self.mode = mode;
}
fn get_mode(&self) -> Mode {
self.mode
}
fn get_pending_task(&self) -> Option<&'static str> {
if self.displayed_tree().has_dir_missing_sum() {
Some("computing stats")
} else if self.displayed_tree().is_missing_git_status_computation() {
Some("computing git status")
} else {
self
.pending_task.as_ref().map(|task| match task {
BrowserTask::Search{ .. } => "searching",
BrowserTask::StageAll(_) => "staging",
})
}
}
fn selected_path(&self) -> Option<&Path> {
Some(&self.displayed_tree().selected_line().path)
}
fn selection(&self) -> Option<Selection<'_>> {
let tree = self.displayed_tree();
let mut selection = tree.selected_line().as_selection();
selection.line = tree.options.pattern.pattern
.get_match_line_count(selection.path)
.unwrap_or(0);
Some(selection)
}
fn tree_options(&self) -> TreeOptions {
self.displayed_tree().options.clone()
}
/// build a cmdResult asking for the addition of a new state
/// being a browser state similar to the current one but with
/// different options
fn with_new_options(
&mut self,
screen: Screen,
change_options: &dyn Fn(&mut TreeOptions) -> &'static str,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut options = tree.options.clone();
let message = change_options(&mut options);
let message = Some(message);
self.modified(
screen,
tree.root().clone(),
options,
message,
in_new_panel,
con,
)
}
fn clear_pending(&mut self) {
self.pending_task = None;
}
fn on_click(
&mut self,
_x: u16,
y: u16,
_screen: Screen,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
self.displayed_tree_mut().try_select_y(y as usize);
Ok(CmdResult::Keep)
}
fn on_double_click(
&mut self,
_x: u16,
y: u16,
screen: Screen,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if self.displayed_tree().selection == y as usize {
self.open_selection_stay_in_broot(screen, con, false, false)
} else {
// A double click always come after a simple click at
// same position. If it's not the selected line, it means
// the click wasn't on a selectable/openable tree line
Ok(CmdResult::Keep)
}
}
fn on_pattern(
&mut self,
pat: InputPattern,
_app_state: &AppState,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if pat.is_none() {
self.filtered_tree = None;
}
if let Some(filtered_tree) = &self.filtered_tree {
if pat != filtered_tree.options.pattern {
self.search(pat, false);
}
} else {
self.search(pat, false);
}
Ok(CmdResult::Keep)
}
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
fn no_verb_status(
&self,
has_previous_state: bool,
con: &AppContext,
) -> Status {
let tree = self.displayed_tree();
if tree.is_empty() {
if tree.build_report.hidden_count > 0 {
let mut parts = Vec::new();
if let Some(md) = con.standard_status.all_files_hidden.clone() {
parts.push(md);
}
if let Some(md) = con.standard_status.all_files_git_ignored.clone() {
parts.push(md);
}
if !parts.is_empty() {
return Status::from_error(parts.join(". "));
}
}
}
let mut ssb = con.standard_status.builder(
PanelStateType::Tree,
tree.selected_line().as_selection(),
);
ssb.has_previous_state = has_previous_state;
ssb.is_filtered = self.filtered_tree.is_some();
ssb.has_removed_pattern = false;
ssb.on_tree_root = tree.selection == 0;
ssb.status()
}
/// do some work, totally or partially, if there's some to do.
/// Stop as soon as the dam asks for interruption
fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
if let Some(pending_task) = self.pending_task.take() {
match pending_task {
BrowserTask::Search { pattern, total } => {
let pattern_str = pattern.raw.clone();
let mut options = self.tree.options.clone();
options.pattern = pattern;
let root = self.tree.root().clone();
let page_height = BrowserState::page_height(screen) as usize;
let builder = TreeBuilder::from(root, options, page_height, con)?;
let filtered_tree = time!(
Info,
"tree filtering",
&pattern_str,
builder.build_tree(total, dam),
);
if let Ok(mut ft) = filtered_tree {
ft.try_select_best_match();
ft.make_selection_visible(BrowserState::page_height(screen));
self.filtered_tree = Some(ft);
}
}
BrowserTask::StageAll(pattern) => {
let tree = self.displayed_tree();
let root = tree.root().clone();
let mut options = tree.options.clone();
let total_search = true;
options.pattern = pattern; // should be the same
let builder = TreeBuilder::from(root, options, con.max_staged_count, con);
let mut paths = builder
.and_then(|mut builder| {
builder.matches_max = Some(con.max_staged_count);
time!(builder.build_paths(
total_search,
dam,
|line| line.file_type.is_file(),
))
})?;
for path in paths.drain(..) {
app_state.stage.add(path);
}
}
}
} else if self.displayed_tree().is_missing_git_status_computation() {
let root_path = self.displayed_tree().root();
let git_status = git::get_tree_status(root_path, dam);
self.displayed_tree_mut().git_status = git_status;
} else {
self.displayed_tree_mut().fetch_some_missing_dir_sum(dam, con);
}
Ok(())
}
More examples
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
pub fn refresh(
&mut self,
page_height: usize,
con: &AppContext,
) -> Result<(), errors::TreeBuildError> {
let builder = TreeBuilder::from(
self.root().to_path_buf(),
self.options.clone(),
page_height,
con,
)?;
let mut tree = builder
.build_tree(
false, // on refresh we always do a non total search
&Dam::unlimited(),
)
.unwrap(); // should not fail
// we save the old selection to try restore it
let selected_path = self.selected_line().path.to_path_buf();
mem::swap(&mut self.lines, &mut tree.lines);
self.scroll = 0;
if !self.try_select_path(&selected_path) {
if self.selection >= self.lines.len() {
self.selection = 0;
}
}
self.make_selection_visible(page_height);
Ok(())
}
sourcepub fn try_select_best_match(&mut self)
pub fn try_select_best_match(&mut self)
select the line with the best matching score
Examples found in repository?
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
fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
if let Some(pending_task) = self.pending_task.take() {
match pending_task {
BrowserTask::Search { pattern, total } => {
let pattern_str = pattern.raw.clone();
let mut options = self.tree.options.clone();
options.pattern = pattern;
let root = self.tree.root().clone();
let page_height = BrowserState::page_height(screen) as usize;
let builder = TreeBuilder::from(root, options, page_height, con)?;
let filtered_tree = time!(
Info,
"tree filtering",
&pattern_str,
builder.build_tree(total, dam),
);
if let Ok(mut ft) = filtered_tree {
ft.try_select_best_match();
ft.make_selection_visible(BrowserState::page_height(screen));
self.filtered_tree = Some(ft);
}
}
BrowserTask::StageAll(pattern) => {
let tree = self.displayed_tree();
let root = tree.root().clone();
let mut options = tree.options.clone();
let total_search = true;
options.pattern = pattern; // should be the same
let builder = TreeBuilder::from(root, options, con.max_staged_count, con);
let mut paths = builder
.and_then(|mut builder| {
builder.matches_max = Some(con.max_staged_count);
time!(builder.build_paths(
total_search,
dam,
|line| line.file_type.is_file(),
))
})?;
for path in paths.drain(..) {
app_state.stage.add(path);
}
}
}
} else if self.displayed_tree().is_missing_git_status_computation() {
let root_path = self.displayed_tree().root();
let git_status = git::get_tree_status(root_path, dam);
self.displayed_tree_mut().git_status = git_status;
} else {
self.displayed_tree_mut().fetch_some_missing_dir_sum(dam, con);
}
Ok(())
}
sourcepub fn try_select_path(&mut self, path: &Path) -> bool
pub fn try_select_path(&mut self, path: &Path) -> bool
return true when we could select the given path
Examples found in repository?
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
pub fn on_path(
path: PathBuf,
tree: &mut Tree,
screen: Screen,
in_new_panel: bool,
) -> CmdResult {
info!("executing :select on path {:?}", &path);
if in_new_panel {
warn!("bang in :select isn't supported yet");
}
if tree.try_select_path(&path) {
tree.make_selection_visible(BrowserState::page_height(screen) as usize);
}
CmdResult::Keep
}
More examples
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
fn modified(
&self,
screen: Screen,
root: PathBuf,
options: TreeOptions,
message: Option<&'static str>,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut new_state = BrowserState::new(root, options, screen, con, &Dam::unlimited());
if let Ok(bs) = &mut new_state {
if tree.selection != 0 {
bs.displayed_tree_mut().try_select_path(&tree.selected_line().path);
}
}
CmdResult::from_optional_state(
new_state,
message,
in_new_panel,
)
}
pub fn root(&self) -> &Path {
self.tree.root()
}
pub fn page_height(screen: Screen) -> usize {
screen.height as usize - 2 // br shouldn't be displayed when the screen is smaller
}
/// return a reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree(&self) -> &Tree {
self.filtered_tree.as_ref().unwrap_or(&self.tree)
}
/// return a mutable reference to the currently displayed tree, which
/// is the filtered tree if there's one, the base tree if not.
pub fn displayed_tree_mut(&mut self) -> &mut Tree {
self.filtered_tree.as_mut().unwrap_or(&mut self.tree)
}
pub fn open_selection_stay_in_broot(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
keep_pattern: bool,
) -> Result<CmdResult, ProgramError> {
let tree = self.displayed_tree();
let line = tree.selected_line();
let mut target = line.target().to_path_buf();
if line.is_dir() {
if tree.selection == 0 {
// opening the root would be going to where we already are.
// We go up one level instead
if let Some(parent) = target.parent() {
target = PathBuf::from(parent);
}
}
let dam = Dam::unlimited();
Ok(CmdResult::from_optional_state(
BrowserState::new(
target,
if keep_pattern {
tree.options.clone()
} else {
tree.options.without_pattern()
},
screen,
con,
&dam,
),
None,
in_new_panel,
))
} else {
match opener::open(&target) {
Ok(exit_status) => {
info!("open returned with exit_status {:?}", exit_status);
Ok(CmdResult::Keep)
}
Err(e) => Ok(CmdResult::error(format!("{:?}", e))),
}
}
}
pub fn go_to_parent(
&mut self,
screen: Screen,
con: &AppContext,
in_new_panel: bool,
) -> CmdResult {
match &self.displayed_tree().selected_line().path.parent() {
Some(path) => CmdResult::from_optional_state(
BrowserState::new(
path.to_path_buf(),
self.displayed_tree().options.without_pattern(),
screen,
con,
&Dam::unlimited(),
),
None,
in_new_panel,
),
None => CmdResult::error("no parent found"),
}
}
}
impl PanelState for BrowserState {
fn tree_root(&self) -> Option<&Path> {
Some(self.root())
}
fn get_type(&self) -> PanelStateType {
PanelStateType::Tree
}
fn set_mode(&mut self, mode: Mode) {
self.mode = mode;
}
fn get_mode(&self) -> Mode {
self.mode
}
fn get_pending_task(&self) -> Option<&'static str> {
if self.displayed_tree().has_dir_missing_sum() {
Some("computing stats")
} else if self.displayed_tree().is_missing_git_status_computation() {
Some("computing git status")
} else {
self
.pending_task.as_ref().map(|task| match task {
BrowserTask::Search{ .. } => "searching",
BrowserTask::StageAll(_) => "staging",
})
}
}
fn selected_path(&self) -> Option<&Path> {
Some(&self.displayed_tree().selected_line().path)
}
fn selection(&self) -> Option<Selection<'_>> {
let tree = self.displayed_tree();
let mut selection = tree.selected_line().as_selection();
selection.line = tree.options.pattern.pattern
.get_match_line_count(selection.path)
.unwrap_or(0);
Some(selection)
}
fn tree_options(&self) -> TreeOptions {
self.displayed_tree().options.clone()
}
/// build a cmdResult asking for the addition of a new state
/// being a browser state similar to the current one but with
/// different options
fn with_new_options(
&mut self,
screen: Screen,
change_options: &dyn Fn(&mut TreeOptions) -> &'static str,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut options = tree.options.clone();
let message = change_options(&mut options);
let message = Some(message);
self.modified(
screen,
tree.root().clone(),
options,
message,
in_new_panel,
con,
)
}
fn clear_pending(&mut self) {
self.pending_task = None;
}
fn on_click(
&mut self,
_x: u16,
y: u16,
_screen: Screen,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
self.displayed_tree_mut().try_select_y(y as usize);
Ok(CmdResult::Keep)
}
fn on_double_click(
&mut self,
_x: u16,
y: u16,
screen: Screen,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if self.displayed_tree().selection == y as usize {
self.open_selection_stay_in_broot(screen, con, false, false)
} else {
// A double click always come after a simple click at
// same position. If it's not the selected line, it means
// the click wasn't on a selectable/openable tree line
Ok(CmdResult::Keep)
}
}
fn on_pattern(
&mut self,
pat: InputPattern,
_app_state: &AppState,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if pat.is_none() {
self.filtered_tree = None;
}
if let Some(filtered_tree) = &self.filtered_tree {
if pat != filtered_tree.options.pattern {
self.search(pat, false);
}
} else {
self.search(pat, false);
}
Ok(CmdResult::Keep)
}
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 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
pub fn refresh(
&mut self,
page_height: usize,
con: &AppContext,
) -> Result<(), errors::TreeBuildError> {
let builder = TreeBuilder::from(
self.root().to_path_buf(),
self.options.clone(),
page_height,
con,
)?;
let mut tree = builder
.build_tree(
false, // on refresh we always do a non total search
&Dam::unlimited(),
)
.unwrap(); // should not fail
// we save the old selection to try restore it
let selected_path = self.selected_line().path.to_path_buf();
mem::swap(&mut self.lines, &mut tree.lines);
self.scroll = 0;
if !self.try_select_path(&selected_path) {
if self.selection >= self.lines.len() {
self.selection = 0;
}
}
self.make_selection_visible(page_height);
Ok(())
}
/// do what must be done after line additions or removals:
/// - sort the lines
/// - compute left branches
pub fn after_lines_changed(&mut self) {
// we need to order the lines to build the tree.
// It's a little complicated because
// - we want a case insensitive sort
// - we still don't want to confuse the children of AA and Aa
// - a node can come from a not parent node, when we followed a link
let mut bid_parents: FnvHashMap<BId, BId> = FnvHashMap::default();
let mut bid_lines: FnvHashMap<BId, &TreeLine> = FnvHashMap::default();
for line in self.lines[..].iter() {
if let Some(parent_bid) = line.parent_bid {
bid_parents.insert(line.bid, parent_bid);
}
bid_lines.insert(line.bid, line);
}
let mut sort_paths: FnvHashMap<BId, String> = FnvHashMap::default();
for line in self.lines[1..].iter() {
let mut sort_path = String::new();
let mut bid = line.bid;
while let Some(l) = bid_lines.get(&bid) {
let lower_name = l.path.file_name().map_or(
"".to_string(),
|name| name.to_string_lossy().to_lowercase(),
);
let sort_prefix = match self.options.sort {
Sort::TypeDirsFirst => {
if l.is_dir() {
" "
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
Sort::TypeDirsLast => {
if l.is_dir() {
"~~~~~~~~~~~~~~"
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
_ => { "" }
};
sort_path = format!(
"{}{}-{}/{}",
sort_prefix,
lower_name,
bid.index(), // to be sure to separate paths having the same lowercase
sort_path,
);
if let Some(&parent_bid) = bid_parents.get(&bid) {
bid = parent_bid;
} else {
break;
}
}
sort_paths.insert(line.bid, sort_path);
}
self.lines[1..].sort_by_key(|line| sort_paths.get(&line.bid).unwrap());
let mut best_index = 0; // index of the line with the best score
for i in 1..self.lines.len() {
if self.lines[i].score > self.lines[best_index].score {
best_index = i;
}
for d in 0..self.lines[i].left_branchs.len() {
self.lines[i].left_branchs[d] = false;
}
}
// then we discover the branches (for the drawing)
// and we mark the last children as pruning, if they have unlisted brothers
let mut last_parent_index: usize = self.lines.len() + 1;
for end_index in (1..self.lines.len()).rev() {
let depth = (self.lines[end_index].depth - 1) as usize;
let start_index = {
let parent_index = match self.lines[end_index].parent_bid {
Some(parent_bid) => {
let mut index = end_index;
loop {
index -= 1;
if self.lines[index].bid == parent_bid {
break;
}
if index == 0 {
break;
}
}
index
}
None => end_index, // Should not happen
};
if parent_index != last_parent_index {
// the line at end_index is the last listed child of the line at parent_index
let unlisted = self.lines[parent_index].unlisted;
if unlisted > 0 && self.lines[end_index].nb_kept_children == 0 {
if best_index == end_index {
//debug!("Avoiding to prune the line with best score");
} else {
//debug!("turning {:?} into Pruning", self.lines[end_index].path);
self.lines[end_index].line_type = TreeLineType::Pruning;
self.lines[end_index].unlisted = unlisted + 1;
self.lines[end_index].name = format!("{} unlisted", unlisted + 1);
self.lines[parent_index].unlisted = 0;
}
}
last_parent_index = parent_index;
}
parent_index + 1
};
for i in start_index..=end_index {
self.lines[i].left_branchs[depth] = true;
}
}
if self.options.needs_sum() {
time!("fetch_file_sum", self.fetch_regular_file_sums()); // not the dirs, only simple files
self.sort_siblings(); // does nothing when sort mode is None
}
}
pub fn is_empty(&self) -> bool {
self.lines.len() == 1
}
pub fn has_branch(&self, line_index: usize, depth: usize) -> bool {
if line_index >= self.lines.len() {
return false;
}
let line = &self.lines[line_index];
depth < usize::from(line.depth) && line.left_branchs[depth]
}
/// select another line
///
/// For example the following one if dy is 1.
pub fn move_selection(&mut self, dy: i32, page_height: usize, cycle: bool) {
let l = self.lines.len();
// we find the new line to select
loop {
if dy < 0 {
let ady = (-dy) as usize;
if !cycle && self.selection < ady {
break;
}
self.selection = (self.selection + l - ady) % l;
} else {
let dy = dy as usize;
if !cycle && self.selection + dy >= l {
break;
}
self.selection = (self.selection + dy) % l;
}
if self.lines[self.selection].is_selectable() {
break;
}
}
// we adjust the scroll
if l > page_height {
if self.selection < 3 {
self.scroll = 0;
} else if self.selection < self.scroll + 3 {
self.scroll = self.selection - 3;
} else if self.selection + 3 > l {
self.scroll = l - page_height;
} else if self.selection + 3 > self.scroll + page_height {
self.scroll = self.selection + 3 - page_height;
}
}
}
/// Scroll the desired amount and return true, or return false if it's
/// already at end or the tree fits the page
pub fn try_scroll(&mut self, dy: i32, page_height: usize) -> bool {
if self.lines.len() <= page_height {
return false;
}
if dy < 0 { // scroll up
if self.scroll == 0 {
return false;
} else {
let ady = -dy as usize;
if ady < self.scroll {
self.scroll -= ady;
} else {
self.scroll = 0;
}
}
} else { // scroll down
let max = self.lines.len() - page_height;
if self.scroll >= max {
return false;
}
self.scroll = (self.scroll + dy as usize).min(max);
}
self.select_visible_line(page_height);
true
}
/// try to select a line by index of visible line
/// (works if y+scroll falls on a selectable line)
pub fn try_select_y(&mut self, y: usize) -> bool {
let y = y + self.scroll;
if y < self.lines.len() {
if self.lines[y].is_selectable() {
self.selection = y;
return true;
}
}
false
}
/// fix the selection so that it's a selectable visible line
fn select_visible_line(&mut self, page_height: usize) {
if self.selection < self.scroll || self.selection >= self.scroll + page_height {
self.selection = self.scroll;
let l = self.lines.len();
loop {
self.selection = (self.selection + l + 1) % l;
if self.lines[self.selection].is_selectable() {
break;
}
}
}
}
pub fn make_selection_visible(&mut self, page_height: usize) {
if page_height >= self.lines.len() || self.selection < 3 {
self.scroll = 0;
} else if self.selection <= self.scroll {
self.scroll = self.selection - 2;
} else if self.selection > self.lines.len() - 2 {
self.scroll = self.lines.len() - page_height;
} else if self.selection >= self.scroll + page_height {
self.scroll = self.selection + 2 - page_height;
}
}
pub fn selected_line(&self) -> &TreeLine {
&self.lines[self.selection]
}
pub fn root(&self) -> &PathBuf {
&self.lines[0].path
}
/// select the line with the best matching score
pub fn try_select_best_match(&mut self) {
let mut best_score = 0;
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if best_score > line.score {
continue;
}
if line.score == best_score {
// in case of equal scores, we prefer the shortest path
if self.lines[idx].depth >= self.lines[self.selection].depth {
continue;
}
}
best_score = line.score;
self.selection = idx;
}
}
/// return true when we could select the given path
pub fn try_select_path(&mut self, path: &Path) -> bool {
for (idx, line) in self.lines.iter().enumerate() {
if !line.is_selectable() {
continue;
}
if path == line.path {
self.selection = idx;
return true;
}
}
false
}
pub fn try_select_first(&mut self) -> bool {
for idx in 0..self.lines.len() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.scroll = 0;
return true;
}
}
false
}
pub fn try_select_last(&mut self, page_height: usize) -> bool {
for idx in (0..self.lines.len()).rev() {
let line = &self.lines[idx];
if line.is_selectable() {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_previous_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_next_same_depth(&mut self, page_height: usize) -> bool {
let depth = self.lines[self.selection].depth;
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() || line.depth != depth {
continue;
}
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
false
}
pub fn try_select_previous_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in (0..self.lines.len()).rev() {
let idx = (self.selection + di) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn try_select_next_filtered<F>(
&mut self,
filter: F,
page_height: usize,
) -> bool where
F: Fn(&TreeLine) -> bool,
{
for di in 0..self.lines.len() {
let idx = (self.selection + di + 1) % self.lines.len();
let line = &self.lines[idx];
if !line.is_selectable() {
continue;
}
if !filter(line) {
continue;
}
if line.score > 0 {
self.selection = idx;
self.make_selection_visible(page_height);
return true;
}
}
false
}
pub fn has_dir_missing_sum(&self) -> bool {
self.options.needs_sum()
&& self
.lines
.iter()
.any(|line| line.line_type == TreeLineType::Dir && line.sum.is_none())
}
pub fn is_missing_git_status_computation(&self) -> bool {
self.git_status.is_not_computed()
}
/// fetch the file_sums of regular files (thus avoiding the
/// long computation which is needed for directories)
pub fn fetch_regular_file_sums(&mut self) {
for i in 1..self.lines.len() {
match self.lines[i].line_type {
TreeLineType::Dir | TreeLineType::Pruning => {}
_ => {
self.lines[i].sum = Some(FileSum::from_file(&self.lines[i].path));
}
}
}
self.sort_siblings();
}
/// compute the file_sum of one directory
///
/// To compute the size of all of them, this should be called until
/// has_dir_missing_sum returns false
pub fn fetch_some_missing_dir_sum(&mut self, dam: &Dam, con: &AppContext) {
// we prefer to compute the root directory last: its computation
// is faster when its first level children are already computed
for i in (0..self.lines.len()).rev() {
if self.lines[i].sum.is_none() && self.lines[i].line_type == TreeLineType::Dir {
self.lines[i].sum = FileSum::from_dir(&self.lines[i].path, dam, con);
self.sort_siblings();
return;
}
}
}
/// Sort files according to the sort option
///
/// (does nothing if it's None)
fn sort_siblings(&mut self) {
match self.options.sort {
Sort::Count => {
// we'll try to keep the same path selected
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let acount = a.sum.map_or(0, |s| s.to_count());
let bcount = b.sum.map_or(0, |s| s.to_count());
bcount.cmp(&acount)
});
self.try_select_path(&selected_path);
}
Sort::Date => {
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let adate = a.sum.map_or(0, |s| s.to_seconds());
let bdate = b.sum.map_or(0, |s| s.to_seconds());
bdate.cmp(&adate)
});
self.try_select_path(&selected_path);
}
Sort::Size => {
let selected_path = self.selected_line().path.to_path_buf();
self.lines[1..].sort_by(|a, b| {
let asize = a.sum.map_or(0, |s| s.to_size());
let bsize = b.sum.map_or(0, |s| s.to_size());
bsize.cmp(&asize)
});
self.try_select_path(&selected_path);
}
_ => {}
}
}
sourcepub fn try_select_first(&mut self) -> bool
pub fn try_select_first(&mut self) -> bool
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_last(&mut self, page_height: usize) -> bool
pub fn try_select_last(&mut self, page_height: usize) -> bool
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_previous_same_depth(&mut self, page_height: usize) -> bool
pub fn try_select_previous_same_depth(&mut self, page_height: usize) -> bool
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_next_same_depth(&mut self, page_height: usize) -> bool
pub fn try_select_next_same_depth(&mut self, page_height: usize) -> bool
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_previous_filtered<F>(
&mut self,
filter: F,
page_height: usize
) -> boolwhere
F: Fn(&TreeLine) -> bool,
pub fn try_select_previous_filtered<F>(
&mut self,
filter: F,
page_height: usize
) -> boolwhere
F: Fn(&TreeLine) -> bool,
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn try_select_next_filtered<F>(
&mut self,
filter: F,
page_height: usize
) -> boolwhere
F: Fn(&TreeLine) -> bool,
pub fn try_select_next_filtered<F>(
&mut self,
filter: F,
page_height: usize
) -> boolwhere
F: Fn(&TreeLine) -> bool,
Examples found in repository?
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
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
sourcepub fn has_dir_missing_sum(&self) -> bool
pub fn has_dir_missing_sum(&self) -> bool
Examples found in repository?
208 209 210 211 212 213 214 215 216 217 218 219 220
fn get_pending_task(&self) -> Option<&'static str> {
if self.displayed_tree().has_dir_missing_sum() {
Some("computing stats")
} else if self.displayed_tree().is_missing_git_status_computation() {
Some("computing git status")
} else {
self
.pending_task.as_ref().map(|task| match task {
BrowserTask::Search{ .. } => "searching",
BrowserTask::StageAll(_) => "staging",
})
}
}
sourcepub fn is_missing_git_status_computation(&self) -> bool
pub fn is_missing_git_status_computation(&self) -> bool
Examples found in repository?
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
fn get_pending_task(&self) -> Option<&'static str> {
if self.displayed_tree().has_dir_missing_sum() {
Some("computing stats")
} else if self.displayed_tree().is_missing_git_status_computation() {
Some("computing git status")
} else {
self
.pending_task.as_ref().map(|task| match task {
BrowserTask::Search{ .. } => "searching",
BrowserTask::StageAll(_) => "staging",
})
}
}
fn selected_path(&self) -> Option<&Path> {
Some(&self.displayed_tree().selected_line().path)
}
fn selection(&self) -> Option<Selection<'_>> {
let tree = self.displayed_tree();
let mut selection = tree.selected_line().as_selection();
selection.line = tree.options.pattern.pattern
.get_match_line_count(selection.path)
.unwrap_or(0);
Some(selection)
}
fn tree_options(&self) -> TreeOptions {
self.displayed_tree().options.clone()
}
/// build a cmdResult asking for the addition of a new state
/// being a browser state similar to the current one but with
/// different options
fn with_new_options(
&mut self,
screen: Screen,
change_options: &dyn Fn(&mut TreeOptions) -> &'static str,
in_new_panel: bool,
con: &AppContext,
) -> CmdResult {
let tree = self.displayed_tree();
let mut options = tree.options.clone();
let message = change_options(&mut options);
let message = Some(message);
self.modified(
screen,
tree.root().clone(),
options,
message,
in_new_panel,
con,
)
}
fn clear_pending(&mut self) {
self.pending_task = None;
}
fn on_click(
&mut self,
_x: u16,
y: u16,
_screen: Screen,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
self.displayed_tree_mut().try_select_y(y as usize);
Ok(CmdResult::Keep)
}
fn on_double_click(
&mut self,
_x: u16,
y: u16,
screen: Screen,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if self.displayed_tree().selection == y as usize {
self.open_selection_stay_in_broot(screen, con, false, false)
} else {
// A double click always come after a simple click at
// same position. If it's not the selected line, it means
// the click wasn't on a selectable/openable tree line
Ok(CmdResult::Keep)
}
}
fn on_pattern(
&mut self,
pat: InputPattern,
_app_state: &AppState,
_con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if pat.is_none() {
self.filtered_tree = None;
}
if let Some(filtered_tree) = &self.filtered_tree {
if pat != filtered_tree.options.pattern {
self.search(pat, false);
}
} else {
self.search(pat, false);
}
Ok(CmdResult::Keep)
}
fn on_internal(
&mut self,
w: &mut W,
internal_exec: &InternalExecution,
input_invocation: Option<&VerbInvocation>,
trigger_type: TriggerType,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let con = &cc.app.con;
let screen = cc.app.screen;
let page_height = BrowserState::page_height(cc.app.screen);
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => {
if let Some(filtered_tree) = &self.filtered_tree {
let filtered_selection = &filtered_tree.selected_line().path;
if self.tree.try_select_path(filtered_selection) {
self.tree.make_selection_visible(page_height);
}
self.filtered_tree = None;
CmdResult::Keep
} else if self.tree.selection > 0 {
self.tree.selection = 0;
CmdResult::Keep
} else {
CmdResult::PopState
}
}
Internal::focus => internal_focus::on_internal(
internal_exec,
input_invocation,
trigger_type,
&self.displayed_tree().selected_line().path,
self.displayed_tree().options.clone(),
app_state,
cc,
),
Internal::select => internal_select::on_internal(
internal_exec,
input_invocation,
trigger_type,
self.displayed_tree_mut(),
app_state,
cc,
),
Internal::up_tree => match self.displayed_tree().root().parent() {
Some(path) => internal_focus::on_path(
path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
bang,
con,
),
None => CmdResult::error("no parent found"),
},
Internal::open_stay => self.open_selection_stay_in_broot(screen, con, bang, false)?,
Internal::open_stay_filter => self.open_selection_stay_in_broot(screen, con, bang, true)?,
Internal::line_down => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, true);
CmdResult::Keep
}
Internal::line_up => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, true);
CmdResult::Keep
}
Internal::line_down_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(count, page_height, false);
CmdResult::Keep
}
Internal::line_up_no_cycle => {
let count = get_arg(input_invocation, internal_exec, 1);
self.displayed_tree_mut().move_selection(-count, page_height, false);
CmdResult::Keep
}
Internal::previous_dir => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::next_dir => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.is_dir(),
page_height,
);
CmdResult::Keep
}
Internal::previous_match => {
self.displayed_tree_mut().try_select_previous_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::next_match => {
self.displayed_tree_mut().try_select_next_filtered(
|line| line.direct_match,
page_height,
);
CmdResult::Keep
}
Internal::previous_same_depth => {
self.displayed_tree_mut().try_select_previous_same_depth(page_height);
CmdResult::Keep
}
Internal::next_same_depth => {
self.displayed_tree_mut().try_select_next_same_depth(page_height);
CmdResult::Keep
}
Internal::page_down => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32, page_height) {
tree.try_select_last(page_height);
}
CmdResult::Keep
}
Internal::page_up => {
let tree = self.displayed_tree_mut();
if !tree.try_scroll(page_height as i32 * -1, page_height) {
tree.try_select_first();
}
CmdResult::Keep
}
Internal::panel_left => {
let areas = &cc.panel.areas;
if areas.is_first() && areas.nb_pos < con.max_panels_count {
// we ask for the creation of a panel to the left
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
PanelPurpose::None,
con,
HDir::Left,
)
} else {
// we let the app handle other cases
CmdResult::HandleInApp(Internal::panel_left_no_open)
}
}
Internal::panel_left_no_open => CmdResult::HandleInApp(Internal::panel_left_no_open),
Internal::panel_right => {
let areas = &cc.panel.areas;
let selected_path = &self.displayed_tree().selected_line().path;
if areas.is_last() && areas.nb_pos < con.max_panels_count {
let purpose = if selected_path.is_file() && cc.app.preview_panel.is_none() {
PanelPurpose::Preview
} else {
PanelPurpose::None
};
// we ask for the creation of a panel to the right
internal_focus::new_panel_on_path(
selected_path.to_path_buf(),
screen,
self.displayed_tree().options.clone(),
purpose,
con,
HDir::Right,
)
} else {
// we ask the app to handle other cases :
// focus the panel to the right or close the leftest one
CmdResult::HandleInApp(Internal::panel_right_no_open)
}
}
Internal::panel_right_no_open => CmdResult::HandleInApp(Internal::panel_right_no_open),
Internal::parent => self.go_to_parent(screen, con, bang),
Internal::print_tree => {
print::print_tree(self.displayed_tree(), cc.app.screen, cc.app.panel_skin, con)?
}
Internal::root_up => {
let tree = self.displayed_tree();
let root = tree.root();
if let Some(new_root) = root.parent() {
self.modified(
screen,
new_root.to_path_buf(),
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error(format!("{:?} has no parent", root))
}
}
Internal::root_down => {
let tree = self.displayed_tree();
if tree.selection > 0 {
let root_len = tree.root().components().count();
let new_root = tree.selected_line().path
.components()
.take(root_len + 1)
.collect();
self.modified(
screen,
new_root,
tree.options.clone(),
None,
bang,
con,
)
} else {
CmdResult::error("No selected line")
}
}
Internal::stage_all_files => {
let pattern = self.displayed_tree().options.pattern.clone();
self.pending_task = Some(BrowserTask::StageAll(pattern));
if cc.app.stage_panel.is_none() {
let stage_options = self.tree.options.without_pattern();
CmdResult::NewPanel {
state: Box::new(StageState::new(app_state, stage_options, con)),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::Keep
}
}
Internal::select_first => {
self.displayed_tree_mut().try_select_first();
CmdResult::Keep
}
Internal::select_last => {
let page_height = BrowserState::page_height(screen);
self.displayed_tree_mut().try_select_last(page_height);
CmdResult::Keep
}
Internal::start_end_panel => {
if cc.panel.purpose.is_arg_edition() {
debug!("start_end understood as end");
CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
}
} else {
debug!("start_end understood as start");
let tree_options = self.displayed_tree().options.clone();
if let Some(input_invocation) = input_invocation {
// we'll go for input arg editing
let path = if let Some(input_arg) = &input_invocation.args {
path::path_from(self.root(), PathAnchor::Unspecified, input_arg)
} else {
self.root().to_path_buf()
};
let arg_type = SelectionType::Any; // We might do better later
let purpose = PanelPurpose::ArgEdition { arg_type };
internal_focus::new_panel_on_path(
path, screen, tree_options, purpose, con, HDir::Right,
)
} else {
// we just open a new panel on the selected path,
// without purpose
internal_focus::new_panel_on_path(
self.displayed_tree().selected_line().path.to_path_buf(),
screen,
tree_options,
PanelPurpose::None,
con,
HDir::Right,
)
}
}
}
Internal::total_search => {
match self.filtered_tree.as_ref().map(|t| t.total_search) {
None => {
CmdResult::error("this verb can be used only after a search")
}
Some(true) => {
CmdResult::error("search was already total: all possible matches have been ranked")
}
Some(false) => {
self.search(self.displayed_tree().options.pattern.clone(), true);
CmdResult::Keep
}
}
}
Internal::quit => CmdResult::Quit,
_ => self.on_internal_generic(
w,
internal_exec,
input_invocation,
trigger_type,
app_state,
cc,
)?,
})
}
fn no_verb_status(
&self,
has_previous_state: bool,
con: &AppContext,
) -> Status {
let tree = self.displayed_tree();
if tree.is_empty() {
if tree.build_report.hidden_count > 0 {
let mut parts = Vec::new();
if let Some(md) = con.standard_status.all_files_hidden.clone() {
parts.push(md);
}
if let Some(md) = con.standard_status.all_files_git_ignored.clone() {
parts.push(md);
}
if !parts.is_empty() {
return Status::from_error(parts.join(". "));
}
}
}
let mut ssb = con.standard_status.builder(
PanelStateType::Tree,
tree.selected_line().as_selection(),
);
ssb.has_previous_state = has_previous_state;
ssb.is_filtered = self.filtered_tree.is_some();
ssb.has_removed_pattern = false;
ssb.on_tree_root = tree.selection == 0;
ssb.status()
}
/// do some work, totally or partially, if there's some to do.
/// Stop as soon as the dam asks for interruption
fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
if let Some(pending_task) = self.pending_task.take() {
match pending_task {
BrowserTask::Search { pattern, total } => {
let pattern_str = pattern.raw.clone();
let mut options = self.tree.options.clone();
options.pattern = pattern;
let root = self.tree.root().clone();
let page_height = BrowserState::page_height(screen) as usize;
let builder = TreeBuilder::from(root, options, page_height, con)?;
let filtered_tree = time!(
Info,
"tree filtering",
&pattern_str,
builder.build_tree(total, dam),
);
if let Ok(mut ft) = filtered_tree {
ft.try_select_best_match();
ft.make_selection_visible(BrowserState::page_height(screen));
self.filtered_tree = Some(ft);
}
}
BrowserTask::StageAll(pattern) => {
let tree = self.displayed_tree();
let root = tree.root().clone();
let mut options = tree.options.clone();
let total_search = true;
options.pattern = pattern; // should be the same
let builder = TreeBuilder::from(root, options, con.max_staged_count, con);
let mut paths = builder
.and_then(|mut builder| {
builder.matches_max = Some(con.max_staged_count);
time!(builder.build_paths(
total_search,
dam,
|line| line.file_type.is_file(),
))
})?;
for path in paths.drain(..) {
app_state.stage.add(path);
}
}
}
} else if self.displayed_tree().is_missing_git_status_computation() {
let root_path = self.displayed_tree().root();
let git_status = git::get_tree_status(root_path, dam);
self.displayed_tree_mut().git_status = git_status;
} else {
self.displayed_tree_mut().fetch_some_missing_dir_sum(dam, con);
}
Ok(())
}
sourcepub fn fetch_regular_file_sums(&mut self)
pub fn fetch_regular_file_sums(&mut self)
fetch the file_sums of regular files (thus avoiding the long computation which is needed for directories)
Examples found in repository?
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
pub fn after_lines_changed(&mut self) {
// we need to order the lines to build the tree.
// It's a little complicated because
// - we want a case insensitive sort
// - we still don't want to confuse the children of AA and Aa
// - a node can come from a not parent node, when we followed a link
let mut bid_parents: FnvHashMap<BId, BId> = FnvHashMap::default();
let mut bid_lines: FnvHashMap<BId, &TreeLine> = FnvHashMap::default();
for line in self.lines[..].iter() {
if let Some(parent_bid) = line.parent_bid {
bid_parents.insert(line.bid, parent_bid);
}
bid_lines.insert(line.bid, line);
}
let mut sort_paths: FnvHashMap<BId, String> = FnvHashMap::default();
for line in self.lines[1..].iter() {
let mut sort_path = String::new();
let mut bid = line.bid;
while let Some(l) = bid_lines.get(&bid) {
let lower_name = l.path.file_name().map_or(
"".to_string(),
|name| name.to_string_lossy().to_lowercase(),
);
let sort_prefix = match self.options.sort {
Sort::TypeDirsFirst => {
if l.is_dir() {
" "
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
Sort::TypeDirsLast => {
if l.is_dir() {
"~~~~~~~~~~~~~~"
} else {
l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
}
}
_ => { "" }
};
sort_path = format!(
"{}{}-{}/{}",
sort_prefix,
lower_name,
bid.index(), // to be sure to separate paths having the same lowercase
sort_path,
);
if let Some(&parent_bid) = bid_parents.get(&bid) {
bid = parent_bid;
} else {
break;
}
}
sort_paths.insert(line.bid, sort_path);
}
self.lines[1..].sort_by_key(|line| sort_paths.get(&line.bid).unwrap());
let mut best_index = 0; // index of the line with the best score
for i in 1..self.lines.len() {
if self.lines[i].score > self.lines[best_index].score {
best_index = i;
}
for d in 0..self.lines[i].left_branchs.len() {
self.lines[i].left_branchs[d] = false;
}
}
// then we discover the branches (for the drawing)
// and we mark the last children as pruning, if they have unlisted brothers
let mut last_parent_index: usize = self.lines.len() + 1;
for end_index in (1..self.lines.len()).rev() {
let depth = (self.lines[end_index].depth - 1) as usize;
let start_index = {
let parent_index = match self.lines[end_index].parent_bid {
Some(parent_bid) => {
let mut index = end_index;
loop {
index -= 1;
if self.lines[index].bid == parent_bid {
break;
}
if index == 0 {
break;
}
}
index
}
None => end_index, // Should not happen
};
if parent_index != last_parent_index {
// the line at end_index is the last listed child of the line at parent_index
let unlisted = self.lines[parent_index].unlisted;
if unlisted > 0 && self.lines[end_index].nb_kept_children == 0 {
if best_index == end_index {
//debug!("Avoiding to prune the line with best score");
} else {
//debug!("turning {:?} into Pruning", self.lines[end_index].path);
self.lines[end_index].line_type = TreeLineType::Pruning;
self.lines[end_index].unlisted = unlisted + 1;
self.lines[end_index].name = format!("{} unlisted", unlisted + 1);
self.lines[parent_index].unlisted = 0;
}
}
last_parent_index = parent_index;
}
parent_index + 1
};
for i in start_index..=end_index {
self.lines[i].left_branchs[depth] = true;
}
}
if self.options.needs_sum() {
time!("fetch_file_sum", self.fetch_regular_file_sums()); // not the dirs, only simple files
self.sort_siblings(); // does nothing when sort mode is None
}
}
sourcepub fn fetch_some_missing_dir_sum(&mut self, dam: &Dam, con: &AppContext)
pub fn fetch_some_missing_dir_sum(&mut self, dam: &Dam, con: &AppContext)
compute the file_sum of one directory
To compute the size of all of them, this should be called until has_dir_missing_sum returns false
Examples found in repository?
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
fn do_pending_task(
&mut self,
app_state: &mut AppState,
screen: Screen,
con: &AppContext,
dam: &mut Dam,
) -> Result<(), ProgramError> {
if let Some(pending_task) = self.pending_task.take() {
match pending_task {
BrowserTask::Search { pattern, total } => {
let pattern_str = pattern.raw.clone();
let mut options = self.tree.options.clone();
options.pattern = pattern;
let root = self.tree.root().clone();
let page_height = BrowserState::page_height(screen) as usize;
let builder = TreeBuilder::from(root, options, page_height, con)?;
let filtered_tree = time!(
Info,
"tree filtering",
&pattern_str,
builder.build_tree(total, dam),
);
if let Ok(mut ft) = filtered_tree {
ft.try_select_best_match();
ft.make_selection_visible(BrowserState::page_height(screen));
self.filtered_tree = Some(ft);
}
}
BrowserTask::StageAll(pattern) => {
let tree = self.displayed_tree();
let root = tree.root().clone();
let mut options = tree.options.clone();
let total_search = true;
options.pattern = pattern; // should be the same
let builder = TreeBuilder::from(root, options, con.max_staged_count, con);
let mut paths = builder
.and_then(|mut builder| {
builder.matches_max = Some(con.max_staged_count);
time!(builder.build_paths(
total_search,
dam,
|line| line.file_type.is_file(),
))
})?;
for path in paths.drain(..) {
app_state.stage.add(path);
}
}
}
} else if self.displayed_tree().is_missing_git_status_computation() {
let root_path = self.displayed_tree().root();
let git_status = git::get_tree_status(root_path, dam);
self.displayed_tree_mut().git_status = git_status;
} else {
self.displayed_tree_mut().fetch_some_missing_dir_sum(dam, con);
}
Ok(())
}
sourcepub fn total_sum(&self) -> FileSum
pub fn total_sum(&self) -> FileSum
compute and return the size of the root
Examples found in repository?
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
pub fn write_on<W: Write>(&self, f: &mut W) -> Result<(), ProgramError> {
#[cfg(not(any(target_family = "windows", target_os = "android")))]
let perm_writer = super::PermWriter::for_tree(self.skin, self.tree);
let tree = self.tree;
let total_size = tree.total_sum();
let scrollbar = if self.in_app {
termimad::compute_scrollbar(
tree.scroll,
tree.lines.len() - 1, // the root line isn't scrolled
self.area.height - 1, // the scrollbar doesn't cover the first line
self.area.top + 1,
)
} else {
None
};
if self.in_app {
f.queue(cursor::MoveTo(self.area.left, self.area.top))?;
}
let mut cw = CropWriter::new(f, self.area.width as usize);
let pattern_object = tree.options.pattern.pattern.object();
self.write_root_line(&mut cw, self.in_app && tree.selection == 0)?;
self.skin.queue_reset(f)?;
let visible_cols: Vec<Col> = tree
.options
.cols_order
.iter()
.filter(|col| col.is_visible(tree, self.app_state))
.cloned()
.collect();
// if necessary we compute the width of the count column
let count_len = if tree.options.show_counts {
tree.lines.iter()
.skip(1) // we don't show the counts of the root
.map(|l| l.sum.map_or(0, |s| s.to_count()))
.max()
.map(|c| format_count(c).len())
.unwrap_or(0)
} else {
0
};
// we compute the length of the dates, depending on the format
let date_len = if tree.options.show_dates {
let date_time: DateTime<Local> = Local::now();
date_time.format(tree.options.date_time_format).to_string().len()
} else {
0 // we don't care
};
for y in 1..self.area.height {
if self.in_app {
f.queue(cursor::MoveTo(self.area.left, y + self.area.top))?;
} else {
write!(f, "\r\n")?;
}
let mut line_index = y as usize;
if line_index > 0 {
line_index += tree.scroll as usize;
}
let mut selected = false;
let mut cw = CropWriter::new(f, self.area.width as usize);
let cw = &mut cw;
if line_index < tree.lines.len() {
let line = &tree.lines[line_index];
selected = self.in_app && line_index == tree.selection;
let label_style = self.label_style(line, selected);
let mut in_branch = false;
let space_style = if selected {
&self.skin.selected_line
} else {
&self.skin.default
};
if visible_cols[0].needs_left_margin() {
cw.queue_char(space_style, ' ')?;
}
let staged = self.app_state
.map_or(false, |a| a.stage.contains(&line.path));
for col in &visible_cols {
let void_len = match col {
Col::Mark => {
self.write_line_selection_mark(cw, &label_style, selected)?
}
Col::Git => {
self.write_line_git_status(cw, line, selected)?
}
Col::Branch => {
in_branch = true;
self.write_branch(cw, line_index, line, selected, staged)?
}
Col::DeviceId => {
#[cfg(not(unix))]
{ 0 }
#[cfg(unix)]
self.write_line_device_id(cw, line, selected)?
}
Col::Permission => {
#[cfg(any(target_family = "windows", target_os = "android"))]
{ 0 }
#[cfg(not(any(target_family = "windows", target_os = "android")))]
perm_writer.write_permissions(cw, line, selected)?
}
Col::Date => {
if let Some(seconds) = line.sum.and_then(|sum| sum.to_valid_seconds()) {
self.write_date(cw, seconds, selected)?
} else {
date_len + 1
}
}
Col::Size => {
if tree.options.sort.prevent_deep_display() {
// as soon as there's only one level displayed we can show the size bars
self.write_line_size_with_bar(cw, line, &label_style, total_size, selected)?
} else {
self.write_line_size(cw, line, &label_style, selected)?
}
}
Col::Count => {
self.write_line_count(cw, line, count_len, selected)?
}
Col::Staged => {
self.write_line_stage_mark(cw, &label_style, staged)?
}
Col::Name => {
in_branch = false;
self.write_line_label(cw, line, &label_style, pattern_object, selected)?
}
};
// void: intercol & replacing missing cells
if in_branch && void_len > 2 {
cond_bg!(void_style, self, selected, self.skin.tree);
cw.repeat(void_style, &BRANCH_FILLING, void_len)?;
} else {
cond_bg!(void_style, self, selected, self.skin.default);
cw.repeat(void_style, &SPACE_FILLING, void_len)?;
}
}
if cw.allowed > 8 && pattern_object.content {
let extract = tree.options.pattern.pattern
.search_content(&line.path, cw.allowed - 2);
if let Some(extract) = extract {
self.write_content_extract(cw, extract, selected)?;
}
}
}
self.extend_line_bg(cw, selected)?;
self.skin.queue_reset(f)?;
if self.in_app {
if let Some((sctop, scbottom)) = scrollbar {
f.queue(cursor::MoveTo(self.area.left + self.area.width - 1, y))?;
let style = if sctop <= y && y <= scbottom {
&self.skin.scrollbar_thumb
} else {
&self.skin.scrollbar_track
};
style.queue_str(f, "▐")?;
}
}
}
if !self.in_app {
write!(f, "\r\n")?;
}
Ok(())
}