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, highlight: true, focus_number: nil, interval:, show_refresh:, content_block:, selection_blocks:, selection_blocks_description:, debug: false) ⇒ Pane

Returns a new instance of Pane.



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
100
101
102
103
104
# File 'lib/wassup/pane.rb', line 67

def initialize(height, width, top, left, title: nil, description: 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.interval = interval
  self.content_block = content_block
  self.selection_blocks = selection_blocks || {}
  self.selection_blocks_description = selection_blocks_description || {}
end

Instance Attribute Details

#caught_errorObject

Returns the value of attribute caught_error.



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

def caught_error
  @caught_error
end

#content_blockObject

Returns the value of attribute content_block.



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

def content_block
  @content_block
end

#content_threadObject

Returns the value of attribute content_thread.



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

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.



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

def focus_handler
  @focus_handler
end

#focus_numberObject

Returns the value of attribute focus_number.



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

def focus_number
  @focus_number
end

#focusedObject

Returns the value of attribute focused.



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

def focused
  @focused
end

#highlightObject

Returns the value of attribute highlight.



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

def highlight
  @highlight
end

#highlighted_lineObject

Returns the value of attribute highlighted_line.



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

def highlighted_line
  @highlighted_line
end

#intervalObject

Returns the value of attribute interval.



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

def interval
  @interval
end

#last_refresh_char_atObject

Returns the value of attribute last_refresh_char_at.



309
310
311
# File 'lib/wassup/pane.rb', line 309

def last_refresh_char_at
  @last_refresh_char_at
end

#last_refreshedObject

Returns the value of attribute last_refreshed.



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

def last_refreshed
  @last_refreshed
end

#refresh_char_countObject

Returns the value of attribute refresh_char_count.



286
287
288
# File 'lib/wassup/pane.rb', line 286

def refresh_char_count
  @refresh_char_count
end

#selected_view_indexObject

Returns the value of attribute selected_view_index.



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

def selected_view_index
  @selected_view_index
end

#selection_blocksObject

Returns the value of attribute selection_blocks.



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

def selection_blocks
  @selection_blocks
end

#selection_blocks_descriptionObject

Returns the value of attribute selection_blocks_description.



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

def selection_blocks_description
  @selection_blocks_description
end

#should_boxObject

Returns the value of attribute should_box.



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

def should_box
  @should_box
end

#show_refreshObject

Returns the value of attribute show_refresh.



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

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.



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

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.



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

def win_height
  @win_height
end

#win_leftObject

Returns the value of attribute win_left.



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

def win_left
  @win_left
end

#win_topObject

Returns the value of attribute win_top.



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

def win_top
  @win_top
end

#win_widthObject

Returns the value of attribute win_width.



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

def win_width
  @win_width
end

Instance Method Details

#closeObject



144
145
146
147
148
149
150
151
152
# File 'lib/wassup/pane.rb', line 144

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

  self.win.clear
  self.win.close
end

#data_linesObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/wassup/pane.rb', line 168

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



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/wassup/pane.rb', line 538

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



446
447
448
# File 'lib/wassup/pane.rb', line 446

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

#load_current_viewObject



265
266
267
268
269
270
271
# File 'lib/wassup/pane.rb', line 265

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.



365
366
367
368
369
370
371
372
# File 'lib/wassup/pane.rb', line 365

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)


154
155
156
157
158
159
# File 'lib/wassup/pane.rb', line 154

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



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

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

#refresh(force: false) ⇒ Object



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

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]Error during refersh[fg=white]")
		content.add_row("[fg=red]at #{Time.now}[fg=while]")
		content.add_row("")
		content.add_row("[fg=yellow]Will try again next interval[fg=white]")

		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



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/wassup/pane.rb', line 287

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



256
257
258
259
260
261
262
263
# File 'lib/wassup/pane.rb', line 256

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)


179
180
181
# File 'lib/wassup/pane.rb', line 179

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

#scroll_downObject

Scroll the display down by one line.



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/wassup/pane.rb', line 521

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



486
487
488
489
490
491
492
493
# File 'lib/wassup/pane.rb', line 486

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



495
496
497
498
499
500
501
502
# File 'lib/wassup/pane.rb', line 495

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.



505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/wassup/pane.rb', line 505

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



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

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



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/wassup/pane.rb', line 347

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



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/wassup/pane.rb', line 463

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



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/wassup/pane.rb', line 310

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



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/wassup/pane.rb', line 324

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

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

#virtual_reloadObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/wassup/pane.rb', line 374

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



432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/wassup/pane.rb', line 432

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



450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/wassup/pane.rb', line 450

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