Class: Cheatorious::Writer::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/cheatorious/writer/tree.rb

Instance Method Summary collapse

Constructor Details

#initializeTree

Returns a new instance of Tree.



7
8
9
10
11
12
13
14
15
16
# File 'lib/cheatorious/writer/tree.rb', line 7

def initialize
  @section_stack = []
  @result        = ""
  @header_footer = []
  @tree          = []
  @query         = nil
  @options       = {}
  @color         = :yellow
  @add_empty_line = true
end

Instance Method Details

#entry(name, *values) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cheatorious/writer/tree.rb', line 62

def entry(name, *values)
  value_text = values.join(", ")
  if @options['reverse']
    value_text = paint(value_text,@query) if @query
  elsif !@options['section']
    name = paint(name,@query) if @query
  end
  indent = @section_stack.size == 0 ? 0 : @section_stack.size+1
  @tree << [indent, "=>".foreground(:blue) + " " + name]
  value_text.split(", ").each do |v|
    @tree << [indent+1, v]
  end
end


44
45
46
47
48
49
50
# File 'lib/cheatorious/writer/tree.rb', line 44

def footer
  line
  line "-" * 80
  line "generated by Cheatorious (https://github.com/lfcipriani/cheatorious)"
  @header_footer << @result
  @result = nil
end

#header(name, author = "", version = "", description = "") ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cheatorious/writer/tree.rb', line 18

def header(name, author = "", version = "", description = "")
  line "-" * 80
  line "#{name} (#{version})"
  line
  line "Author     : #{author[0]} (#{author[1]})"
  line "Description: #{description}"
  line "-" * 80
  line
  @header_footer << @result
  @add_empty_line = true
  @result = ""
end

#resultObject



76
77
78
79
80
# File 'lib/cheatorious/writer/tree.rb', line 76

def result
  @result = @header_footer[0]
  @result += Hirb::Helpers::Tree.render(@tree, :type => :directory, :multi_line_nodes => true)
  @result += @header_footer[1] || ""
end

#search_header(query, results_count, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cheatorious/writer/tree.rb', line 31

def search_header(query, results_count, options)
  @query = query
  @options = options
  search_type = (options.keys - ["writer"]).join(", ")
  search_type += " " if search_type.size > 0
  line "Your #{search_type}search for '#{query.dup.foreground(@color)}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
  line "Your #{search_type}search for '#{query.dup.foreground(@color)}' doesn't returned any result. Try with another keyword." if results_count == 0
  line
  @header_footer << @result
  @add_empty_line = true
  @result = ""
end

#section_endObject



58
59
60
# File 'lib/cheatorious/writer/tree.rb', line 58

def section_end
  @section_stack.pop
end

#section_start(section) ⇒ Object



52
53
54
55
56
# File 'lib/cheatorious/writer/tree.rb', line 52

def section_start(section)
  section = paint(section,@query) if @query && @options['section']
  @tree << [@section_stack.size, "#".foreground(:red) + " " + section]
  @section_stack.push(section)
end