aboutsummaryrefslogtreecommitdiff
path: root/lib/headlines.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/headlines.ml')
-rw-r--r--lib/headlines.ml63
1 files changed, 27 insertions, 36 deletions
diff --git a/lib/headlines.ml b/lib/headlines.ml
index aec8567..853603c 100644
--- a/lib/headlines.ml
+++ b/lib/headlines.ml
@@ -2,17 +2,16 @@ module Grep = Grep
module Common = Common
open Notty
module Input_screen = Input_screen
+module Stitched_article = Stitched_article
type state =
- { tag : string option
- ; pos : int * int
+ { pos : int * int
; scroll : int
; content : (string * string) array
; content_pretty : string array
}
-let rec headline_screen t ({ tag; pos; scroll; content; content_pretty } as state) =
- print_endline (Option.value ~default:"No tag" tag);
+let rec render t ({ pos; scroll; content; content_pretty } as state) =
let x, y = pos in
let img =
let dot = I.string A.(st bold) ">" |> I.pad ~l:0 ~t:(y - scroll)
@@ -32,11 +31,11 @@ let rec headline_screen t ({ tag; pos; scroll; content; content_pretty } as stat
let content_length = Array.length content_pretty in
let scroll_up () =
let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in
- headline_screen t @@ { state with pos = x, max (y - 1) 0; scroll }
+ render t @@ { state with pos = x, max (y - 1) 0; scroll }
in
let scroll_down () =
let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
- headline_screen t @@ { state with pos = x, min (y + 1) (content_length - 1); scroll }
+ render t @@ { state with pos = x, min (y + 1) (content_length - 1); scroll }
in
match Common.Term.event t with
| `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
@@ -44,42 +43,36 @@ let rec headline_screen t ({ tag; pos; scroll; content; content_pretty } as stat
(match s with
| `Down -> scroll_down ()
| `Up -> scroll_up ())
- | `Resize _ -> headline_screen t state
+ | `Resize _ -> render t state
| `Mouse ((`Press _ | `Drag), (_, y), _) ->
- headline_screen t { state with pos = 0, min y content_length }
- | `Key (`ASCII 't', []) ->
+ render t { state with pos = 0, min y content_length }
+ | `Key (`ASCII '@', []) ->
+ let content =
+ Array.map
+ (fun (file_name, _) -> Grep.get_full_file_content_content file_name)
+ content
+ |> Array.to_list
+ in
+ let content = Grep.parse_full_content content in
+ let content_pretty = Grep.pretty_print_parsed_content content |> Array.of_list in
+ Stitched_article.render
+ t
+ { pos = 0, 0; content = content |> Array.of_list; content_pretty; scroll = 0 }
+ | `Key (`ASCII 's', []) ->
let (input_state : Input_screen.state) =
{ screen = img
; user_input = ""
- ; prompt = "TAG: "
+ ; prompt = "GREP: "
; on_enter =
(fun tag ->
let content = Grep.get_tagged_headlines tag () |> Grep.parse_headlines in
let content_pretty = Grep.pretty_format content in
Common.Term.cursor t None;
- headline_screen
- t
- { state with content; content_pretty; pos = 0, 0; scroll = 0 })
- ; on_cancel =
- (fun _ ->
- Common.Term.cursor t None;
- headline_screen t state)
- }
- in
- Input_screen.render t input_state
- | `Key (`ASCII 's', []) ->
- let (input_state : Input_screen.state) =
- { screen = img
- ; user_input = ""
- ; prompt = "SEARCH: "
- ; on_enter =
- (fun _ ->
- Common.Term.cursor t None;
- headline_screen t { state with pos = 0, 0; scroll = 0 })
+ render t { content; content_pretty; pos = 0, 0; scroll = 0 })
; on_cancel =
(fun _ ->
Common.Term.cursor t None;
- headline_screen t state)
+ render t state)
}
in
Input_screen.render t input_state
@@ -89,7 +82,7 @@ let rec headline_screen t ({ tag; pos; scroll; content; content_pretty } as stat
(match d with
| `Up -> scroll_up ()
| `Down -> scroll_down ()
- | _ -> headline_screen t state)
+ | _ -> render t state)
| `Key (`ASCII 'e', []) | `Key (`Enter, []) ->
(* Editor might be set with extra args, in that case we need to separate these *)
let[@warning "-8"] (editor :: args) =
@@ -112,13 +105,13 @@ let rec headline_screen t ({ tag; pos; scroll; content; content_pretty } as stat
match Unix.wait () with
| _, _ ->
Common.Term.cursor t None;
- headline_screen t state
+ render t state
(* Capture resizing events *)
| exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor ()
| exception Unix.Unix_error (_, _, _) -> failwith "ERROR"
in
run_editor ()
- | _ -> headline_screen t state
+ | _ -> render t state
let start (tag : string) () =
@@ -134,6 +127,4 @@ let start (tag : string) () =
exit 0)
else (
let content_pretty = content |> Grep.pretty_format in
- headline_screen
- (Common.Term.create ())
- { tag; pos = 0, 0; scroll = 0; content; content_pretty })
+ render (Common.Term.create ()) { pos = 0, 0; scroll = 0; content; content_pretty })