Class: Wassup::Pane

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

Defined Under Namespace

Classes: Content, Ope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, width, top, left, title: nil, description: nil, alert_level: nil, highlight: true, focus_number: nil, interval:, show_refresh:, content_block:, selection_blocks:, selection_blocks_description:, debug: false) ⇒ Pane

Returns a new instance of Pane.



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
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wassup/pane.rb', line 71

def initialize(height, width, top, left, title: nil, description: nil, alert_level: nil, highlight: true, focus_number: nil, interval:, show_refresh:, content_block:, selection_blocks:, selection_blocks_description:, debug: false)

  if !debug
    self.win_height = Curses.lines * height	
    self.win_width = Curses.cols * width
    self.win_top = Curses.lines * top
    self.win_left = Curses.cols * left

    self.win = Curses::Window.new(self.win_height, self.win_width, self.win_top, self.win_left)
    self.setup_subwin()
  end

  self.focused = false
  self.focus_number = focus_number

  self.highlight = highlight
  self.virtual_scroll = true

  self.top = 0

			self.contents = []
  self.show_refresh = show_refresh
 
  self.selected_view_index = 0

  if !debug
    self.win.refresh
    self.subwin.refresh
  end

  self.title = title
  self.description = description

  self.alert_level = alert_level

  self.interval = interval
  self.content_block = content_block
  self.selection_blocks = selection_blocks || {}
  self.selection_blocks_description = selection_blocks_description || {}
end

Instance Attribute Details

#alert_levelObject

Returns the value of attribute alert_level.



16
17
18
# File 'lib/wassup/pane.rb', line 16

def alert_level
  @alert_level
end

#caught_errorObject

Returns the value of attribute caught_error.



36
37
38
# File 'lib/wassup/pane.rb', line 36

def caught_error
  @caught_error
end

#content_blockObject

Returns the value of attribute content_block.



32
33
34
# File 'lib/wassup/pane.rb', line 32

def content_block
  @content_block
end

#content_threadObject

Returns the value of attribute content_thread.



38
39
40
# File 'lib/wassup/pane.rb', line 38

def content_thread
  @content_thread
end

#contentsObject

Returns the value of attribute contents.



11
12
13
# File 'lib/wassup/pane.rb', line 11

def contents
  @contents
end

#descriptionObject

Returns the value of attribute description.



14
15
16
# File 'lib/wassup/pane.rb', line 14

def description
  @description
end

#focus_handlerObject

Returns the value of attribute focus_handler.



28
29
30
# File 'lib/wassup/pane.rb', line 28

def focus_handler
  @focus_handler
end

#focus_numberObject

Returns the value of attribute focus_number.



19
20
21
# File 'lib/wassup/pane.rb', line 19

def focus_number
  @focus_number
end

#focusedObject

Returns the value of attribute focused.



18
19
20
# File 'lib/wassup/pane.rb', line 18

def focused
  @focused
end

#highlightObject

Returns the value of attribute highlight.



25
26
27
# File 'lib/wassup/pane.rb', line 25

def highlight
  @highlight
end

#highlighted_lineObject

Returns the value of attribute highlighted_line.



23
24
25
# File 'lib/wassup/pane.rb', line 23

def highlighted_line
  @highlighted_line
end

#intervalObject

Returns the value of attribute interval.



30
31
32
# File 'lib/wassup/pane.rb', line 30

def interval
  @interval
end

#last_refresh_char_atObject

Returns the value of attribute last_refresh_char_at.



314
315
316
# File 'lib/wassup/pane.rb', line 314

def last_refresh_char_at
  @last_refresh_char_at
end

#last_refreshedObject

Returns the value of attribute last_refreshed.



31
32
33
# File 'lib/wassup/pane.rb', line 31

def last_refreshed
  @last_refreshed
end

#refresh_char_countObject

Returns the value of attribute refresh_char_count.



291
292
293
# File 'lib/wassup/pane.rb', line 291

def refresh_char_count
  @refresh_char_count
end

