commit 33841662ab653fddfba67d2c86bb39acdcc2aa65
parent 79ee169648c543ecf7c55ee9d81c2724a8e88e73
Author: in0rdr <andreas.gruhler@uzh.ch>
Date: Mon, 21 Nov 2016 17:08:59 +0800
Merge branch 'master' of @mbjd
https://github.com/mbjd/diary into mbjd-master
Diffstat:
M | Makefile | | | 15 | +++++++++++++-- |
M | README.md | | | 47 | +++++++++++++++++++++++++++++++++++++---------- |
M | diary.c | | | 67 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
3 files changed, 114 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,9 +1,20 @@
TARGET = diary
+SRC = diary.c
+INSTALL_DIR = /usr/local/bin
+
+CC = gcc
+CFLAGS = -lncurses
default: $(TARGET)
-$(TARGET):
- gcc diary.c -o $(TARGET) -lncurses
+$(TARGET): $(SRC)
+ $(CC) $(CFLAGS) $(SRC) -o $(TARGET)
clean:
rm -f $(TARGET)
+
+install: $(TARGET)
+ cp $(TARGET) $(INSTALL_DIR)/$(TARGET)
+
+uninstall:
+ rm -f $(INSTALL_DIR)/$(TARGET)
diff --git a/README.md b/README.md
@@ -9,19 +9,46 @@ This is a text based diary, inspired by [khal](https://github.com/pimutils/khal)
```
export EDITOR=vim
```
-
+
2. Compile (requires ncurses):
```
- make clean; make;
+ make
+ ```
+
+3. Install the binary (optional):
+ ```
+ sudo make install
+ ```
+
+ This will copy the binary to /usr/local/bin. To use a different path,
+ change the `INSTALL_DIR` variable in the makefile. You can undo this
+ with `sudo make uninstall`.
+
+4. Run the diary with the folder for the text files as first argument:
```
-
-3. Run the diary with the folder for the text files as first argument:
+ diary ~/.diary
```
- ./diary ~/.diary
+
+ Instead of this, you can also set the environment variable `DIARY_DIR`
+ to the desired directory. If both an argument and the environment
+ variable are given, the argument takes precedence.
+
+ The text files in this folder will be named 'yyyy-mm-dd'.
+
+5. Use the keypad or VIM-like shortcuts to move between dates:
+
```
-
- The text files in this folder will be named 'yyyy-mm-dd'.
-
- (Optionally create an alias for convencience: `alias diary="~/.bin/diary ~/.diary")`
+ e, Enter Edit the current entry
+ t Jump to today
-4. Use the keypad or VIM-like shortcuts to move between dates. Type 't' for today and 'e' to edit. That's it that's all.
+ j, down go forward by 1 week
+ k, up go backward by 1 week
+ h, left go left by 1 day
+ l, right go right by 1 day
+
+ g go to the first date
+ G go to the last date
+
+ J Go forward by 1 month
+ K Go backward by 1 month
+ ```
diff --git a/diary.c b/diary.c
@@ -1,6 +1,11 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <errno.h>
+
+#include <sys/types.h>
+#include <dirent.h>
+
#include <ncurses.h>
#define YEAR_RANGE 1
@@ -158,9 +163,39 @@ void setup_cal_timeframe() {
int main(int argc, char** argv)
{
+ // Get the diary directory via environment variable or argument
+ // If both are given, the argument takes precedence
+ char* diary_dir = NULL;
if (argc < 2) {
- printf("Expect diary directory as command line argument\n");
- return 0;
+ diary_dir = getenv("DIARY_DIR");
+ if (diary_dir == NULL)
+ {
+ fprintf(stderr, "The diary directory must ge given as command line "
+ "argument or in the DIARY_DIR environment variable\n");
+ return 1;
+ }
+ }
+ else
+ {
+ diary_dir = argv[1];
+ }
+
+ // Check if that directory exists
+ DIR* diary_dir_ptr = opendir(diary_dir);
+ if (diary_dir_ptr)
+ {
+ // Directory exists, continue
+ closedir(diary_dir_ptr);
+ }
+ else if (errno = ENOENT)
+ {
+ fprintf(stderr, "The directory '%s' does not exist\n", diary_dir);
+ return 2;
+ }
+ else
+ {
+ fprintf(stderr, "The directory '%s' could not be opened\n", diary_dir);
+ return 1;
}
setup_cal_timeframe();
@@ -182,7 +217,6 @@ int main(int argc, char** argv)
int ch;
struct tm new_date;
- char* diary_dir = argv[1];
// init the current pad possition at the very end,
// such that the cursor is displayed top of screen
int pad_pos = 9999999;
@@ -198,6 +232,7 @@ int main(int argc, char** argv)
char* ecmd = curs_date_edit_cmd(diary_dir);
switch(ch) {
+ // Basic movements
case 'j':
case KEY_DOWN:
new_date.tm_mday += 7;
@@ -218,11 +253,37 @@ int main(int argc, char** argv)
new_date.tm_mday--;
ret = go_to(cal, aside, timelocal(&new_date), &pad_pos);
break;
+
+ // Jump to top/bottom of page
+ case 'g':
+ ret = go_to(cal, aside, timelocal(&cal_start), &pad_pos);
+ break;
+ case 'G':
+ ret = go_to(cal, aside, timelocal(&cal_end), &pad_pos);
+ break;
+
+ // Jump backward/forward by a month
+ case 'K':
+ if (new_date.tm_mday == 1)
+ new_date.tm_mon--;
+ new_date.tm_mday = 1;
+ ret = go_to(cal, aside, timelocal(&new_date), &pad_pos);
+ break;
+ case 'J':
+ new_date.tm_mon++;
+ new_date.tm_mday = 1;
+ ret = go_to(cal, aside, timelocal(&new_date), &pad_pos);
+ break;
+
+ // Today shortcut
case 't':
+ case 'n':
new_date = cur_date;
ret = go_to(cal, aside, raw_time, &pad_pos);
break;
+ // Edit/create a diary entry
case 'e':
+ case '\n':
if (ecmd) {
curs_set(1);
system(ecmd);