From: Amélia Coutard-Sander Date: Sat, 4 Jan 2025 21:52:08 +0000 (+0100) Subject: Pas de crash si complétion d'un fichier qui ne marche pas X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=d2cc055931eb4f00c180397477c7a632cd300950;p=cells.git Pas de crash si complétion d'un fichier qui ne marche pas --- diff --git a/bin/modes.ml b/bin/modes.ml index 8362f62..a36dda5 100644 --- a/bin/modes.ml +++ b/bin/modes.ml @@ -93,25 +93,27 @@ module rec Command : (Mode with type initer = string) = struct String.concat " " (List.map Parse.shquote (ch_last (List.fold_left common_prefix p ps) 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) + try + 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) + with Unix.Unix_error _ -> real_complete cmd [] let autocomplete cmd = match try Parse.shlex cmd with Parse.NonTerminatedString -> Parse.shlex (cmd ^ "'") with