add time command added, is_num helper added for that

This commit is contained in:
Patrick Lipka 2021-12-13 22:05:04 +01:00
parent 28f5b43ca8
commit 1e5f1bd0ee
4 changed files with 77 additions and 6 deletions

View File

@ -15,10 +15,16 @@ std::string underscore_to_space(std::string str){
return str; return str;
} }
std::string trim(const std::string& str) std::string trim(const std::string& str){
{
const char* WhiteSpace = " "; const char* WhiteSpace = " ";
std::size_t start = str.find_first_not_of(WhiteSpace); std::size_t start = str.find_first_not_of(WhiteSpace);
std::size_t end = str.find_last_not_of(WhiteSpace); std::size_t end = str.find_last_not_of(WhiteSpace);
return start == end ? std::string() : str.substr(start, end - start + 1); 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;
}

View File

@ -5,5 +5,5 @@
std::string trim(const std::string &str); std::string trim(const std::string &str);
std::string space_to_underscore(std::string str); std::string space_to_underscore(std::string str);
std::string underscore_to_space(std::string str); std::string underscore_to_space(std::string str);
bool is_num(const std::string &str);
#endif #endif

View File

@ -120,10 +120,31 @@ void parse_input(std::string input, ProjectList *proj_list){
size_t first_name_end = argument.find(" "); size_t first_name_end = argument.find(" ");
command_re(argument.substr(0,first_name_end),argument.substr(first_name_end+1),proj_list); 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){ void command_ls(std::string input, ProjectList *proj_list){
std::string active_proj_str=""; std::string active_proj_str="";
std::string str = input; 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;
}
}

View File

@ -26,6 +26,6 @@ void command_sp(std::string name, ProjectList *proj_list);
void command_st(std::string input, ProjectList *proj_list); void command_st(std::string input, ProjectList *proj_list);
void command_nt(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_re(std::string input, std::string new_name, ProjectList *proj_lits);
void command_at(std::string input, ProjectList *proj_list); void command_at(std::string input, int wtime, ProjectList *proj_list);
void command_rt(std::string input, ProjectList *proj_list); void command_rt(std::string input, int wtime, ProjectList *proj_list);
#endif #endif