multiverse/widgets/
mod.rs

1use itertools::Itertools;
2use ratatui::{prelude::*, widgets::WidgetRef};
3
4pub mod create_room;
5pub mod help;
6pub mod popup_input;
7pub mod recovery;
8pub mod room_list;
9pub mod room_view;
10pub mod search;
11pub mod settings;
12pub mod status;
13
14/// A hyperlink widget that renders a hyperlink in the terminal using [OSC 8].
15///
16/// [OSC 8]: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
17struct Hyperlink<'content> {
18    text: Text<'content>,
19    url: String,
20}
21
22impl<'content> Hyperlink<'content> {
23    fn new(text: impl Into<Text<'content>>, url: impl Into<String>) -> Self {
24        Self { text: text.into(), url: url.into() }
25    }
26}
27
28impl WidgetRef for Hyperlink<'_> {
29    fn render_ref(&self, area: Rect, buffer: &mut Buffer) {
30        self.text.render_ref(area, buffer);
31
32        // this is a hacky workaround for https://github.com/ratatui-org/ratatui/issues/902, a bug
33        // in the terminal code that incorrectly calculates the width of ANSI escape
34        // sequences. It works by rendering the hyperlink as a series of
35        // 2-character chunks, which is the calculated width of the hyperlink
36        // text.
37        for (i, two_chars) in self.text.to_string().chars().chunks(2).into_iter().enumerate() {
38            let text = two_chars.collect::<String>();
39            let hyperlink = format!("\x1B]8;;{}\x07{}\x1B]8;;\x07", self.url, text);
40            buffer[(area.x + i as u16 * 2, area.y)].set_symbol(hyperlink.as_str());
41        }
42    }
43}