class Byebug::ListCommand - byebug-11.0.1 Documentation

# class Byebug::ListCommand

List parts of the source code.

### Public Class Methods

description() click to toggle source

    # File lib/byebug/commands/list.rb, line 22
        def self.description
          <<-DESCRIPTION
            l[ist][[-=]][ nn-mm]

            #{short_description}

            Lists lines 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.
          DESCRIPTION
        end

regexp() click to toggle source

    # File lib/byebug/commands/list.rb, line 18
    def self.regexp
      /^\s* l(?:ist)? (?:\s*([-=])|\s+(\S+))? \s*$/x
    end

short_description() click to toggle source

    # File lib/byebug/commands/list.rb, line 36
    def self.short_description
      "Lists lines of source code"
    end

### Public Instance Methods

execute() click to toggle source

    # File lib/byebug/commands/list.rb, line 40
    def execute
      msg = "No sourcefile available for #{frame.file}"
      raise(msg) unless File.exist?(frame.file)

      b, e = range(@match[2])

      display_lines(b, e)

      processor.prev_line = b
    end

### Private Instance Methods

auto_range(direction) click to toggle source

Set line range to be printed by list

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

    # File lib/byebug/commands/list.rb, line 79
    def auto_range(direction)
      prev_line = processor.prev_line

      if direction == "=" || prev_line.nil?
        source_file_formatter.range_around(frame.line)
      else
        source_file_formatter.range_from(move(prev_line, size, direction))
      end
    end

display_lines(min, max) click to toggle source

Show a range of lines in the current file.

@param min [Integer] Lower bound @param max [Integer] Upper bound

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

      puts source_file_formatter.lines(min, max).join
    end

lower_bound(range) click to toggle source

@param range [String] A string with an integer range format

@return [String] The lower bound of the given range

    # File lib/byebug/commands/list.rb, line 126
    def lower_bound(range)
      split_range(range)[0]
    end

move(line, size, direction = "+") click to toggle source

    # File lib/byebug/commands/list.rb, line 105
    def move(line, size, direction = "+")
      line.send(direction, size)
    end

parse_range(input) click to toggle source

    # File lib/byebug/commands/list.rb, line 89
    def parse_range(input)
      first, err = get_int(lower_bound(input), "List", 1, max_line)
      raise(err) unless first

      if upper_bound(input)
        last, err = get_int(upper_bound(input), "List", 1, max_line)
        raise(err) unless last

        last = amend_final(last)
      else
        first -= (size / 2)
      end

      [first, last || move(first, size - 1)]
    end

range(input) 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 60
    def range(input)
      return auto_range(@match[1] || "+") unless input

      b, e = parse_range(input)
      raise("Invalid line range") unless valid_range?(b, e)

      [b, e]
    end

source_file_formatter() click to toggle source

    # File lib/byebug/commands/list.rb, line 152
    def source_file_formatter
      @source_file_formatter ||= SourceFileFormatter.new(
        frame.file,
        ->(n) { n == frame.line ? "=>" : "  " }
      )
    end

split_range(str) click to toggle source

@param str [String] A string with an integer range format

@return [Array] The upper & lower bounds of the given range

    # File lib/byebug/commands/list.rb, line 144
    def split_range(str)
      str.split(/[-,]/)
    end

upper_bound(range) click to toggle source

@param range [String] A string with an integer range format

@return [String] The upper bound of the given range

    # File lib/byebug/commands/list.rb, line 135
    def upper_bound(range)
      split_range(range)[1]
    end

valid_range?(first, last) click to toggle source

    # File lib/byebug/commands/list.rb, line 69
    def valid_range?(first, last)
      first <= last && (1..max_line).cover?(first) && (1..max_line).cover?(last)
    end