Class: Smol::REPL

Inherits:
Object
  • Object
show all
Includes:
ConfigDisplay, Output
Defined in:
lib/smol/repl.rb

Instance Method Summary collapse

Methods included from Output

#banner, #check_result, #debug, #desc, #failure, #header, #hint, #info, #label, #nl, #out, #success, #table, #verbose, #warning

Constructor Details

#initialize(app, prompt:, history: true, history_file: nil, parent: nil) ⇒ REPL

Returns a new instance of REPL.



9
10
11
12
13
14
15
# File 'lib/smol/repl.rb', line 9

def initialize(app, prompt:, history: true, history_file: nil, parent: nil)
  @app = app
  @prompt = prompt
  @history = history
  @history_file = history_file || File.expand_path("~/.smol_#{app.class.name.downcase}_history")
  @parent = parent
end

Instance Method Details

#runObject



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
# File 'lib/smol/repl.rb', line 17

def run
  setup_readline if @history
  load_history if @history

  show_boot_message

  loop do
    input = read_input
    break if input.nil?

    input = input.strip
    next if input.empty?

    args = input.split(/\s+/)

    case args.first
    when "exit", "quit", "q"
      break
    when "back"
      break if @parent
      warning "not in a sub-app"
    when "help", "h", "?"
      help
    when "config", "c"
      show_config
    when "config:set"
      set_config(args[1], args[2])
    else
      # Check if entering a mounted sub-app
      mount = @app.find_mount(args.first)
      if mount && args.size == 1
        enter_subapp(mount, args.first)
      else
        dispatch(args)
      end
    end

    nl
  end

  save_history if @history
  hint "goodbye"
end