Skip to content

Instantly share code, notes, and snippets.

@novelistparty
Last active January 12, 2017 06:18
Show Gist options
  • Save novelistparty/4981ee2dccfa50b670ee7df771c5b219 to your computer and use it in GitHub Desktop.
Save novelistparty/4981ee2dccfa50b670ee7df771c5b219 to your computer and use it in GitHub Desktop.
a daily log program. Customize and compile.
/*
Created by John Boyle on 10 January 2017.
Updated 11 January 2017: use Markdown output, use ncurses.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ncurses.h>
#define PURPLE "\033[1;35m"
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define BLUE "\033[1;34m"
/* turns off the colors */
#define NC "\033[0m"
#define LOGFILE "/tmp/daily-log.txt"
char * activities[4] = {"play guitar", "figure sketch", "read ocean book", "yoga"};
char date_buf[1024];
char line_buf[1024];
int response;
int main(int argc, char * argv[]) {
size_t N_activity = sizeof(activities)/sizeof(size_t);
/* print list of activities if called as "dl list" */
if (argv[1] && !strcmp("list", argv[1])) {
fprintf(stderr, "Activities:\n");
for (size_t ia = 0; ia < N_activity; ia++) {
fprintf(stderr, "%s%s%s\n", BLUE, activities[ia], NC);
}
exit(EXIT_SUCCESS);
}
initscr();
int H, W;
getmaxyx(stdscr, H, W);
/* get the date */
time_t current_time = time(NULL);
struct tm * t = localtime(&current_time);
strftime(date_buf, sizeof(date_buf), "%e %B %Y", t);
/* ask this b/c I often log for the previous day's activities */
int row = 4;
mvprintw(row, 0, "Entry date: %s", date_buf);
mvprintw(row + 1, 0, "Is that correct? (y/n)");
response = getch();
if ('n' == response) {
/* only accept a y/n here b/c this is important to get right */
do {
move(row + 2, 0); clrtoeol();
mvprintw(row + 2, 0, "Should it be yesterday? (y/n) ");
response = getch();
} while (response != 'y' && response != 'n');
if ('y' == response) {
t->tm_mday -= 1;
strftime(date_buf, sizeof(date_buf), "%e %B %Y", t);
clear();
mvprintw(row, 0, "Log entry date will be %s\n", date_buf);
getch();
clear();
} else if ('n' == response) {
clear();
mvprintw(row, 0, "Changing it to a date other than yesterday not yet supported.");
mvprintw(row + 1, 0, "Log entry date will still be %s\n", date_buf);
mvprintw(row + 2, 0, "Press any key to continue...");
getch();
clear();
}
}
/* start making the log */
FILE * fh_log = fopen(LOGFILE, "a");
/* if error, drop out of curses and give msg */
if (!fh_log) {
endwin();
fprintf(stderr, "%s %sERROR%s: failed to open log file %s\n", argv[0], RED, NC, LOGFILE);
exit(EXIT_FAILURE);
}
fprintf(fh_log, "## %s\n", date_buf);
/* ask about each activity */
for (size_t ia = 0; ia < N_activity; ia++) {
move(row, 0); clrtobot();
mvprintw(row, 0, "Did you %s? (y/n) ", activities[ia]);
response = getch();
mvprintw(row + 1, 0, "Write an optional note about it:");
move(row + 3, 5);
getnstr(line_buf, sizeof(line_buf));
/* print the activity name before the note */
fprintf(fh_log, "* %s\n", activities[ia]);
if ('y' == response) {
fprintf(fh_log, " - Yes. %s\n", line_buf);
} else if ('n' == response) {
fprintf(fh_log, " - Not today. %s\n", line_buf);
} else {
fprintf(fh_log, " - \n");
}
}
fprintf(fh_log, "\n");
fclose(fh_log);
endwin();
return(EXIT_SUCCESS);
}
@novelistparty
Copy link
Author

macOS: compile ncurses version with
cc dl.c -lncurses -o dl

Probably works for any Linux too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment