Skip to content

Commit

Permalink
feat: introduce config and persistent rendering mode
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Sep 15, 2024
1 parent d66b591 commit dbc50a1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/list/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(executable
(name main)
(libraries minttea spices leaves str))
(name main)
(libraries minttea spices leaves str))
1 change: 1 addition & 0 deletions minttea/config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t = { render_mode : [ `clear | `persist ]; fps : int }
10 changes: 6 additions & 4 deletions minttea/minttea.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ module Event = Event
module Command = Command
module App = App
module Program = Program
module Config = Config

let config ?(render_mode = `clear) ?(fps = 60) () = Config.{ render_mode; fps }
let app = App.make

let run ?(fps = 60) ~initial_model app =
let prog = Program.make ~app ~fps in
let run ?(config = config ()) ~initial_model app =
let prog = Program.make ~app ~config in
Program.run prog initial_model;
Logger.trace (fun f -> f "terminating")

let start app ~initial_model =
let start ?(config = config ()) app ~initial_model =
let module App = struct
let start () =
Logger.set_log_level None;
let pid =
spawn_link (fun () ->
run app ~initial_model;
run ~config app ~initial_model;
Logger.trace (fun f -> f "about to shutdown");
shutdown ~status:0 ())
in
Expand Down
10 changes: 8 additions & 2 deletions minttea/minttea.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
open Riot

module Config : sig
type t
end

val config : ?render_mode:[ `clear | `persist ] -> ?fps:int -> unit -> Config.t

module Event : sig
type modifier = No_modifier | Ctrl

Expand Down Expand Up @@ -46,5 +52,5 @@ val app :
unit ->
'model App.t

val run : ?fps:int -> initial_model:'model -> 'model App.t -> unit
val start : 'model App.t -> initial_model:'model -> unit
val run : ?config:Config.t -> initial_model:'model -> 'model App.t -> unit
val start : ?config:Config.t -> 'model App.t -> initial_model:'model -> unit
8 changes: 4 additions & 4 deletions minttea/program.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
open Riot

type Message.t += Timer of unit Ref.t | Shutdown
type 'model t = { app : 'model App.t; fps : int }
type 'model t = { app : 'model App.t; config : Config.t }

let make ~app ~fps = { app; fps }
let make ~app ~config = { app; config }

exception Exit

Expand Down Expand Up @@ -55,14 +55,14 @@ let init { app; _ } initial_model renderer =
Renderer.render renderer view;
loop renderer app initial_model

let run ({ fps; _ } as t) initial_model =
let run ({ config; _ } as t) initial_model =
Printexc.record_backtrace true;
let renderer =
spawn (fun () ->
(* NOTE(@leostera): reintroduce this when riot brings back process-stealing *)
(* process_flag (Priority High); *)
let runner = Process.await_name "Minttea.runner" in
Renderer.run ~fps ~runner)
Renderer.run ~config ~runner)
in
let runner =
spawn (fun () ->
Expand Down
7 changes: 5 additions & 2 deletions minttea/renderer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type t = {
ticker : Timer.timer;
width : int;
height : int;
render_mode : [ `clear | `persist ];
mutable buffer : string;
mutable last_render : string;
mutable lines_rendered : int;
Expand Down Expand Up @@ -61,7 +62,7 @@ and flush t =
let new_lines_this_flush = List.length new_lines in

(* clean last rendered lines *)
if t.lines_rendered > 0 then
if t.render_mode = `clear && t.lines_rendered > 0 then
for _i = 1 to t.lines_rendered - 1 do
Terminal.clear_line ();
Terminal.cursor_up 1
Expand Down Expand Up @@ -107,7 +108,8 @@ let max_fps = 120
let cap fps = Int.max 1 (Int.min fps max_fps) |> Int.to_float
let fps_to_float fps = 1. /. cap fps *. 1_000. |> Int64.of_float

let run ~fps ~runner =
let run ~config ~runner =
let Config.{ render_mode; fps } = config in
let ticker =
Riot.Timer.send_interval ~every:(fps_to_float fps) (self ()) Tick
|> Result.get_ok
Expand All @@ -123,6 +125,7 @@ let run ~fps ~runner =
is_altscreen_active = false;
lines_rendered = 0;
cursor_visibility = `visible;
render_mode;
}

let render pid output = send pid (Render output)
Expand Down
2 changes: 1 addition & 1 deletion minttea/renderer.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Riot

val run : fps:int -> runner:Pid.t -> unit
val run : config:Config.t -> runner:Pid.t -> unit
val render : Pid.t -> string -> unit
val enter_alt_screen : Pid.t -> unit
val exit_alt_screen : Pid.t -> unit
Expand Down

0 comments on commit dbc50a1

Please sign in to comment.