From 9920df1bebbdd1f8a5859a2a05fe1f59837428e3 Mon Sep 17 00:00:00 2001 From: Heath Henley Date: Mon, 18 Nov 2024 19:12:27 -0500 Subject: [PATCH 1/3] Check for binary file warning and don't print --- lib/fs/dune | 1 + lib/fs/fs.ml | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/fs/dune b/lib/fs/dune index da81c55..2277426 100644 --- a/lib/fs/dune +++ b/lib/fs/dune @@ -1,5 +1,6 @@ (library (name fs) (libraries + str ;; Internal dependencies pretty)) diff --git a/lib/fs/fs.ml b/lib/fs/fs.ml index 936de0f..3601559 100644 --- a/lib/fs/fs.ml +++ b/lib/fs/fs.ml @@ -7,6 +7,12 @@ 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*) +let binary_file_pattern = Str.regexp ".*\\[bat warning\\].*Binary.*content*." +let binary_file_warning = "This file is binary and cannot be displayed" + +(* Extracts the file name from a tree node *) + let file_name = function | File (name, _) -> name | Dir (name, _) -> name @@ -32,16 +38,20 @@ let rec sort_tree = function let read_file_contents path = let cmd = "bat --style=numbers,changes --color=always --italic-text=always \ - --paging=never --terminal-width=80 " ^ 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 + let has_binary_warning = Str.string_match binary_file_pattern contents 0 in + match has_binary_warning with + | true -> { lines = [| Pretty.str binary_file_warning |]; offset = 0 } + | _ -> + let lines = + contents + |> String.split_on_char '\n' + |> List.map Pretty.str + |> Array.of_list + in + let offset = 0 in { lines; offset } let rec to_tree path = From 4fd57d79f839df4fe7b46bd21b71d1136cd0b93f Mon Sep 17 00:00:00 2001 From: Heath Henley Date: Thu, 21 Nov 2024 17:53:17 -0500 Subject: [PATCH 2/3] Fix: run ocamlformat on changes --- lib/fs/fs.ml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/fs/fs.ml b/lib/fs/fs.ml index 3601559..6e73ba2 100644 --- a/lib/fs/fs.ml +++ b/lib/fs/fs.ml @@ -38,21 +38,21 @@ let rec sort_tree = function let read_file_contents path = let cmd = "bat --style=numbers,changes --color=always --italic-text=always \ - --paging=never --terminal-width=80 " ^ path + --paging=never --terminal-width=80 " ^ path in let contents = Shell.proc_stdout cmd in let has_binary_warning = Str.string_match binary_file_pattern contents 0 in match has_binary_warning with | true -> { lines = [| Pretty.str binary_file_warning |]; offset = 0 } | _ -> - let lines = - contents - |> String.split_on_char '\n' - |> List.map Pretty.str - |> Array.of_list - in - let offset = 0 in - { lines; offset } + let lines = + contents + |> String.split_on_char '\n' + |> List.map Pretty.str + |> Array.of_list + in + let offset = 0 in + { lines; offset } let rec to_tree path = if Sys.is_directory path then From 16e002a4d7b0d8cd4a0c37262370f2e97030a650 Mon Sep 17 00:00:00 2001 From: Heath Henley Date: Tue, 7 Jan 2025 17:41:06 -0500 Subject: [PATCH 3/3] Improve comment + switch match to if --- lib/fs/fs.ml | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/fs/fs.ml b/lib/fs/fs.ml index 6e73ba2..e2f735d 100644 --- a/lib/fs/fs.ml +++ b/lib/fs/fs.ml @@ -7,10 +7,16 @@ 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*) +(* 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 @@ -41,18 +47,16 @@ let read_file_contents path = --paging=never --terminal-width=80 " ^ path in let contents = Shell.proc_stdout cmd in - let has_binary_warning = Str.string_match binary_file_pattern contents 0 in - match has_binary_warning with - | true -> { lines = [| Pretty.str binary_file_warning |]; offset = 0 } - | _ -> - 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