Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to query only into sub directories in zoxide query #468

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contrib/completions/_zoxide

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions contrib/completions/_zoxide.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contrib/completions/zoxide.bash

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions contrib/completions/zoxide.elv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions contrib/completions/zoxide.fish

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions contrib/completions/zoxide.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/cmd/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ pub struct Query {
#[clap(long, short)]
pub score: bool,

/// Search only through current working directory
#[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")]
pub exclude: Option<String>,
Expand Down
15 changes: 14 additions & 1 deletion src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -30,6 +30,19 @@ impl Query {
stream = stream.with_exclude(path);
}

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 {
let mut fzf = Fzf::new(false)?;
let stdin = fzf.stdin();
Expand Down
8 changes: 8 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 58 additions & 2 deletions src/db/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +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>>,

Expand Down Expand Up @@ -34,6 +38,7 @@ impl<'db, 'file> Stream<'db, 'file> {
check_exists: false,
expire_below,
resolve_symlinks: false,
working_mode: WorkingMode::Global,
exclude_path: None,
}
}
Expand All @@ -43,14 +48,42 @@ impl<'db, 'file> Stream<'db, 'file> {
self
}

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
}

pub fn with_exists(mut self, resolve_symlinks: bool) -> Self {
self.check_exists = true;
self.resolve_symlinks = resolve_symlinks;
self
}

pub fn with_keywords<S: AsRef<str>>(mut self, keywords: &[S]) -> Self {
self.keywords = keywords.iter().map(util::to_lowercase).collect();
let mut keywords_iter = keywords.iter();

let workingdir_mode = keywords[0].as_ref();
if workingdir_mode == "." {
self.working_mode = WorkingMode::Current;
keywords_iter.next();
} 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();
self
}

Expand All @@ -74,6 +107,29 @@ impl<'db, 'file> Stream<'db, 'file> {
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];
return Some(dir);
}
Expand Down
11 changes: 11 additions & 0 deletions templates/bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ function __zoxide_zi() {
result="$(\command zoxide query -i -- "$@")" && __zoxide_cd "${result}"
}

# Jump to sub directories of current directory.
function __zoxide_zw() {
\builtin local result
result="$(\command zoxide query --workingdir --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${result}"
}

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#
Expand All @@ -110,6 +116,11 @@ function {{cmd}}i() {
__zoxide_zi "$@"
}

\builtin unalias {{cmd}}w &>/dev/null || \builtin true
function {{cmd}}w() {
__zoxide_zw "$@"
}

# Load completions.
# - Bash 4.4+ is required to use `@Q`.
# - Completions require line editing. Since Bash supports only two modes of
Expand Down
12 changes: 12 additions & 0 deletions templates/fish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ function __zoxide_zi
and __zoxide_cd $result
end

# Jump to sub directories of current directory.
function __zoxide_zw
set -l result (command zoxide query --workingdir -- $argv)
and __zoxide_cd $result
end

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#
Expand All @@ -126,6 +132,12 @@ function {{cmd}}i
__zoxide_zi $argv
end

abbr --erase {{cmd}}w &>/dev/null
complete --command {{cmd}}w --erase
function {{cmd}}w
__zoxide_zw $argv
end

{%- when None %}

{{ not_configured }}
Expand Down
11 changes: 11 additions & 0 deletions templates/zsh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ function __zoxide_zi() {
result="$(\command zoxide query -i -- "$@")" && __zoxide_cd "${result}"
}

# Jump to sub directories of current directory.
function __zoxide_zw() {
\builtin local result
result="$(\command zoxide query --workingdir -- "$@")" && __zoxide_cd "${result}"
}

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#
Expand All @@ -96,6 +102,11 @@ function {{cmd}}i() {
__zoxide_zi "$@"
}

\builtin unalias {{cmd}}w &>/dev/null || \builtin true
function {{cmd}}w() {
__zoxide_zw "$@"
}

if [[ -o zle ]]; then
function __zoxide_z_complete() {
# Only show completions when the cursor is at the end of the line.
Expand Down