Skip to content

Commit

Permalink
cmd/query: Add --sort-by option
Browse files Browse the repository at this point in the history
Added the option `--sort-by [score|path|last-accessed]` for the `query`
subcommand with default value `score` i.e:

```bash
$ zoxide query --list --score --sort-by last-accessed
   4.0 /home/martin/playground/scala/scala-3-project-template
   3.5 /home/martin/playground/rust
   3.8 /home/martin/playground/python
  12.0 /home/martin/projects/zoxide
  11.8 /home/martin/playground
  16.5 /home/martin/projects/thesis
   6.0 /home/martin/projects/thesis/frontend
...
```

```bash
$ zoxide query --list --score --sort-by path
   0.2 /home/martin/.local/state
   0.2 /home/martin/.local/share/zoxide
   0.2 /home/martin/.local/share
   0.2 /home/martin/.config/protonmail/bridge-v3
   0.2 /home/martin/.config/protonmail
   0.5 /home
   0.5 /etc/nixos/lib/lisp
   0.8 /
...
```

```bash
$ zoxide query --list --score
  16.5 /home/martin/projects/thesis
  16.0 /home/martin/projects/zoxide
   8.0 /home/martin/projects/egcd
   8.0 /persist
   6.0 /home/martin/projects/thesis/frontend
...
```

Fixes #784 as well as #815.
  • Loading branch information
mzacho committed Jul 15, 2024
1 parent 8da8f50 commit ac98f40
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions contrib/completions/_zoxide

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

1 change: 1 addition & 0 deletions contrib/completions/_zoxide.ps1

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

6 changes: 5 additions & 1 deletion contrib/completions/zoxide.bash

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

1 change: 1 addition & 0 deletions contrib/completions/zoxide.elv

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

1 change: 1 addition & 0 deletions contrib/completions/zoxide.fish

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

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

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

11 changes: 11 additions & 0 deletions src/cmd/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ pub struct Query {
#[clap(long, short, conflicts_with = "interactive")]
pub list: bool,

/// Sort result
#[clap(long)]
pub sort_by: Option<Ordering>,

/// Print score with results
#[clap(long, short)]
pub score: bool,
Expand All @@ -174,6 +178,13 @@ pub struct Query {
pub exclude: Option<String>,
}

#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Ordering {
Path,
Score,
LastAccessed,
}

/// Remove a directory from the database
#[derive(Debug, Parser)]
#[clap(
Expand Down
14 changes: 11 additions & 3 deletions src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::{self, Write};

use anyhow::{Context, Result};

use super::Ordering;
use crate::cmd::{Query, Run};
use crate::config;
use crate::db::{Database, Epoch, Stream, StreamOptions};
Expand All @@ -18,7 +19,8 @@ impl Run for Query {
impl Query {
fn query(&self, db: &mut Database) -> Result<()> {
let now = util::current_time()?;
let mut stream = self.get_stream(db, now)?;
let ordering = self.sort_by;
let mut stream = self.get_stream(db, now, ordering)?;

if self.interactive {
self.query_interactive(&mut stream, now)
Expand Down Expand Up @@ -76,10 +78,16 @@ impl Query {
writeln!(handle, "{dir}").pipe_exit("stdout")
}

fn get_stream<'a>(&self, db: &'a mut Database, now: Epoch) -> Result<Stream<'a>> {
fn get_stream<'a>(
&self,
db: &'a mut Database,
now: Epoch,
ordering: Option<Ordering>,
) -> Result<Stream<'a>> {
let mut options = StreamOptions::new(now)
.with_keywords(self.keywords.iter().map(|s| s.as_str()))
.with_exclude(config::exclude_dirs()?);
.with_exclude(config::exclude_dirs()?)
.sort_by(ordering);
if !self.all {
let resolve_symlinks = config::resolve_symlinks();
options = options.with_exists(true).with_resolve_symlinks(resolve_symlinks);
Expand Down
9 changes: 9 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ impl Database {
self.with_dirty_mut(|dirty| *dirty = true);
}

pub fn sort_by_last_accessed(&mut self) {
self.with_dirs_mut(|dirs| {
dirs.sort_unstable_by(|dir1: &Dir, dir2: &Dir| {
dir1.last_accessed.cmp(&dir2.last_accessed)
})
});
self.with_dirty_mut(|dirty| *dirty = true);
}

pub fn dirty(&self) -> bool {
*self.borrow_dirty()
}
Expand Down
18 changes: 17 additions & 1 deletion src/db/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{fs, path};

use glob::Pattern;

use crate::cmd::Ordering;
use crate::db::{Database, Dir, Epoch};
use crate::util::{self, MONTH};

Expand All @@ -15,7 +16,11 @@ pub struct Stream<'a> {

impl<'a> Stream<'a> {
pub fn new(db: &'a mut Database, options: StreamOptions) -> Self {
db.sort_by_score(options.now);
match options.sort_by {
Ordering::Path => db.sort_by_path(),
Ordering::Score => db.sort_by_score(options.now),
Ordering::LastAccessed => db.sort_by_last_accessed(),
}
let idxs = (0..db.dirs().len()).rev();
Stream { db, idxs, options }
}
Expand Down Expand Up @@ -108,6 +113,9 @@ pub struct StreamOptions {
/// Directories that do not exist and haven't been accessed since TTL will
/// be lazily removed.
ttl: Epoch,

/// Ordering of the stream entries
sort_by: Ordering,
}

impl StreamOptions {
Expand All @@ -119,6 +127,7 @@ impl StreamOptions {
exists: false,
resolve_symlinks: false,
ttl: now.saturating_sub(3 * MONTH),
sort_by: Ordering::Score,
}
}

Expand All @@ -145,6 +154,13 @@ impl StreamOptions {
self.resolve_symlinks = resolve_symlinks;
self
}

pub fn sort_by(mut self, ordering: Option<Ordering>) -> Self {
if let Some(o) = ordering {
self.sort_by = o
};
self
}
}

#[cfg(test)]
Expand Down

0 comments on commit ac98f40

Please sign in to comment.