From 3f696169ab1a560d94d169c1a5b744346da4c081 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Wed, 15 May 2024 14:05:03 -0500 Subject: Add done view + visual --- lib/todos.ml | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'lib/todos.ml') diff --git a/lib/todos.ml b/lib/todos.ml index d2af47f..fd9eaf1 100644 --- a/lib/todos.ml +++ b/lib/todos.ml @@ -9,16 +9,21 @@ type state = ; content : (string * string) array ; content_pretty : string array ; goto_headlines : (unit -> unit) -> unit + ; goto_done : (unit -> unit) -> unit } -let init ~goto_headlines = +let title = I.strf ~attr:A.(st bold) "%s" "Todo View" |> I.pad ~l:0 ~t:0 +let content_start = 2 + +let init ~goto_done ~goto_headlines = let content = Grep.get_todos () |> Grep.parse_todo_string in let content_pretty = Grep.pretty_format_todo content in - { pos = 0, 0 + { pos = 0, content_start ; scroll = 0 ; content = content |> Array.of_list ; content_pretty = content_pretty |> Array.of_list ; goto_headlines + ; goto_done } @@ -28,31 +33,35 @@ let load_todos () = todo_content, todo_pretty -let rec render t ({ pos; scroll; content; content_pretty; goto_headlines } as state) = +let rec render + t + ({ pos; scroll; content; content_pretty; goto_headlines; goto_done } as state) + = let x, y = pos in + let content_position = y - content_start in let img = let dot = I.string A.(st bold) ">" |> I.pad ~l:0 ~t:(y - scroll) and elements = Array.mapi (fun i el -> if i == y - scroll - then I.strf ~attr:A.(st underline) "%s" el |> I.pad ~l:2 ~t:i - else I.strf "%s" el |> I.pad ~l:2 ~t:i) + then I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start) + else I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start)) (Array.to_seq content_pretty |> Seq.drop scroll |> Array.of_seq) in let open I in - Array.fold_left (fun sum el -> el sum) dot elements + Array.fold_left (fun sum el -> el sum) (title dot) elements in let _, size_y = Common.Term.size t in Common.Term.image t img; - let content_length = Array.length content_pretty in + let content_end = Array.length content_pretty + (content_start - 1) in let scroll_up () = - let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in - render t @@ { state with pos = x, max (y - 1) 0; scroll } + let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in + render t { state with pos = x, max (y - 1) content_start; scroll } in let scroll_down () = let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in - render t @@ { state with pos = x, min (y + 1) (content_length - 1); scroll } + render t { state with pos = x, min (y + 1) content_end; scroll } in match Common.Term.event t with | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> () @@ -62,7 +71,7 @@ let rec render t ({ pos; scroll; content; content_pretty; goto_headlines } as st | `Up -> scroll_up ()) | `Resize _ -> render t state | `Mouse ((`Press _ | `Drag), (_, y), _) -> - render t { state with pos = 0, min y content_length } + render t { state with pos = 0, min y content_end } | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) } | `Key (`ASCII 's', []) -> let (input_state : Input_screen.state) = @@ -83,10 +92,11 @@ let rec render t ({ pos; scroll; content; content_pretty; goto_headlines } as st in Input_screen.render t input_state | `Key (`ASCII '1', []) -> goto_headlines (fun () -> render t state) + | `Key (`ASCII '3', []) -> goto_done (fun () -> render t state) | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () | `Key (`ASCII 't', []) -> - let selected_file, _ = Array.get content y in + let selected_file, _ = Array.get content (y - content_start) in let _ = Grep.toggle_done selected_file in let content, content_pretty = load_todos () in render @@ -105,7 +115,7 @@ let rec render t ({ pos; scroll; content; content_pretty; goto_headlines } as st let[@warning "-8"] (editor :: args) = String.split_on_char ' ' (Sys.getenv "EDITOR") in - let selected_file, _ = Array.get content y in + let selected_file, _ = Array.get content content_position in let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in let full_args = Array.append (Array.of_list args) [| full_path_file |] in Common.Term.cursor t (Some (0, 0)); -- cgit v1.2.3