Class: Graml::Finder
- Inherits:
-
Object
- Object
- Graml::Finder
- Defined in:
- lib/graml/finder.rb
Constant Summary collapse
- DEFAULT_EXTENSIONS =
%w[yml yaml].freeze
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Instance Method Summary collapse
- #call ⇒ Object
- #colorize(string, color) ⇒ Object
- #filter_by_search(items) ⇒ Object
-
#initialize(input, search: nil, extensions: nil, no_color: false, skip_hidden: true) ⇒ Finder
constructor
A new instance of Finder.
- #matched_string(string) ⇒ Object
- #no_color? ⇒ Boolean
- #prepare_extensions(extensions) ⇒ Object
- #print_item(item, line_number_padding) ⇒ Object
- #regex ⇒ Object
Constructor Details
#initialize(input, search: nil, extensions: nil, no_color: false, skip_hidden: true) ⇒ Finder
Returns a new instance of Finder.
9 10 11 12 13 14 15 |
# File 'lib/graml/finder.rb', line 9 def initialize(input, search: nil, extensions: nil, no_color: false, skip_hidden: true) @input = input @search = search @extensions = prepare_extensions(extensions || DEFAULT_EXTENSIONS) @no_color = no_color @skip_hidden = skip_hidden end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
7 8 9 |
# File 'lib/graml/finder.rb', line 7 def extensions @extensions end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/graml/finder.rb', line 7 def input @input end |
#search ⇒ Object (readonly)
Returns the value of attribute search.
7 8 9 |
# File 'lib/graml/finder.rb', line 7 def search @search end |
Instance Method Details
#call ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/graml/finder.rb', line 27 def call TreeIterator.new(input, skip_hidden: @skip_hidden).each do |pathname| next unless extensions.include?(pathname.extname) items = FlattenYaml.new(pathname).flatten items = filter_by_search(items) if search next unless items.any? line_number_padding = items.map(&:line).max.to_s.length puts colorize(pathname.to_s, :light_blue) # TODO: allow sorting by line number or key # items.sort_by(&:line).each do |item| items.each do |item| print_item(item, line_number_padding) end puts "\n" $stdout.flush end end |
#colorize(string, color) ⇒ Object
67 68 69 |
# File 'lib/graml/finder.rb', line 67 def colorize(string, color) no_color? ? string : string.colorize(color) end |
#filter_by_search(items) ⇒ Object
61 62 63 64 65 |
# File 'lib/graml/finder.rb', line 61 def filter_by_search(items) items.select do |item| regex.match?(item.key) end end |
#matched_string(string) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/graml/finder.rb', line 71 def matched_string(string) return string if search.nil? || no_color? regex.match(string).to_a&.each do |match| string = string.gsub(/#{match}/, match.colorize(:light_red)) end string end |
#no_color? ⇒ Boolean
17 18 19 |
# File 'lib/graml/finder.rb', line 17 def no_color? ENV["TERM"] == "dumb" || ENV["NO_COLOR"] == "true" || @no_color == true end |
#prepare_extensions(extensions) ⇒ Object
21 22 23 24 25 |
# File 'lib/graml/finder.rb', line 21 def prepare_extensions(extensions) extensions.map do |ext| ext.start_with?(".") ? ext : ".#{ext}" end end |
#print_item(item, line_number_padding) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graml/finder.rb', line 50 def print_item(item, line_number_padding) line_number = colorize("#{item.line.to_s.rjust(line_number_padding)}: ", :light_yellow) value = if item.value.length.positive? colorize("=> #{item.value}", :gray) else "" end puts "#{line_number} #{matched_string(item.key)} #{value}\n" end |
#regex ⇒ Object
80 81 82 |
# File 'lib/graml/finder.rb', line 80 def regex @regex ||= Regexp.new(search) end |