Added home only mode to zoxide query

This commit is contained in:
Priyanshu Verma 2022-10-19 14:00:08 +05:30
parent 07b1f72a2b
commit 43dfcd073b
10 changed files with 99 additions and 29 deletions

View File

@ -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]' \

View File

@ -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')

View File

@ -102,7 +102,7 @@ _zoxide() {
return 0
;;
zoxide__query)
opts="-i -l -s -w -h -V --all --interactive --list --score --workingdir --exclude --help --version <KEYWORDS>..."
opts="-i -l -s -c -m -h -V --all --interactive --list --score --currentdir --homedir --exclude --help --version <KEYWORDS>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0

View File

@ -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'

View File

@ -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'

View File

@ -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"],

View File

@ -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")]

View File

@ -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 {

View File

@ -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 {

View File

@ -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<Range<usize>>,
@ -16,7 +18,6 @@ pub struct Stream<'db, 'file> {
check_exists: bool,
expire_below: Epoch,
resolve_symlinks: bool,
workingdir: bool,
exclude_path: Option<String>,
}
@ -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<S: AsRef<str>>(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];