From: Jonas Fonseca Date: Sun, 9 Nov 2008 20:04:45 +0000 (+0100) Subject: IO API: use select(2) to check if pipe is readable when updating a view X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a271d45efe4aa04ed04467caa9bfdac7305699ac;p=tig.git IO API: use select(2) to check if pipe is readable when updating a view --- diff --git a/NEWS b/NEWS index 0463230..31ef9d5 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ Improvements: - Add bash completion for blame. - Tree view: edit files of the current branch. - Run requests: new identifiers %(directory), %(file), and %(ref) + - Improve responsiveness and view loading speed by using select(2). Bug fixes: diff --git a/tig.c b/tig.c index 36c32cd..544fd85 100644 --- a/tig.c +++ b/tig.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -509,6 +510,18 @@ io_strerror(struct io *io) return strerror(io->error); } +static bool +io_can_read(struct io *io) +{ + struct timeval tv = { 0, 500 }; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(io->pipe, &fds); + + return select(io->pipe + 1, &fds, NULL, NULL, &tv) > 0; +} + static ssize_t io_read(struct io *io, void *buf, size_t bufsize) { @@ -2666,24 +2679,20 @@ update_view(struct view *view) { char out_buffer[BUFSIZ * 2]; char *line; - /* The number of lines to read. If too low it will cause too much - * redrawing (and possible flickering), if too high responsiveness - * will suffer. */ - unsigned long lines = view->height; int redraw_from = -1; + bool can_read = TRUE; if (!view->pipe) return TRUE; + if (!io_can_read(view->pipe)) + return TRUE; + /* Only redraw if lines are visible. */ if (view->offset + view->height >= view->lines) redraw_from = view->lines - view->offset; - /* FIXME: This is probably not perfect for backgrounded views. */ - if (!realloc_lines(view, view->lines + lines)) - goto alloc_error; - - while ((line = io_get(view->pipe, '\n', TRUE))) { + for (; (line = io_get(view->pipe, '\n', can_read)); can_read = FALSE) { size_t linelen = strlen(line); if (opt_iconv != ICONV_NONE) { @@ -2702,17 +2711,15 @@ update_view(struct view *view) } } - if (!view->ops->read(view, line)) + if (!realloc_lines(view, view->lines + 1) || + !view->ops->read(view, line)) goto alloc_error; - - if (lines-- == 1) - break; } { + unsigned long lines = view->lines; int digits; - lines = view->lines; for (digits = 0; lines; digits++) lines /= 10;