commit 4b4cd5b2743d2053a1b797d6e9ba19b940c9a0b6
parent 1e502f675d4624b60e25076db8ee931630f8a75e
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Mon, 4 Jan 2021 22:44:54 +0100
add range, weekday and fmt options
Diffstat:
M | README.md | | | 8 | ++++---- |
M | diary.c | | | 98 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
2 files changed, 63 insertions(+), 43 deletions(-)
diff --git a/README.md b/README.md
@@ -86,6 +86,6 @@ The file `~/.config/diary/diary.cfg` should adhere to a basic `key = value` form
| Command Line Option | Config Key | Example Config Value | Default Config Value | Description |
| --- | --- | --- | --- | --- |
| `--dir`, `-d`, or first non-option argument | `diary_dir` | ~/diary | n/a | Diary directory. Path that holds the journal text files. |
-| n/a | `year_range` | 10 | 1 | Number of years to show before/after todays date |
-| n/a | `first_weekday` | 0 | 1 | First weekday, `0` = Sunday, `1` = Monday, ..., `6` = Saturday. Use `0` to display week beginning at Sunday ("S-M-T-W-T-F-S"), or `1` for "M-T-W-T-F-S-S" (default) |
-| n/a | `date_fmt` | %d_%b_%y | %Y-%m-%d | Date format and file name for the files inside the `diary_dir`. For the format specifiers, see [`man strftime`](https://man7.org/linux/man-pages/man3/strftime.3.html). |
-\ No newline at end of file
+| `--year_range` or `-r` | `year_range` | 10 | 1 | Number of years to show before/after todays date |
+| `--first_weekday` or `-w` | `first_weekday` | 0 | 1 | First weekday, `0` = Sunday, `1` = Monday, ..., `6` = Saturday. Use `0` to display week beginning at Sunday ("S-M-T-W-T-F-S"), or `1` for "M-T-W-T-F-S-S" (default) |
+| `--date_fmt` or `-f` | `date_fmt` | %d_%b_%y | %Y-%m-%d | Date format and file name for the files inside the `diary_dir`. For the format specifiers, see [`man strftime`](https://man7.org/linux/man-pages/man3/strftime.3.html). Be careful: If you change this, you might no longer find your existing diary entries, because the diary assumes to find the journal files under another file name. |
+\ No newline at end of file
diff --git a/diary.c b/diary.c
@@ -340,9 +340,12 @@ void usage() {
printf("Edit journal entries from the command line\n");
printf("\n");
printf("Options:\n");
- printf(" -v, --version : Print diary version\n");
- printf(" -h, --help : Show diary help text\n");
- printf(" -d, --dir DIARY_DIR : Diary storage directory (DIARY_DIR)\n");
+ printf(" -v, --version : Print diary version\n");
+ printf(" -h, --help : Show diary help text\n");
+ printf(" -d, --dir DIARY_DIR : Diary storage directory (DIARY_DIR)\n");
+ printf(" -r, --year_range RANGE : Number of years to show before/after todays date\n");
+ printf(" -w, --first_weekday DAY : First day of the week, 0 = Sun, 1 = Mon, ..., 6 = Sat\n");
+ printf(" -f, --date_fmt FMT : Date and file format, change with care\n");
printf("\n");
printf("Full docs and keyboard shortcuts: DIARY(1)\n");
printf("or online via: <https://github.com/in0rdr/diary>\n");
@@ -355,6 +358,37 @@ int main(int argc, char** argv) {
char* config_file_path;
chtype atrs;
+ #ifdef __GNU_LIBRARY__
+ // references: locale(5) and util-linux's cal.c
+ // get the base date, 8-digit integer (YYYYMMDD) returned as char *
+ #ifdef _NL_TIME_WEEK_1STDAY
+ unsigned long d = (uintptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
+ // reference: https://sourceware.org/glibc/wiki/Locales
+ // assign a static date value 19971130 (a Sunday)
+ #else
+ unsigned long d = 19971130;
+ #endif
+ struct tm base = {
+ .tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = d % 100,
+ .tm_mon = (d / 100) % 100 - 1,
+ .tm_year = d / (100 * 100) - 1900
+ };
+ mktime(&base);
+ // first_weekday is base date's day of the week offset by (_NL_TIME_FIRST_WEEKDAY - 1)
+ #ifdef __linux__
+ CONFIG.first_weekday = (base.tm_wday + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
+ #elif defined __MACH__
+ CFIndex first_day_of_week;
+ CFCalendarRef currentCalendar = CFCalendarCopyCurrent();
+ first_day_of_week = CFCalendarGetFirstWeekday(currentCalendar);
+ CFRelease(currentCalendar);
+ CONFIG.first_weekday = (base.tm_wday + first_day_of_week - 1) % 7;
+ #endif
+ #endif
+
// the diary directory defaults to the diary_dir specified in the config file
config_home = getenv("XDG_CONFIG_HOME");
if (config_home == NULL) config_home = XDG_CONFIG_HOME_FALLBACK;
@@ -384,15 +418,18 @@ int main(int argc, char** argv) {
// define options, see GETOPT(3)
static const struct option long_options[] = {
- { "version", no_argument, 0, 'v' },
- { "help", no_argument, 0, 'h' },
- { "dir", required_argument, 0, 'd' },
- { 0, 0, 0, 0 }
+ { "version", no_argument, 0, 'v' },
+ { "help", no_argument, 0, 'h' },
+ { "dir", required_argument, 0, 'd' },
+ { "year_range", required_argument, 0, 'r' },
+ { "first_weekday", required_argument, 0, 'w' },
+ { "date_fmt", required_argument, 0, 'f' },
+ { 0, 0, 0, 0 }
};
// read option characters
while (1) {
- option_char = getopt_long(argc, argv, "vhd:", long_options, &option_index);
+ option_char = getopt_long(argc, argv, "vhd:r:w:f:", long_options, &option_index);
if (option_char == -1) {
break;
@@ -415,6 +452,20 @@ int main(int argc, char** argv) {
CONFIG.diary_dir = (char *) calloc(strlen(optarg) + 1, sizeof(char));
strcpy(CONFIG.diary_dir, optarg);
break;
+ case 'r':
+ // set year range from option character
+ CONFIG.year_range = atoi(optarg);
+ break;
+ case 'w':
+ // set first week day from option character
+ fprintf(stderr, "%i\n", atoi(optarg));
+ CONFIG.first_weekday = atoi(optarg);
+ break;
+ case 'f':
+ // set date format from option character
+ CONFIG.date_fmt = (char *) calloc(strlen(optarg) + 1, sizeof(char));
+ strcpy(CONFIG.date_fmt, optarg);
+ break;
default:
printf("?? getopt returned character code 0%o ??\n", option_char);
}
@@ -441,37 +492,6 @@ int main(int argc, char** argv) {
return 1;
}
- #ifdef __GNU_LIBRARY__
- // references: locale(5) and util-linux's cal.c
- // get the base date, 8-digit integer (YYYYMMDD) returned as char *
- #ifdef _NL_TIME_WEEK_1STDAY
- unsigned long d = (uintptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
- // reference: https://sourceware.org/glibc/wiki/Locales
- // assign a static date value 19971130 (a Sunday)
- #else
- unsigned long d = 19971130;
- #endif
- struct tm base = {
- .tm_sec = 0,
- .tm_min = 0,
- .tm_hour = 0,
- .tm_mday = d % 100,
- .tm_mon = (d / 100) % 100 - 1,
- .tm_year = d / (100 * 100) - 1900
- };
- mktime(&base);
- // first_weekday is base date's day of the week offset by (_NL_TIME_FIRST_WEEKDAY - 1)
- #ifdef __linux__
- CONFIG.first_weekday = (base.tm_wday + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
- #elif defined __MACH__
- CFIndex first_day_of_week;
- CFCalendarRef currentCalendar = CFCalendarCopyCurrent();
- first_day_of_week = CFCalendarGetFirstWeekday(currentCalendar);
- CFRelease(currentCalendar);
- CONFIG.first_weekday = (base.tm_wday + first_day_of_week - 1) % 7;
- #endif
- #endif
-
setup_cal_timeframe();
initscr();