1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! Clickable link widget
//!
//! Displays hyperlinks that can be opened in the default browser.
//! Supports OSC 8 terminal hyperlinks for modern terminals.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{Link, link};
//!
//! // Simple link
//! let github = Link::new("https://github.com/anthropics/claude-code");
//!
//! // Link with custom text
//! let docs = Link::new("https://docs.rs/revue")
//! .text("Documentation")
//! .fg(Color::CYAN);
//!
//! // Using helper function
//! let home = link("https://example.com", "Home Page");
//! ```
use crate::event::{Key, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use crate::layout::Rect;
use crate::style::Color;
use crate::widget::theme::PLACEHOLDER_FG;
use crate::widget::traits::{EventResult, Interactive, RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::browser;
/// Link style
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LinkStyle {
/// Underlined text (default)
#[default]
Underline,
/// Bracketed [link]
Bracketed,
/// With arrow: link →
Arrow,
/// With icon: 🔗 link
Icon,
/// Plain text (no decoration)
Plain,
}
/// Clickable link widget
#[derive(Clone, Debug)]
pub struct Link {
/// URL to open
url: String,
/// Display text (defaults to URL)
text: Option<String>,
/// Link style
style: LinkStyle,
/// Foreground color
fg: Option<Color>,
/// Background color
bg: Option<Color>,
/// Whether link is focused
focused: bool,
/// Whether link is disabled
disabled: bool,
/// Tooltip text
tooltip: Option<String>,
/// Use OSC 8 hyperlinks (terminal-dependent)
osc8: bool,
/// CSS styling properties (id, classes)
props: WidgetProps,
}
impl Link {
/// Create a new link with URL
pub fn new(url: impl Into<String>) -> Self {
Self {
url: url.into(),
text: None,
style: LinkStyle::default(),
fg: None,
bg: None,
focused: false,
disabled: false,
tooltip: None,
osc8: true, // Enable by default
props: WidgetProps::new(),
}
}
/// Create a link with custom display text
pub fn with_text(url: impl Into<String>, text: impl Into<String>) -> Self {
Self::new(url).text(text)
}
/// Set display text
pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
/// Set link style
pub fn style(mut self, style: LinkStyle) -> Self {
self.style = style;
self
}
/// Set foreground color
pub fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
/// Set background color
pub fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
/// Set focused state
pub fn focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
/// Set disabled state
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Set tooltip
pub fn tooltip(mut self, tooltip: impl Into<String>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
/// Enable/disable OSC 8 hyperlinks
pub fn osc8(mut self, enabled: bool) -> Self {
self.osc8 = enabled;
self
}
/// Get the URL
pub fn url(&self) -> &str {
&self.url
}
/// Get display text
pub fn display_text(&self) -> &str {
self.text.as_deref().unwrap_or(&self.url)
}
/// Check if focused
pub fn is_focused(&self) -> bool {
self.focused
}
/// Check if disabled
pub fn is_disabled(&self) -> bool {
self.disabled
}
// Getters for testing
#[doc(hidden)]
pub fn get_style(&self) -> LinkStyle {
self.style
}
#[doc(hidden)]
pub fn get_fg(&self) -> Option<Color> {
self.fg
}
#[doc(hidden)]
pub fn get_bg(&self) -> Option<Color> {
self.bg
}
#[doc(hidden)]
pub fn get_tooltip(&self) -> &Option<String> {
&self.tooltip
}
#[doc(hidden)]
pub fn get_osc8(&self) -> bool {
self.osc8
}
/// Open the link in the system default browser
///
/// Does nothing if the link is disabled.
///
/// # Errors
///
/// Returns `Err(BrowserError)` if:
/// - The URL contains dangerous characters
/// - The URL format is invalid
/// - The browser cannot be opened
///
/// See [`crate::utils::browser::open_url`] for details.
#[cfg(not(target_arch = "wasm32"))]
pub fn open(&self) -> Result<(), browser::BrowserError> {
if self.disabled {
return Ok(());
}
browser::open_url(&self.url)
}
/// Format display text with style
fn format_display(&self) -> String {
let text = self.display_text();
match self.style {
LinkStyle::Underline => text.to_string(),
LinkStyle::Bracketed => format!("[{}]", text),
LinkStyle::Arrow => format!("{} →", text),
LinkStyle::Icon => format!("🔗 {}", text),
LinkStyle::Plain => text.to_string(),
}
}
/// Generate OSC 8 hyperlink escape sequence
fn osc8_start(&self) -> String {
if self.osc8 && !self.disabled {
format!("\x1b]8;;{}\x1b\\", self.url)
} else {
String::new()
}
}
fn osc8_end(&self) -> String {
if self.osc8 && !self.disabled {
"\x1b]8;;\x1b\\".to_string()
} else {
String::new()
}
}
}
impl View for Link {
crate::impl_view_meta!("Link");
fn render(&self, ctx: &mut RenderContext) {
use crate::widget::Text;
let display = self.format_display();
// Build the text with OSC 8 sequences
let content = if self.osc8 {
format!("{}{}{}", self.osc8_start(), display, self.osc8_end())
} else {
display
};
let mut text = Text::new(content);
// Apply colors
let fg = if self.disabled {
Some(PLACEHOLDER_FG)
} else if self.focused {
Some(Color::rgb(100, 200, 255))
} else {
self.fg.or(Some(Color::CYAN))
};
if let Some(fg) = fg {
text = text.fg(fg);
}
if let Some(bg) = self.bg {
text = text.bg(bg);
}
// Apply underline for underline style
if self.style == LinkStyle::Underline && !self.disabled {
text = text.underline();
}
text.render(ctx);
}
}
impl Interactive for Link {
fn handle_key(&mut self, event: &KeyEvent) -> EventResult {
if self.disabled {
return EventResult::Ignored;
}
match event.key {
Key::Enter | Key::Char(' ') => {
// Open the link - return Consumed to let the app handle opening
#[cfg(not(target_arch = "wasm32"))]
{
let _ = self.open();
}
EventResult::ConsumedAndRender
}
_ => EventResult::Ignored,
}
}
fn handle_mouse(&mut self, event: &MouseEvent, _area: Rect) -> EventResult {
if self.disabled {
return EventResult::Ignored;
}
match event.kind {
MouseEventKind::Down(MouseButton::Left) => {
#[cfg(not(target_arch = "wasm32"))]
{
let _ = self.open();
}
EventResult::ConsumedAndRender
}
_ => EventResult::Ignored,
}
}
crate::impl_focus_handlers!(direct);
}
impl_styled_view!(Link);
impl_props_builders!(Link);
/// Create a link
pub fn link(url: impl Into<String>, text: impl Into<String>) -> Link {
Link::with_text(url, text)
}
/// Create a link with just URL
pub fn url_link(url: impl Into<String>) -> Link {
Link::new(url)
}