feat: OSC 8 hyperlinks in terminals
Cells carrying an OSC 8 hyperlink get a dotted underline, and Ctrl/Cmd+ click opens the link. Because a link's text can say anything, hovering one always shows its real target (and says when it won't be opened). Both OSC 8 and plain-text URLs now open through the web-only check, so remote output can't launch file:, ftp: or custom-scheme handlers (plain text ftp:// links used to be opened).
This commit is contained in:
+1
-1
@@ -69,7 +69,7 @@ Use Nexus Terminal when you need to:
|
|||||||
- Find in terminal output, with case-sensitive and regular-expression options and a match counter.
|
- Find in terminal output, with case-sensitive and regular-expression options and a match counter.
|
||||||
- Copy with Ctrl/Cmd+C when text is selected (otherwise it sends an interrupt), paste with Ctrl/Cmd+V.
|
- Copy with Ctrl/Cmd+C when text is selected (otherwise it sends an interrupt), paste with Ctrl/Cmd+V.
|
||||||
- Double-click selects a word, triple-click selects a line, Shift-click extends a selection.
|
- Double-click selects a word, triple-click selects a line, Shift-click extends a selection.
|
||||||
- Ctrl/Cmd-click a web link to open it in your browser.
|
- Ctrl/Cmd-click a web link to open it in your browser, including OSC 8 hyperlinks (from `ls --hyperlink`, `git`, compilers…), which are underlined with a dotted line and show their real target on hover. Only http(s) links open.
|
||||||
- Shift+PageUp/PageDown/Home/End and the mouse wheel scroll the scrollback; on the alternate screen the wheel sends arrow keys, so `tmux`, `screen` and `byobu` scroll naturally.
|
- Shift+PageUp/PageDown/Home/End and the mouse wheel scroll the scrollback; on the alternate screen the wheel sends arrow keys, so `tmux`, `screen` and `byobu` scroll naturally.
|
||||||
- Context menu with Copy, Paste, Select All, Find, Send Snippet, Port Forwards, Split Right, Split Down, Export Output, Clear and Reset.
|
- Context menu with Copy, Paste, Select All, Find, Send Snippet, Port Forwards, Split Right, Split Down, Export Output, Clear and Reset.
|
||||||
- Export the whole scrollback as plain text.
|
- Export the whole scrollback as plain text.
|
||||||
|
|||||||
+1
-1
@@ -249,7 +249,7 @@ Shells that also report their working directory (OSC 7; fish, and zsh/bash with
|
|||||||
- **Copy**: select text and press **Cmd+C** / **Ctrl+C**. With nothing selected, **Ctrl+C** sends an interrupt as usual.
|
- **Copy**: select text and press **Cmd+C** / **Ctrl+C**. With nothing selected, **Ctrl+C** sends an interrupt as usual.
|
||||||
- **Paste**: **Cmd+V** / **Ctrl+V**. Pastes are bracketed when the remote application asks for it.
|
- **Paste**: **Cmd+V** / **Ctrl+V**. Pastes are bracketed when the remote application asks for it.
|
||||||
- **Select**: drag to select, double-click for a word, triple-click for a line, Shift-click to extend.
|
- **Select**: drag to select, double-click for a word, triple-click for a line, Shift-click to extend.
|
||||||
- **Links**: hold **Cmd** / **Ctrl** and click a web link to open it in your browser.
|
- **Links**: hold **Cmd** / **Ctrl** and click a web link to open it in your browser. Programs can also print real hyperlinks (OSC 8, as `ls --hyperlink` and many build tools do); these have a dotted underline, and hovering one shows where it really points. Only http(s) links are opened.
|
||||||
- **Scroll**: the mouse wheel, or **Shift+PageUp**, **Shift+PageDown**, **Shift+Home** and **Shift+End**. On the alternate screen (for example in `tmux`, `screen`, `byobu` or `less`) the wheel sends arrow keys instead.
|
- **Scroll**: the mouse wheel, or **Shift+PageUp**, **Shift+PageDown**, **Shift+Home** and **Shift+End**. On the alternate screen (for example in `tmux`, `screen`, `byobu` or `less`) the wheel sends arrow keys instead.
|
||||||
- Applications that use the mouse (such as `htop`, `vim` or `tmux` with mouse mode) receive mouse clicks and the wheel directly.
|
- Applications that use the mouse (such as `htop`, `vim` or `tmux` with mouse mode) receive mouse clicks and the wheel directly.
|
||||||
|
|
||||||
|
|||||||
@@ -684,7 +684,8 @@ impl TermView {
|
|||||||
if modifiers.command
|
if modifiers.command
|
||||||
&& let Some(url) = self.url_at(origin, cw, ch, *pos)
|
&& let Some(url) = self.url_at(origin, cw, ch, *pos)
|
||||||
{
|
{
|
||||||
ui.ctx().open_url(egui::OpenUrl::new_tab(url));
|
// Only web links: text from a remote host must not launch file: or custom-scheme handlers.
|
||||||
|
crate::widgets::open_web_url(ui.ctx(), &url);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let now = ui.input(|i| i.time);
|
let now = ui.input(|i| i.time);
|
||||||
@@ -738,9 +739,20 @@ impl TermView {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// An OSC 8 link's text can say anything, so its real target is always shown on hover.
|
||||||
|
if let Some(p) = hover
|
||||||
|
&& let Some(link) = self.hyperlink_at(self.point_at(origin, cw, ch, p).0)
|
||||||
|
{
|
||||||
|
egui::Tooltip::always_open(ui.ctx().clone(), ui.layer_id(), egui::Id::new("term-link"), egui::PopupAnchor::Pointer).show(|ui| {
|
||||||
|
ui.label(&link);
|
||||||
|
if !crate::widgets::is_web_url(&link) {
|
||||||
|
ui.label(egui::RichText::new("Not a web link — won't be opened").weak());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
if let Some(p) = hover
|
if let Some(p) = hover
|
||||||
&& ui.input(|i| i.modifiers.command)
|
&& ui.input(|i| i.modifiers.command)
|
||||||
&& self.url_at(origin, cw, ch, p).is_some()
|
&& self.url_at(origin, cw, ch, p).is_some_and(|u| crate::widgets::is_web_url(&u))
|
||||||
{
|
{
|
||||||
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
|
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
|
||||||
} else if hover.is_some() {
|
} else if hover.is_some() {
|
||||||
@@ -748,8 +760,12 @@ impl TermView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The link under `p`: an OSC 8 hyperlink on the cell, else a URL in the text.
|
||||||
fn url_at(&self, origin: Pos2, cw: f32, ch: f32, p: Pos2) -> Option<String> {
|
fn url_at(&self, origin: Pos2, cw: f32, ch: f32, p: Pos2) -> Option<String> {
|
||||||
let (point, _) = self.point_at(origin, cw, ch, p);
|
let (point, _) = self.point_at(origin, cw, ch, p);
|
||||||
|
if let Some(link) = self.hyperlink_at(point) {
|
||||||
|
return Some(link);
|
||||||
|
}
|
||||||
let (text, cols) = self.line_text(point.line);
|
let (text, cols) = self.line_text(point.line);
|
||||||
let re = regex::Regex::new(URL_RE).ok()?;
|
let re = regex::Regex::new(URL_RE).ok()?;
|
||||||
re.find_iter(&text).find_map(|m| {
|
re.find_iter(&text).find_map(|m| {
|
||||||
@@ -759,6 +775,13 @@ impl TermView {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hyperlink_at(&self, point: Point) -> Option<String> {
|
||||||
|
let g = self.term.grid();
|
||||||
|
(point.line >= g.topmost_line() && point.line <= g.bottommost_line() && point.column.0 < self.term.columns())
|
||||||
|
.then(|| g[point].hyperlink().map(|h| h.uri().to_string()))
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn paint(&mut self, ui: &mut Ui, rect: Rect, origin: Pos2, font: &FontId, cw: f32, ch: f32, theme: &TermTheme, focused: bool) {
|
fn paint(&mut self, ui: &mut Ui, rect: Rect, origin: Pos2, font: &FontId, cw: f32, ch: f32, theme: &TermTheme, focused: bool) {
|
||||||
let painter = ui.painter_at(rect);
|
let painter = ui.painter_at(rect);
|
||||||
@@ -818,6 +841,13 @@ impl TermView {
|
|||||||
}
|
}
|
||||||
if cell.flags.intersects(Flags::ALL_UNDERLINES) {
|
if cell.flags.intersects(Flags::ALL_UNDERLINES) {
|
||||||
painter.hline(cell_rect.x_range(), y + ch - 1.5, Stroke::new(1.0, fg));
|
painter.hline(cell_rect.x_range(), y + ch - 1.5, Stroke::new(1.0, fg));
|
||||||
|
} else if cell.hyperlink().is_some() {
|
||||||
|
// OSC 8 links: a dotted underline.
|
||||||
|
let mut x0 = cell_rect.left();
|
||||||
|
while x0 < cell_rect.right() {
|
||||||
|
painter.hline(x0..=(x0 + 1.5).min(cell_rect.right()), y + ch - 1.5, Stroke::new(1.0, fg.gamma_multiply(0.8)));
|
||||||
|
x0 += 3.0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if cell.flags.contains(Flags::STRIKEOUT) {
|
if cell.flags.contains(Flags::STRIKEOUT) {
|
||||||
painter.hline(cell_rect.x_range(), y + ch * 0.55, Stroke::new(1.0, fg));
|
painter.hline(cell_rect.x_range(), y + ch * 0.55, Stroke::new(1.0, fg));
|
||||||
@@ -1136,6 +1166,17 @@ mod tests {
|
|||||||
assert!(t.prompts.len() <= 1);
|
assert!(t.prompts.len() <= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn osc8_links_are_found_on_their_cells() {
|
||||||
|
let mut t = TermView::new(100, 14.0, CursorStyle::Block);
|
||||||
|
t.feed(b"see \x1b]8;;https://example.com/docs\x1b\\the docs\x1b]8;;\x1b\\ now");
|
||||||
|
let at = |c| t.hyperlink_at(Point::new(Line(0), Column(c)));
|
||||||
|
assert_eq!(at(4).as_deref(), Some("https://example.com/docs"));
|
||||||
|
assert_eq!(at(11).as_deref(), Some("https://example.com/docs"));
|
||||||
|
assert_eq!(at(12), None, "the link ends with its text");
|
||||||
|
assert_eq!(at(0), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn feeds_text_and_finds_it() {
|
fn feeds_text_and_finds_it() {
|
||||||
let mut t = TermView::new(1000, 14.0, CursorStyle::Block);
|
let mut t = TermView::new(1000, 14.0, CursorStyle::Block);
|
||||||
|
|||||||
Reference in New Issue
Block a user