From ccddb7af2a9b4845d26809d8e2985a1f8e224bc4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Sat, 4 Jan 2025 22:36:13 +0100 Subject: [PATCH] =?utf8?q?Compl=C3=A9tion=20automatique=20des=20fichiers?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bin/dune | 2 +- bin/modes.ml | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/bin/dune b/bin/dune index 6313089..a45521e 100644 --- a/bin/dune +++ b/bin/dune @@ -1,4 +1,4 @@ (executable (public_name cells) (name main) - (libraries automata graphics)) + (libraries automata graphics unix)) diff --git a/bin/modes.ml b/bin/modes.ml index 5b3beaa..b1f0d46 100644 --- a/bin/modes.ml +++ b/bin/modes.ml @@ -85,13 +85,36 @@ module rec Command : (Mode with type initer = string) = struct List.iter (Printf.fprintf stderr "\t%s\n%!") possibilities; String.concat " " (List.map Parse.shquote cmd) + let complete_file cmd f = + let dir_files dir = + let d = Unix.opendir dir in + let rec gen_list () = + try + let f = Unix.readdir d in + f :: gen_list () + with End_of_file -> [] + in + let files = gen_list () in + Unix.closedir d; + files + in + let files = + match String.rindex_opt f '/' with + | Some 0 -> List.map (( ^ ) "/") (dir_files "/") + | Some i -> List.map (( ^ ) (String.sub f 0 (i + 1))) (dir_files (String.sub f 0 i)) + | None -> dir_files "." + in + real_complete cmd (List.map (fun f -> if (Unix.stat f).st_kind = S_DIR then f ^ "/" else f) files) + let autocomplete cmd = match try Parse.shlex cmd with Parse.NonTerminatedString -> Parse.shlex (cmd ^ "'") with | ["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"] -> "write " - | ["read"] -> "read " + | ["write"] -> complete_file ["write"; ""] "" + | ["write"; n] -> complete_file ["write"; n] n + | ["read"] -> complete_file ["read"; ""] "" + | ["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) -- 2.46.0