better trimming of lines to the right length in tog diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
diff --git a/tog/Makefile b/tog/Makefile
index 4fd5ba1..cb1a8b9 100644
--- a/tog/Makefile
+++ b/tog/Makefile
@@ -6,7 +6,7 @@ SRCS= tog.c delta.c diff.c diffreg.c error.c fileindex.c object.c \
sha1.c worktree.c zbuf.c
CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
-LDADD = -lpanel -lcurses -lutil -lz
+LDADD = -lpanel -lncursesw -lutil -lz
DPADD = ${LIBZ} ${LIBUTIL}
.if defined(PROFILE)
CC = gcc
diff --git a/tog/tog.c b/tog/tog.c
index 3e8076c..651a162 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -17,7 +17,9 @@
#include <sys/queue.h>
#include <errno.h>
+#define _XOPEN_SOURCE_EXTENDED
#include <curses.h>
+#undef _XOPEN_SOURCE_EXTENDED
#include <panel.h>
#include <locale.h>
#include <stdlib.h>
@@ -28,6 +30,7 @@
#include <unistd.h>
#include <util.h>
#include <limits.h>
+#include <wchar.h>
#include "got_error.h"
#include "got_object.h"
@@ -773,13 +776,70 @@ parse_next_line(FILE *f, size_t *len)
return line;
}
+/* Format a line for display, ensuring that it won't overflow COLS columns. */
+static const struct got_error *
+format_line(wchar_t **wlinep, char *line)
+{
+ const struct got_error *err = NULL;
+ int cols = 0;
+ wchar_t *wline = NULL;
+ size_t wlen;
+ int i;
+
+ *wlinep = NULL;
+
+ wlen = mbstowcs(NULL, line, 0);
+ if (wlen == (size_t)-1)
+ return got_error_from_errno();
+
+ wline = calloc(wlen + 1, sizeof(wchar_t));
+ if (wline == NULL)
+ return got_error_from_errno();
+
+ if (mbstowcs(wline, line, wlen) != wlen) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ i = 0;
+ while (i < wlen && cols <= COLS) {
+ int width = wcwidth(wline[i]);
+ switch (width) {
+ case 0:
+ break;
+ case 1:
+ case 2:
+ cols += width;
+ break;
+ case -1:
+ if (wline[i] == L'\t')
+ cols += TABSIZE;
+ break;
+ default:
+ err = got_error_from_errno();
+ goto done;
+ }
+ if (cols <= COLS)
+ i++;
+ }
+ wline[i] = L'\0';
+done:
+ if (err)
+ free(wline);
+ else
+ *wlinep = wline;
+ return err;
+}
+
static const struct got_error *
draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
int *eof, int max_lines)
{
+ const struct got_error *err;
int nlines = 0, nprinted = 0;
char *line;
size_t len;
+ wchar_t *wline;
rewind(f);
werase(tog_diff_view.window);
@@ -796,9 +856,12 @@ draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
continue;
}
- if (len > COLS - 1)
- line[COLS - 1] = '\0';
- waddstr(tog_diff_view.window, line);
+ err = format_line(&wline, line);
+ if (err) {
+ free(line);
+ return err;
+ }
+ waddwstr(tog_diff_view.window, wline);
waddch(tog_diff_view.window, '\n');
if (++nprinted == 1)
*first_displayed_line = nlines;