Skip to content

Commit

Permalink
Fix wrapping of lines with multibyte characters
Browse files Browse the repository at this point in the history
Fixes jonas#988
  • Loading branch information
koutcher committed Apr 12, 2020
1 parent b2694de commit 484aa21
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 51 deletions.
7 changes: 7 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release notes
=============

master
------

Bug fixes:

- Fix wrapping of lines with multibyte characters. (#988)

tig-2.5.1
---------
Expand Down
1 change: 0 additions & 1 deletion include/tig/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void string_copy_rev_from_commit_line(char *dst, const char *src);
#define string_concat_path(dst, path1, path2) \
string_format(dst, !*path1 || path1[strlen(path1) - 1] == '/' ? "%s%s" : "%s/%s", path1, path2)

size_t string_expanded_length(const char *src, size_t srclen, size_t tabsize, size_t max_size);
size_t string_expand(char *dst, size_t dstlen, const char *src, int srclen, int tabsize);

char *string_trim_end(char *name);
Expand Down
4 changes: 3 additions & 1 deletion src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ pager_wrap_line(struct view *view, const char *data, enum line_type type)
size_t lineno = 0;

while (datalen > 0 || !has_first_line) {
int width;
int trimmed;
bool wrapped = !!first_line;
size_t linelen = string_expanded_length(data, datalen, opt_tab_size, view->width - !!wrapped);
size_t linelen = utf8_length(&data, datalen, 0, &width, view->width, &trimmed, wrapped, opt_tab_size);
struct line *line;

line = add_line_text_at_(view, view->lines, data, linelen, type, 1, wrapped);
Expand Down
21 changes: 2 additions & 19 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,6 @@ string_copy_rev_from_commit_line(char *dst, const char *src)
string_copy_rev(dst, src + STRING_SIZE("commit "));
}

size_t
string_expanded_length(const char *src, size_t srclen, size_t tabsize, size_t max_size)
{
size_t size, pos;

for (size = pos = 0; pos < srclen && size < max_size; pos++) {
if (src[pos] == '\t') {
size_t expanded = tabsize - (size % tabsize);

size += expanded;
} else {
size++;
}
}

return pos;
}

size_t
string_expand(char *dst, size_t dstlen, const char *src, int srclen, int tabsize)
{
Expand Down Expand Up @@ -330,7 +312,8 @@ utf8_length(const char **start, int max_chars, size_t skip, int *width, size_t m
if (!unicode)
break;

ucwidth = unicode_width(unicode, tab_size);
ucwidth = unicode == '\t' ? tab_size - (*width % tab_size) :
utf8proc_charwidth((utf8proc_int32_t) unicode);
if (skip > 0) {
skip -= ucwidth <= skip ? ucwidth : skip;
*start += bytes;
Expand Down
67 changes: 37 additions & 30 deletions test/blob/wrap-lines-test
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,45 @@ set line-graphics = utf-8
set wrap-lines = true
EOF

in_work_dir create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"
git_init

test_setup_work_dir()
{
printf 'Как пуст, и вял, и ничтожен почти всякий прожитой день! Как мало следов оставляет он за собою! Как бессмысленно глупо пробежали эти часы за часами!\tИван Сергеевич Тургенев\n' >README.md
git add README.md
git_commit -m "tomorrow"
}

test_tig

assert_equals 'wrapped.screen' <<EOF
# Scala.js Benchmarks
This is a port of the Dart
[benchmark harness](https://github.com/d
+art-lang/benchmark_harness) to
[Scala.js](https://github.com/scala-js/s
+cala-js).
You can see the benchmarks in action
[here](http://jonas.github.io/scala-js-b
+enchmarks/).
All derivative work is the copyright of
+their respective authors and
distributed under their original license
+. All original work unless otherwise
stated is distributed under the [same li
+cense as
Scala.js](https://github.com/jonas/scala
+-js-benchmarks/LICENSE).
## Get started
To run the benchmarks, first install the
+ Scala.js compiler by following the
instructions in the [Scala.js README](ht
+tps://github.com/lampepfl/scala-js).
[blob] README.md - line 1 of 62 28%
Как пуст, и вял, и ничтожен почти всякий
прожитой день! Как мало следов оставляе
т он за собою! Как бессмысленно глупо пр
обежали эти часы за часами! Иван С
геевич Тургенев
[blob] README.md - line 1 of 5 100%
EOF

0 comments on commit 484aa21

Please sign in to comment.