-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from talex5/fs-example
examples/fs: show how to read files while scanning
- Loading branch information
Showing
1 changed file
with
22 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
(* Walks the directory tree rooted at the current directory, | ||
displaying all directory names (skipping hidden directories and `_build`). *) | ||
|
||
open Eio.Std | ||
(* Walk the directory tree rooted at the current directory, | ||
showing a summary for any .mli files. *) | ||
|
||
let ( / ) = Eio.Path.( / ) | ||
|
||
let rec scan t = | ||
let is_doc_comment = String.starts_with ~prefix:"(** " | ||
|
||
(* Print the first line of [t]'s doc-comment, if any *) | ||
let scan_mli t f = | ||
Eio.Path.with_lines t (fun lines -> | ||
Seq.find is_doc_comment lines | ||
|> Option.iter (fun line -> | ||
let stop = String.index_from_opt line 4 '*' |> Option.value ~default:(String.length line) in | ||
Format.fprintf f "%a: %s@." Eio.Path.pp t (String.sub line 4 (stop - 4)) | ||
) | ||
) | ||
|
||
(* Walk the tree rooted at [t] and scan any .mli files found. *) | ||
let rec scan t f = | ||
match Eio.Path.kind ~follow:false t with | ||
| `Directory -> | ||
traceln "Visiting %a" Eio.Path.pp t; | ||
Eio.Path.read_dir t |> List.iter (function | ||
| "_build" -> () | ||
| item when String.starts_with ~prefix:"." item -> () | ||
| item -> scan (t / item) | ||
) | ||
| "_build" | "_opam" -> () (* Don't examine these directories *) | ||
| item when String.starts_with ~prefix:"." item -> () (* Skip hidden items *) | ||
| item -> scan (t / item) f | ||
) | ||
| `Regular_file when Filename.check_suffix (snd t) ".mli" -> scan_mli t f | ||
| _ -> () | ||
|
||
let () = | ||
Eio_main.run @@ fun env -> | ||
scan (Eio.Stdenv.cwd env) | ||
scan (Eio.Stdenv.cwd env) Format.std_formatter |