aboutsummaryrefslogtreecommitdiff
path: root/lib/grep.ml
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2024-05-15 10:45:54 -0500
committerMarc Coquand <marc@mccd.space>2024-05-15 10:45:54 -0500
commit47892663040f7e295cc4052438cf804b040f0389 (patch)
treed299cb4f0741904b1a8d484a6fba365b41592bf5 /lib/grep.ml
parent7038e0073e7f7f672644ca949eeda399d6a533fb (diff)
downloadstitch-47892663040f7e295cc4052438cf804b040f0389.tar.gz
stitch-47892663040f7e295cc4052438cf804b040f0389.tar.bz2
stitch-47892663040f7e295cc4052438cf804b040f0389.zip
Add TODOs, update help, rebind keys
Diffstat (limited to 'lib/grep.ml')
-rw-r--r--lib/grep.ml108
1 files changed, 94 insertions, 14 deletions
diff --git a/lib/grep.ml b/lib/grep.ml
index 9855b6f..ac09f80 100644
--- a/lib/grep.ml
+++ b/lib/grep.ml
@@ -16,6 +16,96 @@ let headline_pattern =
Sys.getenv_opt "STITCH_HEADLINE_PATTERN" |> Option.value ~default:"* "
+let todo_pattern = Sys.getenv_opt "STITCH_TODO" |> Option.value ~default:"* TODO"
+
+let todo_pattern_regexp =
+ Sys.getenv_opt "STITCH_TODO_REGEXP" |> Option.value ~default:"^\\* TODO"
+
+
+let done_pattern = Sys.getenv_opt "STITCH_DONE" |> Option.value ~default:"* DONE"
+
+let done_pattern_regexp =
+ Sys.getenv_opt "STITCH_DONE_REGEXP" |> Option.value ~default:"^\\* DONE"
+
+
+(* Utils *)
+
+let run_calls calls =
+ let open Shexp_process in
+ let open Shexp_process.Infix in
+ eval (chdir execution_directory (calls |- read_all))
+
+
+let get_padding_arr arr =
+ Array.fold_left (fun n (file_name, _) -> Int.max n (String.length file_name)) 0 arr
+
+
+let get_padding_list list =
+ List.fold_left (fun n (file_name, _) -> Int.max n (String.length file_name)) 0 list
+
+
+let pad str n =
+ let padding = n - String.length str in
+ String.concat "" [ str; String.make padding ' ' ]
+
+
+exception Not_A_Tuple of string * string
+
+(* todo parsing *)
+let todo_get_args = [ grep_cmd; todo_pattern_regexp; "-H"; "-r"; "-n"; "--no-messages" ]
+
+let get_todos () =
+ let open Shexp_process in
+ let open Shexp_process.Infix in
+ run_calls (call todo_get_args |- call [ "sort"; "-n"; "-r" ])
+
+
+let parse_todo_files files =
+ List.concat
+ @@ List.mapi
+ (fun file_number ((file_name : string), content) ->
+ let content = String.split_on_char '\n' content in
+ List.mapi
+ (fun line_number line -> file_name, line_number, line, file_number)
+ content)
+ files
+
+
+let parse_todo_string s =
+ String.split_on_char '\n' s
+ |> List.filter_map (fun 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 pretty_format_todo parsed_headlines =
+ let padding = get_padding_list parsed_headlines in
+ List.map
+ (fun (file_name, content) -> String.concat " | " [ pad file_name padding; content ])
+ parsed_headlines
+
+
+let toggle_done file_name =
+ let open Shexp_process in
+ run_calls
+ (call
+ [ "sed"; "-i"; "s/" ^ todo_pattern_regexp ^ "/" ^ done_pattern ^ "/g"; file_name ])
+
+
+let toggle_todo file_name =
+ let open Shexp_process in
+ run_calls
+ (call
+ [ "sed"; "-i"; "s/" ^ done_pattern_regexp ^ "/" ^ todo_pattern ^ "/g"; file_name ])
+
+
+(* Headline parsing *)
let headline_pattern_length = String.length headline_pattern
let find_sort_modification () =
@@ -38,7 +128,9 @@ let run_print ~dir args =
eval (chdir dir (call args |- read_all))
-let headline_args = [ "xargs"; grep_cmd; "^\\*"; "-H"; "-r"; "-n"; "--no-messages" ]
+let headline_args =
+ [ "xargs"; grep_cmd; headline_pattern_regexp; "-H"; "-r"; "-n"; "--no-messages" ]
+
let get_headlines () =
let open Shexp_process in
@@ -71,8 +163,6 @@ let get_tags () =
(call headline_args |- call [ grep_cmd; "-E"; tag_pattern; "-o" ] |- read_all))
-exception Not_A_Tuple of string * string
-
(** Returns a tuple of file name and Content *)
let parse_headlines s =
String.split_on_char '\n' s
@@ -89,19 +179,9 @@ let parse_headlines s =
|> Array.of_list
-(** Used for pretty printing *)
-let get_padding list =
- Array.fold_left (fun n (file_name, _) -> Int.max n (String.length file_name)) 0 list
-
-
-let pad str n =
- let padding = n - String.length str in
- String.concat "" [ str; String.make padding ' ' ]
-
-
(** Turns "2024-03-05.org:* Hello world" into "2024-03-05 | * Hello world" *)
let pretty_format parsed_headlines =
- let padding = get_padding parsed_headlines in
+ let padding = get_padding_arr parsed_headlines in
Array.map
(fun (file_name, content) -> String.concat " | " [ pad file_name padding; content ])
parsed_headlines