#selected_view_indexObject

Returns the value of attribute selected_view_index.



41
42
43
# File 'lib/wassup/pane.rb', line 41

def selected_view_index
  @selected_view_index
end

#selection_blocksObject

Returns the value of attribute selection_blocks.



33
34
35
# File 'lib/wassup/pane.rb', line 33

def selection_blocks
  @selection_blocks
end

#selection_blocks_descriptionObject

Returns the value of attribute selection_blocks_description.



34
35
36
# File 'lib/wassup/pane.rb', line 34

def selection_blocks_description
  @selection_blocks_description
end

#should_boxObject

Returns the value of attribute should_box.



21
22
23
# File 'lib/wassup/pane.rb', line 21

def should_box
  @should_box
end

#show_refreshObject

Returns the value of attribute show_refresh.



39
40
41
# File 'lib/wassup/pane.rb', line 39

def show_refresh
  @show_refresh
end

#subwinObject

Returns the value of attribute subwin.



8
9
10
# File 'lib/wassup/pane.rb', line 8

def subwin
  @subwin
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/wassup/pane.rb', line 13

def title
  @title
end

#topObject

Returns the value of attribute top.



9
10
11
# File 'lib/wassup/pane.rb', line 9

def top
  @top
end

#virtual_scrollObject

Returns the value of attribute virtual_scroll.



26
27
28
# File 'lib/wassup/pane.rb', line 26

def virtual_scroll
  @virtual_scroll
end

#winObject

Returns the value of attribute win.



7
8
9
# File 'lib/wassup/pane.rb', line 7

def win
  @win
end

#win_heightObject

Returns the value of attribute win_height.



43
44
45
# File 'lib/wassup/pane.rb', line 43

def win_height
  @win_height
end

#win_leftObject

Returns the value of attribute win_left.



43
44
45
# File 'lib/wassup/pane.rb', line 43

def win_left
  @win_left
end

#win_topObject

Returns the value of attribute win_top.



43
44
45
# File 'lib/wassup/pane.rb', line 43

def win_top
  @win_top
end

#win_widthObject

Returns the value of attribute win_width.



43
44
45
# File 'lib/wassup/pane.rb', line 43

def win_width
  @win_width
end

Instance Method Details

#closeObject



150
151
152
153
154
155
156
157
158
# File 'lib/wassup/pane.rb', line 150

def close
  unless self.subwin.nil?
    self.subwin.clear
    self.subwin.close
  end

  self.win.clear
  self.win.close
end

#data_linesObject



174
175
176
177
178
179
180
181
182
183
# File 'lib/wassup/pane.rb', line 174

def data_lines
	return [] if self.selected_view_index.nil?
	content = (self.contents || [])[self.selected_view_index]

	if content.nil?
		return []
	else
		content.data.map(&:display)
	end
end

#handle_keyboardObject



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/wassup/pane.rb', line 577

def handle_keyboard
  input = self.subwin.getch

  unless focus_handler.nil?
    handled = focus_handler.call(input)
    return if handled
  end

  if input == "j"
    if virtual_scroll
      self.virtual_scroll_down
    else
      scroll_down
    end
  elsif input == "k"
    if virtual_scroll
      self.virtual_scroll_up
    else
      scroll_up
    end
  elsif input == "h"
    self.scroll_left
  elsif input == "l"
    self.scroll_right
  elsif input == "r"
    self.refresh(force: true)
  elsif input == "c"
    if !self.caught_error.nil?
      text = self.caught_error.backtrace.join("\n")
      if RUBY_PLATFORM.downcase =~ /win32/
        IO.popen('clip', 'w') { |pipe| pipe.puts text }
      else
        IO.popen('pbcopy', 'w') { |pipe| pipe.puts text }
      end
    end
  else
    selection_block = self.selection_blocks[input]
    if !selection_block.nil? && !self.highlighted_line.nil?
	content = self.contents[self.selected_view_index]
	row = content.data[self.highlighted_line]
	data = row.object || row.display
      selection_block.call(data)
    end
  end
