Skip to content

Commit 999ba2b

Browse files
authored
Check for binary file warning and don't print (#11)
* Check for binary file warning and don't print * Fix: run ocamlformat on changes * Improve comment + switch match to if
1 parent 84403bc commit 999ba2b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/fs/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(library
22
(name fs)
33
(libraries
4+
str
45
;; Internal dependencies
56
pretty))

lib/fs/fs.ml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff 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+
1022
let 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

4761
let rec to_tree path =
4862
if Sys.is_directory path then

0 commit comments

Comments
 (0)