From ade6e1bf69887a6e47c8bde8d736ee6316008ab9 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Fri, 24 May 2024 18:35:29 -0500 Subject: Stash --- lib/grep.ml | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'lib/grep.ml') diff --git a/lib/grep.ml b/lib/grep.ml index 94b4b48..cc030d9 100644 --- a/lib/grep.ml +++ b/lib/grep.ml @@ -1,7 +1,6 @@ let execution_directory = Sys.getenv_opt "STITCH_DIRECTORY" |> Option.value ~default:"/anywhere" - let grep_cmd = Sys.getenv_opt "STICH_GREP_CMD" |> Option.value ~default:"grep" let tag_pattern = @@ -134,6 +133,17 @@ let get_tagged_todo tag () = | _ -> "" +let split_file_name_of_grep message = + if String.equal message "" + then None + else ( + let split = Str.bounded_split (Str.regexp ":[0-9]+:") message 2 in + match split with + (* file, line, content *) + | [ file_name; content ] -> Some (file_name, content) + | _ -> raise (Not_A_Tuple (String.concat " SPLIT " split, message))) + + let toggle_done file_name = let open Shexp_process in run_calls @@ -277,19 +287,37 @@ let parse_full_content files = files -let get_file_names tag = +let rec merge_commands b = + let open Shexp_process.Infix in + match b with + | b :: [] -> Shexp_process.call b + | a :: b -> Shexp_process.call a |- merge_commands b + | [] -> failwith "empty" + + +let get_file_names tags = + let rec grep_list ?(init = true) b = + match b with + | [] -> [ [ grep_cmd; "-H"; "-r"; "-n" ] ] + | b :: [] -> + if init then [ [ grep_cmd; "-H"; "-n"; "-E"; b ] ] else [ [ grep_cmd; "-E"; b ] ] + | b :: rest -> + if init + then [ grep_cmd; "-H"; "-n"; "-E"; b ] :: grep_list ~init:false rest + else [ grep_cmd; "-E"; b ] :: grep_list ~init:false rest + in try let cmd = - let open Shexp_process in let open Shexp_process.Infix in - find_sort_name () - |- call [ "xargs"; grep_cmd; "-H"; "-r"; "-l"; "--no-messages"; "-E"; tag ] - |- call [ "sort"; "-n"; "-r" ] - |- read_all + merge_commands (grep_list tags) |- Shexp_process.read_all in Shexp_process.eval (Shexp_process.chdir execution_directory cmd) |> String.split_on_char '\n' - |> List.filter (fun s -> not (String.equal "" s)) + |> List.map (fun grep -> + split_file_name_of_grep grep + |> Option.map (fun (file_name, _) -> file_name) + |> Option.value ~default:"") + |> List.filter (fun s -> not ("" = String.trim s)) with | _ -> [] -- cgit v1.2.3