class Byebug::ListCommand

List parts of the source code.

Public Class Methods

description() click to toggle source
# File lib/byebug/commands/list.rb, line 31
def description
  prettify <<-EOD
    l[ist][[-=]][ nn-mm]

    Lists lines of code forward from current line or from the place where
    code was last listed. If "list-" is specified, lists backwards
    instead. If "list=" is specified, lists from current line regardless
    of where code was last listed. A line range can also be specified to
    list specific sections of code.
  EOD
end
names() click to toggle source
# File lib/byebug/commands/list.rb, line 27
def names
  %w(list)
end

Public Instance Methods

execute() click to toggle source
# File lib/byebug/commands/list.rb, line 12
def execute
  exist = File.exist?(@state.file)
  return errmsg "No sourcefile available for #{@state.file}\n" unless exist

  @match ||= match('list')
  max_lines = n_lines(@state.file)
  b, e = range(@match[2], max_lines)
  return errmsg('Invalid line range') unless valid_range?(b, e, max_lines)

  display_lines(b, e)

  @state.prev_line = b
end
regexp() click to toggle source
# File lib/byebug/commands/list.rb, line 8
def regexp
  /^\s* l(?:ist)? (?:\s*([-=])|\s+(\S+))? \s*$/x
end

Private Instance Methods

amend(line, max_line) click to toggle source
# File lib/byebug/commands/list.rb, line 96
def amend(line, max_line)
  return 1 if line < 1

  [max_line, line].min
end
display_lines(min, max) click to toggle source

Show lines in @state.file from line number <min> to line number <max>.

# File lib/byebug/commands/list.rb, line 115
def display_lines(min, max)
  puts "\n[#{min}, #{max}] in #{@state.file}"

  File.foreach(@state.file).with_index do |line, lineno|
    return if lineno + 1 > max
    next unless (min..max).include?(lineno + 1)

    mark = lineno + 1 == @state.line ? '=> ' : '   '
    puts format("#{mark}%#{max.to_s.size}d: %s", lineno + 1, line)
  end
end
lower(size, direction = '+') click to toggle source
# File lib/byebug/commands/list.rb, line 102
def lower(size, direction = '+')
  return @state.line - size / 2 if direction == '=' || !@state.prev_line

  move(@state.prev_line, size, direction)
end
move(line, size, direction = '+') click to toggle source
# File lib/byebug/commands/list.rb, line 108
def move(line, size, direction = '+')
  line.send(direction, size)
end
parse_range(input, size, max_line) click to toggle source
# File lib/byebug/commands/list.rb, line 80
def parse_range(input, size, max_line)
  first, err = get_int(input.split(/[-,]/)[0], 'List', 1, max_line)
  return [-1, -1] if err

  if input.split(/[-,]/)[1]
    last, _ = get_int(input.split(/[-,]/)[1], 'List', 1, max_line)
    return [-1, -1] unless last

    last = amend(last, max_line)
  else
    first -= (size / 2)
  end

  [first, last || move(first, size - 1)]
end
range(input, max_line) click to toggle source

Line range to be printed by `list`.

If <input> is set, range is parsed from it.

Otherwise it's automatically chosen.

# File lib/byebug/commands/list.rb, line 53
def range(input, max_line)
  size = [Setting[:listsize], max_line].min

  return set_range(size, max_line) unless input

  parse_range(input, size, max_line)
end
set_range(size, max_line) click to toggle source

Set line range to be printed by list

@param size - number of lines to be printed @param max_line - max line number that can be printed

@return first line number to list @return last line number to list

# File lib/byebug/commands/list.rb, line 74
def set_range(size, max_line)
  first = amend(lower(size, @match[1] || '+'), max_line - size + 1)

  [first, move(first, size - 1)]
end
valid_range?(first, last, max) click to toggle source
# File lib/byebug/commands/list.rb, line 61
def valid_range?(first, last, max)
  first <= last && (1..max).include?(first) && (1..max).include?(last)
end