Class: Enolib::TerminalReporter

Inherits:
Reporter
  • Object
show all
Defined in:
lib/enolib/reporters/terminal_reporter.rb

Instance Method Summary collapse

Methods inherited from Reporter

#indicate_line, #question_line, #report_comments, #report_element, #report_elements, #report_line, #report_missing_element, #report_multiline_value, #snippet

Constructor Details

#initialize(context) ⇒ TerminalReporter

Returns a new instance of TerminalReporter.

[View source]

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/enolib/reporters/terminal_reporter.rb', line 49

def initialize(context)
  super(context)

  highest_shown_line_number = @snippet.length

  @snippet.reverse.each_with_index do |tag, index|
    if tag && tag != :omission
      highest_shown_line_number = index + 1
      break
    end
  end

  @line_number_padding = [4, highest_shown_line_number.to_s.length].max
  @header = ''

  if @context.source
    @header += "#{BLACK + BRIGHT_RED_BACKGROUND} #{INDICATORS[EMPHASIZE]} #{' '.rjust(@line_number_padding)} #{RESET} #{BOLD}#{@context.source}#{RESET}\n"
  end
end

Instance Method Details

[View source]

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/enolib/reporters/terminal_reporter.rb', line 69

def print_line(line, tag)
  if tag == :omission
    return "#{DIM + BRIGHT_BLACK_BACKGROUND}#{'...'.rjust(@line_number_padding + 2)}  #{RESET}"
  end

  number = (line + HUMAN_INDEXING).to_s
  instruction = @index[line]

  content = ''
  if instruction
    if instruction[:type] == :comment || instruction[:type] == :unparsed
      content = BRIGHT_BLACK + @context.input[instruction[:ranges][:line][RANGE_BEGIN]...instruction[:ranges][:line][RANGE_END]] + RESET
    else
      content = @context.input[instruction[:ranges][:line][RANGE_BEGIN]...instruction[:ranges][:line][RANGE_END]]

      instruction[:ranges].sort_by { |_type, range| range[0] }.reverse_each do |type, range|
        next if type == :line

        before = content[0...range[RANGE_BEGIN] - instruction[:ranges][:line][RANGE_BEGIN]]
        after = content[range[RANGE_END] - instruction[:ranges][:line][RANGE_BEGIN]..-1]

        # TODO: Here and everywhere: Why is RANGE_BEGIN without anything and like that even working? Check soundness
        content = before + RANGE_STYLE[type] + @context.input[range[RANGE_BEGIN]...range[RANGE_END]] + RESET + after
      end
    end
  end

  "#{GUTTER_STYLE[tag]} #{INDICATORS[tag]} #{number.rjust(@line_number_padding)} #{RESET} #{content}"
end