File tree Expand file tree Collapse file tree 2 files changed +23
-8
lines changed
Expand file tree Collapse file tree 2 files changed +23
-8
lines changed Original file line number Diff line number Diff line change 11(library
22 (name fs)
33 (libraries
4+ str
45 ;; Internal dependencies
56 pretty))
Original file line number Diff line number Diff line change @@ -7,6 +7,18 @@ type tree =
77 | File of string * file_contents lazy_t
88 | Dir of string * tree array
99
10+ (* Regex to used to determine if bat outputs a binary file warning. This is a
11+ bit of a fragile approach, but there is no robust way to determine if a file
12+ is binary or not. Improve this if it becomes a problem.
13+ *)
14+ let binary_file_pattern = Str. regexp " .*\\ [bat warning\\ ].*Binary.*content*."
15+ let binary_file_warning = " This file is binary and cannot be displayed"
16+
17+ let has_binary_warning contents =
18+ Str. string_match binary_file_pattern contents 0
19+
20+ (* Extracts the file name from a tree node *)
21+
1022let file_name = function
1123 | File (name , _ ) -> name
1224 | Dir (name , _ ) -> name
@@ -35,14 +47,16 @@ let read_file_contents path =
3547 --paging=never --terminal-width=80 " ^ path
3648 in
3749 let contents = Shell. proc_stdout cmd in
38- let lines =
39- contents
40- |> String. split_on_char '\n'
41- |> List. map Pretty. str
42- |> Array. of_list
43- in
44- let offset = 0 in
45- { lines; offset }
50+ if has_binary_warning contents then
51+ { lines = [| Pretty. str binary_file_warning |]; offset = 0 }
52+ else
53+ let lines =
54+ contents
55+ |> String. split_on_char '\n'
56+ |> List. map Pretty. str
57+ |> Array. of_list
58+ in
59+ { lines; offset = 0 }
4660
4761let rec to_tree path =
4862 if Sys. is_directory path then
You can’t perform that action at this time.
0 commit comments