File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -3019,7 +3019,23 @@ fn display_file_name(
3019
3019
let absolute_path = fs:: canonicalize ( & path. p_buf ) . unwrap_or_default ( ) ;
3020
3020
let absolute_path = absolute_path. to_string_lossy ( ) ;
3021
3021
3022
- // TODO encode path
3022
+ #[ cfg( not( target_os = "windows" ) ) ]
3023
+ let unencoded_chars = "_-.:~/" ;
3024
+ #[ cfg( target_os = "windows" ) ]
3025
+ let unencoded_chars = "_-.:~/\\ " ;
3026
+
3027
+ // percentage encoding of path
3028
+ let absolute_path: String = absolute_path
3029
+ . chars ( )
3030
+ . map ( |c| {
3031
+ if c. is_alphanumeric ( ) || unencoded_chars. contains ( c) {
3032
+ c. to_string ( )
3033
+ } else {
3034
+ format ! ( "%{:02x}" , c as u8 )
3035
+ }
3036
+ } )
3037
+ . collect ( ) ;
3038
+
3023
3039
// \x1b = ESC, \x07 = BEL
3024
3040
name = format ! ( "\x1b ]8;;file://{hostname}{absolute_path}\x07 {name}\x1b ]8;;\x07 " ) ;
3025
3041
}
Original file line number Diff line number Diff line change @@ -3904,6 +3904,38 @@ fn test_ls_hyperlink() {
3904
3904
. stdout_is ( format ! ( "{file}\n " ) ) ;
3905
3905
}
3906
3906
3907
+ // spell-checker: disable
3908
+ #[ test]
3909
+ fn test_ls_hyperlink_encode_link ( ) {
3910
+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
3911
+
3912
+ #[ cfg( not( target_os = "windows" ) ) ]
3913
+ {
3914
+ at. touch ( "back\\ slash" ) ;
3915
+ at. touch ( "ques?tion" ) ;
3916
+ }
3917
+ at. touch ( "encoded%3Fquestion" ) ;
3918
+ at. touch ( "sp ace" ) ;
3919
+
3920
+ let result = ucmd. arg ( "--hyperlink" ) . succeeds ( ) ;
3921
+ #[ cfg( not( target_os = "windows" ) ) ]
3922
+ {
3923
+ assert ! ( result
3924
+ . stdout_str( )
3925
+ . contains( "back%5cslash\x07 back\\ slash\x1b ]8;;\x07 " ) ) ;
3926
+ assert ! ( result
3927
+ . stdout_str( )
3928
+ . contains( "ques%3ftion\x07 ques?tion\x1b ]8;;\x07 " ) ) ;
3929
+ }
3930
+ assert ! ( result
3931
+ . stdout_str( )
3932
+ . contains( "encoded%253Fquestion\x07 encoded%3Fquestion\x1b ]8;;\x07 " ) ) ;
3933
+ assert ! ( result
3934
+ . stdout_str( )
3935
+ . contains( "sp%20ace\x07 sp ace\x1b ]8;;\x07 " ) ) ;
3936
+ }
3937
+ // spell-checker: enable
3938
+
3907
3939
#[ test]
3908
3940
fn test_ls_color_do_not_reset ( ) {
3909
3941
let scene: TestScenario = TestScenario :: new ( util_name ! ( ) ) ;
You can’t perform that action at this time.
0 commit comments