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 ++++++++++++++++++++++++++++++++++++-------- lib/headlines.ml | 2 +- lib/stitched_article.ml | 19 ++++++++++--------- 3 files changed, 47 insertions(+), 18 deletions(-) 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 | _ -> [] diff --git a/lib/headlines.ml b/lib/headlines.ml index cefe31d..7b12b9e 100644 --- a/lib/headlines.ml +++ b/lib/headlines.ml @@ -150,7 +150,7 @@ let rec render ; content = full_content |> Array.of_list ; content_pretty = full_content_pretty ; scroll = 0 - ; tag + ; tag = (if tag = "" then [] else [ tag ]) ; output = None ; go_back = (fun () -> render t state) ; goto_todos_view diff --git a/lib/stitched_article.ml b/lib/stitched_article.ml index 86a22e9..d053fe1 100644 --- a/lib/stitched_article.ml +++ b/lib/stitched_article.ml @@ -13,13 +13,15 @@ type state = ; goto_todos_view : (unit -> unit) -> unit ; goto_done_view : (unit -> unit) -> unit ; output : string option - ; tag : string + ; tag : string list } let title ~tag = match tag with - | "" -> I.strf ~attr:A.(st bold) "%s" "Notes" |> I.pad ~l:0 ~t:0 - | a -> I.strf ~attr:A.(st bold) "%s > %s" "Notes" a |> I.pad ~l:0 ~t:0 + | [] -> I.strf ~attr:A.(st bold) "%s" "Notes" |> I.pad ~l:0 ~t:0 + | _ -> + let tags_str = String.concat " > " tag in + I.strf ~attr:A.(st bold) "%s > %s" "Notes" tags_str |> I.pad ~l:0 ~t:0 let content_start = 1 @@ -50,8 +52,7 @@ let rec render and elements = Array.mapi (fun i (_, el) -> Compontent.current_line size_x y scroll el (i + content_start)) - (* TODO: Fix this ugly slow conversion *) - (Array.to_seq content_pretty |> Seq.drop scroll |> Array.of_seq) + (content_pretty |> Basic.array_drop scroll) in let open I in output_info Array.fold_left (fun sum el -> el sum) (title ~tag) elements @@ -97,7 +98,7 @@ let rec render in let result = Export.to_path path ~content:export in render t { state with output = Some result }) - else render t { state with output = Some "Export Blank; skipping" }) + else render t { state with output = Some "Export blank; skipping" }) ; on_cancel = (fun _ -> Common.Term.cursor t None; @@ -163,10 +164,10 @@ let rec render ; user_input = "" ; prompt = "REGEXP" ; on_enter = - (fun tag -> + (fun new_tag -> try - let content = Grep.get_file_names tag |> Array.of_list in let oc = open_out "/tmp/stitch-output" in + let content = Grep.get_file_names (new_tag :: tag) |> Array.of_list in Printf.fprintf oc "%s\n" (String.concat "\n" (content |> Array.to_list)); close_out oc; let content = @@ -187,7 +188,7 @@ let rec render ; content_pretty ; pos = 0, content_start ; scroll = 0 - ; tag + ; tag = new_tag :: tag } with | _ -> -- cgit v1.2.3