]> git.f4mnq.fr Git - cells.git/commitdiff
Gestion des fichiers dans le mode commande.
authorAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:38:53 +0000 (14:38 +0100)
committerAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:53:54 +0000 (14:53 +0100)
bin/modes.ml

index b0c5535d38bfec67df43946ac4602c545b80c905..c7c6cbcad27e162bbf1ee598b299606e26c09384 100644 (file)
@@ -59,12 +59,29 @@ module rec Command : (Mode with type initer = string) = struct
           Graphics.moveto 10 8;
           Graphics.draw_string (":" ^ cmd)
 
-  let run_cmd estate cmd =
+  let run_cmd (EditorState (m, st) as estate) cmd =
+          let module M = (val m) in
           match cmd with
           | "quit" ->
                   Graphics.close_graph ();
-                  (estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())))
+                  (estate, Either.left "")
           | "select" -> (estate, Either.right (ModeAux.ModeAndState ((module AutoSelector), AutoSelector.initial ())))
+          | cmd when String.starts_with ~prefix:"write " cmd ->
+                  Out_channel.with_open_text
+                    (String.sub cmd 6 (String.length cmd - 6))
+                    (Automata.serialise (module M) st.board);
+                  (EditorState (m, st), Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())))
+          | cmd when String.starts_with ~prefix:"read " cmd ->
+                  ( EditorState
+                      ( m,
+                        {
+                          st with
+                          board =
+                            In_channel.with_open_text
+                              (String.sub cmd 5 (String.length cmd - 5))
+                              (Automata.deserialise (module M));
+                        } ),
+                    Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())) )
           | _ ->
                   Printf.fprintf stderr "Unknown cmd: `%s'\n%!" cmd;
                   (estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())))
@@ -140,13 +157,6 @@ and Normal : (Mode with type initer = unit) = struct
             | 'l' -> (EditorState (m, ntimes n (chpos 1 0) st), Either.left { n = None })
             | 'i' -> (EditorState (m, ntimes n (chsize 1) st), Either.left { n = None })
             | 'o' -> (EditorState (m, ntimes n (chsize (-1)) st), Either.left { n = None })
-            | 'w' ->
-                    Out_channel.with_open_text "test.auto" (Automata.serialise (module M) st.board);
-                    (EditorState (m, st), Either.left { n = None })
-            | 'r' ->
-                    ( EditorState
-                        (m, { st with board = In_channel.with_open_text "test.auto" (Automata.deserialise (module M)) }),
-                      Either.left { n = None } )
             | _ -> (EditorState (m, st), Either.left { n = None })
   end