Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fs/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name fs)
(libraries
str
;; Internal dependencies
pretty))
30 changes: 22 additions & 8 deletions lib/fs/fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ type tree =
| File of string * file_contents lazy_t
| Dir of string * tree array

(* Regex to used to determine if bat outputs a binary file warning. This is a
bit of a fragile approach, but there is no robust way to determine if a file
is binary or not. Improve this if it becomes a problem.
*)
let binary_file_pattern = Str.regexp ".*\\[bat warning\\].*Binary.*content*."
let binary_file_warning = "This file is binary and cannot be displayed"

let has_binary_warning contents =
Str.string_match binary_file_pattern contents 0

(* Extracts the file name from a tree node *)

let file_name = function
| File (name, _) -> name
| Dir (name, _) -> name
Expand Down Expand Up @@ -35,14 +47,16 @@ let read_file_contents path =
--paging=never --terminal-width=80 " ^ path
in
let contents = Shell.proc_stdout cmd in
let lines =
contents
|> String.split_on_char '\n'
|> List.map Pretty.str
|> Array.of_list
in
let offset = 0 in
{ lines; offset }
if has_binary_warning contents then
{ lines = [| Pretty.str binary_file_warning |]; offset = 0 }
else
let lines =
contents
|> String.split_on_char '\n'
|> List.map Pretty.str
|> Array.of_list
in
{ lines; offset = 0 }

let rec to_tree path =
if Sys.is_directory path then
Expand Down
Loading