aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/fm.c52
-rw-r--r--src/fm.h1
2 files changed, 44 insertions, 9 deletions
diff --git a/src/fm.c b/src/fm.c
index 92526c3..c863cad 100644
--- a/src/fm.c
+++ b/src/fm.c
@@ -22,6 +22,7 @@
static magic_t g_magic_ctx = NULL;
static char *const utils[] = {"vi", "xdg-open"};
static char *const envs[] = {"SHELL", "EDITOR", "SF"};
+static char last_preview_path[PATH_MAX] = "";
bool need_redraw = true;
/* Function forward declarations */
@@ -201,6 +202,12 @@ detect_file_type(const char *filepath)
return desc ? strdup(desc) : strdup("unknown");
}
+static bool
+is_previewed_path(const char *path)
+{
+ return (strcmp(last_preview_path, path) == 0);
+}
+
void
fm_draw_file_stream(window *w, FILE *fp)
{
@@ -220,30 +227,57 @@ fm_preview_file(window *w, const char *path)
if (!w || !path)
return false;
- wclear(w->win);
-
- char *file_type = detect_file_type(path);
-
- if (strstr(file_type, "text") == NULL) {
- mvwprintw(w->win, 0, 0, "%s", file_type);
- free(file_type);
+ if (is_previewed_path(path))
return false;
- }
+ wclear(w->win);
+ char *file_type = detect_file_type(path);
FILE *fp = fopen(path, "r");
if (!fp) {
mvwprintw(w->win, 0, 0, "<Cannot open file>");
free(file_type);
return false;
+ } else if (strstr(file_type, "text")) {
+ fm_draw_file_stream(w, fp);
+ } else {
+ mvwprintw(w->win, 0, 0, "%s", file_type);
}
- fm_draw_file_stream(w, fp);
+ snprintf(last_preview_path, sizeof(last_preview_path), "%s", path);
fclose(fp);
free(file_type);
return true;
}
+bool
+fm_preview_dir(fm_info *info, const char *path)
+{
+ if (!info)
+ return false;
+
+ if (is_previewed_path(path))
+ return false;
+
+ window *right = info->windows[FM_PANE_RIGHT];
+ int right_height = info->windows[FM_PANE_RIGHT]->height;
+
+ wclear(right->win);
+
+ file_list *new_list = info->next;
+ new_list = fm_getdir(new_list, path);
+ if (new_list) {
+ info->next = new_list;
+ fm_draw_entries(right, new_list, -1, 0, right_height);
+ } else {
+ return false;
+ }
+
+ snprintf(last_preview_path, sizeof(last_preview_path), "%s", path);
+
+ return true;
+}
+
void
fm_draw_entries(window *window, file_list *list, size_t cursor, int top,
int bottom)
diff --git a/src/fm.h b/src/fm.h
index 493d1f9..9293bfe 100644
--- a/src/fm.h
+++ b/src/fm.h
@@ -60,6 +60,7 @@ void fm_draw_file_stream(window *w, FILE *fp);
void fm_cd_up(fm_info *i);
void fm_cd_down(fm_info *i);
bool fm_preview_file(window *w, const char *path);
+bool fm_preview_dir(fm_info *info, const char *path);
int open_with_editor(char **argv);
void magic_init(void);
void magic_cleanup(void);