commit 426bda389a208ddbbd2909e395be6eeae2c65cd7
parent 2b049acd4bb13e4d471ae75d5a5921d5c29c2953
Author: Wim Dupont <wim@wimdupont.com>
Date: Sun, 16 Mar 2025 09:33:08 +0100
added span
Diffstat:
M | scal.c | | | 269 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
1 file changed, 148 insertions(+), 121 deletions(-)
diff --git a/scal.c b/scal.c
@@ -30,7 +30,6 @@ static void print_words(void);
static void write_notes(void);
static Note ** get_notes(void);
static Note * get_note(char *line_buf, ssize_t line_size);
-static char * prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill);
static void print_view(void);
static void print_month(const int monthadd, int *y);
static void combo_key(int keypress);
@@ -39,6 +38,7 @@ static void combo_make(int keypress);
static void add_note(struct tm *tm);
static void edit_notes(void);
static void rm_note(struct tm *tm);
+static int prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill);
static int get_curnotes(void);
static int get_note_view_col(Recurring recurring);
static int has_note(struct tm *tm);
@@ -301,8 +301,6 @@ combo_key(int keypress)
void
combo_go(int key)
{
- char pathbuf[PATH_MAX];
-
switch (key) {
case KEY_COMBO_GO_TODAY:
curdate();
@@ -315,8 +313,6 @@ combo_go(int key)
void
combo_make(int key)
{
- char pathbuf[PATH_MAX];
-
cbreak();
switch (key) {
@@ -331,106 +327,11 @@ combo_make(int key)
}
}
-char *
-prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill)
-{
- int c, y, x, nr, phlen = mask == NULL ? 0 : strlen(mask);
- char placeholder[phlen];
- char *startfill;
- size_t len = 0;
-
- replace_digits(placeholder, mask, phlen);
-
- move(maxy-1, 1);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- addstr(question);
- attroff(COLOR_PAIR(PROMPT_COLOR));
- if (prefill != NULL)
- startfill = prefill;
- else if (placeholder != NULL)
- startfill = placeholder;
- else
- startfill = "";
-
- printw(" %s", startfill);
-
- if (prefill != NULL) {
- len = strlen(prefill);
- cpstr(buf, prefill);
- }
-
- move(maxy-1, strlen(question)+2 + len);
-
- echo();
- curs_set(1);
-
- getyx(stdscr, y, x);
-
- while ((c = wgetch(stdscr)) != 10) {
- move(maxy-2, 1);
- clrtoeol();
- move(y,x);
- switch (c) {
- case KEY_BACKSPACE:
- if (len > 0) {
- if (len <= phlen) {
- buf[--(len)] = placeholder[len];
- move(y, --x);
- attron(COLOR_PAIR(PROMPT_COLOR));
- addch(buf[len]);
- attroff(COLOR_PAIR(PROMPT_COLOR));
- move(y, x);
- if (len > 0 && len <= phlen && placeholder[len] != '_') {
- buf[--(len)] = placeholder[len];
- move(y, --x);
- attron(COLOR_PAIR(PROMPT_COLOR));
- addch(buf[len]);
- attroff(COLOR_PAIR(PROMPT_COLOR));
- move(y, x);
- }
- } else {
- buf[--(len)] = '\0';
- move(y, --x);
- clrtoeol();
- }
- }
- break;
- default:
- if (len < size) {
- if (len < phlen) {
- if (!isdigit(c) || (nr = mask[len] - '0') < c - '0') {
- mvprintw(maxy-2, 1, "Only digits (max: %d)", nr);
- move(y,x);
- break;
- }
- buf[(len)++] = c;
- move(y, ++x);
- if (len < phlen && placeholder[len] != '_') {
- buf[len] = placeholder[len];
- len++;
- move(y, ++x);
- }
- } else {
- buf[(len)++] = c;
- move(y, ++x);
- }
- } else
- clrtoeol();
- break;
- }
- }
- noecho();
- curs_set(0);
-
- buf[phlen > 0 ? phlen : len] = '\0';
-}
-
void
add_note(struct tm *tm) {
size_t len;
- char answer[DESCLEN+20], buf[DESCLEN], rec[2], time[6], date[11], prefill[11];
+ int duration = 1;
+ char answer[DESCLEN+20], buf[DESCLEN], rec[2], time[6], date[11], prefill[11], span[2], spandur[3];
if (tm != NULL)
snprintf(prefill, 11, "%04d-%02d-%02d\0",
@@ -438,28 +339,42 @@ add_note(struct tm *tm) {
tm->tm_mon+1,
tm->tm_mday);
- prompt_answer(rec, 1, "Recurring? (0=NO;1=YEARLY;2=MONTHLY;3=WEEKLY)", "3", NULL);
- prompt_answer(date, 10, "Date?", "2999-19-39", prefill);
- prompt_answer(time, 5, "Time?", "29:59", NULL);
- prompt_answer(buf, DESCLEN, "description?", NULL, NULL);
+ if (prompt_answer(rec, 1, "Recurring? (0=NO;1=YEARLY;2=MONTHLY;3=WEEKLY)", "3", "0"))
+ return;
+ if (rec[0] == '0') {
+ if (prompt_answer(span, 1, "Span? (0=NO;1=YES)", "1", "0"))
+ return;
+ if (span[0] == '1') {
+ if (prompt_answer(spandur, 2, "Span duration? (in days)", "99", "01"))
+ return;
+ duration = atoi(spandur);
+ }
+ }
+
+ if (prompt_answer(date, 10, "Date?", "2999-19-39", prefill))
+ return;
+ if (prompt_answer(time, 5, "Time?", "29:59", NULL))
+ return;
+ if (prompt_answer(buf, DESCLEN, "Description?", NULL, NULL))
+ return;
+
snprintf(answer, DESCLEN+20, "%s|%10sT%s|%s", rec, date, time, buf);
len = strlen(answer);
if (len > 19) {
- notes = (Note**) realloc(notes, (sizeof(Note) * (note_count + 1)));
- if (notes == NULL)
- fatal("Fatal: failed to reallocate bytes for notes.\n");
+ for (int i = 1; i <= duration; i++) {
+ notes = (Note**) realloc(notes, (sizeof(Note*) * (note_count + 1)));
+ if (notes == NULL)
+ fatal("Fatal: failed to reallocate bytes for notes.\n");
- notes[note_count] = get_note(answer, len);
- note_count++;
+ notes[note_count] = get_note(answer, len);
+ notes[note_count]->time.tm_mday+=(i-1);
+ mktime(¬es[note_count]->time);
+ note_count++;
+ }
write_notes();
}
-
- move(maxy-1, 1);
- clrtoeol();
- move(maxy-2, 1);
- clrtoeol();
}
void
@@ -503,12 +418,9 @@ rm_note(struct tm *tm) {
return;
move(BORDER_SPACE_SIZE, wmaxx + BORDER_SPACE_SIZE - 1);
-
curs_set(1);
-
getyx(stdscr, y, x);
-
while ((c = wgetch(stdscr)) != 10) {
switch (c) {
case KEY_BACKSPACE:
@@ -604,6 +516,7 @@ get_note(char *line_buf, ssize_t line_size)
{
char timestr[19];
Note *note = (Note*) calloc(1, sizeof(Note));
+
if (note == NULL)
fatal("Fatal: failed to allocate bytes for note.\n");
@@ -656,8 +569,117 @@ print_view(void)
}
int
+prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill)
+{
+ int c, y, x, nr, phlen = mask == NULL ? 0 : strlen(mask), status = 0;
+ char placeholder[phlen];
+ char *startfill;
+ size_t len = 0;
+
+ replace_digits(placeholder, mask, phlen);
+
+ move(maxy-1, 1);
+ clrtoeol();
+
+ attron(COLOR_PAIR(PROMPT_COLOR));
+ addstr(question);
+ attroff(COLOR_PAIR(PROMPT_COLOR));
+ if (prefill != NULL)
+ startfill = prefill;
+ else if (placeholder != NULL)
+ startfill = placeholder;
+ else
+ startfill = "";
+
+ printw(" %s", startfill);
+
+ if (prefill != NULL) {
+ len = strlen(prefill);
+ cpstr(buf, prefill);
+ }
+
+ move(maxy-1, strlen(question)+2 + len);
+
+ echo();
+ curs_set(1);
+
+ getyx(stdscr, y, x);
+
+ while ((c = wgetch(stdscr)) != 10) {
+ move(maxy-2, 1);
+ clrtoeol();
+ move(y,x);
+ switch (c) {
+ case KEY_BACKSPACE:
+ if (len > 0) {
+ if (len <= phlen) {
+ buf[--(len)] = placeholder[len];
+ move(y, --x);
+ attron(COLOR_PAIR(PROMPT_COLOR));
+ addch(buf[len]);
+ attroff(COLOR_PAIR(PROMPT_COLOR));
+ move(y, x);
+ if (len > 0 && len <= phlen && placeholder[len] != '_') {
+ buf[--(len)] = placeholder[len];
+ move(y, --x);
+ attron(COLOR_PAIR(PROMPT_COLOR));
+ addch(buf[len]);
+ attroff(COLOR_PAIR(PROMPT_COLOR));
+ move(y, x);
+ }
+ } else {
+ buf[--(len)] = '\0';
+ move(y, --x);
+ clrtoeol();
+ }
+ }
+ break;
+ default:
+ if (len < size) {
+ if (len < phlen) {
+ if (!isdigit(c) || (nr = mask[len] - '0') < c - '0') {
+ mvprintw(maxy-2, 1, "Only digits (max: %d)", nr);
+ move(y,x);
+ break;
+ }
+ buf[(len)++] = c;
+ move(y, ++x);
+ if (len < phlen && placeholder[len] != '_') {
+ buf[len] = placeholder[len];
+ len++;
+ move(y, ++x);
+ }
+ } else {
+ buf[(len)++] = c;
+ move(y, ++x);
+ }
+ } else
+ clrtoeol();
+ break;
+ }
+ }
+ noecho();
+ curs_set(0);
+
+ if (strchr(buf, '_') || len < phlen) {
+ buf[0] = '\0';
+ status = 1;
+ } else
+ buf[phlen > 0 ? phlen : len] = '\0';
+
+ move(maxy-1, 1);
+ clrtoeol();
+ move(maxy-2, 1);
+ clrtoeol();
+
+ return status;
+}
+
+int
get_curnotes(void)
{
+ int entered = 0;
+
if (curnote_count > 0) {
curnote_count = 0;
free(curnotes);
@@ -676,7 +698,9 @@ get_curnotes(void)
fatal("Fatal: failed to reallocate bytes for notes.\n");
curnotes[curnote_count] = notes[i];
curnote_count++;
- }
+ entered = 1;
+ } else if (entered)
+ break;
}
}
@@ -743,7 +767,6 @@ same_day(struct tm *a, struct tm *b, Recurring recurring)
int
compare_note(const void *a, const void *b)
{
- int recurdif;
const Note *ma = *(Note**)a;
const Note *mb = *(Note**)b;
@@ -803,6 +826,7 @@ void
move_down(void)
{
int wcount = get_days(&highltm);
+
if (highlight < wcount-DAY_COLUMN_SIZE) {
highlight = MIN(highlight+DAY_COLUMN_SIZE, wcount-1);
highltm.tm_mday = highlight+1;
@@ -823,6 +847,7 @@ void
move_right(void)
{
int wcount = get_days(&highltm);
+
if (highlight < wcount-1) {
highlight++;
highltm.tm_mday = highlight+1;
@@ -853,6 +878,7 @@ char *
cpstr(char *dest, const char *src)
{
size_t cplen = strlen(src);
+
memcpy(dest, src, cplen);
dest[cplen] = '\0';
return dest;
@@ -883,6 +909,7 @@ void
curdate(void)
{
time_t t = time(NULL);
+
curtm = *localtime(&t);
highltm = curtm;
highlight = highltm.tm_mday-1;