From 76b479661ae9992932044f9137f2f6d89d9e078b Mon Sep 17 00:00:00 2001 From: verdant Date: Tue, 28 Jul 2026 17:37:44 +0800 Subject: Print permission infomation on the status bar --- display.c | 1 + display.h | 1 + fm.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- fm.h | 5 ++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/display.c b/display.c index 13c19c0..e7dd031 100644 --- a/display.c +++ b/display.c @@ -12,6 +12,7 @@ color_pair_init(void) { init_pair(COLOR_PAIR_FM_HL, COLOR_BLACK, COLOR_WHITE); init_pair(COLOR_PAIR_FM_DIR, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_PAIR_FM_STATUS_BAR, COLOR_BLUE, COLOR_BLACK); } terminal * diff --git a/display.h b/display.h index 64e4ff6..b5437d2 100644 --- a/display.h +++ b/display.h @@ -10,6 +10,7 @@ #define COLOR_PAIR_FM_HL 1 #define COLOR_PAIR_FM_DIR 2 +#define COLOR_PAIR_FM_STATUS_BAR 3 #define STAT_INFO_WIDTH 15 #define BAR_LABEL_WIDTH 7 diff --git a/fm.c b/fm.c index 28a7ff3..5a308fd 100644 --- a/fm.c +++ b/fm.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,36 @@ fm_file_items_realloc(file_list *l) l->items = xrealloc(l->items, sizeof(file_item) * l->cap); } +void +get_permission(mode_t mode, char *str) +{ + if (S_ISDIR(mode)) + str[0] = 'd'; /* Directory */ + else if (S_ISLNK(mode)) + str[0] = 'l'; /* Link */ + else if (S_ISCHR(mode)) + str[0] = 'c'; /* Charcter devices */ + else if (S_ISBLK(mode)) + str[0] = 'b'; /* Block devices */ + else if (S_ISFIFO(mode)) + str[0] = 'p'; /* FIFO */ + else if (S_ISSOCK(mode)) + str[0] = 's'; /* Socket */ + else + str[0] = '-'; /* Files */ + + str[1] = (mode & S_IRUSR) ? 'r' : '-'; + str[2] = (mode & S_IWUSR) ? 'w' : '-'; + str[3] = (mode & S_IXUSR) ? 'x' : '-'; + str[4] = (mode & S_IRGRP) ? 'r' : '-'; + str[5] = (mode & S_IWGRP) ? 'w' : '-'; + str[6] = (mode & S_IXGRP) ? 'x' : '-'; + str[7] = (mode & S_IROTH) ? 'r' : '-'; + str[8] = (mode & S_IWOTH) ? 'w' : '-'; + str[9] = (mode & S_IXOTH) ? 'x' : '-'; + str[10] = '\0'; +} + file_list * fm_getdir(file_list *list, const char *path) { @@ -100,6 +131,8 @@ fm_getdir(file_list *list, const char *path) } struct dirent *d; + struct stat st; + char filepath[PATH_MAX]; while ((d = readdir(dir)) != NULL) { if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue; @@ -117,6 +150,15 @@ fm_getdir(file_list *list, const char *path) new_list->items[new_list->count].is_dir = false; } + memset(filepath, 0, sizeof(filepath)); + snprintf(filepath, sizeof(filepath), "%s/%s", path, d->d_name); + + if (lstat(filepath, &st) == 0) { + get_permission( + st.st_mode, + new_list->items[new_list->count].permission); + } + new_list->count++; } @@ -414,9 +456,11 @@ create_status_bar_win(terminal *t) } void -draw_status_bar(window *bar_win, const char *cwd) +draw_status_bar(window *bar_win, const char *cwd, char permission[11]) { werase(bar_win->win); - - mvwprintw(bar_win->win, bar_win->height - 1, 0, "%s", cwd); + wattron(bar_win->win, COLOR_PAIR(COLOR_PAIR_FM_STATUS_BAR)); + mvwprintw(bar_win->win, bar_win->height - 1, 0, "%s %s", cwd, + permission); + wattroff(bar_win->win, COLOR_PAIR(COLOR_PAIR_FM_STATUS_BAR)); } diff --git a/fm.h b/fm.h index b116a2d..493d1f9 100644 --- a/fm.h +++ b/fm.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "wm.h" @@ -16,6 +17,7 @@ typedef struct { char name[256]; int is_dir; + char permission[11]; } file_item; typedef struct { @@ -62,5 +64,6 @@ int open_with_editor(char **argv); void magic_init(void); void magic_cleanup(void); void create_status_bar_win(terminal *t); -void draw_status_bar(window *bar_win, const char *cwd); +void draw_status_bar(window *bar_win, const char *cwd, char permission[11]); + #endif /* SF_FM_H */ -- cgit v1.2.3