11//! Apply colours and styling to strings.
22
33use std:: cmp:: { max, min} ;
4+ use std:: path:: Path ;
45
56use line_numbers:: { LineNumber , SingleLineSpan } ;
67use owo_colors:: { OwoColorize , Style } ;
@@ -13,6 +14,12 @@ use crate::options::DisplayOptions;
1314use crate :: parse:: syntax:: { AtomKind , MatchKind , MatchedPos , StringKind , TokenKind } ;
1415use crate :: summary:: FileFormat ;
1516
17+ // OSC 8 hyperlink escape sequences
18+ // See: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
19+ const OSC_8_START : & str = "\x1b ]8;;" ;
20+ const OSC_8_ST : & str = "\x1b \\ " ;
21+ const OSC_8_END : & str = "\x1b ]8;;\x1b \\ " ;
22+
1623#[ derive( Clone , Copy , Debug ) ]
1724pub ( crate ) enum BackgroundColor {
1825 Dark ,
@@ -536,8 +543,22 @@ pub(crate) fn apply_line_number_color(
536543 }
537544}
538545
546+ fn make_path_hyperlink ( display_path : & str , line_number : Option < LineNumber > ) -> String {
547+ let Ok ( canonical_path) = Path :: new ( display_path) . canonicalize ( ) else {
548+ return display_path. to_owned ( ) ;
549+ } ;
550+
551+ let mut url = format ! ( "file://{}" , canonical_path. display( ) ) ;
552+ if let Some ( line_num) = line_number {
553+ url. push_str ( & format ! ( "#{}" , line_num. 0 + 1 ) ) ;
554+ }
555+
556+ format ! ( "{OSC_8_START}{url}{OSC_8_ST}{display_path}{OSC_8_END}" )
557+ }
558+
539559pub ( crate ) fn header (
540560 display_path : & str ,
561+ first_line_number : Option < LineNumber > ,
541562 extra_info : Option < & String > ,
542563 hunk_num : usize ,
543564 hunk_total : usize ,
@@ -550,8 +571,14 @@ pub(crate) fn header(
550571 format ! ( "{}/{} --- " , hunk_num, hunk_total)
551572 } ;
552573
574+ let display_path_with_link = if display_options. use_color {
575+ make_path_hyperlink ( display_path, first_line_number)
576+ } else {
577+ display_path. to_owned ( )
578+ } ;
579+
553580 let display_path_pretty = apply_header_color (
554- display_path ,
581+ & display_path_with_link ,
555582 display_options. use_color ,
556583 display_options. background_color ,
557584 hunk_num,
0 commit comments