List last tasks with index

This commit is contained in:
Nikolas Schmidt-Voigt 2021-09-21 22:32:58 +02:00
parent 91ac85e7e8
commit f7d7aa8114
3 changed files with 40 additions and 20 deletions

View file

@ -64,13 +64,8 @@ pub fn list_projects(file_name: &str) -> Result<()> {
pub fn display_last_activity(file_name: &str) -> Result<()> {
let file_content = bartib_file::get_file_content(file_name)?;
let last_activity = getter::get_last_activity_by_end(&file_content);
if let Some(activity) = last_activity {
list::display_single_activity(&activity);
} else {
println!("No activity has been finished yet.")
}
let descriptions_and_projects : Vec<(&String, &String)> = getter::get_descriptions_and_projects(&file_content).into_iter().collect();
list::list_descriptions_and_projects(&descriptions_and_projects[..]);
Ok(())
}

View file

@ -1,3 +1,4 @@
use std::collections::{HashSet, VecDeque};
use chrono::{naive, NaiveDate};
use crate::data::activity;
@ -10,6 +11,22 @@ pub struct ActivityFilter {
pub date: Option<NaiveDate>,
}
pub fn get_descriptions_and_projects(file_content: &[bartib_file::Line]) -> VecDeque<(&String, &String)> {
let mut known_descriptions_and_projects : HashSet<(&String, &String)> = HashSet::new();
let mut descriptions_and_projects : VecDeque<(&String, &String)> = VecDeque::new();
// TODO: sortierung nach Start-Zeit
for description_and_project in get_activities(file_content).map(|a| (&a.description, &a.project)) {
if !known_descriptions_and_projects.contains(&description_and_project) {
known_descriptions_and_projects.insert(description_and_project);
descriptions_and_projects.push_back(description_and_project);
}
}
descriptions_and_projects
}
pub fn get_running_activities(file_content: &[bartib_file::Line]) -> Vec<&activity::Activity> {
get_activities(file_content)
.filter(|activity| !activity.is_stopped())
@ -43,7 +60,7 @@ pub fn filter_activities<'a>(
})
}
pub fn get_last_activity_by_end(file_content: &Vec<bartib_file::Line>) -> Option<&activity::Activity> {
pub fn get_last_activity_by_end(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> {
get_activities(&file_content)
.filter(|activity| activity.is_stopped())
.max_by_key(|activity| activity.end.unwrap_or(naive::MIN_DATE.and_hms(0, 0, 0)))

View file

@ -91,20 +91,28 @@ pub fn list_running_activities(running_activities: &[&activity::Activity]) {
}
}
// displays a single activity
pub fn display_single_activity(activity: &activity::Activity) {
println!("Begin: {}", activity.start.format(conf::FORMAT_DATETIME));
// display a list of projects and descriptions with index number
pub fn list_descriptions_and_projects(descriptions_and_projects : &[(&String, &String)]) {
if descriptions_and_projects.is_empty() {
println!("No activities have been tracked yet");
} else {
let mut descriptions_and_projects_table = table::Table::new(vec![
"Index".to_string(),
"Description".to_string(),
"Project".to_string()
]);
if let Some(end) = activity.end {
println!("End: {}", end.format(conf::FORMAT_DATETIME));
println!(
"Duration: {}",
format_util::format_duration(&activity.get_duration())
);
let mut i = 0;
for (description, project) in descriptions_and_projects {
descriptions_and_projects_table.add_row(
table::Row::new(vec![i.to_string(), description.to_string(), project.to_string()])
);
i += 1;
}
println!("\n{}", descriptions_and_projects_table);
}
println!("Project: {}", activity.project);
println!("Description: {}", activity.description);
}
// create a row for a activity