X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=polymorphism%2F001%2Fsrc%2Fpolymorphism.ml;h=cb65462af268e831b5181c39ef94a10d6d910845;hb=479645750e2782b77b97a06cd8142dcfb8d704dd;hp=c52df9338acb3fe610a43736212cc691502e784b;hpb=8526e3e1220a86ff1d6fee238123611f89a4f242;p=cellular-automata.git diff --git a/polymorphism/001/src/polymorphism.ml b/polymorphism/001/src/polymorphism.ml index c52df93..cb65462 100644 --- a/polymorphism/001/src/polymorphism.ml +++ b/polymorphism/001/src/polymorphism.ml @@ -197,6 +197,39 @@ module Conway : RULE = struct end +module Terminal : sig + type color = [ `green + | `red + ] + + val string_with_color : string -> color -> string + + val clear : unit -> unit + + val reset : unit -> unit +end = struct + type color = [ `green + | `red + ] + + let ansi_code_clear = "\027[2J" (* Clear screen *) + let ansi_code_reset = "\027[1;1H" (* Reset cursor position *) + + let string_of_color = function + | `green -> "\027[0;32m" + | `red -> "\027[1;31m" + + let string_with_color s c = + sprintf "%s%s\027[0m" (string_of_color c) s + + let clear () = + print_string ansi_code_clear + + let reset () = + print_string ansi_code_reset +end + + module Automaton : sig type t @@ -227,6 +260,7 @@ end = struct ; data = Rule.create () } in + Terminal.clear (); { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ()) ; interval = Time.Span.of_float interval ; bar = String.make ks '-' @@ -236,6 +270,7 @@ end = struct cell.data.Cell.pheno let print t = + Terminal.reset (); print_endline t.bar; Matrix.print t.grid ~to_string:cell_to_string; print_endline t.bar