Skip to main content

multiverse/widgets/
mod.rs

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