Module: Eclair::Grid

Extended by:
Grid
Includes:
CommonHelper
Included in:
Grid
Defined in:
lib/eclair/grid.rb

Constant Summary collapse

HEADER_ROWS =
4
SORT_FUNCTIONS =
{
  "Name" => lambda {|i| [i.name.downcase, -i.launch_time.to_i]}, 
}

Instance Method Summary collapse

Methods included from CommonHelper

#config, included

Instance Method Details

#assignObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/eclair/grid.rb', line 63

def assign
  sort_function = lambda {|i| [i.name.downcase, -i.launch_time.to_i]}
  @group_map ||= {}
  if config.group_by
    Aws.instances.group_by(&config.group_by).each do |group, instances|
      if @group_map[group]
        group_cell  = @group_map[group]
      else
        col = columns[target_col]
        group_cell = Group.new(group, col)
        col << group_cell
        @group_map[group] = group_cell
      end
      instances.each do |i|
        unless group_cell.find{|j| j.instance_id == i.instance_id}
          obj = Instance.new(i.instance_id, col)
          group_cell << obj
        end
      end
      group_cell.items.sort_by!(&sort_function)
    end
  else
    col_limit = (Aws.instances.count - 1) / config.columns + 1
    iter = Aws.instances.map{|i| Instance.new(i.instance_id)}.sort_by(&sort_function).each
    columns.each do |col|
      col_limit.times do 
        begin
          i = iter.next
          i.column = col
          col << i
        rescue StopIteration
          break
        end
      end
    end
  end
end

#cancel_searchObject



243
244
245
246
# File 'lib/eclair/grid.rb', line 243

def cancel_search
  x,y = @rollback_cursor
  move_cursor(x: x, y: y, mode: @rollback_mode)
end

#cell_widthObject



53
54
55
# File 'lib/eclair/grid.rb', line 53

def cell_width
  stdscr.maxx/column_count
end

#change_sortObject



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/eclair/grid.rb', line 334

def change_sort
  stored_cursor = cursor
  sort_function = next_sort_function
  columns.each do |column| 
    column.groups.each do |group|
      group.items.sort_by!(&sort_function)
    end
  end
  @x, @y = stored_cursor.x, stored_cursor.y
  render_all
end

#column_countObject



148
149
150
# File 'lib/eclair/grid.rb', line 148

def column_count
  columns.count{|col| !col.empty?}
end

#columnsObject



152
153
154
# File 'lib/eclair/grid.rb', line 152

def columns
  @columns ||= config.columns.times.map{|idx| Column.new(idx)}.to_a
end

#cursorObject



211
212
213
214
215
216
217
218
219
# File 'lib/eclair/grid.rb', line 211

def cursor
  @x ||= -1
  @y ||= -1
  if @x >=0 && @y >= 0
    columns[@x][@y]
  else
    nil
  end
end

#cursor_inspectObject



310
311
312
313
314
# File 'lib/eclair/grid.rb', line 310

def cursor_inspect
  close_screen
  LessViewer.show cursor.info
  render_all
end

#debugObject



316
317
318
319
320
321
322
# File 'lib/eclair/grid.rb', line 316

def debug
  trap("INT") { raise Interrupt }
  close_screen
  binding.pry      
  render_all
  trap("INT") { exit }
end

#end_searchObject



234
235
236
237
238
239
240
241
# File 'lib/eclair/grid.rb', line 234

def end_search
  if cursor
    move_cursor(mode: @rollback_mode)
    @mode = @rollback_mode
  else
    cancel_search
  end
end

#maxyObject



11
12
13
# File 'lib/eclair/grid.rb', line 11

def maxy
  stdscr.maxy - HEADER_ROWS
end

#modeObject



169
170
171
# File 'lib/eclair/grid.rb', line 169

def mode
  @mode ||= :navi
end

#move(key) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/eclair/grid.rb', line 186

def move key
  end_search if mode == :search
  mx,my = {
    KEY_UP => [0,-1],
    "k" => [0,-1],
    KEY_DOWN => [0,1],
    "j" => [0,1],
    KEY_LEFT => [-1,0],
    "h" => [-1,0],
    KEY_RIGHT => [1,0],
    "l" => [1,0],
  }[key]

  newx = (@x + mx) % column_count
  newy = (@y + my - columns[@x].scroll + columns[newx].scroll)
  if my != 0
    newy %= columns[newx].count 
  end
  if newy >= columns[newx].count
    newy = columns[newx].count-1
  end

  move_cursor(x: newx, y: newy)
end

#move_cursor(**options, &block) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/eclair/grid.rb', line 249

def move_cursor **options, &block
  if cursor
    cursor.toggle_select if mode == :navi
    cursor.decurrent
  end

  new_mode = options.delete(:mode)
  if new_mode && mode != new_mode
    case new_mode
    when :search
      start_search
    end
    @mode = new_mode
  end

  if block
    @x, @y = block.call
  else
    @x = options.delete(:x) || @x
    @y = options.delete(:y) || @y
  end

  if cursor
    cursor.toggle_select if mode == :navi
    cursor.current
  end
end

#next_sort_functionObject



