From: Amélia Coutard-Sander Date: Sun, 5 Jan 2025 00:43:18 +0000 (+0100) Subject: Complétions visibles dans la fenêtre X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=e213af55df8d89ee5f013a68f107c17da20a08fe;p=cells.git Complétions visibles dans la fenêtre --- diff --git a/bin/modes.ml b/bin/modes.ml index f5f7c29..c3bdf96 100644 --- a/bin/modes.ml +++ b/bin/modes.ml @@ -49,19 +49,51 @@ let get_name (m : (module Automata.Automaton)) = let module M = (val m) in M.name +let render_world (EditorState (m, { board; current; pos = px, py; size })) = + let module M = (val m) in + let w = (Graphics.size_x () / size) + 1 + and h = (Graphics.size_y () / size) + 1 in + let wx = px - (w / 2) + and wy = py - (h / 2) in + for x = wx to wx + w do + for y = wy to wy + h do + let r, g, b = M.color (Automata.get x y board) in + let r, g, b = (int_of_char r, int_of_char g, int_of_char b) in + Graphics.set_color (Graphics.rgb r g b); + Graphics.fill_rect ((x - wx) * size) ((y - wy) * size) size size; + Graphics.set_color (Graphics.rgb 127 127 127); + Graphics.draw_rect ((x - wx) * size) ((y - wy) * size) size size + done + done; + let r, g, b = M.color current in + let r, g, b = (int_of_char r, int_of_char g, int_of_char b) in + Graphics.set_color (Graphics.rgb r g b); + Graphics.fill_rect (((px - wx) * size) + 5) (((py - wy) * size) + 5) (max (size - 10) 1) (max (size - 10) 1); + Graphics.set_color (Graphics.rgb 127 127 127); + Graphics.draw_rect (((px - wx) * size) + 5) (((py - wy) * size) + 5) (max (size - 10) 1) (max (size - 10) 1) + module rec Command : (Mode with type initer = string) = struct - type state = string + type state = string * string list type initer = string - let initial s = s + let initial s = (s, []) - let render _ cmd = + let render st (cmd, sug) = + render_world st; Graphics.set_color Graphics.white; Graphics.fill_rect 0 0 (Graphics.size_x ()) 25; Graphics.set_color Graphics.black; Graphics.moveto 10 8; - Graphics.draw_string (":" ^ cmd) + Graphics.draw_string (":" ^ cmd); + List.iteri + (fun i s -> + Graphics.set_color Graphics.white; + Graphics.fill_rect 50 ((25 * i) + 25) (Graphics.size_x ()) 25; + Graphics.set_color Graphics.black; + Graphics.moveto 60 ((25 * i) + 25 + 8); + Graphics.draw_string s) + sug let rec last = function | [s] -> s @@ -83,14 +115,10 @@ module rec Command : (Mode with type initer = string) = struct let real_complete cmd completions = let last = last cmd in match List.filter (String.starts_with ~prefix:last) completions with - | [] -> - Printf.fprintf stderr "No completions !\n%!"; - String.concat " " (List.map Parse.shquote cmd) - | [possibility] -> String.concat " " (List.map Parse.shquote (ch_last possibility cmd)) ^ " " + | [] -> (String.concat " " (List.map Parse.shquote cmd), []) + | [possibility] -> (String.concat " " (List.map Parse.shquote (ch_last possibility cmd)) ^ " ", []) | p :: ps -> - Printf.fprintf stderr "Completions:\n%!"; - List.iter (Printf.fprintf stderr "\t%s\n%!") (p :: ps); - String.concat " " (List.map Parse.shquote (ch_last (List.fold_left common_prefix p ps) cmd)) + (String.concat " " (List.map Parse.shquote (ch_last (List.fold_left common_prefix p ps) cmd)), p :: ps) let complete_file cmd f = try @@ -117,7 +145,7 @@ module rec Command : (Mode with type initer = string) = struct let autocomplete cmd = match try Parse.shlex cmd with Parse.NonTerminatedString -> Parse.shlex (cmd ^ "'") with - | ["quit"] -> "quit" + | ["quit"] -> ("quit", []) | ["select"] -> real_complete ["select"; ""] (List.map get_name Automata.automata) | ["select"; n] -> real_complete ["select"; n] (List.map get_name Automata.automata) | ["write"] -> complete_file ["write"; ""] "" @@ -126,7 +154,7 @@ module rec Command : (Mode with type initer = string) = struct | ["read"; n] -> complete_file ["read"; n] n | [] -> real_complete [""] ["quit"; "select"; "write"; "read"] | [cmd] -> real_complete [cmd] ["quit"; "select"; "write"; "read"] - | parsed -> String.concat " " (List.map Parse.shquote parsed) + | parsed -> (String.concat " " (List.map Parse.shquote parsed), []) let run_cmd (EditorState (m, st) as estate) cmd = let module M = (val m) in @@ -134,7 +162,7 @@ module rec Command : (Mode with type initer = string) = struct match Parse.shlex cmd with | ["quit"] -> Graphics.close_graph (); - (estate, Either.left "") + (estate, Either.left ("", [])) | ["select"; auto] -> ( match List.find_opt (fun m -> get_name m = auto) Automata.automata with | Some auto -> @@ -179,12 +207,12 @@ module rec Command : (Mode with type initer = string) = struct Printf.fprintf stderr "Unterminated string !\n%!"; (estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ()))) - let update estate cmd = function - | '\b' -> (estate, Either.left (String.sub cmd 0 (max (String.length cmd - 1) 0))) + let update estate (cmd, _) = function + | '\b' -> (estate, Either.left (String.sub cmd 0 (max (String.length cmd - 1) 0), [])) | '' -> (estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ()))) | '\r' -> run_cmd estate cmd | '\t' -> (estate, Either.left (autocomplete cmd)) - | c -> (estate, Either.left (cmd ^ String.make 1 c)) + | c -> (estate, Either.left (cmd ^ String.make 1 c, [])) end and Normal : (Mode with type initer = unit) = struct @@ -195,28 +223,7 @@ and Normal : (Mode with type initer = unit) = struct let initial () = { n = None } - let render (EditorState (m, { board; current; pos = px, py; size })) _ = - let module M = (val m) in - let w = (Graphics.size_x () / size) + 1 - and h = (Graphics.size_y () / size) + 1 in - let wx = px - (w / 2) - and wy = py - (h / 2) in - for x = wx to wx + w do - for y = wy to wy + h do - let r, g, b = M.color (Automata.get x y board) in - let r, g, b = (int_of_char r, int_of_char g, int_of_char b) in - Graphics.set_color (Graphics.rgb r g b); - Graphics.fill_rect ((x - wx) * size) ((y - wy) * size) size size; - Graphics.set_color (Graphics.rgb 127 127 127); - Graphics.draw_rect ((x - wx) * size) ((y - wy) * size) size size - done - done; - let r, g, b = M.color current in - let r, g, b = (int_of_char r, int_of_char g, int_of_char b) in - Graphics.set_color (Graphics.rgb r g b); - Graphics.fill_rect (((px - wx) * size) + 5) (((py - wy) * size) + 5) (max (size - 10) 1) (max (size - 10) 1); - Graphics.set_color (Graphics.rgb 127 127 127); - Graphics.draw_rect (((px - wx) * size) + 5) (((py - wy) * size) + 5) (max (size - 10) 1) (max (size - 10) 1) + let render st _ = render_world st let update (EditorState (m, st)) n = let module M = (val m) in