mirror of https://github.com/PatrickLipka/tt.git
Read user name and tracking directory from settings file in PREFIX/etc and use them for load/save operations
This commit is contained in:
parent
12376d3e4b
commit
183717c956
6
Makefile
6
Makefile
|
@ -3,6 +3,8 @@ CXXFLAGS := -g -Wall -pedantic
|
|||
OBJFLAGS := $(CXXFLAGS) -c
|
||||
LDFLAGS := -lreadline
|
||||
|
||||
PREFIX ?= /usr/local
|
||||
|
||||
BIN_PATH := bin
|
||||
SRC_PATH := src
|
||||
|
||||
|
@ -12,10 +14,10 @@ OBJ := $(addprefix $(SRC_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
|
|||
default: makedir all
|
||||
|
||||
$(BIN_PATH)/tt: $(OBJ)
|
||||
$(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS)
|
||||
$(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS)
|
||||
|
||||
$(SRC_PATH)/%.o: $(SRC_PATH)/%.cpp
|
||||
$(CXX) $(OBJFLAGS) -o $@ $<
|
||||
$(CXX) -DPREFIX=$(PREFIX) $(OBJFLAGS) -o $@ $<
|
||||
|
||||
makedir:
|
||||
@mkdir -p $(BIN_PATH)
|
||||
|
|
33
src/tt.cpp
33
src/tt.cpp
|
@ -1,4 +1,5 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
@ -25,13 +26,41 @@ std::string get_date(){
|
|||
std::string date_str = std::to_string(year)+"-"+month_str;
|
||||
return date_str;
|
||||
}
|
||||
std::string user_name;
|
||||
|
||||
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir){
|
||||
std::ifstream f(file_name);
|
||||
if (!f){
|
||||
std::cout << "ABORT: Could not open config file " << file_name << "for reading." << std::endl;
|
||||
f.close();
|
||||
exit(1);
|
||||
}
|
||||
std::string line;
|
||||
while(std::getline(f,line)){
|
||||
size_t place_of_eq = line.find("=");
|
||||
std::string key = line.substr(0,place_of_eq);
|
||||
std::string val = line.substr(place_of_eq+1);
|
||||
if (key == "user_name") *user_name = val;
|
||||
if (key == "tracking_directory") *tracking_dir = val;
|
||||
}
|
||||
if (user_name->length() == 0 || tracking_dir->length() == 0){
|
||||
std::cout << "ABORT: Malformed config file " << file_name << std::endl;
|
||||
f.close();
|
||||
exit(1);
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
std::string user_name, tracking_dir, config_file;
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
// connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects
|
||||
signal(SIGINT, handler);
|
||||
tracking = 0;
|
||||
user_name = "Patrick";
|
||||
std::string prefix=STRING(PREFIX);
|
||||
config_file = prefix+"/etc/tt.conf";
|
||||
parse_config_file(config_file, &user_name, &tracking_dir);
|
||||
|
||||
// TEST: set up test list
|
||||
ProjectList proj_list("dec");
|
||||
|
|
7
src/tt.h
7
src/tt.h
|
@ -1,6 +1,13 @@
|
|||
#ifndef TT_H
|
||||
#define TT_H
|
||||
#define STRING(s) STR(s)
|
||||
#define STR(s) #s
|
||||
|
||||
#include <string>
|
||||
extern std::string user_name;
|
||||
extern std::string tracking_dir;
|
||||
extern std::string config_file;
|
||||
|
||||
std::string get_date();
|
||||
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir);
|
||||
#endif
|
||||
|
|
10
src/ui.cpp
10
src/ui.cpp
|
@ -7,6 +7,7 @@
|
|||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <iterator>
|
||||
#include <unistd.h>
|
||||
#include "ui.h"
|
||||
#include "project.h"
|
||||
#include "track.h"
|
||||
|
@ -542,7 +543,6 @@ void command_report(std::string date_str, ProjectList* proj_list){
|
|||
|
||||
if(date_str.length() == 0){
|
||||
// report for current month
|
||||
// get date:
|
||||
std::string date = get_date();
|
||||
std::cout << "Report for " << user_name << ", month: " << date << std::endl << std::endl;
|
||||
|
||||
|
@ -561,7 +561,8 @@ void command_report(std::string date_str, ProjectList* proj_list){
|
|||
}
|
||||
}else{
|
||||
ProjectList list(month);
|
||||
list.load(date_str);
|
||||
std::string file_name = tracking_dir+"/"+date_str;
|
||||
list.load(file_name);
|
||||
if (list.num_projects > 0){
|
||||
std::cout << "Report for " << user_name << ", month: " << date_str << std::endl << std::endl;
|
||||
for (int i=0; i<list.num_projects; i++){
|
||||
|
@ -583,9 +584,12 @@ void command_report(std::string date_str, ProjectList* proj_list){
|
|||
}
|
||||
}
|
||||
|
||||
// save project list to file - POSIX only
|
||||
void command_save(ProjectList *proj_list){
|
||||
std::string date_str = get_date();
|
||||
std::string file_name = date_str;
|
||||
std::string file_name = tracking_dir+"/"+date_str;
|
||||
std::string command = "mkdir -p "+tracking_dir;
|
||||
system (command.c_str());
|
||||
proj_list->save(file_name);
|
||||
std::cout << "Tracking data saved to file " << file_name << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue