tangled
alpha
login
or
join now
anil.recoil.org
/
unpac-unpac
0
fork
atom
The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork
atom
overview
issues
pulls
pipelines
examples/fs: show how to read files while scanning
Thomas Leonard
2 years ago
c4597825
37840760
+22
-11
1 changed file
expand all
collapse all
unified
split
examples
fs
main.ml
+22
-11
examples/fs/main.ml
···
1
1
-
(* Walks the directory tree rooted at the current directory,
2
2
-
displaying all directory names (skipping hidden directories and `_build`). *)
1
1
+
(* Walk the directory tree rooted at the current directory,
2
2
+
showing a summary for any .mli files. *)
3
3
+
4
4
+
let ( / ) = Eio.Path.( / )
3
5
4
4
-
open Eio.Std
6
6
+
let is_doc_comment = String.starts_with ~prefix:"(** "
5
7
6
6
-
let ( / ) = Eio.Path.( / )
8
8
+
(* Print the first line of [t]'s doc-comment, if any *)
9
9
+
let scan_mli t f =
10
10
+
Eio.Path.with_lines t (fun lines ->
11
11
+
Seq.find is_doc_comment lines
12
12
+
|> Option.iter (fun line ->
13
13
+
let stop = String.index_from_opt line 4 '*' |> Option.value ~default:(String.length line) in
14
14
+
Format.fprintf f "%a: %s@." Eio.Path.pp t (String.sub line 4 (stop - 4))
15
15
+
)
16
16
+
)
7
17
8
8
-
let rec scan t =
18
18
+
(* Walk the tree rooted at [t] and scan any .mli files found. *)
19
19
+
let rec scan t f =
9
20
match Eio.Path.kind ~follow:false t with
10
21
| `Directory ->
11
11
-
traceln "Visiting %a" Eio.Path.pp t;
12
22
Eio.Path.read_dir t |> List.iter (function
13
13
-
| "_build" -> ()
14
14
-
| item when String.starts_with ~prefix:"." item -> ()
15
15
-
| item -> scan (t / item)
16
16
-
)
23
23
+
| "_build" | "_opam" -> () (* Don't examine these directories *)
24
24
+
| item when String.starts_with ~prefix:"." item -> () (* Skip hidden items *)
25
25
+
| item -> scan (t / item) f
26
26
+
)
27
27
+
| `Regular_file when Filename.check_suffix (snd t) ".mli" -> scan_mli t f
17
28
| _ -> ()
18
29
19
30
let () =
20
31
Eio_main.run @@ fun env ->
21
21
-
scan (Eio.Stdenv.cwd env)
32
32
+
scan (Eio.Stdenv.cwd env) Format.std_formatter