end

#highlight_scroll_paddingObject



485
486
487
# File 'lib/wassup/pane.rb', line 485

def highlight_scroll_padding
  return (self.subwin.maxy * 0.3).to_i
end

#load_current_viewObject



270
271
272
273
274
275
276
# File 'lib/wassup/pane.rb', line 270

def load_current_view
  self.setup_subwin()

  # this might be bad
  self.highlighted_line = nil
			self.virtual_reload()
end

#load_thingObject

Load the file into memory and put the first part on the curses display.



404
405
406
407
408
409
410
411
# File 'lib/wassup/pane.rb', line 404

def load_thing
  self.data_lines[0..self.subwin.maxy-1].each_with_index do |line, idx|
    self.subwin.setpos(idx, 0)
    self.subwin.addstr(line)
  end
  self.subwin.setpos(0, 0)
  self.subwin.refresh
end

#needs_refresh?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
# File 'lib/wassup/pane.rb', line 160

def needs_refresh?
  return false if self.content_block.nil?
  return false if self.interval.nil?
  return true if self.last_refreshed.nil?
  return Time.now - self.interval > self.last_refreshed
end

#redrawObject



189
190
191
192
193
# File 'lib/wassup/pane.rb', line 189

def redraw
  self.update_box
  self.update_title
  self.load_current_view()
end

#refresh(force: false) ⇒ Object



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/wassup/pane.rb', line 195

def refresh(force: false)
  if force
    self.last_refreshed = nil
  end

  if !needs_refresh?
    return
  end

  thread = self.content_thread
  if !thread.nil?
    if thread.status == "sleep" || thread.status == "run" || thread.status == "aborting"
      self.update_refresh
      return
    elsif thread.status == nil
      return
    elsif thread.status == false
      rtn = thread.value
      if rtn.is_a?(Ope)
        self.caught_error = rtn.error
		content = Wassup::Pane::Content.new("Overview")
        content.add_row("[fg=red]#{rtn.error.message}[fg=while]")
		content.add_row("")
		content.add_row("[fg=gray]Error at #{Time.now}[fg=while]")

		content_directions = Wassup::Pane::Content.new("Directions")
        content_directions.add_row("1. Press 'c' to copy the stacktrace")
        content_directions.add_row("2. Debug pane content block with:")
        content_directions.add_row("    $: wassup --debug")
        content_directions.add_row("3. Stacktrace viewable in next page")

		content_stacktrace = Wassup::Pane::Content.new("Stacktrace")
        rtn.error.backtrace.each do |line|
		  content_stacktrace.add_row(line)
        end

        self.refresh_content([content, content_directions, content_stacktrace])
      elsif rtn.is_a?(Wassup::PaneBuilder::ContentBuilder)
        self.caught_error = nil
        self.refresh_content(rtn.contents)
      end

	self.update_box
	self.update_title
    else
      # This shouldn't happen
      # TODO: also fix this
      return
    end
  else
    the_block = self.content_block
    self.content_thread = Thread.new {
      begin
		builder = Wassup::PaneBuilder::ContentBuilder.new(self.contents)
        content = the_block.call(builder)

		builder
      rescue => ex
        next Ope.new(ex)
      end
    }
self.update_box
self.update_title
  end
end

#refresh_charObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/wassup/pane.rb', line 292

def refresh_char
  return "" unless self.show_refresh

  if self.refresh_char_count.nil?
    self.refresh_char_count = 0
  end

  if self.refreshing?
    array = ['\\', '|', '/', '|']
    rtn = array[self.refresh_char_count]

    self.refresh_char_count += 1
    if self.refresh_char_count >= array.size
      self.refresh_char_count = 0
    end

    return rtn
  else
    return ""
  end
end

#refresh_content(contents) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/wassup/pane.rb', line 261

def refresh_content(contents)
  self.contents = contents

  self.load_current_view()
  self.last_refreshed = Time.now

  self.content_thread = nil
end

