Class: Twterm::TabManager

Inherits:
Object
  • Object
show all
Includes:
Curses, Singleton
Defined in:
lib/twterm/tab_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeTabManager

Returns a new instance of TabManager.



6
7
8
9
10
11
12
# File 'lib/twterm/tab_manager.rb', line 6

def initialize
  @tabs = []
  @index = 0
  @history = []

  @window = stdscr.subwin(3, stdscr.maxx - 30, 0, 0)
end

Instance Method Details

#add(tab_to_add) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twterm/tab_manager.rb', line 14

def add(tab_to_add)
  fail ArgumentError, 'argument must be an instance of Tab::Base' unless tab_to_add.is_a? Tab::Base
  @tabs.each.with_index do |tab, i|
    next unless tab == tab_to_add
    @index = i
    refresh_window
    return false
  end
  @tabs << tab_to_add
  @history.push(@tabs.count)
  refresh_window
  true
end

#add_and_show(tab) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/twterm/tab_manager.rb', line 28

def add_and_show(tab)
  result = add(tab)
  @index = @tabs.count - 1 if result
  current_tab.refresh
  refresh_window
  result
end

#closeObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twterm/tab_manager.rb', line 58

def close
  current_tab.close
  @tabs.delete_at(@index)
  @history.delete_if { |n| n == @index }
  @history = @history.map { |i| i > @index ? i - 1 : i }
  @index = @history.first
  current_tab.refresh
  refresh_window
rescue Tab::NotClosableError
  Notifier.instance.show_error 'This tab cannot be closed'
end

#current_tabObject



36
37
38
39
# File 'lib/twterm/tab_manager.rb', line 36

def current_tab
  @history.unshift(@index).uniq!
  @tabs[@index]
end

#open_newObject



53
54
55
56
# File 'lib/twterm/tab_manager.rb', line 53

def open_new
  tab = Tab::New::Start.new
  add_and_show(tab)
end

#refresh_windowObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/twterm/tab_manager.rb', line 75

def refresh_window
  @window.clear
  current_tab_id = current_tab.object_id

  @window.setpos(1, 1)
  @window.addstr('|  ')
  @tabs.each do |tab|
    if tab.object_id == current_tab_id
      @window.bold do
        @window.addstr(tab.title)
      end
    else
      @window.addstr(tab.title)
    end
    @window.addstr('  |  ')
  end

  @window.refresh
end

#respond_to_key(key) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/twterm/tab_manager.rb', line 95

def respond_to_key(key)
  case key
  when 'h', 2, Key::LEFT
    show_previous
  when 'l', 6, Key::RIGHT
    show_next
  when 'N'
    open_new
  when 'w'
    close
  else
    return false
  end
  true
end

#show_nextObject



41
42
43
44
45
# File 'lib/twterm/tab_manager.rb', line 41

def show_next
  @index = (@index + 1) % @tabs.count
  current_tab.refresh
  refresh_window
end

#show_previousObject



47
48
49
50
51
# File 'lib/twterm/tab_manager.rb', line 47

def show_previous
  @index = (@index - 1) % @tabs.count
  current_tab.refresh
  refresh_window
end

#switch(tab) ⇒ Object



70
71
72
73
# File 'lib/twterm/tab_manager.rb', line 70

def switch(tab)
  close
  add_and_show(tab)
end