Skip to content

Commit

Permalink
Fix: image previews ignoring alternative preview file
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-arch committed Nov 20, 2024
1 parent 0f28af2 commit 29e2fb7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,12 @@ open_reg_exit(char *filename, const int url, const int preview)

tmp_dir = savestring(P_tmpdir, P_tmpdir_len);

if (alt_preview_file && preview == 1) {
const char *env_preview_file = (preview == 1 && !alt_preview_file)
? getenv("CLIFM_ALT_PREVIEW_FILE") : NULL;

if (env_preview_file && *env_preview_file) {
mime_file = savestring(env_preview_file, strlen(env_preview_file));
} else if (alt_preview_file && preview == 1) {
mime_file = savestring(alt_preview_file, strlen(alt_preview_file));
} else {
const size_t mime_file_len = strlen(homedir)
Expand Down Expand Up @@ -855,6 +860,7 @@ set_alt_config_dir(char *dir)
}
} else {
alt_config_dir = savestring(dir, strlen(dir));
flags |= ALT_PREVIEW_FILE;
err(ERR_NO_LOG, PRINT_PROMPT, _("%s: '%s': Using an alternative "
"configuration directory\n"), PROGRAM_NAME, alt_config_dir);
}
Expand Down Expand Up @@ -913,6 +919,7 @@ set_shotgun_file(char *opt)
err_arg_required("--shotgun-file"); /* noreturn */

alt_preview_file = stat_file(opt);
flags |= ALT_PREVIEW_FILE;
}
#endif /* !_NO_LIRA */
#endif /* !_BE_POSIX */
Expand Down Expand Up @@ -1092,6 +1099,7 @@ set_alt_profile(const char *name)

if (validate_profile_name(name) == FUNC_SUCCESS) {
alt_profile = savestring(name, strlen(name));
flags |= ALT_PREVIEW_FILE;
return;
}

Expand Down Expand Up @@ -1752,7 +1760,8 @@ parse_cmdline_args(const int argc, char **argv)
#endif /* !_NO_LIRA */

char *spath = (char *)NULL;
if (xargs.stat == 0 && argv[optind]) { /* Starting path passed as positional parameter */
if (xargs.stat == 0 && argv[optind]) {
/* Starting path passed as positional parameter */
spath = resolve_starting_path(argv[optind]);
} else {
if (path_value) /* Starting path passed via -p */
Expand All @@ -1764,7 +1773,7 @@ parse_cmdline_args(const int argc, char **argv)
free(spath);
} else {
if (xargs.list_and_quit == 1) {
/* Starting path not specified in the command line, let's use
/* Starting path not specified in the command line. Let's use
* the current directory. */
conf.restore_last_path = 0;
set_start_path();
Expand Down
5 changes: 3 additions & 2 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,16 @@ extern time_t curdir_mtime;
#define FIRST_WORD_IS_ELN (1 << 4)
#define IN_BOOKMARKS_SCREEN (1 << 5)
#define STATE_COMPLETING (1 << 6)
/* Instead of a completion for the current word, a BAEJ suggestion points to
* a possible completion as follows: WORD > COMPLETION */
/* Instead of a completion for the current query, a BAEJ suggestion points to
* a possible completion as follows: QUERY > COMPLETION */
#define BAEJ_SUGGESTION (1 << 7)
#define STATE_SUGGESTING (1 << 8)
#define IN_SELBOX_SCREEN (1 << 9)
#define MULTI_SEL (1 << 10)
#define PREVIEWER (1 << 11)
#define NO_FIX_RL_POINT (1 << 12)
#define FAILED_ALIAS (1 << 13)
#define ALT_PREVIEW_FILE (1 << 14)

/* Flags for third party binaries */
#define FZF_BIN_OK (1 << 0)
Expand Down
34 changes: 28 additions & 6 deletions src/tabcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
* tab_complete
* All changes are licensed under GPL-2.0-or-later. */

/* enable_raw_mode, disable_raw_mode, and get_cursor_position functions are
* taken from https://github.com/antirez/linenoise/blob/master/linenoise.c, licensed
* under BSD-2-Clause.
* All changes are licenced under GPL-2.0-or-later. */

#include "helpers.h"

#include <unistd.h>
Expand Down Expand Up @@ -597,6 +592,28 @@ get_last_word(char *str, const int original_query)
return w;
}

/* We have an alternative preview file (set either via --shotgun-file,
* --config-dir, or --profile).
* Let's write the file name into an environment variable
* (CLIFM_ALT_PREVIEW_FILE), so that the clifm instance invoked by fzf
* can handle it. */
static void
setenv_fzf_alt_preview_file(void)
{
static char buf[PATH_MAX + 1] = "";

if (!*buf) {
if (alt_preview_file && *alt_preview_file)
snprintf(buf, sizeof(buf), "%s", alt_preview_file);
else if (config_dir && *config_dir)
snprintf(buf, sizeof(buf), "%s/preview.clifm", config_dir);
else
return;
}

setenv("CLIFM_ALT_PREVIEW_FILE", buf, 1);
}

static void
set_fzf_env_vars(const int height)
{
Expand Down Expand Up @@ -641,6 +658,9 @@ set_fzf_env_vars(const int height)
setenv("CLIFM_TERM_COLUMNS", p, 1);
snprintf(p, sizeof(p), "%d", term_lines);
setenv("CLIFM_TERM_LINES", p, 1);

if (flags & ALT_PREVIEW_FILE)
setenv_fzf_alt_preview_file();
}

static void
Expand All @@ -650,6 +670,8 @@ clear_fzf(void)
unsetenv("CLIFM_FZF_LINE");
unsetenv("CLIFM_TERM_COLUMNS");
unsetenv("CLIFM_TERM_LINES");
if (flags & ALT_PREVIEW_FILE)
unsetenv("CLIFM_ALT_PREVIEW_FILE");
}

/* Calculate the available space for the fzf preview window based on
Expand Down Expand Up @@ -818,7 +840,7 @@ ctrl-d:deselect-all,ctrl-t:toggle-all" : "",
/* Restore the user's shell to its original value. */
user.shell = shell_bk;

if (prev == 1)
if (prev == FZF_INTERNAL_PREVIEWER)
clear_fzf();
if (dr == 1)
flags |= DELAYED_REFRESH;
Expand Down

0 comments on commit 29e2fb7

Please sign in to comment.