Class: Salticid::Interface::TabView

Inherits:
View show all
Defined in:
lib/salticid/interface/tab_view.rb

Instance Attribute Summary

Attributes inherited from View

#height, #left, #top, #width, #window

Instance Method Summary collapse

Methods inherited from View

#hide, #show

Constructor Details

#initialize(interface, params = {}) ⇒ TabView

Returns a new instance of TabView.



5
6
7
8
9
10
# File 'lib/salticid/interface/tab_view.rb', line 5

def initialize(interface, params = {})
  super

  @tabs = []
  @active = -1
end

Instance Method Details

#activeObject

Gets the active tab



13
14
15
# File 'lib/salticid/interface/tab_view.rb', line 13

def active
  @tabs[@active] || nil
end

#active=(tab) ⇒ Object

Sets the active tab



18
19
20
21
# File 'lib/salticid/interface/tab_view.rb', line 18

def active=(tab)
  @active = @tabs.index tab
  render
end

#add(tab) ⇒ Object Also known as: <<

Adds a tab (and switches to it by default)



24
25
26
27
28
# File 'lib/salticid/interface/tab_view.rb', line 24

def add(tab)
  @tabs << tab
  @active = @tabs.size - 1
  render
end

#delete(tab) ⇒ Object

Deletes a tab and switches to the previous tab



33
34
35
36
# File 'lib/salticid/interface/tab_view.rb', line 33

def delete(tab)
  @tabs.delete tab
  previous
end

#each(&block) ⇒ Object

Iterates over each tab



39
40
41
# File 'lib/salticid/interface/tab_view.rb', line 39

def each(&block)
  @tabs.each &block
end

#nextObject

Advances to the next tab



44
45
46
# File 'lib/salticid/interface/tab_view.rb', line 44

def next
  scroll 1
end

#previousObject

Goes to the previous tab



49
50
51
# File 'lib/salticid/interface/tab_view.rb', line 49

def previous
  scroll -1
end

#renderObject

Draws to screen



54
55
56
57
58
59
60
61
62
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
# File 'lib/salticid/interface/tab_view.rb', line 54

def render
  return false if @tabs.empty?
  # Divide into regions

  # Ignore dividers
  width = @width - size + 1
  base_width = width / size
  regions = Array.new(size, base_width)
  
  # Add remainder to successive tabs.
  (width - base_width * size).times do |i|
    regions[i] += 1
  end

  # Move to start
  @window.setpos 0,0

  @tabs.each_with_index do |tab, i|
    if i > 0
      @window.addstr '|'
    end

    color = Interface::COLOR_PAIRS[tab.state]
    @window.color_set color if color
    @window.attron Curses::A_BOLD
    @window.attron Curses::A_REVERSE if i == @active
    @window.addstr tab.to_s.slice(0,regions[i]).center(regions[i])
    @window.attroff Curses::A_REVERSE if i == @active
    @window.attroff Curses::A_BOLD
    @window.color_set Interface::COLOR_PAIRS[:info]
  end

  @window.refresh
end

#resize(dimensions = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/salticid/interface/tab_view.rb', line 89

def resize(dimensions = nil)
  return unless super

  @tabs.each do |tab|
    tab.resize(
      :top => 1,
      :left => 0,
      :height => height - 1,
      :width => width
    )
  end
  active.render
end

#scroll(delta = 1) ⇒ Object

Moves by a number of tabs



104
105
106
107
108
109
# File 'lib/salticid/interface/tab_view.rb', line 104

def scroll(delta = 1)
  active.hide rescue NoMethodError
  @active = (@active + delta).modulo(@tabs.size)
  active.show
  render
end

#shutdownObject



111
112
113
114
115
116
# File 'lib/salticid/interface/tab_view.rb', line 111

def shutdown
  @tabs.each do |tab|
    tab.shutdown
  end
  @tabs = []
end

#sizeObject

Number of tabs



119
120
121
# File 'lib/salticid/interface/tab_view.rb', line 119

def size
  @tabs.size
end

#switch_to_label(label) ⇒ Object

Switches the active tab to the specified label



124
125
126
127
128
129
130
131
# File 'lib/salticid/interface/tab_view.rb', line 124

def switch_to_label(label)
  if index = @tabs.map{ |tab| tab.to_s }.index(label)
    @active = index
    render
  else
    raise RuntimeError.new("no tab labeled #{label}")
  end
end