#refreshing?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/wassup/pane.rb', line 185

def refreshing?
	return !self.content_thread.nil?
end

#scroll_downObject

Scroll the display down by one line.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/wassup/pane.rb', line 560

def scroll_down
  if self.top + self.subwin.maxy < self.data_lines.length
    self.subwin.scrl(1)
    self.top += 1
    str = self.data_lines[self.top + self.subwin.maxy - 1]
    if str
      self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))
      self.subwin.setpos(self.subwin.maxy - 1, 0)
      self.subwin.addstr(str)
      self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))
    end
    return true
  else
    return false
  end
end

#scroll_leftObject



525
526
527
528
529
530
531
532
# File 'lib/wassup/pane.rb', line 525

def scroll_left
  self.selected_view_index -= 1
  if self.selected_view_index < 0
    self.selected_view_index = self.contents.size - 1
  end

  self.load_current_view
end

#scroll_rightObject



534
535
536
537
538
539
540
541
# File 'lib/wassup/pane.rb', line 534

def scroll_right
  self.selected_view_index += 1
  if self.selected_view_index >= self.contents.size
    self.selected_view_index = 0
  end

  self.load_current_view
end

#scroll_upObject

Scroll the display up by one line.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/wassup/pane.rb', line 544

def scroll_up
  if self.top > 0
    self.subwin.scrl(-1)
    self.top -= 1
    str = self.data_lines[top]
    if str
      self.subwin.setpos(0, 0)
      self.subwin.addstr(str)
    end
    return true
  else
    return false
  end
end

#setup_subwinObject



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

def setup_subwin
  top_bump = 0

  unless self.subwin.nil?
    self.subwin.clear()
    self.subwin.close()
    self.subwin = nil
  end

  if (self.contents || []).size > 1
    top_bump = 4

    view_title = self.contents[self.selected_view_index].title || "<No Title>"
    view_title += " " * 100

    self.win.setpos(2, 2)
    self.win.addstr(view_title[0...self.win.maxx()-3])

    subtitle = "(#{self.selected_view_index + 1} out of #{self.contents.size})"
    subtitle += " " * 100
    self.win.setpos(3, 2)
    self.win.addstr(subtitle[0...self.win.maxx()-3])

    self.win.refresh
  end

  if self.win_height == 0
    self.should_box = false
    self.subwin = self.win.subwin(0, 0, 0, 0)
  else
    self.should_box = true
    self.subwin = self.win.subwin(self.win_height - 2 - top_bump, self.win_width - 4, self.win_top + 1 + top_bump, self.win_left + 2)
  end
  self.subwin.nodelay=true
  self.subwin.idlok(true)
  self.subwin.scrollok(true)
end

#update_boxObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/wassup/pane.rb', line 386

def update_box
  return unless self.should_box

  show_focused = self.focused

  if self.focus_number.nil?
    show_focused = true
  end

  self.win.attrset(show_focused ? Curses.color_pair(Wassup::Color::Pair::BORDER_FOCUS) : Curses.color_pair(Wassup::Color::Pair::BORDER))
  self.win.box()
  self.win.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))

  self.win.refresh
end

#update_highlight(inc) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/wassup/pane.rb', line 502

def update_highlight(inc)
  return unless self.highlight 

  prev = self.highlighted_line

  new = nil
  if prev.nil?
    new = 0
  elsif inc.nil?
    new = nil
  elsif !inc.nil?
    new = prev + inc
  end

  if new < 0 || new >= self.data_lines.size
    new = prev
  end

  self.highlighted_line = new

  return new
end

#update_refreshObject



315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/wassup/pane.rb', line 315

def update_refresh
  return unless self.should_box

  self.last_refresh_char_at ||= Time.now

  if Time.now - self.last_refresh_char_at >= 0.15
    self.win.setpos(0, 1)
    self.win.addstr(self.refresh_char)
    self.win.refresh

    self.last_refresh_char_at = Time.now
  end
end

#update_titleObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/wassup/pane.rb', line 329

