walk

Version, currently master branch1 version
  • master branchlatestJul 15, 2021

github.com/alexherbo2/walk.cr

A Crystal library for walking a directory recursively

3 stars
1 dependent
License: Unlicense

Installation

# Add this to your shard.yml
dependencies:
  walk:
    github: alexherbo2/walk.cr
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
1.0.0
License
Unlicense

Dependencies

This version declares no dependencies.

README

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