File: command.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 (111 lines) | stat: -rw-r--r-- 2,270 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require "forwardable"
require_relative "helpers/string"

module Byebug
  #
  # Parent class of all byebug commands.
  #
  # Subclass it and name the subclass ending with the word Command to implement
  # your own custom command.
  #
  # @example Define a custom command
  #
  # class MyCustomCommand < Command
  #   def self.regexp
  #     /custom_regexp/
  #   end
  #
  #   def self.description
  #     "Custom long desc"
  #   end
  #
  #   def.short_description
  #     "Custom short desc"
  #   end
  #
  #   def execute
  #     # My command's implementation
  #   end
  # end
  #
  class Command
    extend Forwardable

    attr_reader :processor

    def initialize(processor, input = self.class.to_s)
      @processor = processor
      @match = match(input)
    end

    def context
      @context ||= processor.context
    end

    def frame
      @frame ||= context.frame
    end

    def arguments
      @match[0].split(" ").drop(1).join(" ")
    end

    def_delegators "self.class", :help, :match

    def_delegator "processor.printer", :print, :pr
    def_delegator "processor.printer", :print_collection, :prc
    def_delegator "processor.printer", :print_variables, :prv

    def_delegators "processor.interface", :errmsg, :puts, :print, :confirm

    class << self
      include Helpers::StringHelper

      #
      # Special methods to allow command filtering in processors
      #
      attr_accessor :allow_in_control, :allow_in_post_mortem

      attr_writer :always_run

      def always_run
        @always_run ||= 0
      end

      #
      # Name of the command, as executed by the user.
      #
      def to_s
        name
          .split("::")
          .map { |n| n.gsub(/Command$/, "").downcase if /Command$/.match?(n) }
          .compact
          .join(" ")
      end

      def columnize(width)
        format(
          "  %-<name>#{width}s -- %<description>s\n",
          name: to_s,
          description: short_description
        )
      end

      #
      # Default help text for a command.
      #
      def help
        prettify(description)
      end

      #
      # Command's regexp match against an input
      #
      def match(input)
        regexp.match(input)
      end
    end
  end
end