walk.cr

A Crystal library for walking a directory recursively

walk.cr

walk.cr is a Crystal library for walking a directory recursively. Comes with support for top-down and bottom-up traversal, and mechanisms for pruning the entries in the directory tree.

Dependencies

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  walk:
    github: alexherbo2/walk.cr
  1. Run shards install.

Usage

Example – Recursively iterate over the given directory and print the path for each entry:

require "walk"

Walk::Down.new("src").each do |path|
  puts path
end

Output example:

src
src/walk
src/walk/down.cr
src/walk/up.cr
src/walk.cr

Example – Skip hidden files and directories:

require "walk"

def hidden_file?(path)
  path.expand.basename.starts_with?('.')
end

Walk::Down.new(".")

.filter do |path|
  !hidden_file?(path)
end

.each do |path|
  puts path
end

Output example:

.
README.md
shard.yml
spec
spec/walk_spec.cr
src
src/walk.cr

Example – Find Git root:

require "walk"

def git_root?(path)
  Dir.exists?(path / ".git")
end

Walk::Up.new("src/walk.cr").find do |path|
  git_root?(path)
end

Result example:

Path["."]

See the source for a complete reference.

Credits

Repository

walk.cr

Owner
Statistic
  • 2
  • 0
  • 0
  • 2
  • 0
  • almost 3 years ago
  • July 14, 2021
License

The Unlicense

Links
Synced at

Tue, 07 May 2024 05:04:17 GMT

Languages