Class: TTY::Tree::PathWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/tree/path_walker.rb

Overview

Walk and collect nodes from directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PathWalker

Create a PathWalker



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tty/tree/path_walker.rb', line 22

def initialize(options = {})
  @files_count = 0
  @dirs_count  = 0
  @nodes       = []
  @filters     = []
  @level       = options.fetch(:level) { -1 }
  @file_limit  = options.fetch(:file_limit) { - 1 }

  unless options[:show_hidden]
    add_filter(-> (p) { !p.basename.to_s.start_with?('.') })
  end

  if options[:only_dirs]
    add_filter(-> (p) { p.directory? })
  end
end

Instance Attribute Details

#dirs_countObject (readonly)



17
18
19
# File 'lib/tty/tree/path_walker.rb', line 17

def dirs_count
  @dirs_count
end

#files_countObject (readonly)



15
16
17
# File 'lib/tty/tree/path_walker.rb', line 15

def files_count
  @files_count
end

#nodesObject (readonly)



13
14
15
# File 'lib/tty/tree/path_walker.rb', line 13

def nodes
  @nodes
end

Instance Method Details

#add_filter(filter) ⇒ Object



39
40
41
# File 'lib/tty/tree/path_walker.rb', line 39

def add_filter(filter)
  @filters << filter
end

#traverse(path) ⇒ Object

Traverse given path recursively

Parameters:

  • path (String)

    the path to traverse



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tty/tree/path_walker.rb', line 49

def traverse(path)
  root_path  = Pathname.new(path)
  empty_path = Pathname.new('')

  unless root_path.directory?
    raise ArgumentError, "#{root_path} is not a directory path"
  end

  @nodes << Node.new(root_path, empty_path, '', 0)

  walk(root_path, root_path.children, '', 1)
end