Sha256: e1de13668c1aa98838c621cea11381084ea7eef5fcae907256065d48f650a019

Contents?: true

Size: 1.58 KB

Versions: 22

Compression:

Stored size: 1.58 KB

Contents

#          Copyright (c) 2008 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

22 entries across 22 versions & 5 rubygems

Version Path
Pistos-ramaze-2008.09 lib/ramaze/snippets/ramaze/caller_lines.rb
Pistos-ramaze-2008.12 lib/ramaze/snippets/ramaze/caller_lines.rb
Pistos-ramaze-2009.01 lib/ramaze/snippets/ramaze/caller_lines.rb
Pistos-ramaze-2009.02 lib/ramaze/snippets/ramaze/caller_lines.rb
clivecrous-ramaze-0.3.9.5 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2008.07 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2008.08 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2008.09 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2008.10 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2008.12 lib/ramaze/snippets/ramaze/caller_lines.rb
manveru-ramaze-2009.01 lib/ramaze/snippets/ramaze/caller_lines.rb
ptomato-ramaze-2009.02.1 lib/ramaze/snippets/ramaze/caller_lines.rb
ptomato-ramaze-2009.02 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.3.0 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.3.5 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-2008.06 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-2009.01 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.3.9.1 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-0.3.9 lib/ramaze/snippets/ramaze/caller_lines.rb
ramaze-2008.11 lib/ramaze/snippets/ramaze/caller_lines.rb