Skip to content

Commit

Permalink
link-parser: Implement history file per dict name
Browse files Browse the repository at this point in the history
  • Loading branch information
ampli committed May 24, 2024
1 parent 0bcaf23 commit 8bbfa43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
18 changes: 13 additions & 5 deletions link-parser/lg_readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,29 @@ static wchar_t * prompt(EditLine *el)
// arguments. So replace them with a new struct "io_params".
static const char *history_file;

static const char history_file_basename[] = "lg_history";
static const char history_file_basename[] = "_history";

void find_history_filepath(const char *dictname, const char *argv0,
const char *prog)
{
const char *xdg_prog = xdg_get_program_name(argv0);
if (NULL == xdg_prog) xdg_prog = prog;

// Prefix the history file name with a dict indication.
// To support sharing the history file by several similar dicts,
// use the dict name up to the first ":" if exists.
char *dictpref = strdup(dictname);
dictpref[strcspn(dictpref, ":")] = '\0';

// Find the location of the history file.
const char *hfile =
xdg_make_path(XDG_BD_STATE, "%s/%s", xdg_prog, history_file_basename);
const char *hfile = xdg_make_path(XDG_BD_STATE, "%s/%s%s", xdg_prog,
dictpref, history_file_basename);
free(dictpref);

if (NULL == hfile)
{
prt_error("Warning: xdg_get_home(XDG_BD_STATE) failed - "
"history file will be in the current directory\n");
prt_error("Warning: xdg_get_home(XDG_BD_STATE) failed; "
"input history will not be supported.\n");
history_file = strdup("dev/null");
return;
}
Expand Down
5 changes: 3 additions & 2 deletions link-parser/link-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ static char * debug = (char *)"";
static char * test = (char *)"";
static bool isatty_io; /* Both input and output are tty. */

static const char prog[] = "link-parser"; // If cannot obtain the program name
static const char prompt[] = "linkparser> ";
static const char *use_prompt(int verbosity_level)
{
Expand Down Expand Up @@ -701,7 +700,9 @@ int main(int argc, char * argv[])
}

if (isatty_io)
find_history_filepath(dictionary_get_lang(dict), argv[0], prog);
{
find_history_filepath(dictionary_get_lang(dict), argv[0], "link-parser");
}

/* Main input loop */
while (true)
Expand Down

0 comments on commit 8bbfa43

Please sign in to comment.