search: use wildcards, make search term required and update changelog

This commit is contained in:
Nikolas Schmidt-Voigt 2024-06-13 23:24:42 +02:00 committed by Nikolas Schmidt-Voigt
parent d248292920
commit 60b9865377
4 changed files with 11 additions and 5 deletions

View file

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### 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)) - 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)) - Option `--no-quotes` to `project` to suppres quotes in the projects list (thanks to [@defigli](https://github.com/defigli))

View file

@ -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 --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 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 ### Edit activities

View file

@ -1,5 +1,6 @@
use anyhow::Result; use anyhow::Result;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use wildmatch::WildMatch;
use crate::conf; use crate::conf;
use crate::data::activity; use crate::data::activity;
@ -200,19 +201,22 @@ pub fn list_last_activities(file_name: &str, number: usize) -> Result<()> {
// searches for the term in descriptions and projects // searches for the term in descriptions and projects
pub fn search(file_name: &str, search_term: Option<&str>) -> Result<()> { 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 file_content = bartib_file::get_file_content(file_name)?;
let descriptions_and_projects: Vec<(&String, &String)> = let descriptions_and_projects: Vec<(&String, &String)> =
getter::get_descriptions_and_projects(&file_content); getter::get_descriptions_and_projects(&file_content);
let search_term_wildmatch = WildMatch::new(&search_term);
let matches: Vec<(usize, &(&String, &String))> = descriptions_and_projects let matches: Vec<(usize, &(&String, &String))> = descriptions_and_projects
.iter() .iter()
.rev() .rev()
.enumerate() .enumerate()
.rev() .rev()
.filter(|(_index, (desc, proj))| { .filter(|(_index, (desc, proj))| {
desc.to_lowercase().contains(&search_term.to_lowercase()) search_term_wildmatch.matches(&desc.to_lowercase())
|| proj.to_lowercase().contains(&search_term.to_lowercase()) || search_term_wildmatch.matches(&proj.to_lowercase())
}) })
.collect(); .collect();

View file

@ -265,7 +265,7 @@ To get started, view the `start` help with `bartib start --help`")
Arg::with_name("search_term") Arg::with_name("search_term")
.value_name("SEARCH_TERM") .value_name("SEARCH_TERM")
.help("the search term") .help("the search term")
.required(false) .required(true)
.takes_value(true) .takes_value(true)
.default_value("''"), .default_value("''"),
), ),