diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c index 3d1301f28..8446f1529 100644 --- a/src/editor/editbuffer.c +++ b/src/editor/editbuffer.c @@ -778,3 +778,30 @@ edit_buffer_write_file (edit_buffer_t * buf, int fd) } /* --------------------------------------------------------------------------------------------- */ +/** + * Calculate percentage of specified character offset + * + * @param buf pointer to editor buffer + * @param p character offset + * + * @return percentage of specified character offset + */ + +int +edit_buffer_calc_percent (const edit_buffer_t * buf, off_t offset) +{ + int percent; + + if (buf->size == 0) + percent = 0; + else if (offset >= buf->size) + percent = 100; + else if (offset > (INT_MAX / 100)) + percent = offset / (buf->size / 100); + else + percent = offset * 100 / buf->size; + + return percent; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h index a55dd5c9d..2e55e9c5c 100644 --- a/src/editor/editbuffer.h +++ b/src/editor/editbuffer.h @@ -51,6 +51,8 @@ off_t edit_buffer_move_backward (const edit_buffer_t * buf, off_t current, long off_t edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size); off_t edit_buffer_write_file (edit_buffer_t * buf, int fd); +int edit_buffer_calc_percent (const edit_buffer_t * buf, off_t offset); + /*** inline functions ****************************************************************************/ static inline int diff --git a/src/editor/editdraw.c b/src/editor/editdraw.c index bea6f97e7..ab6c2e326 100644 --- a/src/editor/editdraw.c +++ b/src/editor/editdraw.c @@ -232,10 +232,9 @@ edit_status_fullscreen (WEdit * edit, int color) if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH) { - size_t percent = 100; + int percent; - if (edit->buffer.lines + 1 != 0) - percent = (edit->buffer.curs_line + 1) * 100 / (edit->buffer.lines + 1); + percent = edit_buffer_calc_percent (&edit->buffer, edit->buffer.curs1); widget_move (h, 0, w - 6 - 6); tty_printf (" %3d%%", percent); }