Module: Thor::Interactive::Command

Defined in:
lib/thor/interactive/command.rb

Overview

Mixin to add an interactive command to Thor applications

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/thor/interactive/command.rb', line 9

def self.included(base)
  base.extend(ClassMethods)
  base.class_eval do
    desc "interactive", "Start an interactive REPL for this application"
    option :prompt, type: :string, desc: "Custom prompt for the REPL"
    option :history_file, type: :string, desc: "Custom history file location"
    
    def interactive
      # Check for nested sessions unless explicitly allowed
      if ENV['THOR_INTERACTIVE_SESSION'] && !self.class.interactive_options[:allow_nested]
        puts "Already in an interactive session."
        puts "To allow nested sessions, configure with: configure_interactive(allow_nested: true)"
        return
      end
      
      opts = self.class.interactive_options.dup
      opts[:prompt] = options[:prompt] || options["prompt"] if options[:prompt] || options["prompt"]
      opts[:history_file] = options[:history_file] || options["history_file"] if options[:history_file] || options["history_file"]
      
      if opts[:ui_mode] == :tui
        require_relative "tui"
        if Thor::Interactive::TUI.available?
          require_relative "tui/ratatui_shell"
          Thor::Interactive::TUI::RatatuiShell.new(self.class, opts).start
        else
          warn "ratatui_ruby gem not found, falling back to standard shell"
          Thor::Interactive::Shell.new(self.class, opts).start
        end
      else
        Thor::Interactive::Shell.new(self.class, opts).start
      end
    end
  end
end

Instance Method Details

#interactive?Boolean

Instance method version for use in commands

Returns:

  • (Boolean)


60
61
62
# File 'lib/thor/interactive/command.rb', line 60

def interactive?
  self.class.interactive?
end