Module: CultomePlayer::Player::Interactive

Included in:
CultomePlayer::Player
Defined in:
lib/cultome_player/player/interactive.rb

Constant Summary collapse

PROMPT =
"cultome> "

Instance Method Summary collapse

Instance Method Details

#begin_sessionObject

Begin a REP loop inside player.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cultome_player/player/interactive.rb', line 7

def begin_session
  @in_session = true
  display c5("Cultome Player v#{CultomePlayer::VERSION}")
  emit_event(:interactive_session_started)

  while in_session?
    begin
      cmd = read_command(PROMPT)
      execute_interactively cmd
    rescue Exception => e
      show_error(e.message)
      e.backtrace.each{|b| display c3(b) } if current_env == :test
    end
  end
end

#execute_interactively(cmd) ⇒ Object



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
# File 'lib/cultome_player/player/interactive.rb', line 23

def execute_interactively(cmd)
  begin
    if cmd.empty?
      # tomamos en ultimo comando
      cmd = last_command

    else
      # agregamos el comando al historia de la session_history
      session_history << cmd
      # seteamos el ultimo comando ejecutado
      # # seteamos el ultimo comando ejecutado
      set_last_command(cmd)
    end

    return false if cmd.nil?

    r = execute cmd

    if r.size > 1
      display c1("#{r.size} commands were executed, Showing result of the last one.")
    end

    show_response(r.last)
  rescue Exception => e
    emit_event(:interactive_exception, e)
    raise e
  end
end

#in_session?Boolean

Check if there is an interactive session in progress.

Returns:

  • (Boolean)

    True if session in progress. False otherwise.



55
56
57
# File 'lib/cultome_player/player/interactive.rb', line 55

def in_session?
  @in_session ||= false
end

#last_commandObject



77
78
79
# File 'lib/cultome_player/player/interactive.rb', line 77

def last_command
  @last_command
end

#session_historyArray<Command>

Command history of this session

Returns:

  • (Array<Command>)

    The history of commands of this session.



69
70
71
# File 'lib/cultome_player/player/interactive.rb', line 69

def session_history
  @session_history ||= []
end

#set_last_command(cmd) ⇒ Object



73
74
75
# File 'lib/cultome_player/player/interactive.rb', line 73

def set_last_command(cmd)
  @last_command = cmd
end

#terminate_sessionObject

Terminates a interactive session.



60
61
62
63
64
# File 'lib/cultome_player/player/interactive.rb', line 60

def terminate_session
  @in_session = false
  save_player_configurations
  emit_event(:interactive_session_ended)
end