Class: ViCli

Inherits:
Object
  • Object
show all
Defined in:
lib/ViCli.rb

Instance Method Summary collapse

Instance Method Details

#add_tasks(index) ⇒ Object



39
40
41
42
43
# File 'lib/ViCli.rb', line 39

def add_tasks index
  tasks = @model.get_tasks index, @config.keys
  callback = self.method :on_fire_callback
  @gui.add_tasks callback, index, tasks
end

#draw_all(state) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/ViCli.rb', line 86

def draw_all state
  return if state.nil?
  return if state[:index] != @gui.current_list_index
  draw_list state
  draw_state state
  add_tasks state[:index]
  @gui.refresh
end

#draw_list(state) ⇒ Object



28
29
30
31
# File 'lib/ViCli.rb', line 28

def draw_list state
  @gui.colors = @model.get_colors # state[:colors]
  @gui.draw_list state[:vm_list] unless state[:vm_list].nil?
end

#draw_state(state) ⇒ Object



103
104
105
106
# File 'lib/ViCli.rb', line 103

def draw_state state
  @gui.add_state state[:state]
  @gui.add_hardware state[:hw]
end

#handle_char(char) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/ViCli.rb', line 76

def handle_char char
  return false if char.nil?
  return true if char == ?q
  @activity = true
  @config. = @gui. if char == ?l
  load_folders if char == 269
  @gui.handle_key char
  @gui.refresh
  false
end

#load_foldersObject



67
68
69
70
71
72
73
74
75
# File 'lib/ViCli.rb', line 67

def load_folders
  @gui.set_status "Loading VM Folders ..."
  args = @config.keys.values.collect { |i| i['args'] }
  args.compact.flatten.uniq.each { |i| @model.find_entities i }
  @model.refresh_list
  @gui.draw_list @model.vm_list
  @watcher.refresh_queue.push @gui.current_list_index
  @gui.set_status "Loaded VM Folders"
end

#mainObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ViCli.rb', line 9

def main
  begin
    @gui = ViGUI.new
    @config = ViCliConfig.new
    @config. = @gui. if @config.
    @watcher = Watcher.new
    @model = VcData.new @config., @config.tree_state
    @gui.setup self.method(:on_focus_callback)
    setup_queues
    load_folders
    main_loop
    @config.tree_state = @model.save_data
  rescue => ex
  ensure
    @gui.unload if !@gui.nil?
    puts ex if ex
    print ex.backtrace.join "\n" if ex
  end
end

#main_loopObject



136
137
138
139
140
141
142
# File 'lib/ViCli.rb', line 136

def main_loop
  while @watcher.wait
    return if handle_char @watcher[:getchar].poop
    draw_all @watcher[:state].poop
    refresh_all @watcher[:sleep].poop
  end
end

#on_fire_callback(index, key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ViCli.rb', line 53

def on_fire_callback index, key
  action = @config.keys[key]
  args = (action['args'] || []).collect {|arg| prepare_arg arg }
  action = action['action'] || action
  redraw = @model.execute_task index, action, *args
  @gui.set_status "execute #{action} on #{@model.get :name, index}"
  if redraw
    draw_list Hash[ :vm_list => @model.vm_list ]#, :colors => @model.get_colors ]
    add_tasks index
    @gui.refresh
  else
    @watcher.refresh_queue.push @gui.current_list_index
  end
end

#on_focus_callback(index) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ViCli.rb', line 32

def on_focus_callback index
  add_tasks index
  #TODO needs cached data for state and hardware
  draw_state @model.get :cached_state, index
  @gui.refresh
  @watcher.refresh_queue.push @gui.current_list_index
end

#prepare_arg(arg) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ViCli.rb', line 44

def prepare_arg arg
  entities = @model.find_entities arg
  return nil if entities.empty?
  return entities.first[:ref] if entities.length == 1
  @watcher.pause :getchar
  selected = @gui.select entities.collect {|e| e[:name] }
  @watcher.resume :getchar
  entities.each {|e| return e[:ref] if e[:name] == selected }
end

#refresh_all(x) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/ViCli.rb', line 94

def refresh_all x
  return if x.nil?
  @gui.draw_events @model.recent_events
  @gui.refresh
  @watcher.refresh_queue.push @gui.current_list_index
  return @activity = false if @activity
  @gui.set_status ''
  @gui.refresh
end

#setup_queuesObject



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
# File 'lib/ViCli.rb', line 107

def setup_queues
  #@watcher.register(:resize) { |q| loop { q.push sleep 5 } }
  @watcher.register(:getchar) do |q|
    loop do
      begin
        loop { q.push @gui.getchar }
      rescue => ex
        Thread.stop
      end
    end
  end
  @watcher.register(:sleep) do |q|
    loop do
      start = Time.now
      @model.get_colors true
      time = 5 - (Time.now - start)
      q.push sleep(time > 0 ? time : 1)
    end
  end
  @watcher.register(:state) do |q|
    loop do
      state = Hash[ :index => @watcher.refresh_queue.poop ]
      # watch out for concurrency
      # state[:vm_list] = @model.vm_list
      state = state.update @model.get(:state, state[:index])
      q.push state
    end
  end
end