From 98378e80e00731c4086eea2e44930108b949b3eb Mon Sep 17 00:00:00 2001 From: xavi Date: Sat, 14 Sep 2024 19:45:57 -0700 Subject: [PATCH] Modified display_task_list to display active id --- Makefile | 13 ++++---- src/dodo.c | 91 ++++++++++++++++++++++++++++++++++++++++++++---------- src/dodo.h | 13 +++++--- 3 files changed, 91 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index d5243de..a420fa5 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ DFLAGS = -DDB_PATH=\"$(DB_FILE)\" all: dodo -$(OBJ): $(X_STRING_OBJ) $(SQLITE_OBJ) $(HEADERS) +$(OBJ): $(SRC) $(X_STRING_OBJ) $(SQLITE_OBJ) $(HEADERS) $(CC) -c -o $(OBJ_DIR)/$(@F) $(INC_DIRS) $(CFLAGS) $(DFLAGS) $(SRC_DIR)/$(@F:.o=.c) $(X_STRING_OBJ): $(X_STRING_HEADERS) @@ -49,12 +49,13 @@ $(X_STRING_OBJ): $(X_STRING_HEADERS) $(SQLITE_OBJ): $(SQLITE_HEADERS) $(CC) -c -o $(OBJ_DIR)/$(@F) $(CFLAGS) $(SQLITE_SRC) -dodo: $(OBJ) - $(CC) -o $@ $(CFLAGS) $(ALL_OBJS) - -test: $(OBJ) $(TEST_SRC) - $(CC) -o $@ $(TEST_SRC) $(INC_DIRS) $(DFLAGS) $(CFLAGS) obj/dodo.o $(X_STRING_OBJ) $(SQLITE_OBJ) +dodo: test $(OBJ) $(X_STRING_OBJ) $(SQLITE_OBJ) + $(CC) -o $@ $(CFLAGS) $(OBJ) $(X_STRING_OBJ) $(SQLITE_OBJ) ./test + rm ./test + +test: $(OBJ) $(TEST_SRC) $(X_STRING_OBJ) $(SQLITE_OBJ) + $(CC) -o $@ $(TEST_SRC) $(INC_DIRS) $(DFLAGS) $(CFLAGS) obj/dodo.o $(X_STRING_OBJ) $(SQLITE_OBJ) install: diff --git a/src/dodo.c b/src/dodo.c index 3f066e4..b25bf5c 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -116,18 +116,64 @@ int opendb(sqlite3 **db, char* filename){ return(rc); } -// Print Heading TODO: obvi make this better -void display_heading(){ +void display_task_list_heading(char* heading){ + int width = (FIXED_ID_WIDTH + FIXED_TITLE_WIDTH + FIXED_DATE_WIDTH + x_strlen(heading) ); + int width_div_2 = width / 2; + printf("%s%*s", X_BOLD, width_div_2, heading); + printf("%*s", width_div_2 - x_strlen(heading), ""); + printf(" "); +} + +void display_column_heading(const char* str){ + int width = 0; + + if ( x_strcmp(str, "id") == 0 ){ + width = FIXED_ID_WIDTH; + } + else if ( x_strcmp(str, "title") == 0 ){ + width = FIXED_TITLE_WIDTH; + } + else if ( x_strcmp(str, "due_date") == 0 ){ + width = FIXED_DATE_WIDTH; + } + else{ + width = FIXED_MAX_WIDTH; + } + + printf("%s%s%-*.*s%s", X_BOLD, X_UNDL, width, width, str, X_RST); +} + +void display_column_headings_for_all_task_lists(){ + int i = 0; + char* str; + for ( i = 0; i < NUM_TASK_LISTS; i++){ + str = "id"; + display_column_heading(str); + str = "title"; + display_column_heading(str); + str = "due_date"; + display_column_heading(str); + printf(" "); + } printf("\n"); - printf("%s Today %s", X_BOLD, X_RST); - printf(" %s Backlog %s", X_BOLD, X_RST); - printf(" %s Blocked %s", X_BOLD, X_RST); +} + +// Print Heading +void display_heading(){ + char* colnames = "active_id,title,due_date"; + char * str; + int width = 0; + + printf("\n"); + str = "Today"; + display_task_list_heading(str); + str = "Backlog"; + display_task_list_heading(str); + str = "Blocked"; + display_task_list_heading(str); printf("\n"); - printf("%s%stitle due date %s", X_BOLD, X_UNDL, X_RST); - printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST); - printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST); - printf("\n"); + display_column_headings_for_all_task_lists(); } // pass in the args and return the title and due date @@ -162,11 +208,11 @@ int get_num_rows(sqlite3 *db, char* table, char* status){ } // Print with FIXED_WIDTH -int print_fixed_width(const unsigned char* str){ +int print_fixed_width(const unsigned char* str, int width){ if (str){ - printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, str); + printf("%-*.*s", width, width, str); }else{ - printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, ""); + printf("%-*.*s", width, width, ""); } } @@ -177,12 +223,12 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c int i = 0; int num_rows = -1; int num_cols = 0; + int fixed_width = 0; char* errmsg; const unsigned char* col_val; + const unsigned char* col_name; sqlite3_stmt* out_stmt; - // Start col for the current task list - X_goright(start_col); // Get the number of rows in current task list num_rows = get_num_rows(db, table, status); @@ -197,13 +243,26 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c } // TODO: prob should be a func begin + // Start col for the current task list + X_goright(start_col); + // while there is still rows available while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ // for each column print the column num_cols = sqlite3_column_count(out_stmt); for (i = 0; i < num_cols; i++){ col_val = sqlite3_column_text(out_stmt, i); - print_fixed_width(col_val); + col_name = sqlite3_column_name(out_stmt, i); + if ( x_strcmp(col_name, "title") == 0 ){ + fixed_width = FIXED_TITLE_WIDTH; + } + else if ( x_strcmp(col_name, "active_id") == 0 ){ + fixed_width = FIXED_ID_WIDTH; + } + else if ( x_strcmp(col_name, "due_date") == 0 ){ + fixed_width = FIXED_DATE_WIDTH; + } + print_fixed_width(col_val, fixed_width); } // move down one and over to the start of the current task column printf("\n"); @@ -236,7 +295,7 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c int view_tasks(sqlite3 *db){ char* table = "tasks"; char* status = "today"; - char* colnames = "title,due_date"; + char* colnames = "active_id,title,due_date"; // Print Heading display_heading(); diff --git a/src/dodo.h b/src/dodo.h index 3123d50..3181aeb 100644 --- a/src/dodo.h +++ b/src/dodo.h @@ -3,10 +3,15 @@ #define SQLQUERY_MAX 100 #define ARG_MAX 100 +#define NUM_TASK_LISTS 3 +#define FIXED_TITLE_WIDTH 19 +#define FIXED_DATE_WIDTH 14 +#define FIXED_ID_WIDTH 4 +#define FIXED_COLUMN_WIDTH (FIXED_ID_WIDTH + FIXED_TITLE_WIDTH +FIXED_DATE_WIDTH) +#define FIXED_MAX_WIDTH (FIXED_COLUMN_WIDTH) #define TODAY_COL_START -1 -#define BACKLOG_COL_START 33 -#define BLOCKED_COL_START 66 -#define FIXED_WIDTH 19 +#define BACKLOG_COL_START (FIXED_COLUMN_WIDTH + 1) +#define BLOCKED_COL_START (2 * (FIXED_COLUMN_WIDTH + 1)) typedef struct{ int year; @@ -39,7 +44,7 @@ int opendb(sqlite3 **db, char* filename); void display_heading(); int parse_args(int argc, char** argv, char** title, char** due_date); int get_num_rows(sqlite3 *db, char* table, char* status); -int print_fixed_width(const unsigned char* str); +int print_fixed_width(const unsigned char* str, int width); int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status); // main functions