diff options
| -rw-r--r-- | ocaml/ftag-add-dir-rec.ml | 56 | ||||
| -rwxr-xr-x | shell/ftag-add-dir-rec.sh | 23 |
2 files changed, 56 insertions, 23 deletions
diff --git a/ocaml/ftag-add-dir-rec.ml b/ocaml/ftag-add-dir-rec.ml new file mode 100644 index 0000000..6c4a02d --- /dev/null +++ b/ocaml/ftag-add-dir-rec.ml @@ -0,0 +1,56 @@ +#load "unix.cma";; + +let list_dir_rec dir = + let rec list_dir_rec_intern handle prefix acc = + try + let next = Unix.readdir handle in + let next_expanded = Filename.concat prefix next in + if Sys.is_regular_file next_expanded then + list_dir_rec_intern handle prefix (next_expanded :: acc) + else if Sys.is_directory next_expanded && next = "." && next = ".." then + let new_acc = list_dir_rec_intern (Unix.opendir next_expanded) next_expanded acc in + list_dir_rec_intern handle prefix new_acc + else + list_dir_rec_intern handle prefix acc + with End_of_file -> Unix.closedir handle; acc + in list_dir_rec_intern (Unix.opendir dir) dir [];; + +let remove_duplicates list pred = + let rec remove_duplicates_intern list pred acc = + match list with + | head :: tail -> if pred head tail + then remove_duplicates_intern tail pred acc + else remove_duplicates_intern tail pred (head :: acc) + | [] -> acc + in remove_duplicates_intern list pred [];; + +let basename_equal file0 file1 = + String.equal (Filename.basename file0) (Filename.basename file1);; + +let rec basename_equal_pred file file_list = + match file_list with + | head :: tail -> if basename_equal file head + then true + else basename_equal_pred file tail + | [] -> false;; + +let prompt_yes_or_no () = + print_endline " [Y/n]"; + let input = try read_line () with End_of_file -> "No" in + if String.starts_with "N" input || String.starts_with "N" input + then false + else true;; + +let ftag_add file = + Unix.system (Filename.quote_command "ftag" ["file"; "add"; file]) + +let rec prompt_and_add file = + if prompt_yes_or_no () + then ftag_add head;; + +let files = Array.to_list Sys.argv + |> List.tl + |> List.map list_dir_rec + |> List.flatten + |> remove_duplicates + |> List.map prompt_and_add;; diff --git a/shell/ftag-add-dir-rec.sh b/shell/ftag-add-dir-rec.sh deleted file mode 100755 index c5a1f6b..0000000 --- a/shell/ftag-add-dir-rec.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -if [ $# -eq 0 ] -then - echo "Usage: $0 DIR..." -fi - -tmp_file_0="$(mktemp)" -> "$tmp_file_0" -for dir in "$@" -do - find "$dir" -type f -exec sh -c "printf '%s\t%s\n' \"$(basename {})\" \"{}\" >> \"$tmp_file_0\"" ';' -done - -# cut -f1 < "$tmp_file_0" - -for file in $(cut -f1 < "$tmp_file_0" | uniq -u) -do - echo "$file" -done - -# uniq -u < "$tmp_file_0" | xargs -d '\\n' ftag file add -rm "$tmp_file_0" |