def update_title
  return unless self.should_box

  title = self.title || "<No Title>"
  
  if self.focus_number.nil?
    full_title = title
  else 
    full_title = "#{self.focus_number} - #{title}"
  end
  full_title += " "

  self.win.setpos(0, 3)
  self.win.addstr(full_title)

  self.win.setpos(0, 3 + full_title.size)
  alert = ""
  alert_count = 0
  if self.contents
    alert_count = self.contents.map { |c| c.data.size }.inject(0, :+)
  end
  case self.alert_level
  when AlertLevel::HIGH
 self.win.attrset(Curses.color_pair(Wassup::Color::Pair::RED))
    if alert_count == 1
      alert += "(#{alert_count} HIGH ALERT)"
    elsif alert_count > 0
      alert += "(#{alert_count} HIGH ALERTS)"
    end
  when AlertLevel::MEDIUM
 self.win.attrset(Curses.color_pair(Wassup::Color::Pair::YELLOW))
    if alert_count == 1
      alert += "(#{alert_count} MEDIUM ALERT)"
    elsif alert_count > 0
      alert += "(#{alert_count} MEDIUM ALERTS)"
    end
  when AlertLevel::LOW
 self.win.attrset(Curses.color_pair(Wassup::Color::Pair::CYAN))
    if alert_count == 1
      alert += "(#{alert_count} LOW ALERT)"
    elsif alert_count > 0
      alert += "(#{alert_count} LOW ALERTS)"
    end
  end
  self.win.addstr(alert)
			self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))

  self.win.refresh
end

#virtual_reloadObject



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/wassup/pane.rb', line 413

def virtual_reload
  return if self.data_lines.nil? || self.data_lines.empty?

  # TODO: This errored out but might be because thread stuff???
  self.data_lines[self.top..(self.top+self.subwin.maxy-1)].each_with_index do |line, idx|

    write_full_line = false
    should_highlight = self.focused && self.highlight && (idx + self.top) == self.highlighted_line

    max_char = self.subwin.maxx()-3 
   
self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))

splits = line.split(/\[.*?\]/) # returns ["hey something", "other thing", "okay"]
scans = line.scan(/\[.*?\]/) #returns ["red", "white"]
scans = scans.map do |str|
	if str.start_with?('[fg=')
		str = str.gsub('[fg=', '').gsub(']','')
		Wassup::Color.new(str)
	else
		str
	end
end

all_parts = splits.zip(scans).flatten.compact
		
char_count = 0

if should_highlight
	self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::HIGHLIGHT))
end

all_parts.each do |part|
	if part.is_a?(Wassup::Color)
		#color = Curses.color_pair([1,2,3,4].sample)
		if !should_highlight
			self.subwin.attrset(Curses.color_pair(part.color_pair))
		end
	else
		new_char_count = char_count + part.size
		if new_char_count >= max_char
			part = part[0...(max_char - char_count)]  
		end

		self.subwin.setpos(idx, char_count)
		self.subwin.addstr(part)

		char_count += part.size
	end

end

self.subwin.attrset(Curses.color_pair(Wassup::Color::Pair::NORMAL))
self.subwin.clrtoeol()
  end
  self.subwin.refresh
end

#virtual_scroll_downObject



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/wassup/pane.rb', line 471

def virtual_scroll_down
  self.update_highlight(1)

  bottom = self.top + self.subwin.maxy
  if bottom < self.data_lines.size
    # only move highlight and dont scroll if near top
    if !self.highlight || self.highlighted_line > highlight_scroll_padding
      self.top += 1
    end
  end

  self.virtual_reload
end

#virtual_scroll_upObject



489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/wassup/pane.rb', line 489

def virtual_scroll_up
  self.update_highlight(-1)

  if self.top > 0
    # only move highlight and dont scroll if near bottom
    if !self.highlight || (self.data_lines.size - self.highlighted_line) >= highlight_scroll_padding
      self.top -= 1
    end
  end

  self.virtual_reload
end