Skip to content

Commit

Permalink
search: use wildcards, make search term required and update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolassv committed Jun 13, 2024
1 parent d248292 commit 60b9865
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Subcommand `search` to search the list of last activities for terms (thanks to [@Pyxels](https://github.com/Pyxels))
- Subcommand `status` to display the total duration of activities today, in the current week and in the current month (thanks to [@airenas](https://github.com/airenas))
- Option `--no-quotes` to `project` to suppres quotes in the projects list (thanks to [@defigli](https://github.com/defigli))

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ bartib list --from 2021-09-01 --to 2021-09-05 # list activities in a given ti
bartib list --project "The most exciting project" # list activities for a given project
bartib list --round 15m # rounds the start and end time to the nearest duration. Durations can be in minutes or hours. E.g. 15m or 4h

bartib search # search all descriptions and projects for a specific term
bartib search "exiting" # search all descriptions and projects for a specific term
bartib search "e*t?ng" # use '?' and '*' as wildcards
```

### Edit activities
Expand Down
10 changes: 7 additions & 3 deletions src/controller/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use chrono::NaiveDateTime;
use wildmatch::WildMatch;

use crate::conf;
use crate::data::activity;
Expand Down Expand Up @@ -200,19 +201,22 @@ pub fn list_last_activities(file_name: &str, number: usize) -> Result<()> {

// searches for the term in descriptions and projects
pub fn search(file_name: &str, search_term: Option<&str>) -> Result<()> {
let search_term = search_term.unwrap_or("");
let search_term = search_term
.map(|term| format!("*{}*", term.to_lowercase()))
.unwrap_or("".to_string());
let file_content = bartib_file::get_file_content(file_name)?;

let descriptions_and_projects: Vec<(&String, &String)> =
getter::get_descriptions_and_projects(&file_content);
let search_term_wildmatch = WildMatch::new(&search_term);
let matches: Vec<(usize, &(&String, &String))> = descriptions_and_projects
.iter()
.rev()
.enumerate()
.rev()
.filter(|(_index, (desc, proj))| {
desc.to_lowercase().contains(&search_term.to_lowercase())
|| proj.to_lowercase().contains(&search_term.to_lowercase())
search_term_wildmatch.matches(&desc.to_lowercase())
|| search_term_wildmatch.matches(&proj.to_lowercase())
})
.collect();

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ To get started, view the `start` help with `bartib start --help`")
Arg::with_name("search_term")
.value_name("SEARCH_TERM")
.help("the search term")
.required(false)
.required(true)
.takes_value(true)
.default_value("''"),
),
Expand Down

0 comments on commit 60b9865

Please sign in to comment.