Class: DeadEnd::DisplayCodeWithLineNumbers

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_end/display_code_with_line_numbers.rb

Overview

Outputs code with highlighted lines

Whatever is passed to this class will be rendered even if it is “marked invisible” any filtering of output should be done before calling this class.

DisplayCodeWithLineNumbers.new(
  lines: lines,
  highlight_lines: [lines[2], lines[3]]
).call
# =>
    1
    2  def cat
  ❯ 3    Dir.chdir
  ❯ 4    end
    5  end
    6

Constant Summary collapse

TERMINAL_HIGHLIGHT =

Bold, italics

"\e[1;3m"
TERMINAL_END =
"\e[0m"

Instance Method Summary collapse

Constructor Details

#initialize(lines:, highlight_lines: [], terminal: false) ⇒ DisplayCodeWithLineNumbers

Returns a new instance of DisplayCodeWithLineNumbers.



25
26
27
28
29
30
# File 'lib/dead_end/display_code_with_line_numbers.rb', line 25

def initialize(lines:, highlight_lines: [], terminal: false)
  @lines = Array(lines).sort
  @terminal = terminal
  @highlight_line_hash = Array(highlight_lines).each_with_object({}) { |line, h| h[line] = true }
  @digit_count = @lines.last&.line_number.to_s.length
end

Instance Method Details

#callObject



32
33
34
35
36
# File 'lib/dead_end/display_code_with_line_numbers.rb', line 32

def call
  @lines.map do |line|
    format_line(line)
  end.join
end