Added parser, ls and start commands and search routines needed for them, ProjectList now has constructor setting month and num_projects

This commit is contained in:
Patrick Lipka 2021-12-10 13:22:46 +01:00
parent 363372f34e
commit 7c54302cb0
5 changed files with 141 additions and 6 deletions

View File

@ -27,6 +27,36 @@ void Project::set_active_task(int id){
active_task = &tasks[id]; active_task = &tasks[id];
} }
Task* Project::find_task_by_name(std::string task_name){
for (int i=0; i<num_tasks; i++){
if(tasks[i].name == task_name){
return &(tasks[i]);
}else{
std::cout << "Task " << task_name << " not found in project " << name << std::endl;;
}
}
return NULL;
}
int Project::find_task_id_by_name(std::string task_name){
for (int i=0; i<num_tasks; i++){
if(tasks[i].name == task_name){
return i;
}
}
std::cout << "Task " << task_name << " not found in project " << name << std::endl;
return -1;
}
// ProjectList definitions:
ProjectList::ProjectList(std::string month){
ProjectList::month = month;
ProjectList::num_projects = 0;
}
void ProjectList::add_project(Project proj){ void ProjectList::add_project(Project proj){
projects.push_back(proj); projects.push_back(proj);
set_active_project(num_projects); set_active_project(num_projects);
@ -120,3 +150,25 @@ void ProjectList::load(std::string file_name){
delete [] active_task_arr; delete [] active_task_arr;
inf.close(); inf.close();
} }
Project* ProjectList::find_project_by_name(std::string proj_name){
std::cout << proj_name <<std::endl;
for (int i=0; i<num_projects; i++){
if(projects[i].name == proj_name){
return &(projects[i]);
}else{
std::cout << "Project " << proj_name << " not found in projects list" << std::endl;
}
}
return NULL;
}
int ProjectList::find_project_id_by_name(std::string proj_name){
for (int i=0; i<num_projects; i++){
if(projects[i].name == proj_name){
return i;
}
}
std::cout << "Project " << proj_name << " not found in projects list" << std::endl;
return -1;
}

View File

@ -16,10 +16,14 @@ class Project{
int active_task_id; int active_task_id;
Task *active_task; Task *active_task;
void set_active_task(int id); void set_active_task(int id);
Task *find_task_by_name(std::string task_name);
int find_task_id_by_name(std::string task_name);
}; };
class ProjectList{ class ProjectList{
public: public:
ProjectList(std::string month);
std::string month;
int num_projects; int num_projects;
std::vector<Project> projects; std::vector<Project> projects;
void add_project(Project proj); void add_project(Project proj);
@ -29,6 +33,9 @@ class ProjectList{
void set_active_project(int id); void set_active_project(int id);
void save(std::string file_name); void save(std::string file_name);
void load(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 #endif

View File

@ -16,7 +16,7 @@ int main(){
tracking = 0; tracking = 0;
// TEST: set up test list // TEST: set up test list
ProjectList proj_list; ProjectList proj_list("dec");
Project proj("Test Project"); Project proj("Test Project");
Task task1("Task 1"),task2("Task 2"),task3("Task 3"); Task task1("Task 1"),task2("Task 2"),task3("Task 3");
proj.add_task(task1); proj.add_task(task1);
@ -25,13 +25,14 @@ int main(){
proj_list.add_project(proj); proj_list.add_project(proj);
init_autocomplete(&proj_list); init_autocomplete(&proj_list);
// use GNU readline for auto completion and history when parsing command input // use GNU readline for auto completion and history when parsing command input
while(1) { while(1) {
rl_attempted_completion_function = tt_name_completion; rl_attempted_completion_function = tt_name_completion;
char *buffer = readline("tt> "); char *buffer = readline("tt> ");
if (buffer){ if (buffer){
printf("Entered: %s\n", buffer); std::string str = buffer;
parse_input(str,&proj_list);
add_history(buffer); add_history(buffer);
free(buffer); free(buffer);
} }

View File

@ -5,7 +5,7 @@
#include <readline/history.h> #include <readline/history.h>
#include "ui.h" #include "ui.h"
#include "project.h" #include "project.h"
#include "track.h"
// Use GNU readline library for autocompletion and command history // Use GNU readline library for autocompletion and command history
std::string command_names[num_commands]={ std::string command_names[num_commands]={
@ -52,6 +52,13 @@ std::string space_to_underscore(std::string str){
return 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){ void init_autocomplete(ProjectList *proj_list){
// add command names to auto complete list // add command names to auto complete list
for (int i=0; i<num_commands; i++){ for (int i=0; i<num_commands; i++){
@ -68,3 +75,63 @@ void init_autocomplete(ProjectList *proj_list){
} }
} }
} }
void parse_input(std::string input, ProjectList *proj_list){
if(input.find("ls") != std::string::npos){
if (input.length()>2){
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; i<proj_list->num_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; j<proj->num_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);
}
}
}

View File

@ -12,4 +12,12 @@ char **tt_name_completion(const char* text, int start, int end);
char *tt_name_generator(const char *text, int state); char *tt_name_generator(const char *text, int state);
void init_autocomplete(ProjectList *proj_list); void init_autocomplete(ProjectList *proj_list);
std::string space_to_underscore(std::string); 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 #endif