New project command added and parser updated

This commit is contained in:
Patrick Lipka 2021-12-10 15:58:27 +01:00
parent 7c54302cb0
commit b6139369f7
3 changed files with 46 additions and 18 deletions

View File

@ -152,12 +152,9 @@ void ProjectList::load(std::string file_name){
} }
Project* ProjectList::find_project_by_name(std::string proj_name){ Project* ProjectList::find_project_by_name(std::string proj_name){
std::cout << proj_name <<std::endl;
for (int i=0; i<num_projects; i++){ for (int i=0; i<num_projects; i++){
if(projects[i].name == proj_name){ if(projects[i].name == proj_name){
return &(projects[i]); return &(projects[i]);
}else{
std::cout << "Project " << proj_name << " not found in projects list" << std::endl;
} }
} }
return NULL; return NULL;
@ -169,6 +166,5 @@ int ProjectList::find_project_id_by_name(std::string proj_name){
return i; return i;
} }
} }
std::cout << "Project " << proj_name << " not found in projects list" << std::endl;
return -1; return -1;
} }

View File

@ -77,42 +77,62 @@ void init_autocomplete(ProjectList *proj_list){
} }
void parse_input(std::string input, ProjectList *proj_list){ void parse_input(std::string input, ProjectList *proj_list){
if(input.find("ls") != std::string::npos){ size_t command_end = input.find(" ");
if (input.length()>2){ std::string command = input.substr(0,command_end);
command_ls(input.substr(3),proj_list); std::string argument = input.substr(command_end+1);
}else{
if (command == "ls"){
if (command_end == std::string::npos){
command_ls("",proj_list); command_ls("",proj_list);
}
} else if(input.find("start") != std::string::npos){
if (input.length()>5){
command_start(input.substr(6),proj_list);
}else{ }else{
command_ls(argument,proj_list);
}
}else if (command == "start"){
if (command_end == std::string::npos){
command_start("",proj_list); command_start("",proj_list);
}else{
command_start(argument,proj_list);
}
}else if (command == "np"){
if (command_end == std::string::npos){
std::cout << "new peoject: Please specify project name." << std::endl;
}else{
command_np(argument,proj_list);
} }
} }
} }
// list projects/tasks
void command_ls(std::string input, ProjectList *proj_list){ void command_ls(std::string input, ProjectList *proj_list){
std::string str = input; std::string str = input;
str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
if (str.length() == 0){ if (str.length() == 0){
// list projects // list projects
std::string active_proj_str = proj_list->active_project->name;
if(proj_list->active_project->num_tasks > 0) active_proj_str += "/" + proj_list->active_project->active_task->name;
std::cout << "Active Project/Task: " << active_proj_str << std::endl;
std::cout << std::endl;
std::cout << "List of Projects:" << std::endl; std::cout << "List of Projects:" << std::endl;
for (int i=0; i<proj_list->num_projects; i++){ for (int i=0; i<proj_list->num_projects; i++){
std::cout << proj_list->projects[i].name << std::endl; std::cout << proj_list->projects[i].name << std::endl;
} }
std::cout << std::endl;
}else{ }else{
// list tasks for given project // list tasks for given project
Project *proj = proj_list->find_project_by_name(underscore_to_space(str)); Project *proj = proj_list->find_project_by_name(underscore_to_space(str));
std::cout << "List of Tasks for Project " << proj->name << ":" << std::endl; if(proj == NULL){
for(int j=0; j<proj->num_tasks; j++){ std::cout << "Project " << str << " does not exists." << std::endl;
std::cout << proj->tasks[j].name << std::endl; }else{
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;
}
} }
} }
} }
// start tracking of active project or specific project
void command_start(std::string input, ProjectList *proj_list){ void command_start(std::string input, ProjectList *proj_list){
std::string str = input; std::string str = input;
str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
@ -135,3 +155,15 @@ void command_start(std::string input, ProjectList *proj_list){
} }
} }
} }
// create new project
void command_np(std::string name, ProjectList *proj_list){
if (proj_list->find_project_id_by_name(name) == -1){
Project proj(name);
std::string auto_complete_str = space_to_underscore(name);
autocomplete_names.push_back(auto_complete_str);
proj_list->add_project(proj);
}else{
std::cout << "A project with the name " << name << " already exists." << std::endl;
}
}

View File

@ -19,5 +19,5 @@ void parse_input(std::string input, ProjectList *proj_list);
// command definitions // command definitions
void command_ls(std::string input, ProjectList *proj_list); void command_ls(std::string input, ProjectList *proj_list);
void command_start(std::string input, ProjectList *proj_list); void command_start(std::string input, ProjectList *proj_list);
void command_np(std::string name, ProjectList *proj_list);
#endif #endif