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, #print_wrapped, #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
35
36
37
38
39
40
41
42
43
44
45
46
# 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] })"

    Qcmd.while_quiet do
      connect_to_machine_by_name options[:machine], options[:machine_passcode]
    end

    if options[:workspace_given]
      Qcmd.while_quiet do
        connect_to_workspace_by_name options[:workspace], options[:workspace_passcode]
      end

      if options[:command_given]
        handle_message options[:command]
        puts %[sent command "#{ options[:command] }"]
        exit 0
      end
    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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qcmd/cli.rb', line 48

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



71
72
73
74
75
76
77
78
# File 'lib/qcmd/cli.rb', line 71

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



80
81
82
83
84
85
86
# File 'lib/qcmd/cli.rb', line 80

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_failed_workspace_command(command) ⇒ Object



222
223
224
225
226
# File 'lib/qcmd/cli.rb', line 222

def handle_failed_workspace_command command
  print_wrapped(%[the command, "#{ command }" can't be processed yet. you must
                  first connect to a machine and a workspace
                  before issuing other commands.])
end

#handle_message(message) ⇒ Object



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/qcmd/cli.rb', line 120

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

  case command
  when 'exit', 'quit', 'q'
    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?
      failed_workspace_command message
      return
    end

    # reload cues
    server.load_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

  when 'cue', 'c'
    if !Qcmd.context.workspace_connected?
      failed_workspace_command message
      return
    end

    # 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("available cue commands are: #{Qcmd::InputCompleter::ReservedCueWords.join(', ')}")
    elsif cue_action.nil?
      server.send_workspace_command("cue/#{ cue_number }")
    else
      server.send_cue_command(cue_number, cue_action, *args)
    end

  when 'workspace'
    workspace_command = args.shift

    if !Qcmd.context.workspace_connected?
      handle_failed_workspace_command message
      return
    end

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

  else
    if Qcmd.context.workspace_connected?
      if Qcmd::InputCompleter::ReservedWorkspaceWords.include?(command)
        server.send_workspace_command(command, *args)
      else
        if %r[/] =~ command
          # might be legit OSC command, try sending
          server.send_command(command, *args)
        else
          print "unrecognized command: #{ command }"
        end
      end
    else
      handle_failed_workspace_command message
    end
  end
end

#resetObject



100
101
102
103
104
# File 'lib/qcmd/cli.rb', line 100

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

#startObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/qcmd/cli.rb', line 106

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



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/qcmd/cli.rb', line 88

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

  server.connect_to_workspace workspace
  if Qcmd.context.workspace_connected? && Qcmd.context.workspace.cues
    print "loaded #{pluralize Qcmd.context.workspace.cues.size, 'cue'}"
    self.prompt = "#{ Qcmd.context.machine.name }:#{ workspace.name }> "
  end
end