From 8bf8f5dc46ab25fd8e5d9817097e3147d179c375 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 14 Jan 2025 22:30:03 +0530 Subject: [PATCH] Finish up testing for multicell URL detection --- kitty/line.c | 2 +- kitty/line.h | 2 -- kitty/screen.c | 2 +- kitty_tests/multicell.py | 3 +- kitty_tests/screen.py | 74 +++++++++++++++++++++++----------------- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/kitty/line.c b/kitty/line.c index b519b8d0d..9327ffd85 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -309,7 +309,7 @@ line_url_end_at(Line *self, index_type x, bool check_short, char_type sentinel, ans = n; } #undef is_not_ok - if (ans < self->xnum - 1 || !next_line_starts_with_url_chars) { + if (next_char_pos(self, ans, 1) < self->xnum || !next_line_starts_with_url_chars) { while (ans > x && !self->cpu_cells[ans].ch_is_idx && can_strip_from_end_of_url(self->cpu_cells[ans].ch_or_idx)) { n = prev_char_pos(self, ans, 1); if (n >= self->xnum || n < x) break; diff --git a/kitty/line.h b/kitty/line.h index 1485f5a66..d086f6a74 100644 --- a/kitty/line.h +++ b/kitty/line.h @@ -9,8 +9,6 @@ #include "text-cache.h" -// TODO: URL detection with multicell - typedef union CellAttrs { struct { uint16_t decoration : 3; diff --git a/kitty/screen.c b/kitty/screen.c index 38bc24896..7570e4c9f 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -3714,7 +3714,7 @@ extend_url(Screen *screen, Line *line, index_type *x, index_type *y, char_type s while (count++ < 10) { bool in_hostname = last_hostname_char_pos >= line->xnum; has_newline = !line->cpu_cells[line->xnum-1].next_char_was_wrapped; - if (*x != line->xnum - 1 || (!newlines_allowed && has_newline)) break; + if (next_char_pos(line, *x, 1) < line->xnum || (!newlines_allowed && has_newline)) break; bool next_line_starts_with_url_chars = false; line = screen_visual_line(screen, *y + 2 * scale); if (line) { diff --git a/kitty_tests/multicell.py b/kitty_tests/multicell.py index 7236f2f28..790d6e53b 100644 --- a/kitty_tests/multicell.py +++ b/kitty_tests/multicell.py @@ -681,7 +681,7 @@ def set_link(url=None, id=None): self.ae(s.current_url_text(), 'ab') # URL detection - s = self.create_screen(cols=40) + s = self.create_screen(cols=60) s.reset() url = 'http://moo.com' @@ -689,3 +689,4 @@ def set_link(url=None, id=None): s.detect_url(0, 0) self.ae(s.current_url_text(), url) asu((0, 0, len(url)*2 - 1), (1, 0, len(url)*2 - 1)) + # More tests for URL detection are in screen.py in detect_url() diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 865cd8ff6..e3d040111 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -7,7 +7,7 @@ from kitty.rgb import color_names from kitty.window import pagerhist -from . import BaseTest, parse_bytes +from . import BaseTest, draw_multicell, parse_bytes class TestScreen(BaseTest): @@ -1112,37 +1112,8 @@ def ac(idx, count): ac(9, 10) def test_detect_url(self): - s = self.create_screen(cols=30) - - def ae(expected, x=3, y=0): - s.detect_url(x, y) - url = ''.join(s.text_for_marked_url()) - self.assertEqual(expected, url) - - def t(url, x=0, y=0, before='', after='', expected=''): - s.reset() - s.cursor.x = x - s.cursor.y = y - s.draw(before + url + after) - ae(expected or url, x=x + 1 + len(before), y=y) - - - t('http://moo.com') - t('http://moo.com/something?else=+&what-') - t('http://moo.com#fragme') - for (st, e) in '() {} [] <>'.split(): - t('http://moo.com', before=st, after=e) - for trailer in ')-=': - t('http://moo.com' + trailer) - for trailer in '{}([<>': # )]> - t('http://moo.com', after=trailer) - t('http://moo.com', x=s.columns - 9) - t('https://wraps-by-one-char.com', before='[', after=']') - t('http://[::1]:8080') - t('https://wr[aps-by-one-ch]ar.com') - t('http://[::1]:8080/x', after='[') # ] - t('http://[::1]:8080/x]y34', expected='http://[::1]:8080/x') - t('https://wraps-by-one-char.com[]/x', after='[') # ] + detect_url(self) + detect_url(self, scale=2) def test_prompt_marking(self): # ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}})))))))))))))))))))))) @@ -1376,3 +1347,42 @@ def q(send, expected=None): q({'transparent_background_color2': '?'}, {'transparent_background_color2': (Color(255, 0, 0), 126)}) q({'transparent_background_color2': '#ffffff@-1'}) q({'transparent_background_color2': '?'}, {'transparent_background_color2': (Color(255, 255, 255), 255)}) + + +def detect_url(self, scale=1): + s = self.create_screen(cols=30 * scale) + + def ae(expected, x=3, y=0): + s.detect_url(x * scale, y * scale) + url = ''.join(s.text_for_marked_url()) + self.assertEqual(expected, url) + + def t(url, x=0, y=0, before='', after='', expected=''): + s.reset() + s.cursor.x = x + s.cursor.y = y + text = before + url + after + if scale == 1: + s.draw(text) + else: + draw_multicell(s, text, scale=scale) + ae(expected or url, x=x + 1 + len(before), y=y) + + + t('http://moo.com') + t('http://moo.com/something?else=+&what-') + t('http://moo.com#fragme') + for (st, e) in '() {} [] <>'.split(): + t('http://moo.com', before=st, after=e) + for trailer in ')-=': + t('http://moo.com' + trailer) + for trailer in '{}([<>': # )]> + t('http://moo.com', after=trailer) + if scale == 1: + t('http://moo.com', x=s.columns - 9) + t('https://wraps-by-one-char.com', before='[', after=']') + t('http://[::1]:8080') + t('https://wr[aps-by-one-ch]ar.com') + t('http://[::1]:8080/x', after='[') # ] + t('http://[::1]:8080/x]y34', expected='http://[::1]:8080/x') + t('https://wraps-by-one-char.com[]/x', after='[') # ]