Skip to content

Commit

Permalink
Add --score flag to show score with query result
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Jun 10, 2020
1 parent 283446f commit 4b5f2e7
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 136 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `$_ZO_FZF_OPTS` to specify custom options for `fzf`
- `zoxide query --list` to list all matches
- `zoxide query --score` to show score along with result

### Changed

Expand Down
89 changes: 44 additions & 45 deletions Cargo.lock

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

8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ brew install zoxide
nix-env -iA nixpkgs.zoxide
```

#### On OpenBSD

```sh
pkg_add zoxide
```

#### Other (using Cargo) [![crates.io](https://img.shields.io/crates/v/zoxide)](https://crates.io/crates/zoxide)

```sh
Expand Down Expand Up @@ -197,6 +191,6 @@ eval "$(zoxide init zsh)"
- `$_ZO_EXCLUDE_DIRS`: list of directories separated by platform-specific characters
("`:`" on Linux/macOS, "`;`" on Windows) to be excluded from the database
- `$_ZO_FZF_OPTS`: custom flags to pass to `fzf`
- `$_ZO_MAXAGE`: sets the maximum total rank after which entries start getting deleted
- `$_ZO_MAXAGE`: sets the maximum total age after which entries start getting deleted

[`dirs` documentation]: https://docs.rs/dirs/latest/dirs/fn.data_local_dir.html
47 changes: 45 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use float_ord::FloatOrd;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use std::fmt::{self, Display, Formatter};
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -182,7 +183,7 @@ pub struct DbMatches<'a> {
impl<'a> DbMatches<'a> {
pub fn new(db: &'a mut Db, now: Epoch, keywords: &[String]) -> DbMatches<'a> {
db.dirs
.sort_unstable_by_key(|dir| FloatOrd(dir.get_frecency(now)));
.sort_unstable_by_key(|dir| FloatOrd(dir.get_score(now)));

let idxs = (0..db.dirs.len()).rev();
let keywords = keywords
Expand Down Expand Up @@ -257,7 +258,7 @@ impl Dir {
true
}

pub fn get_frecency(&self, now: Epoch) -> Rank {
pub fn get_score(&self, now: Epoch) -> Rank {
const HOUR: Epoch = 60 * 60;
const DAY: Epoch = 24 * HOUR;
const WEEK: Epoch = 7 * DAY;
Expand All @@ -273,4 +274,46 @@ impl Dir {
self.rank * 0.25
}
}

pub fn display(&self) -> DirDisplay {
DirDisplay { dir: self }
}

pub fn display_score(&self, now: Epoch) -> DirScoreDisplay {
DirScoreDisplay { dir: self, now }
}
}

pub struct DirDisplay<'a> {
dir: &'a Dir,
}

impl Display for DirDisplay<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.dir.path)
}
}

pub struct DirScoreDisplay<'a> {
dir: &'a Dir,
now: Epoch,
}

impl Display for DirScoreDisplay<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
const SCORE_MIN: Rank = 0.0;
const SCORE_MAX: Rank = 9999.0;

let score = self.dir.get_score(self.now);

let score_clamped = if score > SCORE_MAX {
SCORE_MAX
} else if score > SCORE_MIN {
score
} else {
SCORE_MIN
};

write!(f, "{:>4.0} {}", score_clamped, self.dir.path)
}
}

0 comments on commit 4b5f2e7

Please sign in to comment.