From 061b603616bc182bd0e8aa0d2cb2eb8eaef569a1 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Tue, 13 Mar 2018 03:03:26 -0600 Subject: [PATCH] add vertical scroll arrows Signed-off-by: Brand Huntsman --- src/proto.h | 1 + src/utils.c | 6 ++++ src/winio.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/proto.h b/src/proto.h index 551aa650..c2d68e24 100644 --- a/src/proto.h +++ b/src/proto.h @@ -615,6 +615,7 @@ filestruct *fsfromline(ssize_t lineno); #ifdef DEBUG void dump_filestruct(const filestruct *inptr); #endif +int nroundf(float value); /* Most functions in winio.c. */ void record_macro(void); diff --git a/src/utils.c b/src/utils.c index 91f279a4..61587e87 100644 --- a/src/utils.c +++ b/src/utils.c @@ -605,3 +605,9 @@ void dump_filestruct(const filestruct *inptr) } } #endif /* DEBUG */ + +/* Round float to int without using libm. */ +int nroundf(float value) +{ + return (value > 0 ? (int)(value + 0.5) : (int)(value - 0.4999999)); +} diff --git a/src/winio.c b/src/winio.c index 9257fe79..9774c0b0 100644 --- a/src/winio.c +++ b/src/winio.c @@ -1986,6 +1986,56 @@ int buffer_number(openfilestruct *buffer) return count; } +#define SCROLLBAR_LEN 4 + /* The number of cells always reserved for scrollbar. */ + +/* Draw up/down arrows or percentage on right side of titlebar. */ +static void scrollbar(bool refresh){ + bool at_top = (openfile->edittop == openfile->fileage); + bool at_bot = (openfile->edittop->lineno + editwinrows - 1 >= + openfile->filebot->lineno); + + wattron(topwin, interface_color_pair[TITLE_BAR]); + + wmove(topwin, 0, COLS - 3); + + if (at_top && at_bot) { + waddstr(topwin, " "); + } else if (at_top) { +#ifdef ENABLE_UTF8 + if(using_utf8()) + waddstr(topwin, " ▼ "); + else +#endif + waddstr(topwin, " + "); +// waddstr(topwin, " v "); + } else if (at_bot) { +#ifdef ENABLE_UTF8 + if(using_utf8()) + waddstr(topwin, " ▲ "); + else +#endif + waddstr(topwin, " - "); +// waddstr(topwin, " ^ "); + } else { + char pctstr[4]; + int pct = nroundf(100.0 * openfile->edittop->lineno / + (openfile->filebot->lineno - editwinrows)); + if (pct < 10) + snprintf(pctstr, sizeof(pctstr), " %d ", pct); + else if (pct < 100) + snprintf(pctstr, sizeof(pctstr), "%d ", pct); + else + snprintf(pctstr, sizeof(pctstr), "100"); + waddstr(topwin, pctstr); + } + + wattroff(topwin, interface_color_pair[TITLE_BAR]); + + if(refresh) + wrefresh(topwin); +} + /* If path is NULL, we're in normal editing mode, so display the current * version of nano, the current filename, and whether the current file * has been modified on the titlebar. If path isn't NULL, we're either @@ -2058,36 +2108,35 @@ void titlebar(const char *path) if (prefixlen > 0) prefixlen++; pathlen = strlenpt(path); - statelen = strlenpt(state) + 2; - if (statelen > 2) { + statelen = strlenpt(state); + if (statelen > 0) { pathlen++; pluglen = 0; } /* Only print the version message when there is room for it. */ - if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS) + if (verlen + prefixlen + pathlen + pluglen + statelen + SCROLLBAR_LEN <= COLS) mvwaddstr(topwin, 0, 2, upperleft); else { verlen = 2; /* If things don't fit yet, give up the placeholder. */ - if (verlen + prefixlen + pathlen + pluglen + statelen > COLS) + if (verlen + prefixlen + pathlen + pluglen + statelen + SCROLLBAR_LEN > COLS) pluglen = 0; - /* If things still don't fit, give up the side spaces. */ - if (verlen + prefixlen + pathlen + pluglen + statelen > COLS) { + /* If things still don't fit, give up the left side space. */ + if (verlen + prefixlen + pathlen + pluglen + statelen + SCROLLBAR_LEN > COLS) verlen = 0; - statelen -= 2; - } } free(indicator); /* 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 + SCROLLBAR_LEN) - + (prefixlen + pathlen)) / 2; /* Only print the prefix when there is room for it. */ - if (verlen + prefixlen + pathlen + pluglen + statelen <= COLS) { + if (verlen + prefixlen + pathlen + pluglen + statelen + SCROLLBAR_LEN <= COLS) { mvwaddstr(topwin, 0, offset, prefix); if (prefixlen > 0) waddstr(topwin, " "); @@ -2095,26 +2144,29 @@ void titlebar(const char *path) wmove(topwin, 0, offset); /* Print the full path if there's room; otherwise, dottify it. */ - if (pathlen + pluglen + statelen <= COLS) { + if (pathlen + pluglen + statelen + SCROLLBAR_LEN <= COLS) { caption = display_string(path, 0, pathlen, FALSE); waddstr(topwin, caption); free(caption); - } else if (5 + statelen <= COLS) { + } else if (5 + statelen + SCROLLBAR_LEN <= COLS) { waddstr(topwin, "..."); - caption = display_string(path, 3 + pathlen - COLS + statelen, - COLS - statelen, FALSE); + caption = display_string(path, + 3 + pathlen - COLS + statelen + SCROLLBAR_LEN, + COLS - statelen - SCROLLBAR_LEN, FALSE); waddstr(topwin, caption); free(caption); } /* Right-align the state if there's room; otherwise, trim it. */ - if (statelen > 0 && statelen <= COLS) - mvwaddstr(topwin, 0, COLS - statelen, state); + if (statelen > 0 && statelen + SCROLLBAR_LEN <= COLS) + mvwaddstr(topwin, 0, COLS - (statelen + SCROLLBAR_LEN), state); else if (statelen > 0) mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS)); wattroff(topwin, interface_color_pair[TITLE_BAR]); + scrollbar(FALSE); + wrefresh(topwin); } @@ -3207,6 +3259,8 @@ void edit_redraw(filestruct *old_current, update_type manner) (old_current != openfile->current && get_page_start(openfile->placewewant) > 0)) update_line(openfile->current, openfile->current_x); + + scrollbar(TRUE); } /* Refresh the screen without changing the position of lines. Use this @@ -3239,6 +3293,8 @@ void edit_refresh(void) while (row < editwinrows) blank_row(edit, row++, 0, COLS); + scrollbar(TRUE); + place_the_cursor(); wnoutrefresh(edit); @@ -3300,6 +3356,8 @@ void total_refresh(void) edit_refresh(); wipe_statusbar(); bottombars(currmenu); + + scrollbar(TRUE); } /* Display the main shortcut list on the last two rows of the bottom -- 2.16.1