(edit_buffer_calc_percent): new editor buffer API.

Modify way to calculate persentage: use byte offers instead of line one.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-07-16 14:09:50 +04:00
parent dc03e9e28d
commit 00f118e963
3 changed files with 31 additions and 3 deletions

View File

@ -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;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -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

View File

@ -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);
}