From 2cfc535d80a15536fd1a441ba725d5961cdf9d7c Mon Sep 17 00:00:00 2001 From: Ariadna Vigo Date: Wed, 24 Jun 2020 18:13:54 +0200 Subject: Cras now uses a filepath provided at the CLI --- README.md | 19 +++++++++--------- cras.c | 69 ++++++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f3cd026..1e624da 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,12 @@ $ make ### Set up your task list To start using Cras, you first need to set up your task list. This is done by -using the -s command line option and entering a short description of your task -in a new line. End your list hitting EOF (Ctrl+D) in a *blank line*. +using the -s command line option and a file name. Then enter a short +description of your task in a new line. End your list hitting EOF (Ctrl+D) in a + *blank line*. ``` -$ cras -s +$ cras -s mytask First task Second task Third task @@ -31,16 +32,16 @@ Third task You may also pipe in a text file if you so prefer. ``` -$ cras -s < mytasklist +$ cras -s todo-today < mytasklist ``` ### Printing out your current list To print out your current list, you may use either of two options: a long, detailed output, and a short summary (ideal for status bars). The long-form -output is read just by running Cras without any further options: +output is read just by running Cras on your file without any further options: ``` -$ cras +$ cras my-dev-todo Tasks due for: Sat Jun 20 15:57:28 2020 #01 [TODO] Write README.md @@ -53,7 +54,7 @@ Tasks due for: Sat Jun 20 15:57:28 2020 The short-form output is shown by using the -o option: ``` -$ cras -o +$ cras -o my-dev-todo 3/0/3 to do/done/total ``` @@ -62,8 +63,8 @@ When you've completed a task, use -t and the task number (as shown by the long-form output) to mark it as done. ``` -$ cras -t 2 -$ cras +$ cras -t 2 my-dev-todo +$ cras my-dev-todo Tasks due for: Sat Jun 20 15:57:28 2020 #01 [TODO] Write README.md diff --git a/cras.c b/cras.c index b97aaa8..3462402 100644 --- a/cras.c +++ b/cras.c @@ -15,11 +15,20 @@ static char *argv0; /* Required here by arg.h */ #define TASK_TODO_STR "\033[31;1m[TODO]\033[0m" #define TASK_DONE_STR "\033[32;1m[DONE]\033[0m" +#define NUMARG_SIZE 5 /* 4 digits + '\0' for the numerical arg of -t/-T */ + enum { SHORT_OUTPUT, LONG_OUTPUT }; +enum { + DEF_MODE, + SET_MODE, + OUT_MODE, + MARK_MODE +}; + static void die(const char *fmt, ...); static char *print_task_status(TaskLst tasks, int num); static void print_short_output(TaskLst tasks); @@ -31,13 +40,7 @@ static void read_user_input(TaskLst *tasks, FILE *fp); static void usage(void); static void set_tasks_mode(const char *crasfile); static void output_mode(const char *crasfile, int mode); -static void mark_tasks_mode(const char *crasfile, const char *id, int mode); - -/* - * crasfile_path defined here just for testing. It will be moved later to - * config.(def.)h - */ -static char crasfile_path[] = "crasfile"; +static void mark_tasks_mode(const char *crasfile, const char *id, int value); static void die(const char *fmt, ...) { @@ -170,7 +173,7 @@ output_mode(const char *crasfile, int mode) } static void -mark_tasks_mode(const char *crasfile, const char *id, int mode) +mark_tasks_mode(const char *crasfile, const char *id, int value) { int tasknum; char *endptr; @@ -178,13 +181,16 @@ mark_tasks_mode(const char *crasfile, const char *id, int mode) tasknum = strtol(id, &endptr, 10); if (endptr[0] != '\0') - die("%s not a number", id); + die("'%s' not a number.", id); tasklst_init(&tasks); read_crasfile(&tasks, crasfile); + if (tasknum <= 0) + die("Task number must be greater than zero."); + if (tasknum <= tasklst_tasks_total(tasks)) - tasks.status[tasknum - 1] = mode; + tasks.status[tasknum - 1] = value; else die("Task #%d does not exist.", tasknum); @@ -194,26 +200,47 @@ mark_tasks_mode(const char *crasfile, const char *id, int mode) int main(int argc, char *argv[]) { + char numarg[NUMARG_SIZE]; + int mode, task_value; + + mode = DEF_MODE; ARGBEGIN { case 's': - set_tasks_mode(crasfile_path); - return 0; + mode = SET_MODE; + break; case 'o': - output_mode(crasfile_path, SHORT_OUTPUT); - return 0; + mode = OUT_MODE; + break; case 't': - mark_tasks_mode(crasfile_path, EARGF(usage()), - TASK_DONE); - return 0; + mode = MARK_MODE; + task_value = TASK_DONE; + strncpy(numarg, EARGF(usage()), NUMARG_SIZE); + break; case 'T': - mark_tasks_mode(crasfile_path, EARGF(usage()), - TASK_TODO); - return 0; + mode = MARK_MODE; + task_value = TASK_TODO; + strncpy(numarg, EARGF(usage()), NUMARG_SIZE); + break; default: usage(); /* usage() dies, so nothing else needed. */ } ARGEND; + if (argc <= 0) + usage(); + + switch (mode) { + case SET_MODE: + set_tasks_mode(argv[0]); + return 0; + case OUT_MODE: + output_mode(argv[0], SHORT_OUTPUT); + return 0; + case MARK_MODE: + mark_tasks_mode(argv[0], numarg, task_value); + return 0; + } + /* Default behavior: long-form output */ - output_mode(crasfile_path, LONG_OUTPUT); + output_mode(argv[0], LONG_OUTPUT); return 0; } -- cgit v1.2.3