From 7d718405a6c3b02745201ebad02002936298fc82 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Tue, 21 May 2024 11:18:23 +0100 Subject: [PATCH] Add examples/fs showing how to walk a directory tree --- examples/fs/dune | 3 +++ examples/fs/main.ml | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 examples/fs/dune create mode 100644 examples/fs/main.ml diff --git a/examples/fs/dune b/examples/fs/dune new file mode 100644 index 000000000..fb95515c7 --- /dev/null +++ b/examples/fs/dune @@ -0,0 +1,3 @@ +(executable + (name main) + (libraries eio_main)) diff --git a/examples/fs/main.ml b/examples/fs/main.ml new file mode 100644 index 000000000..d91e29d89 --- /dev/null +++ b/examples/fs/main.ml @@ -0,0 +1,21 @@ +(* Walks the directory tree rooted at the current directory, + displaying all directory names (skipping hidden directories and `_build`). *) + +open Eio.Std + +let ( / ) = Eio.Path.( / ) + +let rec scan t = + 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) + ) + | _ -> () + +let () = + Eio_main.run @@ fun env -> + scan (Eio.Stdenv.cwd env)