From d2459f135a9ff76cd0cab07ec5f60c15e076fa4d Mon Sep 17 00:00:00 2001 From: Tom Adams Date: Fri, 10 Apr 2009 14:53:20 +0100 Subject: [PATCH 01/11] Use strftime for the date. --- ttyclock.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index 67e831b..f0bddd5 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -156,12 +156,10 @@ update_hour(void) ttyclock->date.minute[1] = ttyclock->tm->tm_min % 10; /* Set date string */ - sprintf(ttyclock->date.datestr, - "%.2d/%.2d/%d%s", - ttyclock->tm->tm_mday, - ttyclock->tm->tm_mon + 1, - ttyclock->tm->tm_year + 1900, - ttyclock->meridiem); + strftime(ttyclock->date.datestr, + sizeof(ttyclock->date.datestr), + "%d/%m/%Y", + ttyclock->tm); /* Set seconds */ ttyclock->date.second[0] = ttyclock->tm->tm_sec / 10; From f119cd20b0a4ea3ff249043d00726e86bc74bb3a Mon Sep 17 00:00:00 2001 From: Tom Adams Date: Fri, 10 Apr 2009 16:31:49 +0100 Subject: [PATCH 02/11] Added option to change date format. --- ttyclock.c | 12 ++++++++++-- ttyclock.h | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index f0bddd5..544e566 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -158,7 +158,7 @@ update_hour(void) /* Set date string */ strftime(ttyclock->date.datestr, sizeof(ttyclock->date.datestr), - "%d/%m/%Y", + ttyclock->option.format, ttyclock->tm); /* Set seconds */ @@ -403,13 +403,18 @@ main(int argc, char **argv) {"twelve", 0, NULL, 't'}, {"rebound", 0, NULL, 'r'}, {"center", 0, NULL, 'c'}, + {"format", 1, NULL, 'f'}, {NULL, 0, NULL, 0} }; /* Alloc ttyclock */ ttyclock = malloc(sizeof(ttyclock_t)); - while ((c = getopt_long(argc,argv,"tvsrcih", + /* Date format */ + ttyclock->option.format = malloc(sizeof(char)*100); + strncpy(ttyclock->option.format, "%d/%m/%Y", 100); + + while ((c = getopt_long(argc,argv,"tvsrcihf:", long_options, NULL)) != -1) { switch(c) @@ -442,6 +447,9 @@ main(int argc, char **argv) case 'r': ttyclock->option.rebound = True; break; + case 'f': + strncpy(ttyclock->option.format, optarg, 100); + break; } } diff --git a/ttyclock.h b/ttyclock.h index 1486ac7..e7dc623 100644 --- a/ttyclock.h +++ b/ttyclock.h @@ -55,6 +55,7 @@ -c, --center Set the clock at the center of the terminal \n\ -t, --twelve Set the hour in 12h format \n\ -r, --rebound Do rebound the clock \n\ + -f, --format Set the date format \n\ -v, --version Show tty-clock version \n\ -i, --info Show some info about tty-clock \n\ -h, --help Show this page " @@ -74,6 +75,7 @@ typedef struct Bool twelve; Bool center; Bool rebound; + char *format; } option; /* Clock geometry */ From fea5a8c1c5bf1825f3f494430ae85b43dfd35e49 Mon Sep 17 00:00:00 2001 From: Tom Adams Date: Fri, 10 Apr 2009 16:43:43 +0100 Subject: [PATCH 03/11] Updated README. --- README | 1 + 1 file changed, 1 insertion(+) diff --git a/README b/README index b218469..8ccbb41 100755 --- a/README +++ b/README @@ -2,6 +2,7 @@ tty-clock usage : tty-clock [-option] -s, --second Show seconds -t, --twelve Set the hour in 12h format -r, --rebound Do rebound the clock + -f, --format Set the date format -v, --version Show tty-clock version -i, --info Show some info about tty-clock -h, --help Show this page From e7a243b934356932f4dc87921c368b5910e53f47 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Fri, 10 Apr 2009 19:29:24 +0200 Subject: [PATCH 04/11] Fix typo. --- ttyclock.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index 544e566..a736731 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -157,9 +157,9 @@ update_hour(void) /* Set date string */ strftime(ttyclock->date.datestr, - sizeof(ttyclock->date.datestr), - ttyclock->option.format, - ttyclock->tm); + sizeof(ttyclock->date.datestr), + ttyclock->option.format, + ttyclock->tm); /* Set seconds */ ttyclock->date.second[0] = ttyclock->tm->tm_sec / 10; From f13dc1490df862d8f1ea36d73a329420821981e3 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 17:50:02 +0200 Subject: [PATCH 05/11] Remove full argument, fix typo and alloc --- ttyclock.c | 36 ++++++++++++++++-------------------- ttyclock.h | 12 ------------ 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index a736731..ee5e24d 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -165,8 +165,6 @@ update_hour(void) ttyclock->date.second[0] = ttyclock->tm->tm_sec / 10; ttyclock->date.second[1] = ttyclock->tm->tm_sec % 10; - - return; } @@ -368,7 +366,7 @@ key_event(void) case 't': case 'T': ttyclock->option.twelve = !ttyclock->option.twelve; - /* Set the new ttyclock->date.datestr for resize date window */ + /* Set the new ttyclock->date.datestr to resize date window */ update_hour(); clock_move(ttyclock->geo.x, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h); break; @@ -394,45 +392,42 @@ main(int argc, char **argv) { int c; - struct option long_options[] = - { - {"help", 0, NULL, 'h'}, - {"version", 0, NULL, 'v'}, - {"info", 0, NULL, 'i'}, - {"second", 0, NULL, 's'}, - {"twelve", 0, NULL, 't'}, - {"rebound", 0, NULL, 'r'}, - {"center", 0, NULL, 'c'}, - {"format", 1, NULL, 'f'}, - {NULL, 0, NULL, 0} - }; - /* Alloc ttyclock */ ttyclock = malloc(sizeof(ttyclock_t)); /* Date format */ - ttyclock->option.format = malloc(sizeof(char)*100); + ttyclock->option.format = malloc(sizeof(char) * 100); + /* Default date format */ strncpy(ttyclock->option.format, "%d/%m/%Y", 100); - while ((c = getopt_long(argc,argv,"tvsrcihf:", - long_options, NULL)) != -1) + while ((c = getopt(argc, argv, "tvsrcihf:")) != -1) { switch(c) { case 'h': default: - puts(HELPSTR); + printf("tty-clock usage : tty-clock [-option] \n" + " -s Show seconds \n" + " -c Set the clock at the center of the terminal \n" + " -t Set the hour in 12h format \n" + " -r Do rebound the clock \n" + " -f Set the date format \n" + " -v Show tty-clock version \n" + " -i Show some info about tty-clock \n" + " -h Show this page \n"); free(ttyclock); exit(EXIT_SUCCESS); break; case 'i': puts("TTY-Clock 2 © by Martin Duquesnoy (xorg62@gmail.com)"); free(ttyclock); + free(ttyclock->option.format); exit(EXIT_SUCCESS); break; case 'v': puts("TTY-Clock 2 © devel version"); free(ttyclock); + free(ttyclock->option.format); exit(EXIT_SUCCESS); break; case 's': @@ -464,6 +459,7 @@ main(int argc, char **argv) } free(ttyclock); + free(ttyclock->option.format); endwin(); return 0; diff --git a/ttyclock.h b/ttyclock.h index e7dc623..1a662fb 100644 --- a/ttyclock.h +++ b/ttyclock.h @@ -44,22 +44,10 @@ /* Macro */ #define NORMFRAMEW 35 #define SECFRAMEW 54 -#define UPDATETIME 25000000 #define DATEWINH 3 #define AMSIGN " [AM]" #define PMSIGN " [PM]" -/* Help string */ -#define HELPSTR "tty-clock usage : tty-clock [-option] \n\ - -s, --second Show seconds \n\ - -c, --center Set the clock at the center of the terminal \n\ - -t, --twelve Set the hour in 12h format \n\ - -r, --rebound Do rebound the clock \n\ - -f, --format Set the date format \n\ - -v, --version Show tty-clock version \n\ - -i, --info Show some info about tty-clock \n\ - -h, --help Show this page " - typedef enum { False, True } Bool; /* Global ttyclock struct */ From e397a36fd7538f80b42d0ff3cbf21d1a059d7bb6 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 17:54:51 +0200 Subject: [PATCH 06/11] Update README --- README | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README b/README index 8ccbb41..397f03d 100755 --- a/README +++ b/README @@ -1,8 +1,8 @@ tty-clock usage : tty-clock [-option] - -s, --second Show seconds - -t, --twelve Set the hour in 12h format - -r, --rebound Do rebound the clock - -f, --format Set the date format - -v, --version Show tty-clock version - -i, --info Show some info about tty-clock - -h, --help Show this page + -s Show seconds + -t Set the hour in 12h format + -r Do rebound the clock + -f Set the date format + -v Show tty-clock version + -i Show some info about tty-clock + -h Show this page From 695a33a74f8f22695c68702d3bf6b8b28c9131e1 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 18:32:00 +0200 Subject: [PATCH 07/11] Add -C option to choose clock color --- README | 16 +++++++++------- ttyclock.c | 13 ++++++++++--- ttyclock.h | 1 + 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README b/README index 397f03d..83ec2a9 100755 --- a/README +++ b/README @@ -1,8 +1,10 @@ tty-clock usage : tty-clock [-option] - -s Show seconds - -t Set the hour in 12h format - -r Do rebound the clock - -f Set the date format - -v Show tty-clock version - -i Show some info about tty-clock - -h Show this page + -s Show seconds + -c Set the clock at the center of the terminal + -C Set the clock color. + -t Set the hour in 12h format + -r Do rebound the clock + -f Set the date format + -v Show tty-clock version + -i Show some info about tty-clock + -h Show this page diff --git a/ttyclock.c b/ttyclock.c index ee5e24d..6017870 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -52,8 +52,8 @@ init(void) /* Init color pair */ init_pair(0, bg, bg); - init_pair(1, bg, COLOR_GREEN); - init_pair(2, COLOR_GREEN, bg); + init_pair(1, bg, ttyclock->option.color); + init_pair(2, ttyclock->option.color, bg); refresh(); /* Init signal handler */ @@ -399,8 +399,10 @@ main(int argc, char **argv) ttyclock->option.format = malloc(sizeof(char) * 100); /* Default date format */ strncpy(ttyclock->option.format, "%d/%m/%Y", 100); + /* Default color */ + ttyclock->option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */ - while ((c = getopt(argc, argv, "tvsrcihf:")) != -1) + while ((c = getopt(argc, argv, "tvsrcihf:C:")) != -1) { switch(c) { @@ -409,6 +411,7 @@ main(int argc, char **argv) printf("tty-clock usage : tty-clock [-option] \n" " -s Show seconds \n" " -c Set the clock at the center of the terminal \n" + " -C Set the clock color. \n" " -t Set the hour in 12h format \n" " -r Do rebound the clock \n" " -f Set the date format \n" @@ -436,6 +439,10 @@ main(int argc, char **argv) case 'c': ttyclock->option.center = True; break; + case 'C': + if(atoi(optarg) >= 0 && atoi(optarg) < 8) + ttyclock->option.color = atoi(optarg); + break; case 't': ttyclock->option.twelve = True; break; diff --git a/ttyclock.h b/ttyclock.h index 1a662fb..729eb82 100644 --- a/ttyclock.h +++ b/ttyclock.h @@ -64,6 +64,7 @@ typedef struct Bool center; Bool rebound; char *format; + int color; } option; /* Clock geometry */ From c8593cd0a750f36139f18dc8573028f6369f03a1 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 21:58:42 +0200 Subject: [PATCH 08/11] Add keybind [0;7] to change color! --- README | 2 +- ttyclock.c | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README b/README index 83ec2a9..a84bc9b 100755 --- a/README +++ b/README @@ -1,7 +1,7 @@ tty-clock usage : tty-clock [-option] -s Show seconds -c Set the clock at the center of the terminal - -C Set the clock color. + -C Set the clock color -t Set the hour in 12h format -r Do rebound the clock -f Set the date format diff --git a/ttyclock.c b/ttyclock.c index 6017870..598fa49 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -316,10 +316,11 @@ set_center(Bool b) void key_event(void) { + int i, c; halfdelay(1); - switch(getch()) + switch((c = getch())) { case KEY_UP: case 'k': @@ -382,6 +383,14 @@ key_event(void) if(ttyclock->option.rebound && ttyclock->option.center) ttyclock->option.center = False; break; + default: + for(i = 0; i < 8; ++i) + if(c == (i + '0')) + { + ttyclock->option.color = i; + init(); + } + break; } return; @@ -411,7 +420,7 @@ main(int argc, char **argv) printf("tty-clock usage : tty-clock [-option] \n" " -s Show seconds \n" " -c Set the clock at the center of the terminal \n" - " -C Set the clock color. \n" + " -C Set the clock color \n" " -t Set the hour in 12h format \n" " -r Do rebound the clock \n" " -f Set the date format \n" From a32ee816f7856cb24f6bfef6f2ed91e07d476693 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 22:03:26 +0200 Subject: [PATCH 09/11] Improve color switch thing --- ttyclock.c | 13 +++++++------ ttyclock.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index 598fa49..f8c44de 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -36,7 +36,7 @@ void init(void) { struct sigaction sig; - int bg = COLOR_BLACK; + ttyclock->bg = COLOR_BLACK; /* Init ncurses */ initscr(); @@ -48,12 +48,12 @@ init(void) /* Init default terminal color */ if(use_default_colors() == OK) - bg = -1; + ttyclock->bg = -1; /* Init color pair */ - init_pair(0, bg, bg); - init_pair(1, bg, ttyclock->option.color); - init_pair(2, ttyclock->option.color, bg); + init_pair(0, ttyclock->bg, ttyclock->bg); + init_pair(1, ttyclock->bg, ttyclock->option.color); + init_pair(2, ttyclock->option.color, ttyclock->bg); refresh(); /* Init signal handler */ @@ -388,7 +388,8 @@ key_event(void) if(c == (i + '0')) { ttyclock->option.color = i; - init(); + init_pair(1, ttyclock->bg, i); + init_pair(2, i, ttyclock->bg); } break; } diff --git a/ttyclock.h b/ttyclock.h index 729eb82..7940d1f 100644 --- a/ttyclock.h +++ b/ttyclock.h @@ -55,6 +55,7 @@ typedef struct { /* while() boolean */ Bool running; + int bg; /* Running option */ struct From 68afd752e34e836cc7cfef61659ff13218d28f6d Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 22:48:43 +0200 Subject: [PATCH 10/11] Re-Add meridiem sign to date string and fix help page --- ttyclock.c | 14 ++++++++------ ttyclock.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ttyclock.c b/ttyclock.c index f8c44de..7f8d9d4 100644 --- a/ttyclock.c +++ b/ttyclock.c @@ -132,6 +132,7 @@ void update_hour(void) { int ihour; + char tmpstr[128]; ttyclock->tm = localtime(&(ttyclock->lt)); ttyclock->lt = time(NULL); @@ -141,7 +142,7 @@ update_hour(void) if(ttyclock->option.twelve) ttyclock->meridiem = ((ihour > 12) ? PMSIGN : AMSIGN); else - ttyclock->meridiem = ""; + ttyclock->meridiem = "\0"; /* Manage hour for twelve mode */ ihour = ((ttyclock->option.twelve && ihour > 12) ? (ihour - 12) : ihour); @@ -156,10 +157,11 @@ update_hour(void) ttyclock->date.minute[1] = ttyclock->tm->tm_min % 10; /* Set date string */ - strftime(ttyclock->date.datestr, - sizeof(ttyclock->date.datestr), + strftime(tmpstr, + sizeof(tmpstr), ttyclock->option.format, ttyclock->tm); + sprintf(ttyclock->date.datestr, "%s%s", tmpstr, ttyclock->meridiem); /* Set seconds */ ttyclock->date.second[0] = ttyclock->tm->tm_sec / 10; @@ -418,13 +420,13 @@ main(int argc, char **argv) { case 'h': default: - printf("tty-clock usage : tty-clock [-option] \n" + printf("usage : tty-clock [-sctrvih] [-C [0-7]] [-f format] \n" " -s Show seconds \n" " -c Set the clock at the center of the terminal \n" - " -C Set the clock color \n" + " -C [0-7] Set the clock color \n" " -t Set the hour in 12h format \n" " -r Do rebound the clock \n" - " -f Set the date format \n" + " -f format Set the date format \n" " -v Show tty-clock version \n" " -i Show some info about tty-clock \n" " -h Show this page \n"); diff --git a/ttyclock.h b/ttyclock.h index 7940d1f..f830a37 100644 --- a/ttyclock.h +++ b/ttyclock.h @@ -82,7 +82,7 @@ typedef struct unsigned int hour[2]; unsigned int minute[2]; unsigned int second[2]; - char datestr[16]; + char datestr[256]; } date; /* time.h utils */ From 15a90b7191c95d02f4a80524ce05cc291e7a8c6f Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sat, 11 Apr 2009 22:58:45 +0200 Subject: [PATCH 11/11] Update README --- README | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README b/README index a84bc9b..e18294d 100755 --- a/README +++ b/README @@ -1,10 +1,10 @@ -tty-clock usage : tty-clock [-option] +usage : tty-clock [-sctrvih] [-C [0-7]] [-f format] -s Show seconds -c Set the clock at the center of the terminal - -C Set the clock color + -C [0-7] Set the clock color -t Set the hour in 12h format -r Do rebound the clock - -f Set the date format + -f format Set the date format -v Show tty-clock version -i Show some info about tty-clock -h Show this page