text_area basic nav

This commit is contained in:
cqc
2023-01-07 07:24:29 -06:00
parent 048ea0eab4
commit dfef26fcf5
2 changed files with 22 additions and 17 deletions

View File

@ -1228,6 +1228,7 @@ module Nottui = struct
(*val is_empty : status -> bool*)
val status : handle -> status Lwd.t
val var : handle -> var
val has_focus : status -> bool
val merge : status -> status -> status
val pp_var : Format.formatter -> var -> unit
@ -1242,6 +1243,7 @@ module Nottui = struct
(v, Lwd.map ~f:(fun i -> Handle (i, v)) (Lwd.get v))
let empty : status = Empty
let var (h : handle) : var = fst h
let status (h : handle) : status Lwd.t = snd h
let has_focus = function
@ -1259,6 +1261,8 @@ module Nottui = struct
let request ((v, _) : handle) = request_var v
let release ((v, _) : handle) =
Log.debug (fun m ->
m "Focus.release v=%d clock=%d" (Lwd.peek v) !clock);
incr clock;
Lwd.set v 0
@ -2793,13 +2797,13 @@ module Nottui_widgets = struct
| None -> `Unhandled)
| None -> `Unhandled
let multifield_of_string ?(table = Lwd_table.make ()) (s : string) :
let edit_area_of_string ?(table = Lwd_table.make ()) (s : string) :
line Lwd_table.t =
(* Append lines from s to table *)
List.iter (line_append ~table) (String.split_on_char '\n' s);
table
let rec focus_compare focus : int =
let focus_val focus : int =
Focus.(
match focus with
| Empty -> 0
@ -2812,8 +2816,7 @@ module Nottui_widgets = struct
| Empty, _ -> Ui.empty
| Handle (_, _), _ -> ui
| Conflict _, (X (a, b) | Y (a, b) | Z (a, b)) ->
if focus_compare a.focus < focus_compare b.focus then
find_focus b
if focus_val a.focus < focus_val b.focus then find_focus b
else find_focus a
| Conflict _, Atom _ -> Ui.empty
| ( Conflict _,
@ -2827,18 +2830,21 @@ module Nottui_widgets = struct
| Shift_area (t, _, _) ) ) ->
find_focus t)
(* let focused_row_of_table (table : line Lwd_table.t) =
Lwd_table.map_reduce
(fun row (line : line) -> (Some row, line))
( (None, line_empty ()),
(fun a b -> if focus_compare (Focus.status (snd a).focus) < focus_compare ).focus then b else a) )
table *)
let focus_handle_compare a b =
if
Lwd.peek (Focus.var (snd a).focus)
< Lwd.peek (Focus.var (snd b).focus)
then b
else a
let focus_move table f = `Unhandled
let focused_row_of_table (table : line Lwd_table.t) =
Lwd_table.map_reduce
(fun row (line : line) -> (Some row, line))
((None, line_empty ()), focus_handle_compare)
table
let edit_area ?(table = Lwd_table.make ()) ?(focus = Focus.make ())
() : Ui.t Lwd.t =
(* create the cursor var and focus on first table row *)
let cursor = Lwd.var @@ Lwd_table.first table in
Option.iter
(fun cursor ->
@ -2853,10 +2859,10 @@ module Nottui_widgets = struct
table
|> Lwd.join
|> Lwd.map2
~f:(fun focus ->
~f:(fun (focus, _) ->
Ui.keyboard_area ~focus (fun k ->
Log.debug (fun m ->
m "line_table handler %a" Ui.pp_key k);
m "edit_area handler %a" Ui.pp_key k);
match k with
| `Uchar u, [ `Ctrl ] when eq_uc_c u 'n' ->
cursor_move cursor (fun c -> Lwd_table.next c)
@ -2900,13 +2906,12 @@ module Nottui_widgets = struct
Lwd.set line_prev.state
( str_prev ^ str,
String.length str_prev );
Lwd.set cursor (Some row_prev);
Lwd_table.remove row;
`Handled)
else `Unhandled))
| `Uchar u, [ `Ctrl ] when eq_uc_c u 'k' -> `Handled
| _ -> `Unhandled))
(Focus.status focus)
(Lwd.pair (Focus.status focus) (focused_row_of_table table))
(** Tab view, where exactly one element of [l] is shown at a time. *)
let tabs (tabs : (string * (unit -> Ui.t Lwd.t)) list) : Ui.t Lwd.t