A Nix-friendly SQLite-enhanced fork of Flitter, a speedrunning split timer for Unix-style terminals
Revisão | d166d2264b343b337c4f21c69d2773e56b03dbeb (tree) |
---|---|
Hora | 2023-05-31 04:03:33 |
Autor | Corbin <cds@corb...> |
Commiter | Corbin |
Remove two more gettimeofday().
@@ -1,10 +1,11 @@ | ||
1 | 1 | open Core |
2 | 2 | open Timer_types |
3 | 3 | |
4 | -type t = { | |
5 | - timer : Timer_types.timer; | |
4 | +type 'a t = { | |
5 | + timer : 'a; | |
6 | 6 | display : Display.t; |
7 | 7 | last_draw : float; |
8 | + now : float; | |
8 | 9 | hotkeys_stream : Hotkeys.t; |
9 | 10 | } |
10 | 11 |
@@ -13,16 +14,16 @@ type event = Draw_tick | Key of Hotkeys.keypress | ||
13 | 14 | let draw_rate = 60. |
14 | 15 | let period = 1. /. draw_rate |
15 | 16 | |
16 | -let draw_event flitter = | |
17 | - let deadline = flitter.last_draw +. period in | |
18 | - let delay = deadline -. Core_unix.gettimeofday () in | |
17 | +let draw_event l = | |
18 | + let deadline = l.last_draw +. period in | |
19 | + let delay = deadline -. l.now in | |
19 | 20 | let%lwt () = |
20 | 21 | if Float.(delay > 0.) then Lwt_unix.sleep delay else Lwt.return_unit |
21 | 22 | in |
22 | 23 | Lwt.return Draw_tick |
23 | 24 | |
24 | -let keyboard_event flitter = | |
25 | - match%lwt Lwt_stream.get flitter.hotkeys_stream with | |
25 | +let keyboard_event l = | |
26 | + match%lwt Lwt_stream.get l.hotkeys_stream with | |
26 | 27 | | Some keypress -> Lwt.return (Key keypress) |
27 | 28 | | None -> failwith "Hotkeys stream exited unexpectedly" |
28 | 29 |
@@ -31,10 +32,10 @@ let array_replace arr i value = | ||
31 | 32 | copy.(i) <- value; |
32 | 33 | copy |
33 | 34 | |
34 | -let handle_key flitter (t, key_str) = | |
35 | - let timer = flitter.timer in | |
35 | +let handle_key l (t, key_str) = | |
36 | + let timer = l.timer in | |
36 | 37 | let new_timer = |
37 | - match flitter.timer.state with | |
38 | + match timer.state with | |
38 | 39 | | Idle -> ( |
39 | 40 | match key_str with |
40 | 41 | | "space" | "j" -> |
@@ -126,45 +127,44 @@ let handle_key flitter (t, key_str) = | ||
126 | 127 | | "q" -> raise Stdlib.Exit |
127 | 128 | | _ -> timer) |
128 | 129 | in |
129 | - { flitter with timer = new_timer } | |
130 | + { l with timer = new_timer } | |
130 | 131 | |
131 | -let handle_draw flitter = | |
132 | - let now = Core_unix.gettimeofday () in | |
133 | - let timer = | |
134 | - { flitter.timer with state = update_now flitter.timer.state now } | |
135 | - in | |
136 | - Display.draw flitter.display timer; | |
137 | - { flitter with timer; last_draw = now } | |
132 | +let handle_draw l = | |
133 | + let timer = { l.timer with state = update_now l.timer.state l.now } in | |
134 | + Display.draw l.display timer; | |
135 | + { l with timer; last_draw = l.now } | |
138 | 136 | |
139 | -let rec handle_events flitter events = | |
137 | +let rec handle_events l events = | |
140 | 138 | match events with |
141 | 139 | | evt :: remaining_evts -> |
142 | 140 | let new_flitter = |
143 | 141 | match evt with |
144 | - | Draw_tick -> handle_draw flitter | |
145 | - | Key keypress -> handle_key flitter keypress | |
142 | + | Draw_tick -> handle_draw l | |
143 | + | Key keypress -> handle_key l keypress | |
146 | 144 | in |
147 | 145 | handle_events new_flitter remaining_evts |
148 | - | [] -> flitter | |
146 | + | [] -> l | |
149 | 147 | |
150 | 148 | let make timer = |
151 | 149 | let%lwt hotkeys_stream = Hotkeys.make_stream () in |
150 | + let now = Core_unix.gettimeofday () in | |
152 | 151 | Lwt.return |
153 | 152 | { |
154 | 153 | timer; |
155 | 154 | display = Display.make (); |
156 | 155 | (* Make sure we're due for a draw *) |
157 | - last_draw = Core_unix.gettimeofday () -. period; | |
156 | + last_draw = now -. period; | |
157 | + now; | |
158 | 158 | hotkeys_stream; |
159 | 159 | } |
160 | 160 | |
161 | -let loop flitter = | |
162 | - let rec loop' flitter = | |
163 | - let%lwt events = Lwt.npick [ draw_event flitter; keyboard_event flitter ] in | |
164 | - match handle_events flitter events with | |
161 | +let loop l = | |
162 | + let rec loop' l = | |
163 | + let%lwt events = Lwt.npick [ draw_event l; keyboard_event l ] in | |
164 | + match handle_events l events with | |
165 | 165 | | new_flitter -> loop' new_flitter |
166 | 166 | | exception Stdlib.Exit -> |
167 | - Display.close flitter.display; | |
167 | + Display.close l.display; | |
168 | 168 | Lwt.return () |
169 | 169 | in |
170 | - loop' flitter | |
170 | + loop' l |
@@ -1,4 +1,4 @@ | ||
1 | -type t | |
1 | +type 'a t | |
2 | 2 | |
3 | -val make : Timer_types.timer -> t Lwt.t | |
4 | -val loop : t -> unit Lwt.t | |
3 | +val make : 'a -> 'a t Lwt.t | |
4 | +val loop : Timer_types.timer t -> unit Lwt.t |