Skip to content

Commit 33d4e01

Browse files
authored
Merge pull request #745 from talex5/fs-example
examples/fs: show how to read files while scanning
2 parents 3784076 + c459782 commit 33d4e01

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

examples/fs/main.ml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
(* Walks the directory tree rooted at the current directory,
2-
displaying all directory names (skipping hidden directories and `_build`). *)
3-
4-
open Eio.Std
1+
(* Walk the directory tree rooted at the current directory,
2+
showing a summary for any .mli files. *)
53

64
let ( / ) = Eio.Path.( / )
75

8-
let rec scan t =
6+
let is_doc_comment = String.starts_with ~prefix:"(** "
7+
8+
(* Print the first line of [t]'s doc-comment, if any *)
9+
let scan_mli t f =
10+
Eio.Path.with_lines t (fun lines ->
11+
Seq.find is_doc_comment lines
12+
|> Option.iter (fun line ->
13+
let stop = String.index_from_opt line 4 '*' |> Option.value ~default:(String.length line) in
14+
Format.fprintf f "%a: %s@." Eio.Path.pp t (String.sub line 4 (stop - 4))
15+
)
16+
)
17+
18+
(* Walk the tree rooted at [t] and scan any .mli files found. *)
19+
let rec scan t f =
920
match Eio.Path.kind ~follow:false t with
1021
| `Directory ->
11-
traceln "Visiting %a" Eio.Path.pp t;
1222
Eio.Path.read_dir t |> List.iter (function
13-
| "_build" -> ()
14-
| item when String.starts_with ~prefix:"." item -> ()
15-
| item -> scan (t / item)
16-
)
23+
| "_build" | "_opam" -> () (* Don't examine these directories *)
24+
| item when String.starts_with ~prefix:"." item -> () (* Skip hidden items *)
25+
| item -> scan (t / item) f
26+
)
27+
| `Regular_file when Filename.check_suffix (snd t) ".mli" -> scan_mli t f
1728
| _ -> ()
1829

1930
let () =
2031
Eio_main.run @@ fun env ->
21-
scan (Eio.Stdenv.cwd env)
32+
scan (Eio.Stdenv.cwd env) Format.std_formatter

0 commit comments

Comments
 (0)