]> git.ameliathe1st.gay Git - cells.git/commitdiff
Complétion automatique des fichiers
authorAmélia Coutard-Sander <git@ameliathe1st.gay>
Sat, 4 Jan 2025 21:36:13 +0000 (22:36 +0100)
committerAmélia Coutard-Sander <git@ameliathe1st.gay>
Sat, 4 Jan 2025 21:36:13 +0000 (22:36 +0100)
bin/dune
bin/modes.ml

index 6313089927478898cb978cb7dcb740d99568bffe..a45521e101d850bb03fba0ac3a3b7bfe721873e2 100644 (file)
--- a/bin/dune
+++ b/bin/dune
@@ -1,4 +1,4 @@
 (executable
  (public_name cells)
  (name main)
- (libraries automata graphics))
+ (libraries automata graphics unix))
index 5b3beaab642b1e1306ffaf1a4e77eba983ee77c3..b1f0d4678fe9441ead5deeaa4559585013b07c2f 100644 (file)
@@ -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)