Skip to content

Commit

Permalink
Update listing CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Dec 20, 2023
1 parent f8eb02b commit b502c92
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jumpy"
version = "0.3.11"
version = "0.3.12"
edition = "2021"
authors = ["Clément Nerma <[email protected]>"]
license = "Apache-2.0"
Expand Down
8 changes: 7 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ pub struct Query {
}

#[derive(Args)]
pub struct List {}
pub struct List {
#[clap(long, help = "Display the entry's ranking")]
pub ranking: bool,

#[clap(long, help = "Sort by score")]
pub sort_by_score: bool,
}

#[derive(Args)]
pub struct Del {
Expand Down
12 changes: 4 additions & 8 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,8 @@ impl Index {
Some(path)
}

pub fn list(&self) -> Vec<&str> {
let mut entries: Vec<_> = self.scored_entries.iter().map(IndexEntry::from).collect();

entries.sort_by(|a, b| b.cmp(a));

entries.iter().map(|result| result.path).collect()
pub fn iter(&self) -> impl Iterator<Item = IndexEntry> {
self.scored_entries.iter().map(IndexEntry::from)
}

pub fn remove(&mut self, path: &str) -> Result<(), &'static str> {
Expand Down Expand Up @@ -217,8 +213,8 @@ impl Index {

#[derive(PartialEq, Eq)]
pub struct IndexEntry<'a> {
path: &'a str,
score: u64,
pub path: &'a str,
pub score: u64,
}

impl<'a> From<(&'a String, &'a u64)> for IndexEntry<'a> {
Expand Down
30 changes: 27 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod index;

use clap::Parser;

use crate::index::IndexEntry;

static INDEX_FILENAME: &str = "jumpy.db";

fn fail(message: &str) -> ! {
Expand Down Expand Up @@ -76,9 +78,31 @@ fn main() {
}
}

Action::List(List {}) => {
for dir in index.list() {
println!("{dir}");
Action::List(List {
ranking,
sort_by_score,
}) => {
let mut entries = index.iter().collect::<Vec<_>>();

if !sort_by_score {
entries.sort_by_key(|entry| entry.path);
} else {
entries.sort_by(|a, b| a.score.cmp(&b.score).reverse());
}

let longest_score = entries
.iter()
.map(|entry| entry.score)
.max()
.map(|score| score.to_string().len())
.unwrap_or(0);

for IndexEntry { path, score } in entries {
if !ranking {
println!("{path}");
} else {
println!("{score:>longest_score$} {path}");
}
}
}

Expand Down

0 comments on commit b502c92

Please sign in to comment.