Feature added: natural time string input to at and rt commands

This commit is contained in:
Patrick Lipka 2022-01-25 14:45:05 +01:00
parent fb5b4eaced
commit 8d745afd80
5 changed files with 61 additions and 11 deletions

View File

@ -196,8 +196,8 @@ Time worked on project: 00:00:06
Tracking data saved to file /home/patrick/track/2021-12 Tracking data saved to file /home/patrick/track/2021-12
~~~ ~~~
#### `at <seconds> [<Project Name>/][<Task Name>]` #### `at <time>[s/m/h] [<Project Name>/][<Task Name>]`
Manually adds `<seconds>` seconds to task's work time. \ Manually adds `<time>` to task's work time. If `<time>` is followed by `m` or `h`, `<time>` is interpreted as minutes / hours. With `s` or without a postfix, time is interpreted as seconds. \
If the optional parameter `<Task Name>` is present, the time is beeing added to the Task with name `<Task Name>` of the currently active project.\ If the optional parameter `<Task Name>` is present, the time is beeing added to the Task with name `<Task Name>` of the currently active project.\
If also the optional parameter `<Project Name>` is present, the time is beeing added to the task with name `<Task Name>` of project `<Project Name>`. If also the optional parameter `<Project Name>` is present, the time is beeing added to the task with name `<Task Name>` of project `<Project Name>`.

View File

@ -1,4 +1,5 @@
#include <string> #include <string>
#include <iostream>
#include "string_utils.h" #include "string_utils.h"
// replaces white spaces by underscores, used for readline completion // replaces white spaces by underscores, used for readline completion
@ -32,3 +33,45 @@ bool is_num(const std::string &str){
} }
return true; return true;
} }
// convert string "X[s,m,h]" to seconds
int string2sec(std::string str){
int seconds = 0;
size_t place_of_hour = str.find("h");
size_t place_of_minute = str.find("m");
size_t place_of_second = str.find("s");
std::string timestr=str;
int factor = 1;
if (is_num(str)){
seconds = std::stoi(str);
return seconds;
}else if (place_of_hour < place_of_minute){
if (place_of_hour < place_of_second){
//seconds = std::stoi(str.substr(0,place_of_hour))*3600;
timestr = str.substr(0,place_of_hour);
factor = 3600;
}else {
//seconds = std::stoi(str.substr(0,place_of_second));
timestr = str.substr(0,place_of_second);
}
}else if (place_of_minute < place_of_hour){
if (place_of_minute < place_of_second){
//seconds = std::stoi(str.substr(0,place_of_minute))*60;
timestr = str.substr(0,place_of_minute);
factor = 60;
}else{
//seconds = std::stoi(str.substr(0,place_of_second));
timestr = str.substr(0,place_of_second);
}
}else{
timestr = str.substr(0,place_of_second);
}
if (is_num(timestr) && timestr.length() > 0){
seconds = std::stoi(timestr)*factor;
} else{
std::cout << "Wrong format of time string. Allowed: <X> OR <X>s OR <X>m OR <X>h." << std::endl;
}
return seconds;
}

View File

@ -6,4 +6,5 @@ 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); bool is_num(const std::string &str);
int string2sec(std::string str);
#endif #endif

View File

@ -3,7 +3,7 @@
#include <string> #include <string>
#define TT_VERSION 1.0.1 #define TT_VERSION 1.1.0
// global variables: // global variables:
extern std::string user_name; extern std::string user_name;

View File

@ -129,18 +129,21 @@ void parse_input(std::string input, ProjectList *proj_list){
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"){ }else if (command == "at"){
int seconds = 0;
if (command_end == std::string::npos){ if (command_end == std::string::npos){
std::cout << "add time: please specify time to add and task name [optional]." << std::endl; std::cout << "add time: please specify time to add and task name [optional]." << std::endl;
}else{ }else{
// only 1 argument // only 1 argument
if(is_num(trim(argument))){ if(argument.find(" ") == std::string::npos){
command_at("",stoi(argument),proj_list); seconds = string2sec(argument);
command_at("",seconds,proj_list);
}else{ }else{
size_t start_of_name = argument.find(" "); size_t start_of_name = argument.find(" ");
std::string wtime_str = argument.substr(0,start_of_name); std::string wtime_str = argument.substr(0,start_of_name);
std::string task_name = argument.substr(start_of_name+1); std::string task_name = argument.substr(start_of_name+1);
if (is_num(wtime_str) && start_of_name != std::string::npos){ seconds = string2sec(wtime_str);
command_at(task_name,stoi(wtime_str),proj_list); if (start_of_name != std::string::npos){
command_at(task_name,seconds,proj_list);
}else{ }else{
std::cout << "add time: please specify time to add and task name [optional]." << std::endl; std::cout << "add time: please specify time to add and task name [optional]." << std::endl;
} }
@ -149,18 +152,21 @@ void parse_input(std::string input, ProjectList *proj_list){
} }
}else if (command == "rt"){ }else if (command == "rt"){
int seconds = 0;
if (command_end == std::string::npos){ if (command_end == std::string::npos){
std::cout << "remove time: please specify time to remove and task name [optional]." << std::endl; std::cout << "remove time: please specify time to remove and task name [optional]." << std::endl;
}else{ }else{
// only 1 argument // only 1 argument
if(is_num(trim(argument))){ if(argument.find(" ") == std::string::npos){
command_rt("",stoi(argument),proj_list); seconds = string2sec(argument);
command_rt("",seconds,proj_list);
}else{ }else{
size_t start_of_name = argument.find(" "); size_t start_of_name = argument.find(" ");
std::string wtime_str = argument.substr(0,start_of_name); std::string wtime_str = argument.substr(0,start_of_name);
std::string task_name = argument.substr(start_of_name+1); std::string task_name = argument.substr(start_of_name+1);
if (is_num(wtime_str) && start_of_name != std::string::npos){ seconds = string2sec(wtime_str);
command_rt(task_name,stoi(wtime_str),proj_list); if (start_of_name != std::string::npos){
command_rt(task_name,seconds,proj_list);
}else{ }else{
std::cout << "remove time: please specify time to remove and task name [optional]." << std::endl; std::cout << "remove time: please specify time to remove and task name [optional]." << std::endl;
} }