summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-02-04 16:30:16 +0100
committerqkzk <qu3nt1n@gmail.com>2023-02-04 16:30:16 +0100
commitd9963460e26ba328956a2596c50208df9e8f0bbf (patch)
tree2562f4408ef5baf59d73ce3e49bd1eabeaf3796e /src
parent2a327b04c9bcba135588514d6e7bb98299d851b1 (diff)
completion in search from tree
Diffstat (limited to 'src')
-rw-r--r--src/completion.rs51
-rw-r--r--src/tab.rs29
-rw-r--r--src/tree.rs2
3 files changed, 54 insertions, 28 deletions
diff --git a/src/completion.rs b/src/completion.rs
index 5097993..6894bb8 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -3,6 +3,7 @@ use std::fs::{self, ReadDir};
use crate::fileinfo::PathContent;
use crate::fm_error::FmResult;
use crate::mode::Mode;
+use crate::tree::ColoredString;
/// Different kind of completions
#[derive(Clone, Default, Copy)]
@@ -22,7 +23,7 @@ pub enum InputCompleted {
/// showing where the user is in the vec.
#[derive(Clone, Default)]
pub struct Completion {
- pub kind: InputCompleted,
+ kind: InputCompleted,
/// Possible completions
pub proposals: Vec<String>,
/// Which completion is selected by the user
@@ -93,27 +94,9 @@ impl Completion {
self.proposals.clear();
}
- /// Fill the completions items from some parameters, depending on the mode.
- /// In Exec mode, we search for executable in $PATH starting with what the user typed.
- /// In Goto mode, we search for valid absolute & relative paths starting with what the user typed.
- /// In Search mode, we search for filenames in current directory starting with what the user typed.
- pub fn complete(
- &mut self,
- input_string: &str,
- path_content: &PathContent,
- current_path: &str,
- ) -> FmResult<()> {
- match self.kind {
- InputCompleted::Exec => self.exec(input_string),
- InputCompleted::Goto => self.goto(input_string, current_path),
- InputCompleted::Search => self.search(input_string, path_content),
- InputCompleted::Nothing => Ok(()),
- }
- }
-
/// Goto completion.
/// Looks for the valid path completing what the user typed.
- fn goto(&mut self, input_string: &str, current_path: &str) -> FmResult<()> {
+ pub fn goto(&mut self, input_string: &str, current_path: &str) -> FmResult<()> {
self.update_from_input(input_string, current_path);
let (parent, last_name) = split_input_string(input_string);
if last_name.is_empty() {
@@ -164,7 +147,7 @@ impl Completion {
}
/// Looks for programs in $PATH completing the one typed by the user.
- fn exec(&mut self, input_string: &str) -> FmResult<()> {
+ pub fn exec(&mut self, input_string: &str) -> FmResult<()> {
let mut proposals: Vec<String> = vec![];
if let Some(paths) = std::env::var_os("PATH") {
for path in std::env::split_paths(&paths).filter(|path| path.exists()) {
@@ -187,7 +170,11 @@ impl Completion {
}
/// Looks for file within current folder completing what the user typed.
- fn search(&mut self, input_string: &str, path_content: &PathContent) -> FmResult<()> {
+ pub fn search_from_normal(
+ &mut self,
+ input_string: &str,
+ path_content: &PathContent,
+ ) -> FmResult<()> {
self.update(
path_content
.content
@@ -198,6 +185,26 @@ impl Completion {
);
Ok(())
}
+
+ /// Looks for file within tree completing what the user typed.
+ pub fn search_from_tree(
+ &mut self,
+ input_string: &str,
+ content: &[(String, ColoredString)],
+ ) -> FmResult<()> {
+ self.update(
+ content
+ .iter()
+ .filter(|(_, s)| s.text.contains(input_string))
+ .map(|(_, s)| {
+ let text = s.text.replace("▸ ", "");
+ text.replace("▾ ", "")
+ })
+ .collect(),
+ );
+
+ Ok(())
+ }
}
/// true if the filename starts with a pattern
diff --git a/src/tab.rs b/src/tab.rs
index 7148bbc..bfdaec7 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -3,7 +3,7 @@ use std::path;
use users::UsersCache;
use crate::args::Args;
-use crate::completion::Completion;
+use crate::completion::{Completion, InputCompleted};
use crate::config::Colors;
use crate::content_window::ContentWindow;
use crate::fileinfo::{FileInfo, FileKind, PathContent};
@@ -98,10 +98,29 @@ impl Tab {
/// Fill the input string with the currently selected completion.
pub fn fill_completion(&mut self) -> FmResult<()> {
- self.completion.set_kind(&self.mode);
- let current_path = self.path_content_str().unwrap_or_default().to_owned();
- self.completion
- .complete(&self.input.string(), &self.path_content, &current_path)
+ // self.completion.set_kind(&self.mode);
+ match self.mode {
+ Mode::InputCompleted(InputCompleted::Goto) => {
+ let current_path = self.path_content_str().unwrap_or_default().to_owned();
+ self.completion.goto(&self.input.string(), &current_path)
+ }
+ Mode::InputCompleted(InputCompleted::Exec) => {
+ self.completion.exec(&self.input.string())
+ }
+ Mode::InputCompleted(InputCompleted::Search)
+ if matches!(self.previous_mode, Mode::Normal) =>
+ {
+ self.completion
+ .search_from_normal(&self.input.string(), &self.path_content)
+ }
+ Mode::InputCompleted(InputCompleted::Search)
+ if matches!(self.previous_mode, Mode::Tree) =>
+ {
+ self.completion
+ .search_from_tree(&self.input.string(), &self.directory.content)
+ }
+ _ => Ok(()),
+ }
}
/// Refresh the current view.
diff --git a/src/tree.rs b/src/tree.rs
index 9dc9f63..17309e3 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -428,7 +428,7 @@ impl Tree {
return Some(self.node.position.clone());
}
- for tree in self.leaves.iter_mut() {
+ for tree in self.leaves.iter_mut().rev() {
let Some(position) = tree.select_first_match(key) else { continue };
return Some(position);
}