From 7c54302cb0bdc99993e8d718366cd3f81ab4bbe9 Mon Sep 17 00:00:00 2001 From: Patrick Lipka Date: Fri, 10 Dec 2021 13:22:46 +0100 Subject: [PATCH] Added parser, ls and start commands and search routines needed for them, ProjectList now has constructor setting month and num_projects --- src/project.cpp | 52 +++++++++++++++++++++++++++++++++++++ src/project.h | 7 +++++ src/tt.cpp | 11 ++++---- src/ui.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++- src/ui.h | 8 ++++++ 5 files changed, 141 insertions(+), 6 deletions(-) diff --git a/src/project.cpp b/src/project.cpp index 7341882..4f0f486 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -27,6 +27,36 @@ void Project::set_active_task(int id){ active_task = &tasks[id]; } +Task* Project::find_task_by_name(std::string task_name){ + for (int i=0; i projects; void add_project(Project proj); @@ -29,6 +33,9 @@ class ProjectList{ void set_active_project(int id); void save(std::string file_name); void load(std::string file_name); + Project *find_project_by_name(std::string proj_name); + int find_project_id_by_name(std::string proj_name); + }; #endif diff --git a/src/tt.cpp b/src/tt.cpp index 4928ea8..8733b59 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -16,22 +16,23 @@ int main(){ tracking = 0; // TEST: set up test list - ProjectList proj_list; + ProjectList proj_list("dec"); Project proj("Test Project"); - Task task1("Task 1"),task2("Task2"),task3("Task 3"); + Task task1("Task 1"),task2("Task 2"),task3("Task 3"); proj.add_task(task1); proj.add_task(task2); proj.add_task(task3); proj_list.add_project(proj); - + init_autocomplete(&proj_list); - // use GNU readline for auto completion and history when parsing command input while(1) { + rl_attempted_completion_function = tt_name_completion; char *buffer = readline("tt> "); if (buffer){ - printf("Entered: %s\n", buffer); + std::string str = buffer; + parse_input(str,&proj_list); add_history(buffer); free(buffer); } diff --git a/src/ui.cpp b/src/ui.cpp index 0aaf6bf..c3f78fa 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -5,7 +5,7 @@ #include #include "ui.h" #include "project.h" - +#include "track.h" // Use GNU readline library for autocompletion and command history std::string command_names[num_commands]={ @@ -52,6 +52,13 @@ std::string space_to_underscore(std::string str){ return str; } +std::string underscore_to_space(std::string str){ + for(size_t i = 0; i < str.length(); i++){ + if(str[i] == '_') str[i] = ' '; + } + return str; +} + void init_autocomplete(ProjectList *proj_list){ // add command names to auto complete list for (int i=0; i2){ + command_ls(input.substr(3),proj_list); + }else{ + command_ls("",proj_list); + } + } else if(input.find("start") != std::string::npos){ + if (input.length()>5){ + command_start(input.substr(6),proj_list); + }else{ + command_start("",proj_list); + } + } +} + + +void command_ls(std::string input, ProjectList *proj_list){ + std::string str = input; + str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); + + if (str.length() == 0){ + // list projects + std::cout << "List of Projects:" << std::endl; + for (int i=0; inum_projects; i++){ + std::cout << proj_list->projects[i].name << std::endl; + } + }else{ + // list tasks for given project + Project *proj = proj_list->find_project_by_name(underscore_to_space(str)); + std::cout << "List of Tasks for Project " << proj->name << ":" << std::endl; + for(int j=0; jnum_tasks; j++){ + std::cout << proj->tasks[j].name << std::endl; + } + } +} + +void command_start(std::string input, ProjectList *proj_list){ + std::string str = input; + str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); + + if (str.length() == 0){ + // start tracking for active task + track(proj_list->active_project); + }else{ + // change active task and start tracking + int place_of_slash = str.find("/"); + std::string proj_name = str.substr(0,place_of_slash); + std::string task_name = str.substr(place_of_slash+1); + Project *proj = proj_list->find_project_by_name(underscore_to_space(proj_name)); + int id=proj->find_task_id_by_name(underscore_to_space(task_name)); + if(id >= 0){ + std::cout << "id=" << id << std::endl; + proj->set_active_task(id); + std::cout << "now active: "<< proj->active_task->name << std::endl; + track(proj); + } + } +} diff --git a/src/ui.h b/src/ui.h index 34d52a9..0b497c1 100644 --- a/src/ui.h +++ b/src/ui.h @@ -12,4 +12,12 @@ char **tt_name_completion(const char* text, int start, int end); char *tt_name_generator(const char *text, int state); void init_autocomplete(ProjectList *proj_list); std::string space_to_underscore(std::string); +std::string underscore_to_space(std::string); + +void parse_input(std::string input, ProjectList *proj_list); + +// command definitions +void command_ls(std::string input, ProjectList *proj_list); +void command_start(std::string input, ProjectList *proj_list); + #endif