focus_root and :focus_user_home internal verbs
For now there's no default mapping but they can be called if
you define the relevant verbs in config:
[[verbs]]
invocation = "root"
execution = ":focus_root"
[[verbs]]
invocation = "home"
execution = ":focus_user_home"
(more ways to invocate them expected soon)
use std::io;
+use std::path::{Path, PathBuf};
+use directories::UserDirs;
use crate::app::AppStateCmdResult;
use crate::app_context::AppContext;
use crate::browser_states::BrowserState;
use crate::commands::Command;
use crate::external::{self, Launchable};
+use crate::flat_tree::Tree;
use crate::help_states::HelpState;
use crate::screens::Screen;
use crate::task_sync::TaskLifetime;
@@ -12,6 +15,22 @@ use crate::tree_options::{OptionBool, TreeOptions};
use crate::verb_invocation::VerbInvocation;
use crate::verbs::{Verb, VerbExecutor};
+fn focus_path(
+ path: PathBuf,
+ screen: &mut Screen,
+ tree: &Tree,
+) -> AppStateCmdResult {
+ AppStateCmdResult::from_optional_state(
+ BrowserState::new(
+ path,
+ tree.options.clone(),
+ screen,
+ &TaskLifetime::unlimited(),
+ ),
+ tree.options.pattern.to_command(),
+ )
+}
+
impl VerbExecutor for BrowserState {
fn execute_verb(
&self,
@@ -32,38 +51,23 @@ impl VerbExecutor for BrowserState {
if !path.is_dir() {
path = path.parent().unwrap().to_path_buf();
}
- AppStateCmdResult::from_optional_state(
- BrowserState::new(
- path,
- tree.options.clone(),
- screen,
- &TaskLifetime::unlimited(),
- ),
- tree.options.pattern.to_command(),
- )
+ focus_path(path, screen, tree)
}
- ":help" => {
- AppStateCmdResult::NewState(Box::new(HelpState::new(screen, con)), Command::new())
+ ":focus_root" => focus_path(PathBuf::from("/"), screen, tree),
+ ":focus_user_home" => match UserDirs::new() {
+ Some(ud) => focus_path(ud.home_dir().to_path_buf(), screen, tree),
+ None => AppStateCmdResult::DisplayError("no user home directory found".to_string()), // does this happen ?
}
+ ":help" => AppStateCmdResult::NewState(Box::new(HelpState::new(screen, con)), Command::new()),
":open" => AppStateCmdResult::from(Launchable::opener(line.target())),
":parent" => match &line.path.parent() {
- Some(path) => AppStateCmdResult::from_optional_state(
- BrowserState::new(
- path.to_path_buf(),
- tree.options.clone(),
- screen,
- &TaskLifetime::unlimited(),
- ),
- tree.options.pattern.to_command(),
- ),
+ Some(path) => focus_path(path.to_path_buf(), screen, tree),
None => AppStateCmdResult::DisplayError("no parent found".to_string()),
- },
+ }
":print_path" => external::print_path(&line.target(), con)?,
":print_tree" => external::print_tree(&tree, screen, con)?,
":toggle_dates" => self.with_new_options(screen, &|o| o.show_dates ^= true),
- ":toggle_files" => {
- self.with_new_options(screen, &|o: &mut TreeOptions| o.only_folders ^= true)
- }
+ ":toggle_files" => self.with_new_options(screen, &|o: &mut TreeOptions| o.only_folders ^= true),
":toggle_hidden" => self.with_new_options(screen, &|o| o.show_hidden ^= true),
":toggle_git_ignore" => self.with_new_options(screen, &|options| {
options.respect_git_ignore = match options.respect_git_ignore {
