cex

C/Curses file EXplorer
git clone git://git.wimdupont.com/cex.git
Log | Files | Refs | README | LICENSE

commit ad95a608322bf969248050d14046ed26770040fb
parent 3a77b693550a7c0694176c9d3531d778de9d19c1
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 22 Nov 2025 09:56:27 +0100

truncate path to one line

Diffstat:
Mcex.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/cex.c b/cex.c @@ -109,6 +109,7 @@ static void make_open_default(void); static void make_mime_default(const char *mime, const char *app); static int rm_file(const char *fname); static int rename_file(const char *fname); +static char *truncate_buf_prefix(char *buf, size_t bufsize, size_t max_len); static char *human_readable_bytes(char *buf, intmax_t bytes); static char *prompt_answer(char *buf, size_t size, const char *question); static char *cpstr(char *dest, const char *src); @@ -679,16 +680,22 @@ update_child_win(void) void print_top_title(void) { - move(1, 0); - clrtoeol(); + char path[PATH_MAX + sizeof(curwin.path)]; + move(0, 0); clrtoeol(); + + snprintf(path, sizeof(path), "%s/%s", curwin.path, curwin.winfiles[curwin.highlight].d_name); attron(COLOR_PAIR(TOP_TITLE_COLOR)); - printw("%s:%s/%s", username, curwin.path, curwin.winfiles[curwin.highlight].d_name); #if GIT_INFO + truncate_buf_prefix(path, sizeof(path), maxx - strlen(username) - 2 - strlen(gitbranch)); + printw("%s:%s", username, path); attron(COLOR_PAIR(GIT_BRANCH_COLOR)); printw(gitbranch); attroff(COLOR_PAIR(GIT_BRANCH_COLOR)); +#else + truncate_buf_prefix(path, sizeof(path), maxx - strlen(username) - 2); + printw("%s:%s", username, path); #endif attroff(COLOR_PAIR(TOP_TITLE_COLOR)); } @@ -1288,6 +1295,45 @@ get_st_mode(DirWin *dirwin, size_t index) } char * +truncate_buf_prefix(char *buf, size_t bufsize, size_t max_len) +{ + const char ellipsis[] = ".."; + const size_t ell_len = sizeof(ellipsis) - 1; + size_t keep, src_index, cur_len = 0; + + if (!buf || bufsize == 0 || max_len < ell_len + 2) { + if (bufsize > 0) buf[bufsize - 1] = '\0'; + return buf; + } + + cur_len = strlen(buf); + if (cur_len <= max_len - 1) + return buf; + + keep = max_len - 1 - ell_len; + + if (keep == 0) { + if (max_len >= ell_len + 1) { + memcpy(buf, ellipsis, ell_len); + buf[ell_len] = '\0'; + } else { + buf[0] = '.'; + buf[1] = '\0'; + } + return buf; + } + + src_index = cur_len - keep; + memmove(buf + ell_len, buf + src_index, keep + 1); + memcpy(buf, ellipsis, ell_len); + + if (ell_len + keep >= max_len) + buf[max_len - 1] = '\0'; + + return buf; +} + +char * human_readable_bytes(char *buf, intmax_t bytes) { double dblBytes = bytes; @@ -1815,7 +1861,7 @@ retrieve_git_branch(char *buf, size_t bufsize, const char *repo_path) if (branch_name != NULL) { init_status_options(&opts); - snprintf(buf, bufsize, " (%s%s)", branch_name, is_dirty(repo, &opts) ? " *" : ""); + snprintf(buf, bufsize, " (%s%s)", branch_name, is_dirty(repo, &opts) ? "*" : ""); } git_reference_free(head_ref);