From 1e5f1bd0eedb0a6492e8e773181d6a3238b38034 Mon Sep 17 00:00:00 2001 From: Patrick Lipka Date: Mon, 13 Dec 2021 22:05:04 +0100 Subject: [PATCH] add time command added, is_num helper added for that --- src/string_utils.cpp | 10 +++++-- src/string_utils.h | 2 +- src/ui.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++- src/ui.h | 4 +-- 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/string_utils.cpp b/src/string_utils.cpp index 5e72026..61db341 100644 --- a/src/string_utils.cpp +++ b/src/string_utils.cpp @@ -15,10 +15,16 @@ std::string underscore_to_space(std::string str){ return str; } -std::string trim(const std::string& str) -{ +std::string trim(const std::string& str){ const char* WhiteSpace = " "; std::size_t start = str.find_first_not_of(WhiteSpace); std::size_t end = str.find_last_not_of(WhiteSpace); return start == end ? std::string() : str.substr(start, end - start + 1); } + +bool is_num(const std::string &str){ + for (const char &c : str){ + if (std::isdigit(c) == 0) return false; + } + return true; +} diff --git a/src/string_utils.h b/src/string_utils.h index 3a99d77..460b860 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -5,5 +5,5 @@ std::string trim(const std::string &str); std::string space_to_underscore(std::string str); std::string underscore_to_space(std::string str); - +bool is_num(const std::string &str); #endif diff --git a/src/ui.cpp b/src/ui.cpp index 9bc1151..52f034c 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -120,10 +120,31 @@ void parse_input(std::string input, ProjectList *proj_list){ size_t first_name_end = argument.find(" "); command_re(argument.substr(0,first_name_end),argument.substr(first_name_end+1),proj_list); } + }else if (command == "at"){ + if (command_end == std::string::npos){ + std::cout << "add time: please specify time to add and task name [optional]." << std::endl; + }else{ + // only 1 argument + if(is_num(trim(argument))){ + command_at("",stoi(argument),proj_list); + }else{ + size_t start_of_name = argument.find(" "); + std::string wtime_str = argument.substr(0,start_of_name); + std::string task_name = argument.substr(start_of_name+1); + if (is_num(wtime_str) && start_of_name != std::string::npos){ + command_at(task_name,stoi(wtime_str),proj_list); + }else{ + std::cout << "add time: please specify time to add and task name [optional]." << std::endl; + } + + } + } + } } -// list projects/tasks + + // list projects/tasks void command_ls(std::string input, ProjectList *proj_list){ std::string active_proj_str=""; std::string str = input; @@ -393,3 +414,47 @@ void command_re(std::string input, std::string new_name, ProjectList *proj_list) } } } + + +// add time to task +void command_at(std::string input, int wtime, ProjectList *proj_list){ + size_t place_of_slash = input.find("/"); + std::string proj_name = input.substr(0,place_of_slash); + std::string task_name = input.substr(place_of_slash+1); + Project *proj = proj_list->find_project_by_name(trim(underscore_to_space(proj_name))); + if(place_of_slash == std::string::npos){ + if(proj_name.length()==0){ + // add time to active task + proj_list->active_project->active_task->add_time(wtime); + std::cout << wtime << "s added to task " << proj_list->active_project->name << "/" << proj_list->active_project->active_task->name << std::endl; + return; + }else{ + // add time to task in same project + std::cout << "adding time to task in same proj" << std::endl; + std::cout << "try to find task " << trim(underscore_to_space(proj_name)) << std::endl; + int id=proj_list->active_project->find_task_id_by_name(trim(underscore_to_space(proj_name))); + std::cout << "id: " << id << std::endl; + if (id >= 0){ + proj_list->active_project->tasks[id].add_time(wtime); + std::cout << wtime << "s added to task " << proj_list->active_project->name << "/" << proj_list->active_project->tasks[id].name << std::endl; + return; + }else{ + std::cout << "Task " << proj_list->active_project->name << "/" << proj_name << " does not exist." << std::endl; + return; + } + + } + } + if (proj == NULL){ + std::cout << "Project " << underscore_to_space(proj_name) << " does not exist." << std::endl; + return; + } + int id=proj->find_task_id_by_name(trim(underscore_to_space(task_name))); + if(id >= 0){ + proj->tasks[id].add_time(wtime); + std::cout << wtime << "s added to task " << proj->name << "/" << proj->tasks[id].name << std::endl; + }else{ + std::cout << "Task " << underscore_to_space(input) << " does not exist." << std::endl; + } +} + diff --git a/src/ui.h b/src/ui.h index 80c6bf9..71240d1 100644 --- a/src/ui.h +++ b/src/ui.h @@ -26,6 +26,6 @@ void command_sp(std::string name, ProjectList *proj_list); void command_st(std::string input, ProjectList *proj_list); void command_nt(std::string input, ProjectList *proj_list); void command_re(std::string input, std::string new_name, ProjectList *proj_lits); -void command_at(std::string input, ProjectList *proj_list); -void command_rt(std::string input, ProjectList *proj_list); +void command_at(std::string input, int wtime, ProjectList *proj_list); +void command_rt(std::string input, int wtime, ProjectList *proj_list); #endif