11//! Apply colours and styling to strings.
22
33use std:: cmp:: { max, min} ;
4+ use std:: path:: Path ;
45
6+ use crossterm:: tty:: IsTty ;
57use line_numbers:: { LineNumber , SingleLineSpan } ;
68use owo_colors:: { OwoColorize , Style } ;
79use unicode_width:: { UnicodeWidthChar , UnicodeWidthStr } ;
@@ -13,6 +15,12 @@ use crate::options::DisplayOptions;
1315use crate :: parse:: syntax:: { AtomKind , MatchKind , MatchedPos , StringKind , TokenKind } ;
1416use crate :: summary:: FileFormat ;
1517
18+ // OSC 8 hyperlink escape sequences
19+ // See: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
20+ const OSC_8_START : & str = "\x1b ]8;;" ;
21+ const OSC_8_ST : & str = "\x1b \\ " ;
22+ const OSC_8_END : & str = "\x1b ]8;;\x1b \\ " ;
23+
1624#[ derive( Clone , Copy , Debug ) ]
1725pub ( crate ) enum BackgroundColor {
1826 Dark ,
@@ -536,8 +544,27 @@ pub(crate) fn apply_line_number_color(
536544 }
537545}
538546
547+ fn make_path_hyperlink ( display_path : & str , line_number : Option < LineNumber > ) -> String {
548+ if !std:: io:: stdout ( ) . is_tty ( ) {
549+ return display_path. to_owned ( ) ;
550+ }
551+
552+ let path = Path :: new ( display_path) ;
553+
554+ let Ok ( canonical_path) = path. canonicalize ( ) else {
555+ return display_path. to_owned ( ) ;
556+ } ;
557+
558+ let mut url = format ! ( "file://{}" , canonical_path. display( ) ) ;
559+ if let Some ( line_num) = line_number {
560+ url. push_str ( & format ! ( ":{}" , line_num. 0 + 1 ) ) ;
561+ }
562+ format ! ( "{OSC_8_START}{url}{OSC_8_ST}{display_path}{OSC_8_END}" )
563+ }
564+
539565pub ( crate ) fn header (
540566 display_path : & str ,
567+ first_line_number : Option < LineNumber > ,
541568 extra_info : Option < & String > ,
542569 hunk_num : usize ,
543570 hunk_total : usize ,
@@ -551,7 +578,7 @@ pub(crate) fn header(
551578 } ;
552579
553580 let display_path_pretty = apply_header_color (
554- display_path,
581+ & make_path_hyperlink ( display_path, first_line_number ) ,
555582 display_options. use_color ,
556583 display_options. background_color ,
557584 hunk_num,
0 commit comments