aboutsummaryrefslogtreecommitdiff
path: root/lib/grep.ml
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2024-05-14 16:20:57 -0500
committerMarc Coquand <marc@mccd.space>2024-05-14 16:20:57 -0500
commit0bc0958e789847d3065f4d084a96117d62d18691 (patch)
tree7447ff4e8d22aaa69c94ffe44e0e6b9cbf3dfeec /lib/grep.ml
parent20aa8079b40ee794bc84ead7283622c78f49afb5 (diff)
downloadstitch-0bc0958e789847d3065f4d084a96117d62d18691.tar.gz
stitch-0bc0958e789847d3065f4d084a96117d62d18691.tar.bz2
stitch-0bc0958e789847d3065f4d084a96117d62d18691.zip
Add nice headings
Diffstat (limited to 'lib/grep.ml')
-rw-r--r--lib/grep.ml71
1 files changed, 53 insertions, 18 deletions
diff --git a/lib/grep.ml b/lib/grep.ml
index 9638c37..7e5c3b9 100644
--- a/lib/grep.ml
+++ b/lib/grep.ml
@@ -3,25 +3,33 @@ let execution_directory =
let grep_cmd = Sys.getenv_opt "STICH_GREP_CMD" |> Option.value ~default:"ugrep"
-let tag_pattern = Sys.getenv_opt "STITCH_TAG_PATTERN" |> Option.value ~default:":[a-z-]+:"
+
+let tag_pattern =
+ Sys.getenv_opt "STITCH_TAG_PATTERN" |> Option.value ~default:"\\:[a-z-]+\\:"
+
+
+let headline_pattern_regexp =
+ Sys.getenv_opt "STITCH_HEADLINE_PATTERN_REGEXP" |> Option.value ~default:"^\\* "
+
let headline_pattern =
- Sys.getenv_opt "STITCH_HEADLINE_PATTERN" |> Option.value ~default:"^\\* "
+ Sys.getenv_opt "STITCH_HEADLINE_PATTERN" |> Option.value ~default:"* "
+let headline_pattern_length = String.length headline_pattern
+
let find_sort_modification () =
let open Shexp_process in
let open Shexp_process.Infix in
call [ "find"; "."; "-printf"; "%Ts/%f\\n" ]
|- call [ "sort"; "-n" ]
|- call [ "cut"; "-c12-" ]
- |- call [ "sed"; "/^\\./d" ]
let find_sort_name () =
let open Shexp_process in
let open Shexp_process.Infix in
- call [ "find"; "." ] |- call [ "sort"; "-n" ] |- call [ "cut"; "-c3-" ]
+ call [ "find"; "." ] |- call [ "cut"; "-c3-" ]
let run_print ~dir args =
@@ -32,18 +40,13 @@ let run_print ~dir args =
let headline_args = [ "xargs"; grep_cmd; "^\\*"; "-H"; "-r"; "-n"; "--no-messages" ]
-(* type sort = *)
-(* | Name *)
-(* | LastModified *)
-
-(* let sort_to_cmd = function *)
-(* | Name -> find_sort_name () *)
-(* | LastModified -> find_sort_modification () *)
-
let get_headlines () =
let open Shexp_process in
let open Shexp_process.Infix in
- eval (chdir execution_directory (find_sort_name () |- call headline_args |- read_all))
+ eval
+ (chdir
+ execution_directory
+ (find_sort_name () |- call headline_args |- call [ "sort"; "-n"; "-r" ] |- read_all))
let get_tagged_headlines tag () =
@@ -54,7 +57,8 @@ let get_tagged_headlines tag () =
execution_directory
(find_sort_name ()
|- call headline_args
- |- call [ grep_cmd; "-E"; "--no-messages"; tag ]
+ |- call [ grep_cmd; "--no-messages"; "-E"; tag ]
+ |- call [ "sort"; "-n"; "-r" ]
|- read_all))
@@ -105,10 +109,41 @@ let pretty_format parsed_headlines =
(** Full body parsing *)
-let get_full_content () =
- run_print
- ~dir:execution_directory
- [ grep_cmd; "^\\*"; "-h"; "-r"; "-n"; "-C"; "9999"; "--separator='|'" ]
+let get_full_content_command file = [ "cat"; file ]
+
+let get_full_file_content_content file =
+ let open Shexp_process in
+ let open Shexp_process.Infix in
+ ( file
+ , eval
+ (chdir
+ execution_directory
+ (find_sort_name ()
+ |- call headline_args
+ |- call (get_full_content_command file)
+ |- read_all)) )
+
+
+let parse_full_content files =
+ List.concat_map
+ (fun ((file_name : string), content) ->
+ let content = String.split_on_char '\n' content in
+ List.mapi (fun line_number line -> file_name, line_number, line) content)
+ files
+
+
+type display_type =
+ | Bold of string
+ | Normal of string
+
+let pretty_print_parsed_content parsed_files =
+ let padding = String.make headline_pattern_length ' ' in
+ List.concat_map
+ (fun (file_name, line_number, line_content) ->
+ if line_number == 0
+ then [ Bold ("------- " ^ file_name); Normal line_content ]
+ else [ Normal (padding ^ line_content) ])
+ parsed_files
(* let parse_file_headline collection full = *)
(* match full with *)