Skip to content

Commit d899f82

Browse files
committed
Remove hyperlink when wrapping lines
1 parent 6e37b75 commit d899f82

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/printer.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -537,22 +537,24 @@ impl<'a> Printer for InteractivePrinter<'a> {
537537
match chunk {
538538
// Regular text.
539539
EscapeSequence::Text(text) => {
540-
let text = &*self.preprocess(text, &mut cursor_total);
540+
let text = self.preprocess(text, &mut cursor_total);
541541
let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
542542

543543
write!(
544544
handle,
545-
"{}",
545+
"{}{}",
546546
as_terminal_escaped(
547547
style,
548548
&format!("{}{}", self.ansi_style, text_trimmed),
549549
true_color,
550550
colored_output,
551551
italics,
552552
background_color
553-
)
553+
),
554+
self.ansi_style.to_reset_sequence(),
554555
)?;
555556

557+
// Pad the rest of the line.
556558
if text.len() != text_trimmed.len() {
557559
if let Some(background_color) = background_color {
558560
let ansi_style = Style {
@@ -632,7 +634,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
632634
// It wraps.
633635
write!(
634636
handle,
635-
"{}\n{}",
637+
"{}{}\n{}",
636638
as_terminal_escaped(
637639
style,
638640
&format!("{}{}", self.ansi_style, line_buf),
@@ -641,6 +643,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
641643
self.config.use_italic_text,
642644
background_color
643645
),
646+
self.ansi_style.to_reset_sequence(),
644647
panel_wrap.clone().unwrap()
645648
)?;
646649

src/vscreen.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ impl AnsiStyle {
2323
}
2424
}
2525
}
26+
27+
pub fn to_reset_sequence(&mut self) -> String {
28+
match &mut self.attributes {
29+
Some(a) => a.to_reset_sequence(),
30+
None => String::new(),
31+
}
32+
}
2633
}
2734

2835
impl Display for AnsiStyle {
@@ -35,6 +42,8 @@ impl Display for AnsiStyle {
3542
}
3643

3744
struct Attributes {
45+
has_sgr_sequences: bool,
46+
3847
foreground: String,
3948
background: String,
4049
underlined: String,
@@ -67,16 +76,18 @@ struct Attributes {
6776
strike: String,
6877

6978
/// The hyperlink sequence.
70-
/// FORMAT: \x1B]8;<ID>;<HREF>\e\\
79+
/// FORMAT: \x1B]8;{ID};{URL}\e\\
7180
///
7281
/// `\e\\` may be replaced with BEL `\x07`.
73-
/// Setting both <ID> and <HREF> to an empty string represents no hyperlink.
82+
/// Setting both {ID} and {URL} to an empty string represents no hyperlink.
7483
hyperlink: String,
7584
}
7685

7786
impl Attributes {
7887
pub fn new() -> Self {
7988
Attributes {
89+
has_sgr_sequences: false,
90+
8091
foreground: "".to_owned(),
8192
background: "".to_owned(),
8293
underlined: "".to_owned(),
@@ -135,6 +146,8 @@ impl Attributes {
135146
}
136147

137148
fn sgr_reset(&mut self) {
149+
self.has_sgr_sequences = false;
150+
138151
self.foreground.clear();
139152
self.background.clear();
140153
self.underlined.clear();
@@ -152,6 +165,7 @@ impl Attributes {
152165
.map(|p| p.parse::<u16>())
153166
.map(|p| p.unwrap_or(0)); // Treat errors as 0.
154167

168+
self.has_sgr_sequences = true;
155169
while let Some(p) = iter.next() {
156170
match p {
157171
0 => self.sgr_reset(),
@@ -214,6 +228,28 @@ impl Attributes {
214228
_ => format!("\x1B[{}m", color),
215229
}
216230
}
231+
232+
/// Gets an ANSI escape sequence to reset all the known attributes.
233+
pub fn to_reset_sequence(&self) -> String {
234+
let mut buf = String::with_capacity(17);
235+
236+
// TODO: Enable me in a later pull request.
237+
// if self.has_sgr_sequences {
238+
// buf.push_str("\x1B[m");
239+
// }
240+
241+
if !self.hyperlink.is_empty() {
242+
buf.push_str("\x1B]8;;\x1B\\"); // Disable hyperlink.
243+
}
244+
245+
// TODO: Enable me in a later pull request.
246+
// if !self.charset.is_empty() {
247+
// // https://espterm.github.io/docs/VT100%20escape%20codes.html
248+
// buf.push_str("\x1B(B\x1B)B"); // setusg0 and setusg1
249+
// }
250+
251+
buf
252+
}
217253
}
218254

219255
impl Display for Attributes {

tests/integration_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,7 @@ fn ansi_hyperlink_emitted_when_wrapped() {
17961796
.write_stdin("\x1B]8;;http://example.com/\x1B\\Hyperlinks..........Wrap across lines.\n")
17971797
.assert()
17981798
.success()
1799-
.stdout("\x1B]8;;http://example.com/\x1B\\\x1B]8;;http://example.com/\x1B\\Hyperlinks..........\n\x1B]8;;http://example.com/\x1B\\Wrap across lines.\n")
1799+
.stdout("\x1B]8;;http://example.com/\x1B\\\x1B]8;;http://example.com/\x1B\\Hyperlinks..........\x1B]8;;\x1B\\\n\x1B]8;;http://example.com/\x1B\\Wrap across lines.\n")
18001800
// FIXME: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ should not be emitted twice.
18011801
.stderr("");
18021802
}

0 commit comments

Comments
 (0)