From 7579952c3a14086fd7a2bee3d3ae4287b259b917 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= <git@ameliathe1st.gay>
Date: Mon, 16 Dec 2024 23:36:23 +0100
Subject: [PATCH] =?utf8?q?Gestion=20propre=20de=20la=20fermeture=20de=20fe?=
 =?utf8?q?n=C3=AAtre?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 bin/main.ml | 45 +++++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 20 deletions(-)

diff --git a/bin/main.ml b/bin/main.ml
index 09d3154..1ee164a 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -89,17 +89,20 @@ let pick f cs =
                 Graphics.draw_rect (x - 5) (y - 5) 200 20;
                 List.iteri (fun i -> draw_text (-i - 1)) (List.map f below);
                 Graphics.synchronize ();
-                match Graphics.read_key () with
-                | 'z' -> (
-                        match above with
-                        | [] -> aux [] curr below
-                        | c :: cs -> aux cs c (curr :: below))
-                | 's' -> (
-                        match below with
-                        | [] -> aux above curr []
-                        | c :: cs -> aux (curr :: above) c cs)
-                | ' ' -> curr
-                | _ -> aux above curr below
+                if Graphics.key_pressed ()
+                then
+                  match Graphics.read_key () with
+                  | 'z' -> (
+                          match above with
+                          | [] -> aux [] curr below
+                          | c :: cs -> aux cs c (curr :: below))
+                  | 's' -> (
+                          match below with
+                          | [] -> aux above curr []
+                          | c :: cs -> aux (curr :: above) c cs)
+                  | ' ' -> curr
+                  | _ -> aux above curr below
+                else aux above curr below
         in
         match cs with
         | c :: cs -> aux [] c cs
@@ -109,12 +112,14 @@ let () =
         Graphics.open_graph "";
         Graphics.set_window_title "Automaton";
         Graphics.auto_synchronize false;
-        let m =
-                pick
-                  (fun m ->
-                    let module M = (val m : Automata.Automaton) in
-                    M.name)
-                  Automata.automata
-        in
-        let module M = (val m) in
-        run (module M)
+        try
+          let m =
+                  pick
+                    (fun m ->
+                      let module M = (val m : Automata.Automaton) in
+                      M.name)
+                    Automata.automata
+          in
+          let module M = (val m) in
+          run (module M)
+        with Graphics.Graphic_failure _ -> ()
-- 
2.46.0