Add history persistence to file#23
Conversation
waltertross
left a comment
There was a problem hiding this comment.
(sorry, still not used to github)
waltertross
left a comment
There was a problem hiding this comment.
I cleared my previous all too hasty review. This one I'm happy with.
|
|
||
| /* Config */ | ||
| #define BUFFER_MAX 1024 | ||
| #define HIST_DIR "clac" |
There was a problem hiding this comment.
| #define HIST_DIR "clac" | |
| #define CLAC_DIR "clac" |
|
|
||
| static sds history_config() { | ||
| sds directory = NULL; | ||
| sds filename = NULL; |
There was a problem hiding this comment.
| sds filename = NULL; | |
| sds filename = NULL; | |
| int directory_ok; |
| sds filename = NULL; | ||
|
|
||
| if (getenv("XDG_DATA_HOME") != NULL) { | ||
| directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_DIR); |
There was a problem hiding this comment.
| directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_DIR); | |
| directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), CLAC_DIR); |
| directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_DIR); | ||
| filename = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_FILE); | ||
| } else if (getenv("HOME") != NULL) { | ||
| directory = buildpath("%s/.local/share/%s", getenv("HOME"), HIST_DIR); |
There was a problem hiding this comment.
| directory = buildpath("%s/.local/share/%s", getenv("HOME"), HIST_DIR); | |
| directory = buildpath("%s/.local/share/%s", getenv("HOME"), CLAC_DIR); |
| return NULL; | ||
| } | ||
|
|
||
| mkdir(directory, 0700); |
There was a problem hiding this comment.
| mkdir(directory, 0700); | |
| directory_ok = mkdir(directory, 0700) == 0 || errno == EEXIST; |
| mkdir(directory, 0700); | ||
| sdsfree(directory); | ||
|
|
||
| return filename; |
There was a problem hiding this comment.
| return filename; | |
| return directory_ok ? filename : NULL; |
| #include <errno.h> | ||
| #include <math.h> | ||
| #include <sys/stat.h> | ||
| #include <sys/types.h> |
There was a problem hiding this comment.
can we delete this line? It doesn't seem to be used (compilation passes without it).
|
I didn't handle the mkdir return value because there's not much we can do, the app should just ignore any error condition on that directory. If Linenoise can save its history there, it will do so, otherwise it won't. As for the imports, I copied them from |
Hey there. Thank you for this great tool. I added history saving and loading to file (
~/.local/share/clac/history, configurable with XDG env) because I often need to review or amend previous calculations, so I guess other people would find it useful too. Besides, I didn't do much, because Linenoise already has the required functions.