commit d6957f21b8cbda8ae47c38eb032e376991614010
parent e4ea5ca87aa8d723ed8e0323dd61b52799d69189
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sat, 4 Dec 2021 15:54:45 +0100
Merge pull request #80 from in0rdr/feature/68/fmt-cmd
add fmt-cmd option
Diffstat:
5 files changed, 74 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -121,6 +121,7 @@ The file `${XDG_CONFIG_HOME:-~/.config}/diary/diary.cfg` should adhere to a basi
| `--dir`, `-d`, or first non-option argument | `dir` | ~/diary | n/a | Diary directory. Path that holds the journal text files. If unset, defaults to environment variable `$DIARY_DIR`.|
| `--editor` or `-e` | `editor` | vim | (empty) | Editor to open journal files with. If unset, defaults to environment variable `$EDITOR`. If no editor is provided, the diary is opened read-only. |
| `--fmt` or `-f` | `fmt` | %d_%b_%y | %Y-%m-%d | Date format and file name for the files inside the `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. Hence, a change in FMT shows an empty diary, at first. Rename all files in the DIARY_DIR to migrate to a new FMT. |
+| `--fmt-cmd` or `-F` | `fmt_cmd` | `fmt -w75 -s` | (empty) | Journal entries are formatted with this command. If not set (default), the characters are printed to the preview screen character by character (without word wrap). For example, to enable word wrap after 75 characters, use "fmt -s" as FMT_CMD. |
| `--range` or `-r` | `range` | 10 | 1 | Number of years to show before/after todays date |
| `--weekday` or `-w` | `weekday` | 0 | 1 | First weekday, `7` = Sunday, `1` = Monday, ..., `6` = Saturday. Use `7` (or `0`) to display week beginning at Sunday ("S-M-T-W-T-F-S"), or `1` for "M-T-W-T-F-S-S". If `glibc` is installed, the first day of the week is derived from the current locale setting (`$LANG`, see `man locale`). Without `glibc`, the first weekday defaults to 1 (Monday), unless specified otherwise with this option. |
| n/a | `google_calendar` | diary | (empty) | Displayname of Google Calendar for [CalDAV sync](#CalDAV-sync) |
diff --git a/man1/diary.1 b/man1/diary.1
@@ -33,6 +33,12 @@ automatically migrated to the new FMT and do not show up with a new FMT
specifier. Consequently, a change in FMT shows an empty diary at first.
Rename all files in the DIARY_DIR to migrate to a new FMT.
.TP
+\fB\-F\fR, \fB\-\-fmt-cmd\fR=\fI\,FMT_CMD\/\fR
+Journal entries are formatted with this command. If not set (default),
+the characters are printed to the preview screen character by character
+(without word wrap). For example, to enable word wrap after 75 characters,
+use "fmt -s" as FMT_CMD.
+.TP
\fB\-r\fR, \fB\-\-range\fR=\fI\,RANGE\/\fR
RANGE is the number of years to show before/after todays date. Defaults to 1 year.
.TP
@@ -127,6 +133,8 @@ range = 1
weekday = 1
# Date and file format, change with care
fmt = %Y-%m-%d
+# Text format command for entry preview
+fmt_cmd =
# Editor to open journal files with
editor =
# Google calendar name for CalDAV sync
diff --git a/src/diary.c b/src/diary.c
@@ -81,7 +81,12 @@ void draw_calendar(WINDOW* number_pad, WINDOW* month_pad, const char* diary_dir,
void display_entry(const char* dir, size_t dir_size, const struct tm* date, WINDOW* win, int width) {
char path[100];
char* ppath = path;
+ FILE* fp;
int c;
+ char* entry;
+ char* fmtcmd;
+ size_t s, fmtcmd_s;
+ char* tmp_fmt_txt_file = "/tmp/diary_tmp_fmt_entry";
// get entry path
fpath(dir, dir_size, date, &ppath, sizeof path);
@@ -93,13 +98,49 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND
wclear(win);
if (date_has_entry(dir, dir_size, date)) {
- FILE* fp = fopen(path, "r");
- if (fp == NULL) perror("Error opening file");
-
wmove(win, 0, 0);
- while((c = getc(fp)) != EOF) waddch(win, c);
- fclose(fp);
+ if (strcmp(CONFIG.fmt_cmd, "") == 0) {
+ // no formatting command defined, read and print lines
+ fp = fopen(path, "r");
+ while((c = getc(fp)) != EOF) waddch(win, c);
+ fclose(fp);
+ } else {
+ // construct the formatting command
+ fmtcmd_s = strlen(CONFIG.fmt_cmd) + strlen(path) + strlen(">") + strlen(tmp_fmt_txt_file) + 3;
+ fmtcmd = malloc(fmtcmd_s);
+ strcpy(fmtcmd, CONFIG.fmt_cmd);
+ strcat(fmtcmd, " ");
+ strcat(fmtcmd, path);
+ strcat(fmtcmd, " >");
+ strcat(fmtcmd, tmp_fmt_txt_file);
+
+ // format the entry
+ system(fmtcmd);
+
+ // read formatted entry
+ FILE* fp = fopen(tmp_fmt_txt_file, "r");
+ if (fp == NULL) perror("Error opening file");
+
+ fseek(fp, 0, SEEK_END);
+ long entry_bytes = ftell(fp);
+ entry = calloc(entry_bytes + 1, sizeof(char));
+ rewind(fp);
+
+ // read formatted entry
+ s = fread(entry, 1, entry_bytes, fp);
+ fclose(fp);
+ // remove(tmp_fmt_txt_file);
+ if (unlink(tmp_fmt_txt_file) == -1) {
+ perror("unlink tokenfile");
+ }
+ // display formatted entry
+ entry[s] = '\0';
+ waddstr(win, entry);
+
+ free(fmtcmd);
+ free(entry);
+ }
}
wrefresh(win);
@@ -195,6 +236,7 @@ bool read_config(const char* file_path) {
char key_buf[80];
char value_buf[80];
+ char* value_multiword;
char line[256];
FILE * pfile;
@@ -216,6 +258,12 @@ bool read_config(const char* file_path) {
} else if (strcmp("fmt", key_buf) == 0) {
CONFIG.fmt = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.fmt, value_buf);
+ } else if (strcmp("fmt_cmd", key_buf) == 0) {
+ value_multiword = strstr(line, "="); // multiword value
+ value_multiword++; // strip the "="
+ value_multiword[strlen(value_multiword) - 1] = '\0'; // strip newline
+ CONFIG.fmt_cmd = (char *) malloc(strlen(value_multiword) + 1 * sizeof(char));
+ strcpy(CONFIG.fmt_cmd, value_multiword);
} else if (strcmp("editor", key_buf) == 0) {
CONFIG.editor = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.editor, value_buf);
@@ -251,6 +299,7 @@ void usage() {
printf(" -d, --dir DIARY_DIR : Diary storage directory DIARY_DIR\n");
printf(" -e, --editor EDITOR : Editor to open journal files with\n");
printf(" -f, --fmt FMT : Date and file format, change with care\n");
+ printf(" -F, --fmt-cmd FMT_CMD : Format entry preview with command FMT_CMD\n");
printf(" -r, --range RANGE : RANGE is the number of years to show before/after today's date\n");
printf(" -w, --weekday DAY : First day of the week, 0 = Sun, 1 = Mon, ..., 6 = Sat\n");
printf("\n");
@@ -345,6 +394,7 @@ int main(int argc, char** argv) {
{ "dir", required_argument, 0, 'd' },
{ "editor", required_argument, 0, 'e' },
{ "fmt", required_argument, 0, 'f' },
+ { "fmt-cmd", required_argument, 0, 'F' },
{ "range", required_argument, 0, 'r' },
{ "weekday", required_argument, 0, 'w' },
{ 0, 0, 0, 0 }
@@ -352,7 +402,7 @@ int main(int argc, char** argv) {
// read option characters
while (1) {
- option_char = getopt_long(argc, argv, "vhd:r:w:f:e:", long_options, &option_index);
+ option_char = getopt_long(argc, argv, "vhd:r:w:f:F:e:", long_options, &option_index);
if (option_char == -1) {
break;
@@ -390,6 +440,11 @@ int main(int argc, char** argv) {
CONFIG.fmt = (char *) calloc(strlen(optarg) + 1, sizeof(char));
strcpy(CONFIG.fmt, optarg);
break;
+ case 'F':
+ // set formatting command
+ CONFIG.fmt_cmd = (char *) calloc(strlen(optarg) + 1, sizeof(char));
+ strcpy(CONFIG.fmt_cmd, optarg);
+ break;
case 'e':
// set default editor from option character
CONFIG.editor = (char *) calloc(strlen(optarg) + 1, sizeof(char));
diff --git a/src/utils.c b/src/utils.c
@@ -403,6 +403,7 @@ config CONFIG = {
.range = 1,
.weekday = 1,
.fmt = "%Y-%m-%d",
+ .fmt_cmd = "",
.editor = "",
.google_tokenfile = GOOGLE_OAUTH_TOKEN_FILE,
.google_clientid = GOOGLE_OAUTH_CLIENT_ID,
diff --git a/src/utils.h b/src/utils.h
@@ -44,6 +44,9 @@ typedef struct
int weekday;
// 2020-12-31
char* fmt;
+ // Text formatting command/utility fmt,
+ // default width 75 chars (-w75), do not refill lines (-s)
+ char* fmt_cmd;
// Editor to open journal files with
char* editor;
// File for Google OAuth access token