Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions clac.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
Copy link

@waltertross waltertross Feb 24, 2020

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).

#include "linenoise.h"
#include "sds.h"

Expand All @@ -45,6 +47,8 @@

/* Config */
#define BUFFER_MAX 1024
#define HIST_DIR "clac"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define HIST_DIR "clac"
#define CLAC_DIR "clac"

#define HIST_FILE "clac/history"
#define WORDS_FILE "clac/words"
#define CAPACITY 0xFF

Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -496,6 +500,26 @@ static void config() {
}
}

static sds history_config() {
sds directory = NULL;
sds filename = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sds filename = NULL;
sds filename = NULL;
int directory_ok;


if (getenv("XDG_DATA_HOME") != NULL) {
directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_DIR);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), HIST_DIR);
directory = buildpath("%s/%s", getenv("XDG_DATA_HOME"), CLAC_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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
directory = buildpath("%s/.local/share/%s", getenv("HOME"), HIST_DIR);
directory = buildpath("%s/.local/share/%s", getenv("HOME"), CLAC_DIR);

filename = buildpath("%s/.local/share/%s", getenv("HOME"), HIST_FILE);
} else {
return NULL;
}

mkdir(directory, 0700);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mkdir(directory, 0700);
directory_ok = mkdir(directory, 0700) == 0 || errno == EEXIST;

sdsfree(directory);

return filename;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return filename;
return directory_ok ? filename : NULL;

}

int main(int argc, char **argv) {
char *line;

Expand All @@ -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;
Expand All @@ -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;
Expand Down