Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit 1a30ccff635e8bebb32c8dd361194e1d4cd3556e Author: rani <clagv.randomgames@gmail.com> Date: Sun Jan 11 23:35:40 2026 +0000 Add: replace home directory prefix in CWD with '~' diff --git a/include/dir.h b/include/dir.h index e01ac22..9de5e89 100644 --- a/include/dir.h +++ b/include/dir.h @@ -846 +847 @@ const char * dir_basename(const char * path);  size_t dir_len(const dir_t * dir);  cvector(dirent_t) dir_entries(const dir_t * dir);  void dir_sort(const dir_t * dir); +const char * dir_get_home(void);    char dirent_crepr(const dirent_t * de); // character representing dir entry  char dirent_creprl(const dirent_t * de); // like above, but for the link diff --git a/include/ui.h b/include/ui.h index 0f8214b..e2fa624 100644 --- a/include/ui.h +++ b/include/ui.h @@ -567 +567 @@ typedef const char * const prompt_opts_t[];  void ui_init(void);  void ui_deinit(void);  void ui_reinit(void); -void ui_set_title(const char * title); +void ui_set_title(const char * title, const char * home);  void ui_status_info(const char * status);  void ui_status_error(const char * status);  void ui_erase(void); diff --git a/src/dir.c b/src/dir.c index 7b22d2f..9010ccc 100644 --- a/src/dir.c +++ b/src/dir.c @@ -5843 +58440 @@ int dirent_open(const dirent_t * de) {   waitpid(pid, NULL, 0);   return 0;  } + +static void dir_normalize_path(char * path) { + if (!path || !*path) return; + + char * base = path + 1; + // remove duplicate separators + for (char * p = base; *p; p++) { + // a + a'b = a + b + if (*p != '/' || *(base - 1) != '/') + *base++ = *p; + } + *base = 0; + + // remove trailing separators + while (base > path + 1 && *(base - 1) == '/') { + *(base - 1) = 0; + base--; + } +} + +const char * dir_get_home(void) { + static char home_buf[PATH_MAX+1]; + + uid_t uid = getuid(); + struct passwd * pw = getpwuid(uid); + if (!pw) { + const char * envhome = getenv("HOME"); + if (!envhome) return NULL; + strncpy(home_buf, envhome, PATH_MAX); + home_buf[PATH_MAX] = 0; + + if (!home_buf[0]) return NULL; // idk but HOME should be absolute + dir_normalize_path(home_buf); + return home_buf; + } + return pw->pw_dir; +} diff --git a/src/main.c b/src/main.c index 0a4e8f5..9a6f85b 100644 --- a/src/main.c +++ b/src/main.c @@ -15 +16 @@  #include <errno.h>  #include <stdio.h> +#include <string.h>  #include <stddef.h>  #include <ncurses.h>  #include <stdbool.h> @@ -116 +129 @@    int main(int argc, char ** argv) {   const char * cwd = dir_get_cwd(); + // dir_get_home may return a static field so it must be copied here + char * home = (char*)dir_get_home(); + if (home) home = strdup(home);     dir_t dir;   dir_list(cwd, &dir); @@ -247 +287 @@ int main(int argc, char ** argv) {   size_t dirlen = dir_len(&dir);   cvector(dirent_t) entries = dir_entries(&dir);   - ui_set_title(cwd); + ui_set_title(cwd, home);   ui_print_dir(&dir, cursor);   ui_print_cursor(cursor, dirlen);   @@ -1836 +1877 @@ int main(int argc, char ** argv) {   }    finished: + free(home);   dir_free(&dir);   ui_deinit();  } diff --git a/src/ui.c b/src/ui.c index 75c7f09..261d245 100644 --- a/src/ui.c +++ b/src/ui.c @@ -1078 +10715 @@ void win_set(WINDOW * win, const char * str, int attrs) {   wattroff(win, attrs);  }   -void ui_set_title(const char * title) { - win_set(titlewin, title, 0); +void ui_set_title(const char * title, const char * home) { + size_t homelen = strlen(home); + if (strncmp(title, home, homelen) == 0) { + title += homelen; + win_set(titlewin, "~", 0); + waddstr(titlewin, title); + } else { + win_set(titlewin, title, 0); + }  }    void ui_status_info(const char * status) {