-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add history persistence to file #23
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry, still not used to github)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cleared my previous all too hasty review. This one I'm happy with.
@@ -45,6 +47,8 @@ | |||
|
|||
/* Config */ | |||
#define BUFFER_MAX 1024 | |||
#define HIST_DIR "clac" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define HIST_DIR "clac" | |
#define CLAC_DIR "clac" |
@@ -496,6 +500,26 @@ static void config() { | |||
} | |||
} | |||
|
|||
static sds history_config() { | |||
sds directory = NULL; | |||
sds filename = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return filename; | |
return directory_ok ? filename : NULL; |
@@ -34,6 +34,8 @@ | |||
#include <ctype.h> | |||
#include <errno.h> | |||
#include <math.h> | |||
#include <sys/stat.h> | |||
#include <sys/types.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.