Skip to content

Commit

Permalink
IO API: use select(2) to check if pipe is readable when updating a view
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas committed Jan 13, 2009
1 parent 18e2a6a commit a271d45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -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:

Expand Down
35 changes: 21 additions & 14 deletions tig.c
Expand Up @@ -34,6 +34,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand Down

0 comments on commit a271d45

Please sign in to comment.