diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit dfb3d8b4a02b353543f07443d8b488031e09c92c
parent ad2717203e816273b38c4e5e7bc613ddcfd4cd1b
Author: Balduin Dettling <balduindettling@gmail.com>
Date:   Sat, 24 Dec 2016 12:43:50 +0100

Added shortcuts to jump to next/previous entry

Diffstat:
MREADME.md | 3+++
Mdiary.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -49,6 +49,9 @@ Note: for *BSD users run gmake. h, left go left by 1 day l, right go right by 1 day + N go to the previous diary entry + n go to the next diary entry + g go to the first date G go to the last date diff --git a/diary.c b/diary.c @@ -256,6 +256,50 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath strcat(*rpath, dstr); } +/* + * Finds the most recent date before <current> that has a diary entry, or + * <current> itself if there is no previous diary entry + */ +struct tm find_previous_date(const struct tm current, const char* diary_dir, size_t diary_dir_size) +{ + + time_t start = mktime(&cal_start); + struct tm it = current; + it.tm_mday--; + + while (mktime(&it) >= start) { + if (date_has_entry(diary_dir, diary_dir_size, &it)) { + return it; + } + + it.tm_mday--; + } + + return current; +} + +/* + * Finds the next date after <current> that has a diary entry, or <current> + * if there is no next diary entry + */ +struct tm find_next_date(const struct tm current, const char* diary_dir, size_t diary_dir_size) +{ + + time_t end = mktime(&cal_end); + struct tm it = current; + it.tm_mday++; + + while (mktime(&it) <= end) { + if (date_has_entry(diary_dir, diary_dir_size, &it)) { + return it; + } + + it.tm_mday++; + } + + return current; +} + int main(int argc, char** argv) { setlocale(LC_ALL, ""); char diary_dir[80]; @@ -503,6 +547,16 @@ int main(int argc, char** argv) { LINES - 1, ASIDE_WIDTH + CAL_WIDTH); } break; + // Move to the previous diary entry + case 'N': + new_date = find_previous_date(new_date, diary_dir, strlen(diary_dir)); + mv_valid = go_to(cal, aside, mktime(&new_date), &pad_pos); + break; + // Move to the next diary entry + case 'n': + new_date = find_next_date(new_date, diary_dir, strlen(diary_dir)); + mv_valid = go_to(cal, aside, mktime(&new_date), &pad_pos); + break; } if (mv_valid) {