PHP 8.3.31
Preview: display_code_with_line_numbers.rb Size: 1.77 KB
/proc/self/root/opt/alt/ruby32/share/ruby/syntax_suggest/display_code_with_line_numbers.rb

# frozen_string_literal: true

module SyntaxSuggest
  # 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
  class DisplayCodeWithLineNumbers
    TERMINAL_HIGHLIGHT = "\e[1;3m" # Bold, italics
    TERMINAL_END = "\e[0m"

    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

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

    private def format_line(code_line)
      # Handle trailing slash lines
      code_line.original.lines.map.with_index do |contents, i|
        format(
          empty: code_line.empty?,
          number: (code_line.number + i).to_s,
          contents: contents,
          highlight: @highlight_line_hash[code_line]
        )
      end.join
    end

    private def format(contents:, number:, empty:, highlight: false)
      string = +""
      string << if highlight
        "> "
      else
        "  "
      end

      string << number.rjust(@digit_count).to_s
      if empty
        string << contents
      else
        string << "  "
        string << TERMINAL_HIGHLIGHT if @terminal && highlight
        string << contents
        string << TERMINAL_END if @terminal
      end
      string
    end
  end
end

Directory Contents

Dirs: 1 × Files: 25

Name Size Perms Modified Actions
capture DIR
- drwxr-xr-x 2026-04-30 06:35:36
Edit Download
5.16 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
6.90 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
4.87 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
6.67 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
8.74 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
3.12 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
2.13 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
5.62 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
6.46 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
3.82 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
3.00 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.77 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.75 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
2.41 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
4.06 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.12 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.50 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.50 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.42 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
1.26 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
2.00 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
837 B lrw-r--r-- 2026-04-07 14:12:52
Edit Download
2.92 KB lrw-r--r-- 2026-04-07 14:12:52
Edit Download
705 B lrw-r--r-- 2026-04-07 14:12:52
Edit Download
76 B lrw-r--r-- 2026-04-07 14:12:52
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).