From: Amélia Coutard-Sander Date: Sat, 4 Jan 2025 17:51:33 +0000 (+0100) Subject: Gestion des fichiers dans le mode commande. X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=16704e0a61202c61530bdf0d00c14a19cd647b12;p=cells.git Gestion des fichiers dans le mode commande. --- diff --git a/bin/modes.ml b/bin/modes.ml index 74d1330..dec0497 100644 --- a/bin/modes.ml +++ b/bin/modes.ml @@ -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