File: source_file_formatter.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require_relative "helpers/file"
require_relative "setting"

module Byebug
  #
  # Formats specific line ranges in a source file
  #
  class SourceFileFormatter
    include Helpers::FileHelper

    attr_reader :file, :annotator

    def initialize(file, annotator)
      @file = file
      @annotator = annotator
    end

    def lines(min, max)
      File.foreach(file).with_index.map do |line, lineno|
        next unless (min..max).cover?(lineno + 1)

        format(
          "%<annotation>s %<lineno>#{max.to_s.size}d: %<source>s",
          annotation: annotator.call(lineno + 1),
          lineno: lineno + 1,
          source: line
        )
      end
    end

    def lines_around(center)
      lines(*range_around(center))
    end

    def range_around(center)
      range_from(center - size / 2)
    end

    def range_from(min)
      first = amend_initial(min)

      [first, first + size - 1]
    end

    def amend_initial(line)
      amend(line, max_initial_line)
    end

    def amend_final(line)
      amend(line, max_line)
    end

    def max_initial_line
      max_line - size + 1
    end

    def max_line
      @max_line ||= n_lines(file)
    end

    def size
      [Setting[:listsize], max_line].min
    end

    def amend(line, ceiling)
      [ceiling, [1, line].max].min
    end
  end
end