Sha256: 7445dcb6f73a27157d9e950a34eea8d74aba87fd32b0de570489da7f1c38a5f3

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

#          Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

module Ramaze

  # Gives you some context around a specific line in a file.
  # the size argument works in both directions + the actual line,
  # size = 2 gives you 5 lines of source, the returned array has the
  # following format.
  #   [
  #     line = [
  #              lineno           = Integer,
  #              line             = String,
  #              is_searched_line = (lineno == initial_lineno)
  #            ],
  #     ...,
  #     ...
  #   ]
  # Example:
  #  __caller_lines__('/usr/lib/ruby/1.8/debug.rb', 122, 2) # ->
  #   [
  #     [ 120, "  def check_suspend",                               false ],
  #     [ 121, "    return if Thread.critical",                     false ],
  #     [ 122, "    while (Thread.critical = true; @suspend_next)", true  ],
  #     [ 123, "      DEBUGGER__.waiting.push Thread.current",      false ],
  #     [ 124, "      @suspend_next = false",                       false ]
  #   ]

  def self.caller_lines(file, line, size = 4)
    return [[0, file, true]] if file == '(eval)'
    lines = File.readlines(File.expand_path(file)) rescue []
    current = line.to_i - 1

    first = current - size
    first = first < 0 ? 0 : first

    last = current + size
    last = last > lines.size ? lines.size : last

    log = lines[first..last] || []

    area = []

    log.each_with_index do |line, index|
      index = index + first + 1
      area << [index, line.chomp, index == current + 1]
    end

    area
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ramaze-0.0.9 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.1.3 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.1.0 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.1.1 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.1.2 lib/ramaze/snippets/ramaze/caller_lines.rb