File: file.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 (63 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: true

module Byebug
  module Helpers
    #
    # Utilities for interaction with files
    #
    module FileHelper
      #
      # Reads lines of source file +filename+ into an array
      #
      def get_lines(filename)
        File.foreach(filename).reduce([]) { |acc, elem| acc << elem.chomp }
      end

      #
      # Reads line number +lineno+ from file named +filename+
      #
      def get_line(filename, lineno)
        File.open(filename) do |f|
          f.gets until f.lineno == lineno - 1
          f.gets
        end
      end

      #
      # Returns the number of lines in file +filename+ in a portable,
      # one-line-at-a-time way.
      #
      def n_lines(filename)
        File.foreach(filename).reduce(0) { |acc, _elem| acc + 1 }
      end

      #
      # Regularize file name.
      #
      def normalize(filename)
        return filename if virtual_file?(filename)

        return File.basename(filename) if Setting[:basename]

        File.exist?(filename) ? File.realpath(filename) : filename
      end

      #
      # A short version of a long path
      #
      def shortpath(fullpath)
        components = Pathname(fullpath).each_filename.to_a
        return fullpath if components.size <= 2

        File.join("...", components[-3..-1])
      end

      #
      # True for special files like -e, false otherwise
      #
      def virtual_file?(name)
        ["(irb)", "-e", "(byebug)", "(eval)"].include?(name)
      end
    end
  end
end