From de23616589a1db455b4d3d1a8456654f36bc0e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 5 Dec 2022 13:45:47 +0100 Subject: [PATCH] better line expansion --- .../src/WebLinkProvider.ts | 67 +++++++++++-------- .../src/WebLinksAddon.ts | 3 +- bin/test_weblinks.sh | 3 + 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/addons/xterm-addon-web-links/src/WebLinkProvider.ts b/addons/xterm-addon-web-links/src/WebLinkProvider.ts index 0dd4c9be..541f614f 100644 --- a/addons/xterm-addon-web-links/src/WebLinkProvider.ts +++ b/addons/xterm-addon-web-links/src/WebLinkProvider.ts @@ -45,21 +45,14 @@ export class LinkComputer { public static computeLink(y: number, regex: RegExp, terminal: Terminal, activate: (event: MouseEvent, uri: string) => void): ILink[] { const rex = new RegExp(regex.source, (regex.flags || '') + 'g'); - const [lines, startLineIndex] = LinkComputer._getFullLineString(y - 1, terminal); + const [lines, startLineIndex] = LinkComputer._getWindowedLineStrings(y - 1, terminal); const line = lines.join(''); - // Don't try if the wrapped line if excessively large as the regex matching will block the main - // thread. - if (line.length > 1024) { - // TODO: more sophisticated handling with individual line introspection? - return []; - } - let match; let stringIndex = -1; const result: ILink[] = []; - while ((match = rex.exec(line)) !== null) { + while (match = rex.exec(line)) { const text = match[0]; // Get index, match.index is for the outer match which includes negated chars @@ -117,29 +110,49 @@ export class LinkComputer { } /** - * Gets the entire line for the buffer line - * @param lineIndex The index of the line being translated. + * Get wrapped content lines for the current line index. + * The top/bottom line expansion stops at whitespaces or length > 2048. + * Returns an array with line strings and the top line index. */ - private static _getFullLineString(lineIndex: number, terminal: Terminal): [string[], number] { + private static _getWindowedLineStrings(lineIndex: number, terminal: Terminal): [string[], number] { let line: any; - - // expand top let topIdx = lineIndex; - while ((line = terminal.buffer.active.getLine(topIdx)) && line.isWrapped) { - topIdx--; - } - - // expand bottom - let bottomIdx = lineIndex + 1; - while ((line = terminal.buffer.active.getLine(bottomIdx)) && line.isWrapped) { - bottomIdx++; - } - + let bottomIdx = lineIndex; + let length = 0; + let content = ''; const lines: string[] = []; - for (let idx = topIdx; idx < bottomIdx; ++idx) { - lines.push(terminal.buffer.active.getLine(idx)?.translateToString(true)!); - } + if ((line = terminal.buffer.active.getLine(lineIndex))) { + const currentContent = line.translateToString(true); + + // expand top, stop on whitespaces or length > 2048 + if (line.isWrapped && currentContent[0] !== ' ') { + length = 0; + while ((line = terminal.buffer.active.getLine(--topIdx)) && length < 2048) { + content = line.translateToString(true); + length += content.length; + lines.push(content); + if (!line.isWrapped || content.indexOf(' ') !== -1) { + break; + } + } + lines.reverse(); + } + + // append current line + lines.push(currentContent); + + // expand bottom, stop on whitespaces or length > 2048 + length = 0; + while ((line = terminal.buffer.active.getLine(++bottomIdx)) && line.isWrapped && length < 2048) { + content = line.translateToString(true); + length += content.length; + lines.push(content); + if (content.indexOf(' ') !== -1) { + break; + } + } + } return [lines, topIdx]; } diff --git a/addons/xterm-addon-web-links/src/WebLinksAddon.ts b/addons/xterm-addon-web-links/src/WebLinksAddon.ts index ccf45e74..44dabd45 100644 --- a/addons/xterm-addon-web-links/src/WebLinksAddon.ts +++ b/addons/xterm-addon-web-links/src/WebLinksAddon.ts @@ -10,7 +10,8 @@ import { ILinkProviderOptions, WebLinkProvider } from './WebLinkProvider'; // up to first whitespace, `"` or `'` as url // NOTE: The repeated end clause is needed to not match a dangling `:` // resembling the old (...)*([^:"\'\\s]) final path clause -const strictUrlRegex = /https?:[/]{2}[^\s^"^']*[^\s^"^'^:]/; +// also exclude final interpunction like ,.!? +const strictUrlRegex = /https?:[/]{2}[^\s^"^']*[^\s^"^'^:^,^.^!^?]/; function handleLink(event: MouseEvent, uri: string): void { const newWindow = window.open(); diff --git a/bin/test_weblinks.sh b/bin/test_weblinks.sh index a50db788..4488b983 100755 --- a/bin/test_weblinks.sh +++ b/bin/test_weblinks.sh @@ -20,3 +20,6 @@ echo "¥¥¥ https://ko.wikipedia.org/wiki/위키백과:대문 aaa https://ko # not matching at all echo "http://test:password@example.com/some_path" + +# overly long text with urls with final interpunction +echo "Lorem ipsum dolor sit amet, consetetur sadipscing elitr https://ko.wikipedia.org/wiki/위키백과:대문, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat: http://test:password@example.com/some_path." \ No newline at end of file