From 43dfcd073b0efdd74fd5e86cc0fa5da2605313aa Mon Sep 17 00:00:00 2001 From: Priyanshu Verma Date: Wed, 19 Oct 2022 14:00:08 +0530 Subject: [PATCH] Added home only mode to zoxide query --- contrib/completions/_zoxide | 6 ++-- contrib/completions/_zoxide.ps1 | 6 ++-- contrib/completions/zoxide.bash | 2 +- contrib/completions/zoxide.elv | 6 ++-- contrib/completions/zoxide.fish | 3 +- contrib/completions/zoxide.ts | 14 +++++++- src/cmd/cmd.rs | 8 +++-- src/cmd/query.rs | 16 +++++++-- src/db/mod.rs | 8 +++++ src/db/stream.rs | 59 ++++++++++++++++++++++++--------- 10 files changed, 99 insertions(+), 29 deletions(-) diff --git a/contrib/completions/_zoxide b/contrib/completions/_zoxide index 45fdf67..6a7c96a 100644 --- a/contrib/completions/_zoxide +++ b/contrib/completions/_zoxide @@ -70,8 +70,10 @@ _arguments "${_arguments_options[@]}" \ '(-i --interactive)--list[List all matching directories]' \ '-s[Print score with results]' \ '--score[Print score with results]' \ -'-w[Search only through current working directory]' \ -'--workingdir[Search only through current working directory]' \ +'(-m --homedir)-c[Search only through current working directory]' \ +'(-m --homedir)--currentdir[Search only through current working directory]' \ +'(-c --currentdir)-m[Search only through home directory]' \ +'(-c --currentdir)--homedir[Search only through home directory]' \ '-h[Print help information]' \ '--help[Print help information]' \ '-V[Print version information]' \ diff --git a/contrib/completions/_zoxide.ps1 b/contrib/completions/_zoxide.ps1 index 6e6fae4..4d7aa2d 100644 --- a/contrib/completions/_zoxide.ps1 +++ b/contrib/completions/_zoxide.ps1 @@ -67,8 +67,10 @@ Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock { [CompletionResult]::new('--list', 'list', [CompletionResultType]::ParameterName, 'List all matching directories') [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'Print score with results') [CompletionResult]::new('--score', 'score', [CompletionResultType]::ParameterName, 'Print score with results') - [CompletionResult]::new('-w', 'w', [CompletionResultType]::ParameterName, 'Search only through current working directory') - [CompletionResult]::new('--workingdir', 'workingdir', [CompletionResultType]::ParameterName, 'Search only through current working directory') + [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Search only through current working directory') + [CompletionResult]::new('--currentdir', 'currentdir', [CompletionResultType]::ParameterName, 'Search only through current working directory') + [CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'Search only through home directory') + [CompletionResult]::new('--homedir', 'homedir', [CompletionResultType]::ParameterName, 'Search only through home directory') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information') diff --git a/contrib/completions/zoxide.bash b/contrib/completions/zoxide.bash index a034288..c113da4 100644 --- a/contrib/completions/zoxide.bash +++ b/contrib/completions/zoxide.bash @@ -102,7 +102,7 @@ _zoxide() { return 0 ;; zoxide__query) - opts="-i -l -s -w -h -V --all --interactive --list --score --workingdir --exclude --help --version ..." + opts="-i -l -s -c -m -h -V --all --interactive --list --score --currentdir --homedir --exclude --help --version ..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/contrib/completions/zoxide.elv b/contrib/completions/zoxide.elv index a6dccd7..83068b7 100644 --- a/contrib/completions/zoxide.elv +++ b/contrib/completions/zoxide.elv @@ -60,8 +60,10 @@ set edit:completion:arg-completer[zoxide] = {|@words| cand --list 'List all matching directories' cand -s 'Print score with results' cand --score 'Print score with results' - cand -w 'Search only through current working directory' - cand --workingdir 'Search only through current working directory' + cand -c 'Search only through current working directory' + cand --currentdir 'Search only through current working directory' + cand -m 'Search only through home directory' + cand --homedir 'Search only through home directory' cand -h 'Print help information' cand --help 'Print help information' cand -V 'Print version information' diff --git a/contrib/completions/zoxide.fish b/contrib/completions/zoxide.fish index b2de498..45cc084 100644 --- a/contrib/completions/zoxide.fish +++ b/contrib/completions/zoxide.fish @@ -21,7 +21,8 @@ complete -c zoxide -n "__fish_seen_subcommand_from query" -l all -d 'Show delete complete -c zoxide -n "__fish_seen_subcommand_from query" -s i -l interactive -d 'Use interactive selection' complete -c zoxide -n "__fish_seen_subcommand_from query" -s l -l list -d 'List all matching directories' complete -c zoxide -n "__fish_seen_subcommand_from query" -s s -l score -d 'Print score with results' -complete -c zoxide -n "__fish_seen_subcommand_from query" -s w -l workingdir -d 'Search only through current working directory' +complete -c zoxide -n "__fish_seen_subcommand_from query" -s c -l currentdir -d 'Search only through current working directory' +complete -c zoxide -n "__fish_seen_subcommand_from query" -s m -l homedir -d 'Search only through home directory' complete -c zoxide -n "__fish_seen_subcommand_from query" -s h -l help -d 'Print help information' complete -c zoxide -n "__fish_seen_subcommand_from query" -s V -l version -d 'Print version information' complete -c zoxide -n "__fish_seen_subcommand_from remove" -s i -l interactive -d 'Use interactive selection' diff --git a/contrib/completions/zoxide.ts b/contrib/completions/zoxide.ts index 78232df..ed8bc2e 100644 --- a/contrib/completions/zoxide.ts +++ b/contrib/completions/zoxide.ts @@ -144,8 +144,20 @@ const completion: Fig.Spec = { description: "Print score with results", }, { - name: ["-w", "--workingdir"], + name: ["-c", "--currentdir"], description: "Search only through current working directory", + exclusiveOn: [ + "-m", + "--homedir", + ], + }, + { + name: ["-m", "--homedir"], + description: "Search only through home directory", + exclusiveOn: [ + "-c", + "--currentdir", + ], }, { name: ["-h", "--help"], diff --git a/src/cmd/cmd.rs b/src/cmd/cmd.rs index 1e8fd42..9c69d3c 100644 --- a/src/cmd/cmd.rs +++ b/src/cmd/cmd.rs @@ -118,8 +118,12 @@ pub struct Query { pub score: bool, /// Search only through current working directory - #[clap(long, short)] - pub workingdir: bool, + #[clap(long, short, conflicts_with = "homedir")] + pub currentdir: bool, + + /// Search only through home directory + #[clap(long, short, conflicts_with = "currentdir", short = 'm')] + pub homedir: bool, /// Exclude a path from results #[clap(long, value_hint = ValueHint::DirPath, value_name = "path")] diff --git a/src/cmd/query.rs b/src/cmd/query.rs index a5d7487..851fad9 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -4,7 +4,7 @@ use anyhow::{Context, Result}; use crate::cmd::{Query, Run}; use crate::config; -use crate::db::{Database, DatabaseFile}; +use crate::db::{Database, DatabaseFile, WorkingMode}; use crate::error::BrokenPipeHandler; use crate::util::{self, Fzf}; @@ -29,8 +29,18 @@ impl Query { if let Some(path) = &self.exclude { stream = stream.with_exclude(path); } - if self.workingdir { - stream = stream.with_workingdir(self.workingdir); + + match stream.working_mode { + WorkingMode::NoKeywordPass => { + if self.currentdir { + stream = stream.with_currentdir(); + } else if self.homedir { + stream = stream.with_homedir(); + } else { + stream = stream.with_globaldir(); + } + } + WorkingMode::Current | WorkingMode::Home | WorkingMode::Global => {} } if self.interactive { diff --git a/src/db/mod.rs b/src/db/mod.rs index cf9b54a..df43ca4 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -17,6 +17,14 @@ pub struct Database<'file> { pub data_dir: &'file Path, } +#[derive(Debug)] +pub enum WorkingMode { + Current, + Home, + Global, + NoKeywordPass, +} + impl<'file> Database<'file> { pub fn save(&mut self) -> Result<()> { if !self.modified { diff --git a/src/db/stream.rs b/src/db/stream.rs index 155b9fd..abddeee 100644 --- a/src/db/stream.rs +++ b/src/db/stream.rs @@ -2,12 +2,14 @@ use std::iter::Rev; use std::ops::Range; use std::{fs, path}; -use crate::db::{Database, Dir, Epoch}; +use crate::db::{Database, Dir, Epoch, WorkingMode}; use crate::util; use std::env; pub struct Stream<'db, 'file> { + pub working_mode: WorkingMode, + db: &'db mut Database<'file>, idxs: Rev>, @@ -16,7 +18,6 @@ pub struct Stream<'db, 'file> { check_exists: bool, expire_below: Epoch, resolve_symlinks: bool, - workingdir: bool, exclude_path: Option, } @@ -37,7 +38,7 @@ impl<'db, 'file> Stream<'db, 'file> { check_exists: false, expire_below, resolve_symlinks: false, - workingdir: false, + working_mode: WorkingMode::Global, exclude_path: None, } } @@ -47,8 +48,18 @@ impl<'db, 'file> Stream<'db, 'file> { self } - pub fn with_workingdir(mut self, workingdir: bool) -> Self { - self.workingdir = workingdir; + pub fn with_globaldir(mut self) -> Self { + self.working_mode = WorkingMode::Global; + self + } + + pub fn with_homedir(mut self) -> Self { + self.working_mode = WorkingMode::Home; + self + } + + pub fn with_currentdir(mut self) -> Self { + self.working_mode = WorkingMode::Current; self } @@ -61,13 +72,15 @@ impl<'db, 'file> Stream<'db, 'file> { pub fn with_keywords>(mut self, keywords: &[S]) -> Self { let mut keywords_iter = keywords.iter(); - let workigdir_mode = keywords[0].as_ref(); - if workigdir_mode == "." { - self.workingdir = true; + let workingdir_mode = keywords[0].as_ref(); + if workingdir_mode == "." { + self.working_mode = WorkingMode::Current; keywords_iter.next(); - } else if workigdir_mode == env::var("HOME").unwrap_or_else(|_| String::from("~")) { - self.workingdir = false; + } else if workingdir_mode == env::var("HOME").unwrap_or_else(|_| String::from("~")) { + self.working_mode = WorkingMode::Home; keywords_iter.next(); + } else { + self.working_mode = WorkingMode::NoKeywordPass; } self.keywords = keywords_iter.map(util::to_lowercase).collect(); @@ -94,11 +107,27 @@ impl<'db, 'file> Stream<'db, 'file> { continue; } - // Check if working directory only mode is on and do the check if it is a sub directory of the working directory - if self.workingdir - && !dir.path.starts_with(env::current_dir().unwrap_or_default().to_str().unwrap_or_default()) - { - continue; + match self.working_mode { + WorkingMode::Current => { + if !dir.path.starts_with(env::current_dir().unwrap_or_default().to_str().unwrap_or_default()) { + { + continue; + } + } + } + + WorkingMode::Home => { + if !dir + .path + .starts_with(&env::var("HOME").unwrap_or_else(|_| env::var("UserProfile").unwrap_or_default())) + { + { + continue; + } + } + } + + WorkingMode::Global | WorkingMode::NoKeywordPass => {} } let dir = &self.db.dirs[idx];