From 231731d0790db52eeadb51660137a006494b45ad Mon Sep 17 00:00:00 2001 From: OIX Date: Sun, 3 Jan 2021 08:52:28 +0100 Subject: [PATCH] tweak: optimized size adjustment of titlebar content - prefix & path remain centered in non-editor mode (ie. in file browser), when side spaces are given up - room for 'is modified'-flag is reseved only when required Signed-off-by: OIX --- src/winio.c | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/winio.c b/src/winio.c index 9ed6824..5ebdaa2 100644 --- a/src/winio.c +++ b/src/winio.c @@ -1913,7 +1913,7 @@ void show_states_at(WINDOW *window) * directory or the title of help text, that is: whatever is in path. */ void titlebar(const char *path) { - size_t verlen, prefixlen, pathlen, statelen; + size_t verlen, prefixlen, pathlen = 0, statelen = 0; /* The width of the different title-bar elements, in columns. */ size_t pluglen = 0; /* The width that "Modified" would take up. */ @@ -1976,8 +1976,11 @@ void titlebar(const char *path) if (ISSET(VIEW_MODE)) state = _("View"); #ifndef NANO_TINY - else if (ISSET(STATEFLAGS)) - state = "+.xxxxx"; + else if (ISSET(STATEFLAGS)) { + statelen = 5; // IMLRS + if (openfile->modified) + pathlen = breadth(path) + 2; // _* + } #endif else if (openfile->modified) state = _("Modified"); @@ -1988,14 +1991,18 @@ void titlebar(const char *path) } /* Determine the widths of the four elements, including their padding. */ - verlen = breadth(upperleft) + 3; + verlen = breadth(upperleft); + verlen += verlen ? 3 : 2; // right hand side spaces [+ gap] prefixlen = breadth(prefix); if (prefixlen > 0) prefixlen++; - pathlen = breadth(path); - statelen = breadth(state) + 2; - if (statelen > 2) - pathlen++; + if (!pathlen) + pathlen = breadth(path); + if (!statelen) + statelen = breadth(state); + if (statelen) + pathlen++; // gap between path & state + statelen += 2; // left hand side spaces /* Only print the version message when there is room for it. */ if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS) @@ -2003,29 +2010,33 @@ void titlebar(const char *path) else { verlen = 2; /* If things don't fit yet, give up the placeholder. */ - if (verlen + prefixlen + pathlen + pluglen + statelen > COLS) - pluglen = 0; - /* If things still don't fit, give up the side spaces. */ if (verlen + prefixlen + pathlen + pluglen + statelen > COLS) { - verlen = 0; - statelen -= 2; + pluglen = 0; + /* If things still don't fit, give up the side spaces. */ + if (verlen + prefixlen + pathlen + statelen > COLS) { + verlen = 0; + statelen -= 2; + /* If things are still to large, drop the prefix */ + if (prefixlen + pathlen + statelen > COLS) + prefixlen=0; + } } } free(ranking); /* If we have side spaces left, center the path name. */ - if (verlen > 0) - offset = verlen + (COLS - (verlen + pluglen + statelen) - - (prefixlen + pathlen)) / 2; + offset = verlen + (COLS - (verlen + pluglen + statelen) - + (prefixlen + pathlen)) / 2; + if (offset > COLS || (!verlen && statelen > 2)) offset = 0; /* Only print the prefix when there is room for it. */ - if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS) { + if (prefixlen && verlen + prefixlen + pathlen + pluglen + statelen <= COLS) { mvwaddstr(topwin, 0, offset, prefix); if (prefixlen > 0) waddstr(topwin, " "); } else - wmove(topwin, 0, offset); + wmove(topwin, 0, offset + prefixlen / 2); /* Print the full path if there's room; otherwise, dottify it. */ if (pathlen + pluglen + statelen <= COLS) { @@ -2043,20 +2054,20 @@ void titlebar(const char *path) #ifndef NANO_TINY /* When requested, show on the title bar the state of three options and * the state of the mark and whether a macro is being recorded. */ - if (*state && ISSET(STATEFLAGS) && !ISSET(VIEW_MODE)) { + if (statelen > 2 && ISSET(STATEFLAGS) && !ISSET(VIEW_MODE)) { if (openfile->modified && COLS > 1) waddstr(topwin, " *"); if (statelen < COLS) { - wmove(topwin, 0, COLS + 2 - statelen); + wmove(topwin, 0, COLS - statelen); show_states_at(topwin); } } else #endif - { + if (statelen) { /* If there's room, right-align the state word; otherwise, clip it. */ - if (statelen > 0 && statelen <= COLS) + if (statelen <= COLS) mvwaddstr(topwin, 0, COLS - statelen, state); - else if (statelen > 0) + else mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS)); } -- 2.7.4