324
325
326
327
328
# File 'lib/eclair/grid.rb', line 324

def next_sort_function
  @sort_function_idx ||= -1
  @sort_function_idx = (@sort_function_idx + 1) % SORT_FUNCTIONS.count
  SORT_FUNCTIONS.values[@sort_function_idx]
end

#queryObject



221
222
223
224
225
226
# File 'lib/eclair/grid.rb', line 221

def query
  return nil if @search_str == ""
  result = columns.map(&:expand).flatten.grep(Instance).map(&:name).max_by{|name| name.score @search_str}
  return nil if result.score(@search_str) == 0.0
  result
end

#reloadObject



346
347
348
349
350
351
352
353
# File 'lib/eclair/grid.rb', line 346

def reload
  clear
  addstr("reloading")
  refresh
  Aws.reload_instances
  assign
  render_all
end

#render_allObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/eclair/grid.rb', line 101

def render_all
  clear
  columns.each do |cols|
    cols.each do |c|
      c.render
    end
  end
  render_header
  refresh
end

#render_headerObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eclair/grid.rb', line 15

def render_header
  if mode == :search
    if cursor
      header = ["Searching #{@search_str}", "Found #{cursor.name}", ""]
    else
      header = ["Searching #{@search_str}", "None Found", ""]
    end
  else
    header = cursor.header
  end

  header.each_with_index do |line,i|
    setpos(i,0)
    clrtoeol
    addstr(line)
  end
  render_help
end

#render_helpObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eclair/grid.rb', line 34

def render_help
  setpos(3,0)
  clrtoeol

  helps = {
    "Enter" => "SSH",
    "Space" => "Select",
    "[a-z0-9]" =>  "Search",
    "?" => "Inspect",
    "!" => "Open Ruby REPL",
  }

  attron(Color.fetch(*config.help_color)) do
    addstr helps.map{ |key, action|   
      " #{key} => #{action}"
    }.join("    ").slice(0,stdscr.maxx).ljust(stdscr.maxx)
  end
end

#resizeObject



304
305
306
307
308
# File 'lib/eclair/grid.rb', line 304

def resize
  columns.each{|col| col.scroll = 0}
  render_all
  cursor.check_scroll
end

#rowsObject



156
157
158
# File 'lib/eclair/grid.rb', line 156

def rows
  columns.map(&:count).max
end

#search(key) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/eclair/grid.rb', line 277

def search key
  @search_str ||= ""
  
  move_cursor(mode: :search) do 
    if key
      @search_str = @search_str+key
    else
      @search_str.chop!
    end

    goto = query

    result = nil
    columns.each do |col|
      target = col.find {|item| item.is_a?(Instance) && item.name == goto}
      if target
        result = [target.x, target.y]
        break
      end
    end

    result || [-1,-1]
  end
  
  render_header 
end

#selectObject



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/eclair/grid.rb', line 173

def select
  end_search if mode == :search
  if mode == :navi
    @mode = :select
    cursor.toggle_select
  end
  cursor.toggle_select
  if selected.empty?
    @mode = :navi
    cursor.toggle_select
  end
end

#selectedObject



165
166
167
# File 'lib/eclair/grid.rb', line 165

def selected
  @selected ||= []
end

#sorted_byObject



330
331
332
# File 'lib/eclair/grid.rb', line 330

def sorted_by
  SORT_FUNCTIONS.keys[@sort_function_idx]
end

#sshObject



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
# File 'lib/eclair/grid.rb', line 112

def ssh
  targets = selected.select{|i| i.is_a?(Instance) && i.connectable?}
  return if targets.empty?
  close_screen

  cmd = ""
  if targets.count == 1
    target = targets.first
    cmd = target.ssh_cmd
  else
    cmds = []
    session_name = nil
    session_cmd = nil

    targets.each_with_index do |target, i|
      if i==0
        if ENV['TMUX']
          cmds << "tmux new-window -- '#{target.ssh_cmd}'"
        else
          session_name = "eclair#{Time.now.to_i}"
          session_cmd = "-t #{session_name}"
          cmds << "tmux new-session -d -s #{session_name} -- '#{target.ssh_cmd}'"
        end
      else
        cmds << "tmux split-window #{session_cmd} -- '#{target.ssh_cmd}'"
        cmds << "tmux select-layout #{session_cmd} tiled"
      end
    end
    cmds << "tmux set-window-option #{session_cmd} synchronize-panes on" 
    cmds << "tmux attach #{session_cmd}" unless ENV['TMUX']
    cmd = cmds.join(" && ")
  end
  system cmd
  exit
end

#startObject



57
58
59
60
61
# File 'lib/eclair/grid.rb', line 57

def start
  assign
  move_cursor(x: 0, y: 0)
  render_all
end

#start_searchObject



228
229
230
231
232
# File 'lib/eclair/grid.rb', line 228

def start_search
  @rollback_cursor = [@x, @y]
  @rollback_mode = @mode
  @search_str = ""
end

#target_colObject



160
161
162
163
# File 'lib/eclair/grid.rb', line 160

def target_col
  counts = columns.map(&:count)
  counts.index(counts.min)
end