-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -34,6 +34,8 @@ | |||||||
#include <ctype.h> | ||||||||
#include <errno.h> | ||||||||
#include <math.h> | ||||||||
#include <sys/stat.h> | ||||||||
#include <sys/types.h> | ||||||||
#include "linenoise.h" | ||||||||
#include "sds.h" | ||||||||
|
||||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
#define HIST_FILE "clac/history" | ||||||||
#define WORDS_FILE "clac/words" | ||||||||
#define CAPACITY 0xFF | ||||||||
|
||||||||
|
@@ -475,8 +479,8 @@ static char *hints(const char *input, int *color, int *bold) { | |||||||
return result; | ||||||||
} | ||||||||
|
||||||||
static sds buildpath(const char *fmt, const char *dir) { | ||||||||
return sdscatfmt(sdsempty(), fmt, dir, WORDS_FILE); | ||||||||
static sds buildpath(const char *fmt, const char *dir, const char *file) { | ||||||||
return sdscatfmt(sdsempty(), fmt, dir, file); | ||||||||
} | ||||||||
|
||||||||
static void config() { | ||||||||
|
@@ -485,9 +489,9 @@ static void config() { | |||||||
if (getenv("CLAC_WORDS") != NULL) { | ||||||||
filename = sdsnew(getenv("CLAC_WORDS")); | ||||||||
} else if (getenv("XDG_CONFIG_HOME") != NULL) { | ||||||||
filename = buildpath("%s/%s", getenv("XDG_CONFIG_HOME")); | ||||||||
filename = buildpath("%s/%s", getenv("XDG_CONFIG_HOME"), WORDS_FILE); | ||||||||
} else if (getenv("HOME") != NULL) { | ||||||||
filename = buildpath("%s/.config/%s", getenv("HOME")); | ||||||||
filename = buildpath("%s/.config/%s", getenv("HOME"), WORDS_FILE); | ||||||||
} | ||||||||
|
||||||||
if (filename) { | ||||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
filename = buildpath("%s/.local/share/%s", getenv("HOME"), HIST_FILE); | ||||||||
} else { | ||||||||
return NULL; | ||||||||
} | ||||||||
|
||||||||
mkdir(directory, 0700); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
sdsfree(directory); | ||||||||
|
||||||||
return filename; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
int main(int argc, char **argv) { | ||||||||
char *line; | ||||||||
|
||||||||
|
@@ -518,9 +542,15 @@ int main(int argc, char **argv) { | |||||||
exit(1); | ||||||||
} | ||||||||
|
||||||||
sds history = history_config(); | ||||||||
|
||||||||
linenoiseSetHintsCallback(hints); | ||||||||
linenoiseSetCompletionCallback(completion); | ||||||||
|
||||||||
if (history) { | ||||||||
linenoiseHistoryLoad(history); | ||||||||
} | ||||||||
|
||||||||
while((line = linenoise("> ")) != NULL) { | ||||||||
if (!strcmp(line, "words")) { | ||||||||
node *curr = head; | ||||||||
|
@@ -541,9 +571,14 @@ int main(int argc, char **argv) { | |||||||
sdsclear(result); | ||||||||
linenoiseHistoryAdd(line); | ||||||||
free(line); | ||||||||
|
||||||||
if (history) { | ||||||||
linenoiseHistorySave(history); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
sdsfree(result); | ||||||||
sdsfree(history); | ||||||||
cleanup(); | ||||||||
|
||||||||
return 0; | ||||||||
|
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).