summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2022-12-17 22:20:11 +0100
committerqkzk <qu3nt1n@gmail.com>2022-12-17 22:20:11 +0100
commit4a8e3d8b576c35b82456216a36a02f8da726a301 (patch)
tree21ec98351e6424ec66150d8b2e8a81e3f7018069 /src
parentecc0503842b91ecae0e29ce9f7c11ec975e27d6a (diff)
enum for completion kind
Diffstat (limited to 'src')
-rw-r--r--src/completion.rs50
-rw-r--r--src/tab.rs19
2 files changed, 57 insertions, 12 deletions
diff --git a/src/completion.rs b/src/completion.rs
index 2da4437..681fdd9 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -2,11 +2,27 @@ use std::fs::{self, ReadDir};
use crate::fileinfo::PathContent;
use crate::fm_error::FmResult;
+use crate::mode::Mode;
+
+/// Different kind of completions
+#[derive(Clone, Default)]
+pub enum CompletionKind {
+ /// No completion needed
+ #[default]
+ Nothing,
+ /// Complete a directory path in filesystem
+ Goto,
+ /// Complete a filename from current directory
+ Search,
+ /// Complete an executable name from $PATH
+ Exec,
+}
/// Holds a `Vec<String>` of possible completions and an `usize` index
/// showing where the user is in the vec.
#[derive(Clone, Default)]
pub struct Completion {
+ pub kind: CompletionKind,
/// Possible completions
pub proposals: Vec<String>,
/// Which completion is selected by the user
@@ -14,6 +30,14 @@ pub struct Completion {
}
impl Completion {
+ pub fn set_kind(&mut self, mode: &Mode) {
+ self.kind = match *mode {
+ Mode::Exec => CompletionKind::Exec,
+ Mode::Goto => CompletionKind::Goto,
+ Mode::Search => CompletionKind::Search,
+ _ => CompletionKind::Nothing,
+ }
+ }
/// Is there any completion option ?
pub fn is_empty(&self) -> bool {
self.proposals.is_empty()
@@ -69,9 +93,27 @@ 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: Option<String>,
+ ) -> FmResult<()> {
+ match self.kind {
+ CompletionKind::Exec => self.exec(input_string),
+ CompletionKind::Goto => self.goto(input_string, current_path),
+ CompletionKind::Search => self.search(input_string, path_content),
+ CompletionKind::Nothing => Ok(()),
+ }
+ }
+
/// Goto completion.
/// Looks for the valid path completing what the user typed.
- pub fn goto(&mut self, input_string: &str, current_path: Option<String>) -> FmResult<()> {
+ fn goto(&mut self, input_string: &str, current_path: Option<String>) -> FmResult<()> {
let (parent, last_name) = split_input_string(input_string);
if last_name.is_empty() {
return Ok(());
@@ -108,7 +150,7 @@ impl Completion {
}
/// Looks for programs in $PATH completing the one typed by the user.
- pub fn exec(&mut self, input_string: &String) -> FmResult<()> {
+ fn exec(&mut self, input_string: &str) -> FmResult<()> {
let mut proposals: Vec<String> = vec![];
for path in std::env::var_os("PATH")
.unwrap_or_default()
@@ -131,7 +173,7 @@ impl Completion {
}
/// Looks for file within current folder completing what the user typed.
- pub fn search(&mut self, input_string: &String, path_content: &PathContent) -> FmResult<()> {
+ fn search(&mut self, input_string: &str, path_content: &PathContent) -> FmResult<()> {
self.update(
path_content
.files
@@ -145,7 +187,7 @@ impl Completion {
}
/// true if the filename starts with a pattern
-fn filename_startswith(entry: &std::fs::DirEntry, pattern: &String) -> bool {
+fn filename_startswith(entry: &std::fs::DirEntry, pattern: &str) -> bool {
entry
.file_name()
.to_string_lossy()
diff --git a/src/tab.rs b/src/tab.rs
index ae97dc0..4f9dd03 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -85,14 +85,17 @@ impl Tab {
/// Fill the input string with the currently selected completion.
pub fn fill_completion(&mut self) -> FmResult<()> {
- match self.mode {
- Mode::Goto => self.completion.goto(&self.input.string(), self.path_str()),
- Mode::Exec => self.completion.exec(&self.input.string()),
- Mode::Search => self
- .completion
- .search(&self.input.string(), &self.path_content),
- _ => Ok(()),
- }
+ self.completion.set_kind(&self.mode);
+ self.completion
+ .complete(&self.input.string(), &self.path_content, self.path_str())
+ // match self.mode {
+ // Mode::Goto => self.completion.goto(&self.input.string(), self.path_str()),
+ // Mode::Exec => self.completion.exec(&self.input.string()),
+ // Mode::Search => self
+ // .completion
+ // .search(&self.input.string(), &self.path_content),
+ // _ => Ok(()),
+ // }
}
/// Refresh the current view.