Class: Qcmd::CLI

Inherits:
Object
  • Object
show all
Includes:
Plaintext
Defined in:
lib/qcmd/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plaintext

#ascii_qlab, #centered_text, #columns, #joined_wrapped_text, #log, #pluralize, #print, #right_text, #split_text, #table, #word_wrap, #wrapped_text

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qcmd/cli.rb', line 18

def initialize options={}
  Qcmd.debug "(launching with options: #{options.inspect})"
  # start local listening port
  Qcmd.context = Qcmd::Context.new

  self.prompt = '> '

  if options[:machine_given]
    Qcmd.debug "(autoconnecting to #{ options[:machine] })"
    connect_to_machine_by_name options[:machine], options[:machine_passcode]
    if options[:workspace_given]
      connect_to_workspace_by_name options[:workspace], options[:workspace_passcode]
    end
  end

  start
end

Instance Attribute Details

#promptObject

Returns the value of attribute prompt.



12
13
14
# File 'lib/qcmd/cli.rb', line 12

def prompt
  @prompt
end

#serverObject

Returns the value of attribute server.



12
13
14
# File 'lib/qcmd/cli.rb', line 12

def server
  @server
end

Class Method Details

.launch(options = {}) ⇒ Object



14
15
16
# File 'lib/qcmd/cli.rb', line 14

def self.launch options={}
  new options
end

Instance Method Details

#connect(machine, passcode) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/qcmd/cli.rb', line 36

def connect machine, passcode
  if machine.nil?
    print "A valid machine is needed to connect!"
    return
  end

  Qcmd.context.machine = machine
  Qcmd.context.workspace = nil

  if server.nil?
    # set client connection and start listening port
    self.server = Qcmd::Server.new :receive => 53001
  else
    # change client connection
    server.connect_to_client
  end
  server.run

  server.load_workspaces

  self.prompt = "#{ machine.name }> "
end

#connect_to_machine_by_name(machine_name, passcode) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/qcmd/cli.rb', line 59

def connect_to_machine_by_name machine_name, passcode
  if machine = Qcmd::Network.find(machine_name)
    print "connecting to machine: #{machine_name}"
    connect machine, passcode
  else
    print 'sorry, that machine could not be found'
  end
end

#connect_to_workspace_by_name(workspace_name, passcode) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/qcmd/cli.rb', line 68

def connect_to_workspace_by_name workspace_name, passcode
  if workspace = Qcmd.context.machine.find_workspace(workspace_name)
    workspace.passcode = passcode
    print "connecting to workspace: #{workspace_name}"
    use_workspace workspace
  end
end

#handle_message(message) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/qcmd/cli.rb', line 105

def handle_message message
  args    = Qcmd::Parser.parse(message)
  command = args.shift

  case command
  when 'exit'
    print 'exiting...'
    exit 0
  when 'connect'
    Qcmd.debug "(connect command received args: #{ args.inspect })"

    machine_name = args.shift
    passcode     = args.shift

    connect_to_machine_by_name machine_name, passcode
  when 'disconnect'
    reset
    Qcmd::Network.browse_and_display
  when 'use'
    Qcmd.debug "(use command received args: #{ args.inspect })"

    workspace_name = args.shift
    passcode       = args.shift

    Qcmd.debug "(using workspace: #{ workspace_name.inspect })"

    connect_to_workspace_by_name workspace_name, passcode
  when 'cues'
    if !Qcmd.context.workspace_connected?
      print "You must be connected to a workspace before you can view a cue list."
    elsif Qcmd.context.workspace.cues
      print
      print centered_text(" Cues ", '-')
      table ['Number', 'Id', 'Name', 'Type'], Qcmd.context.workspace.cues.map {|cue|
        [cue.number, cue.id, cue.name, cue.type]
      }
      print
    end
  when 'cue'
    # pull off cue number
    cue_number = args.shift
    cue_action = args.shift

    if cue_number.nil?
      print "no cue command given. cue commands should be in the form:"
      print
      print "  > cue NUMBER COMMAND ARGUMENTS"
      print
      print wrapped_text("available cue commands are: #{Qcmd::InputCompleter::ReservedCueWords.inspect}")
    elsif cue_action.nil?
      server.send_workspace_command(cue_number)
    else
      server.send_cue_command(cue_number, cue_action, *args)
    end
  when 'workspace'
    workspace_command = args.shift

    if workspace_command.nil?
      print wrapped_text("no workspace command given. available workspace commands are: #{Qcmd::InputCompleter::ReservedWorkspaceWords.inspect}")
    else
      server.send_workspace_command(workspace_command, *args)
    end

  else
    server.send_command(command, *args)
  end
end

#resetObject



85
86
87
88
89
# File 'lib/qcmd/cli.rb', line 85

def reset
  Qcmd.context.reset
  server.stop
  self.prompt = "> "
end

#startObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/qcmd/cli.rb', line 91

def start
  loop do
    # blocks the whole Ruby VM
    message = Readline.readline(prompt, true)

    if message.nil? || message.size == 0
      Qcmd.debug "(got: #{ message.inspect })"
      next
    end

    handle_message(message)
  end
end

#use_workspace(workspace) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/qcmd/cli.rb', line 76

def use_workspace workspace
  Qcmd.debug %[(connecting to workspace: "#{workspace.name}")]
  # set workspace in context. Will unset later if there's a problem.
  Qcmd.context.workspace = workspace
  self.prompt = "#{ Qcmd.context.machine.name }:#{ workspace.name }> "

  server.connect_to_workspace workspace
end