Sha256: c2f0b335ba6f0e3211b05439bd8ba04fe08fe39a1c0d3c228a43ecbb06345cf8

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

module CodeBuddy
  class StackFrame

    CODE_WINDOW = 10

    attr_reader :path
    attr_reader :line

    def initialize(exception_string)
      if exception_string =~ /\s*([^:\s]+):([0-9]+)\s*/
        @path = $1
        @line = $2.to_i
      else
        @path = exception_string
        @line = 0
      end
      code
    end

    def code
      @code ||= begin
        html_args = { :line_numbers => :inline, :wrap => :span }
        lines_of_code = File.new(path).readlines

        first_line_to_show      = [1, line-CODE_WINDOW].max
        last_line_to_show       = [lines_of_code.length, line + 1 + CODE_WINDOW].min
        code_to_show            = lines_of_code[first_line_to_show-1 .. last_line_to_show-1]
        formatted_lines = CodeRay.scan(code_to_show,   :ruby).
                                       html(:line_numbers      => :inline,
                                            :wrap              => :span,
                                            :bold_every        => false,
                                            :line_number_start => first_line_to_show)
        
        highlighted_line = line - first_line_to_show + 1
        formatted_lines_array = formatted_lines.split("\n")
        formatted_lines_array[highlighted_line-1] = "<span class='container selected'>#{formatted_lines_array[highlighted_line-1]}<span class='overlay'></span></span>"
        formatted_lines_array.join("\n")
      rescue => exception
        "<span class=\"coderay\">Unable to read file:\n&nbsp;\"#{@path}\"</span>"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
code_buddy-0.0.6 lib/code_buddy/stack_frame.rb
code_buddy-0.0.5 lib/code_buddy/stack_frame.rb
code_buddy-0.0.4 lib/code_buddy/stack_frame.rb
code_buddy-0.0.3 lib/code_buddy/stack_frame.rb
code_buddy-0.0.2 lib/code_buddy/stack_frame.rb
code_buddy-0.0.1 lib/code_buddy/stack_frame.rb