diff options
| author | verdant <im@verdant.ee> | 2026-08-03 21:01:05 +0800 |
|---|---|---|
| committer | verdant <im@verdant.ee> | 2026-08-03 21:01:05 +0800 |
| commit | 8fa00affda48e06a759915bf12e01a5ffca65e45 (patch) | |
| tree | 52b2098af1d97b15c76a262388fc1d28bdf24fa6 /src | |
| parent | 55e02a195373325216a47ebddc98039faf59b51c (diff) | |
| download | sf-8fa00affda48e06a759915bf12e01a5ffca65e45.tar.gz sf-8fa00affda48e06a759915bf12e01a5ffca65e45.zip | |
Add width character and icon support
Diffstat (limited to 'src')
| -rw-r--r-- | src/display.c | 2 | ||||
| -rw-r--r-- | src/fm.c | 14 | ||||
| -rw-r--r-- | src/icon.c | 32 | ||||
| -rw-r--r-- | src/icon.h | 27 |
4 files changed, 71 insertions, 4 deletions
diff --git a/src/display.c b/src/display.c index e7dd031..f35bf33 100644 --- a/src/display.c +++ b/src/display.c @@ -1,6 +1,7 @@ #include "display.h" #include <curses.h> +#include <locale.h> #include "err.h" #include "log.h" @@ -18,6 +19,7 @@ color_pair_init(void) terminal * ncurses_init(void) { + setlocale(LC_ALL, ""); initscr(); raw(); curs_set(0); @@ -2,6 +2,7 @@ #include "fm.h" #include "display.h" #include "err.h" +#include "icon.h" #include "log.h" #include "process.h" #include "utils.h" @@ -323,18 +324,23 @@ fm_draw_entries(window *window, file_list *list, size_t cursor, int top, for (int i = top; i < bottom && (size_t)i < list->count; i++) { int y = i - top; - int color = 0; - if ((size_t)i == cursor) + + const char *icon = + get_file_icon(list->items[i].name, list->items[i].is_dir); + + if ((size_t)i == cursor) { color = COLOR_PAIR_FM_HL; - else if (list->items[i].is_dir) + } else if (list->items[i].is_dir) { color = COLOR_PAIR_FM_DIR; + } if (color != 0) { wattron(window->win, COLOR_PAIR(color)); } - mvwprintw(window->win, y, 0, "%s", list->items[i].name); + mvwprintw(window->win, y, 0, "%s %s", icon, + list->items[i].name); if (color != 0) { wattroff(window->win, COLOR_PAIR(color)); diff --git a/src/icon.c b/src/icon.c new file mode 100644 index 0000000..d131307 --- /dev/null +++ b/src/icon.c @@ -0,0 +1,32 @@ +#include "icon.h" +#include <stdbool.h> +#include <string.h> + +const char * +get_file_icon(const char *filename, bool is_dir) +{ + if (is_dir) { + return DEFAULT_DIR_ICON; + } + + size_t exact_count = + sizeof(exact_match_icons) / sizeof(exact_match_icons[0]); + for (size_t i = 0; i < exact_count; i++) { + if (strcmp(filename, exact_match_icons[i].key) == 0) { + return exact_match_icons[i].icon; + } + } + + const char *ext = strrchr(filename, '.'); + if (ext && ext != filename) { + ext++; // skip "." + size_t ext_count = sizeof(ext_icons) / sizeof(ext_icons[0]); + for (size_t i = 0; i < ext_count; i++) { + if (strcasecmp(ext, ext_icons[i].key) == 0) { + return ext_icons[i].icon; + } + } + } + + return DEFAULT_FILE_ICON; +} diff --git a/src/icon.h b/src/icon.h new file mode 100644 index 0000000..bf276c0 --- /dev/null +++ b/src/icon.h @@ -0,0 +1,27 @@ +#ifndef SF_ICON_H +#define SF_ICON_H + +#include <stdbool.h> + +#define DEFAULT_DIR_ICON "" +#define DEFAULT_FILE_ICON "" + +typedef struct { + const char *key; + const char *icon; // Nerd Font icon +} icon_pair; + +static const icon_pair exact_match_icons[] = { + {"Makefile", ""}, {"Dockerfile", ""}, {".gitignore", ""}, + {".clang-format", ""}, {"README.md", ""}, +}; + +static const icon_pair ext_icons[] = { + {"c", ""}, {"h", ""}, {"cpp", ""}, {"py", ""}, {"rs", ""}, + {"go", ""}, {"sh", ""}, {"org", ""}, {"md", ""}, {"json", ""}, + {"png", ""}, {"jpg", ""}, {"zip", ""}, {"tar", ""}, {"gz", ""}, +}; + +const char *get_file_icon(const char *filename, bool is_dir); + +#endif /* SF_ICON_H */ |
