Cleanup, reduce Lwd.set calls

This commit is contained in:
Reynir Björnsson 2023-05-12 14:39:02 +02:00
parent 1724e5c994
commit 3f43398866
2 changed files with 10 additions and 10 deletions

View file

@ -64,7 +64,9 @@ module User_prompt = struct
let text, position = let text, position =
Utils.render_cursor ~width:(max 0 (w - 3)) state.cursor Utils.render_cursor ~width:(max 0 (w - 3)) state.cursor
in in
Lwd.set cursor (position + 1, y); let new_cursor = (position + 1, y) in
if new_cursor <> (Lwd.peek cursor) then
Lwd.set cursor new_cursor;
I.hcat [ I.char A.empty ' ' 1 1 ; text ] I.hcat [ I.char A.empty ' ' 1 1 ; text ]
end end
@ -116,7 +118,10 @@ let make ~quit ~message cursor =
let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in
let state = Lwd.var (make quit message) in let state = Lwd.var (make quit message) in
let position = Lwd.var (0, 0) in let position = Lwd.var (0, 0) in
let hook = Lwd.set state in let hook state' =
if (Lwd.peek state).cursor != state'.cursor then
Lwd.set state state'
in
let update_prompt state (y, w) = let update_prompt state (y, w) =
let user = User_prompt.render ~cursor ~y ~w state in let user = User_prompt.render ~cursor ~y ~w state in
Ui.keyboard_area (handler ~hook state) (Ui.atom user) Ui.keyboard_area (handler ~hook state) (Ui.atom user)

View file

@ -51,27 +51,22 @@ let render { w; h } msgs =
done; done;
Ui.atom (I.vsnap ~align:`Bottom h !image) Ui.atom (I.vsnap ~align:`Bottom h !image)
let handler ~hook:_ _state _buffer _key = `Unhandled
let make w = let make w =
let ( let* ) x f = Lwd.bind ~f x in let ( let* ) x f = Lwd.bind ~f x in
let ( let+ ) x f = Lwd.map ~f x in let ( let+ ) x f = Lwd.map ~f x in
let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in
let state = Lwd.var { w = 0; h = 0; p = 0 } in let state = Lwd.var { w = 0; h = 0 } in
let hook = Lwd.set state in
let* document = let* document =
let+ state = Lwd.get state let+ state = Lwd.get state
and+ buffer = Lwd.get w in and+ buffer = Lwd.get w in
Ui.keyboard_area render state buffer
(handler ~hook state buffer)
(render state buffer)
in in
let update_size ~w ~h = let update_size ~w ~h =
let state' = Lwd.peek state in let state' = Lwd.peek state in
if state'.w <> w || state'.h <> h then Lwd.set state { state' with w; h } if state'.w <> w || state'.h <> h then Lwd.set state { w; h }
in in
let measure_size document = let measure_size document =