From e9119c2981b92d35e8186859bf4a71a1b55fd8bf Mon Sep 17 00:00:00 2001 From: xavi Date: Sun, 6 Oct 2024 15:24:38 -0700 Subject: [PATCH] Added color functions for due_date --- src/dodo.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/dodo.h | 1 + tags | 17 ++++++++++ tests/tests.c | 16 ++++++++-- 4 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/dodo.c b/src/dodo.c index cef48c2..c8d6d5a 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -543,6 +543,92 @@ void display_heading(){ display_column_headings_for_all_task_lists(); } +int get_num_days_between_dates(date *d1, date *d2){ + struct tm s_d1 = {0}; + struct tm s_d2 = {0}; + time_t sec1; + time_t sec2; + double diff; + int diff_in_days; + + s_d1.tm_year = d1->year - 1900; + s_d1.tm_mon = d1->month - 1; + s_d1.tm_mday = d1->day - 1; + + + s_d2.tm_year = d2->year - 1900; + s_d2.tm_mon = d2->month - 1; + s_d2.tm_mday = d2->day - 1; + + sec1 = mktime(&s_d1); + sec2 = mktime(&s_d2); + + diff = difftime(sec2, sec1); + + diff_in_days = diff / ( 60 * 60 * 24 ); + + + return diff_in_days; + +} + +int extract_date(const char *due_date, date *d){ + char temp[5]; + + temp[0] = due_date[0]; + temp[1] = due_date[1]; + temp[2] = '\0'; + + d->day = atoi(temp); + + temp[0] = due_date[3]; + temp[1] = due_date[4]; + temp[2] = '\0'; + + d->month = atoi(temp); + + temp[0] = due_date[6]; + temp[1] = due_date[7]; + temp[2] = due_date[8]; + temp[3] = due_date[9]; + temp[4] = '\0'; + + d->year = atoi(temp); + + return 0; +} + + +int set_color_of_date(const char *due_date){ + + if (!due_date){ + return -1; + } + int days_until_due_date = 0; + date *d = malloc(sizeof(date)); + date *today = malloc(sizeof(date)); + + extract_date(due_date, d); + get_today(today); + + + days_until_due_date = get_num_days_between_dates(today, d); + + if ( days_until_due_date > 14 ){ + printf("%s", X_GREEN); + } + else if ( days_until_due_date > 7 ){ + printf("%s", X_CYN); + } + else if ( days_until_due_date > 3 ){ + printf("%s", X_CYN); + } + else{ + printf("%s", X_RED); + } + +} + int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){ static int max_rows = -1; int rc = 0; @@ -593,7 +679,6 @@ int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){ else if ( x_strcmp(col_name, "due_date") == 0 ){ fixed_width = FIXED_DATE_WIDTH; set_color_of_date(col_val); - printf("%s", X_GREEN); } print_fixed_width(col_val, fixed_width); } diff --git a/src/dodo.h b/src/dodo.h index 0ea9ba1..2eecbfe 100644 --- a/src/dodo.h +++ b/src/dodo.h @@ -58,6 +58,7 @@ int init_date(struct tm*, int day_offset); int get_due_date(filtered_tasks *task, char* due_date); int get_today(date *d); int get_day_of_week(date *d, int target_day_of_week); +int set_color_of_date(); // sql generators TODO: might be able to boil this down to 1 func int prepare_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); diff --git a/tags b/tags index fee4157..70ac8c2 100644 --- a/tags +++ b/tags @@ -2658,9 +2658,11 @@ X_CTYPES_HEADERS Makefile /^X_CTYPES_HEADERS = $(wildcard $(X_CTYPES_DIR)\/src\/ X_CTYPES_OBJ Makefile /^X_CTYPES_OBJ = $(patsubst $(X_CTYPES_DIR)\/src\/%.c, $(OBJ_DIR)\/%.o, $(X_CTYPES_SRC))$/;" m X_CTYPES_SRC Makefile /^X_CTYPES_SRC = $(wildcard $(X_CTYPES_DIR)\/src\/*.c)$/;" m X_CYN src/x_curses.h /^#define X_CYN /;" d +X_GREEN src/x_curses.h /^#define X_GREEN /;" d X_GREEN src/xlibc/x_ctypes/src/tests/tests.c /^#define X_GREEN /;" d file: X_GREEN tests/tests.c /^#define X_GREEN /;" d file: X_MACURS_H src/x_curses.h /^#define X_MACURS_H$/;" d +X_RED src/x_curses.h /^#define X_RED /;" d X_RED src/xlibc/x_ctypes/src/tests/tests.c /^#define X_RED /;" d file: X_RED tests/tests.c /^#define X_RED /;" d file: X_RST src/x_curses.h /^#define X_RST /;" d @@ -3079,7 +3081,9 @@ currentCount src/sqlite3/src/sqlite3.c /^ u32 currentCount; \/* Current numbe currentOut src/sqlite3/src/sqlite3.c /^ u32 currentOut; \/* Current checkout, including internal fragmentation *\/$/;" m struct:Mem5Global typeref:typename:u32 file: currentTimeFunc src/sqlite3/src/sqlite3.c /^static void currentTimeFunc($/;" f typeref:typename:void file: d src/dodo.c /^ date *d = malloc(sizeof(date));$/;" l function:get_due_date typeref:typename:date * file: +d src/dodo.c /^ date *d = malloc(sizeof(date));$/;" l function:set_color_of_date typeref:typename:date * file: d src/sqlite3/src/sqlite3.c /^ int d = 0; \/* adjust exponent for shifting decimal point *\/$/;" l function:sqlite3AtoF typeref:typename:int file: +d tests/tests.c /^ char d[10] = "04-08-2024";$/;" l function:test_date_difference typeref:typename:char[10] file: d1 src/sqlite3/src/sqlite3.c /^ DateTime d1, d2;$/;" l function:timediffFunc typeref:typename:DateTime file: d2 src/sqlite3/src/sqlite3.c /^ DateTime d1, d2;$/;" l function:timediffFunc typeref:typename:DateTime file: data src/sqlite3/src/sqlite3.c /^ void *data; \/* Data associated with this element *\/$/;" m struct:HashElem typeref:typename:void * file: @@ -3140,6 +3144,8 @@ desc src/sqlite3/src/sqlite3.h /^ unsigned char desc; \/* True for DES dest src/xlibc/x_string/src/tests/str_cmp.c /^ char dest[100];$/;" l function:check_strcpy typeref:typename:char[100] file: dest_ptr src/xlibc/x_string/src/x_string.c /^ char* dest_ptr = dest;$/;" l function:x_strcpy typeref:typename:char * file: dfltLockMode src/sqlite3/src/sqlite3.c /^ u8 dfltLockMode; \/* Default locking-mode for attached dbs *\/$/;" m struct:sqlite3 typeref:typename:u8 file: +diff src/dodo.c /^ double diff;$/;" l function:get_num_days_between_dates typeref:typename:double file: +diff_in_days src/dodo.c /^ int diff_in_days;$/;" l function:get_num_days_between_dates typeref:typename:int file: directMode src/sqlite3/src/sqlite3.c /^ u8 directMode; \/* Direct rendering mode means take data directly$/;" m struct:AggInfo typeref:typename:u8 file: disableLookaside src/sqlite3/src/sqlite3.c /^ u8 disableLookaside; \/* Number of times lookaside has been disabled *\/$/;" m struct:Parse typeref:typename:u8 file: disableTriggers src/sqlite3/src/sqlite3.c /^ u8 disableTriggers; \/* True to disable triggers *\/$/;" m struct:Parse typeref:typename:u8 file: @@ -3254,6 +3260,7 @@ expired src/sqlite3/src/sqlite3.c /^ bft expired:2; \/* 1: recompile V explain src/sqlite3/src/sqlite3.c /^ bft explain:2; \/* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN *\/$/;" m struct:Vdbe typeref:typename:bft:2 file: explain src/sqlite3/src/sqlite3.c /^ u8 explain; \/* True if the EXPLAIN flag is found on the query *\/$/;" m struct:Parse typeref:typename:u8 file: expmask src/sqlite3/src/sqlite3.c /^ u32 expmask; \/* Binding to these vars invalidates VM *\/$/;" m struct:Vdbe typeref:typename:u32 file: +extract_date src/dodo.c /^int extract_date(const char *due_date, date *d){$/;" f typeref:typename:int fail src/xlibc/x_ctypes/src/tests/tests.c /^int fail(){$/;" f typeref:typename:int fail src/xlibc/x_string/src/tests/str_cmp.c /^int fail(){$/;" f typeref:typename:int fail tests/tests.c /^int fail(){$/;" f typeref:typename:int @@ -3309,6 +3316,7 @@ get_day_of_week src/dodo.c /^int get_day_of_week(date *d, int target_day_of_week get_day_of_week src/dodo.h /^int get_day_of_week(date *d, int target_day_of_week);$/;" p typeref:typename:int get_due_date src/dodo.c /^int get_due_date(filtered_tasks *task, char* passed_date){$/;" f typeref:typename:int get_due_date src/dodo.h /^int get_due_date(filtered_tasks *task, char* due_date);$/;" p typeref:typename:int +get_num_days_between_dates src/dodo.c /^int get_num_days_between_dates(date *d1, date *d2){$/;" f typeref:typename:int get_num_rows src/dodo.c /^int get_num_rows(sqlite3 *db, char* table, char* status){$/;" f typeref:typename:int get_num_rows src/dodo.h /^int get_num_rows(sqlite3 *db, char* table, char* status);$/;" p typeref:typename:int get_today src/dodo.c /^int get_today(date *d){$/;" f typeref:typename:int @@ -4645,9 +4653,13 @@ sNameToken src/sqlite3/src/sqlite3.c /^ Token sNameToken; \/* Token with sNow src/sqlite3/src/sqlite3.c /^ struct tm sNow;$/;" l function:currentTimeFunc typeref:struct:tm file: sRes src/sqlite3/src/sqlite3.c /^ sqlite3_str sRes;$/;" l function:strftimeFunc typeref:typename:sqlite3_str file: sRes src/sqlite3/src/sqlite3.c /^ sqlite3_str sRes;$/;" l function:timediffFunc typeref:typename:sqlite3_str file: +s_d1 src/dodo.c /^ struct tm s_d1 = {0}; $/;" l function:get_num_days_between_dates typeref:struct:tm file: +s_d2 src/dodo.c /^ struct tm s_d2 = {0};$/;" l function:get_num_days_between_dates typeref:struct:tm file: safety_level src/sqlite3/src/sqlite3.c /^ u8 safety_level; \/* How aggressive at syncing data to disk *\/$/;" m struct:Db typeref:typename:u8 file: schemaFlags src/sqlite3/src/sqlite3.c /^ u16 schemaFlags; \/* Flags associated with this schema *\/$/;" m struct:Schema typeref:typename:u16 file: schema_cookie src/sqlite3/src/sqlite3.c /^ int schema_cookie; \/* Database schema version number for this file *\/$/;" m struct:Schema typeref:typename:int file: +sec1 src/dodo.c /^ time_t sec1;$/;" l function:get_num_days_between_dates typeref:typename:time_t file: +sec2 src/dodo.c /^ time_t sec2;$/;" l function:get_num_days_between_dates typeref:typename:time_t file: seekHit src/sqlite3/src/sqlite3.c /^ u16 seekHit; \/* See the OP_SeekHit and OP_IfNoHope opcodes *\/$/;" m struct:VdbeCursor typeref:typename:u16 file: seekOp src/sqlite3/src/sqlite3.c /^ u8 seekOp; \/* Most recent seek operation on this cursor *\/$/;" m struct:VdbeCursor typeref:typename:u8 file: seekResult src/sqlite3/src/sqlite3.c /^ int seekResult; \/* Result of previous sqlite3BtreeMoveto() or 0$/;" m struct:VdbeCursor typeref:typename:int file: @@ -4660,6 +4672,8 @@ self src/sqlite3/src/sqlite3.c /^ pthread_t self = pthread_self();$/;" l func seqCount src/sqlite3/src/sqlite3.c /^ i64 seqCount; \/* Sequence counter *\/$/;" m struct:VdbeCursor typeref:typename:i64 file: setDateTimeToCurrent src/sqlite3/src/sqlite3.c /^static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){$/;" f typeref:typename:int file: setRawDateNumber src/sqlite3/src/sqlite3.c /^static void setRawDateNumber(DateTime *p, double r){$/;" f typeref:typename:void file: +set_color_of_date src/dodo.c /^int set_color_of_date(const char *due_date){$/;" f typeref:typename:int +set_color_of_date src/dodo.h /^int set_color_of_date();$/;" p typeref:typename:int set_up_db tests/tests.c /^int set_up_db(sqlite3 **db){$/;" f typeref:typename:int sgn src/sqlite3/src/sqlite3.c /^ int sgn = 0;$/;" l function:parseTimezone typeref:typename:int file: sharedCacheEnabled src/sqlite3/src/sqlite3.c /^ int sharedCacheEnabled; \/* true if shared-cache mode enabled *\/$/;" m struct:Sqlite3Config typeref:typename:int file: @@ -7147,6 +7161,7 @@ task src/dodo.c /^ filtered_tasks* task = malloc(sizeof(filtered_tasks));$/;" l tasks src/dodo.c /^ filtered_tasks* tasks = malloc(sizeof(filtered_tasks));$/;" l function:view_tasks typeref:typename:filtered_tasks * file: tblHash src/sqlite3/src/sqlite3.c /^ Hash tblHash; \/* All tables indexed by name *\/$/;" m struct:Schema typeref:typename:Hash file: temp src/dodo.c /^ char *temp;$/;" l function:concat_with_mem_cleanup typeref:typename:char * file: +temp src/dodo.c /^ char temp[5];$/;" l function:extract_date typeref:typename:char[5] file: temp src/sqlite3/src/sqlite3.c /^ u8 temp;$/;" l function:sqlite3VdbeMemTranslate typeref:typename:u8 file: temp_store src/sqlite3/src/sqlite3.c /^ u8 temp_store; \/* 1: file 2: memory 0: default *\/$/;" m struct:sqlite3 typeref:typename:u8 file: test Makefile /^test: $(OBJ) $(TEST_SRC) $(X_STRING_OBJ) $(X_CTYPES_OBJ) $(SQLITE_OBJ)$/;" t @@ -7154,6 +7169,7 @@ test src/xlibc/x_ctypes/src/Makefile /^test: $(TEST_OBJ) $(OBJ)$/;" t test_add_new_task_no_date tests/tests.c /^int test_add_new_task_no_date(){$/;" f typeref:typename:int test_add_new_task_with_date_and_delete tests/tests.c /^int test_add_new_task_with_date_and_delete(){$/;" f typeref:typename:int test_add_then_update_task_status_then_delete tests/tests.c /^int test_add_then_update_task_status_then_delete(){$/;" f typeref:typename:int +test_date_difference tests/tests.c /^int test_date_difference(){$/;" f typeref:typename:int test_del_task tests/tests.c /^int test_del_task(){$/;" f typeref:typename:int test_display_task tests/tests.c /^int test_display_task(){$/;" f typeref:typename:int test_oom_breakpoint src/sqlite3/src/sqlite3.c /^# define test_oom_breakpoint(/;" d file: @@ -7185,6 +7201,7 @@ tnum src/sqlite3/src/sqlite3.c /^ Pgno tnum; \/* DB Page containi tnum src/sqlite3/src/sqlite3.c /^ Pgno tnum; \/* Root BTree page for this table *\/$/;" m struct:Table typeref:typename:Pgno file: toFree src/sqlite3/src/sqlite3.c /^ u32 toFree;$/;" l function:memsys3MallocUnsafe typeref:typename:u32 file: toLocaltime src/sqlite3/src/sqlite3.c /^static int toLocaltime($/;" f typeref:typename:int file: +today src/dodo.c /^ date *today = malloc(sizeof(date));$/;" l function:set_color_of_date typeref:typename:date * file: token src/sqlite3/src/sqlite3.c /^ void *token; \/* id that may be used to recursive triggers *\/$/;" m struct:SubProgram typeref:typename:void * file: token src/sqlite3/src/sqlite3.c /^ void *token; \/* Copy of SubProgram.token *\/$/;" m struct:VdbeFrame typeref:typename:void * file: totalAlloc src/sqlite3/src/sqlite3.c /^ u64 totalAlloc; \/* Total of all malloc calls - includes internal frag *\/$/;" m struct:Mem5Global typeref:typename:u64 file: diff --git a/tests/tests.c b/tests/tests.c index 624ea0c..b8a8ef4 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -15,7 +15,7 @@ // SET THE NUMBER OF TESTS -#define NUM_TESTS 7 +#define NUM_TESTS 8 // DEFINE REUSABLE TEST SETUPS int set_up_db(sqlite3 **db){ @@ -140,10 +140,21 @@ int test_update_task(){ } return update_task(db, 4, argv_1); +} + +int test_date_difference(){ + printf("%s... ", __func__); + + char d[10] = "10-10-2024"; + + set_color_of_date(d); + + return 1; } + // DUMMY TEST int fail(){ printf("%s... ", __func__); @@ -181,7 +192,8 @@ int main(){ tests[3] = test_del_task; tests[4] = test_add_new_task_with_date_and_delete; tests[5] = test_add_then_update_task_status_then_delete; - tests[6] = test_display_task; + tests[6] = test_date_difference; + tests[7] = test_display_task; // END OF PASSING TESTS