Class: Twterm::TabManager

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

Constant Summary collapse

DUMPED_TABS_FILE =
"#{ENV['HOME']}/.twterm/dumped_tabs"

Instance Method Summary collapse

Methods included from Utils

check_type

Methods included from Subscriber

included, #subscribe, #unsubscribe

Methods included from Publisher

#publish

Constructor Details

#initializeTabManager

Returns a new instance of TabManager.



72
73
74
75
76
77
78
79
80
# File 'lib/twterm/tab_manager.rb', line 72

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

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

  subscribe(Event::Screen::Resize, :resize)
end

Instance Method Details

#add(tab_to_add) ⇒ Object



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

def add(tab_to_add)
  check_type Tab::Base, tab_to_add

  @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



31
32
33
34
35
36
37
# File 'lib/twterm/tab_manager.rb', line 31

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

#closeObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twterm/tab_manager.rb', line 39

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
  publish(Event::Notification.new(:error, 'this tab cannot be closed'))
end

#current_tabObject



51
52
53
54
# File 'lib/twterm/tab_manager.rb', line 51

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

#dump_tabsObject



56
57
58
59
60
61
62
63
64
# File 'lib/twterm/tab_manager.rb', line 56

def dump_tabs
  data = @tabs.each_with_object([]) do |tab, arr|
    next unless tab.is_a? Tab::Dumpable
    arr << [tab.class, tab.title, tab.dump]
  end
  File.open(DUMPED_TABS_FILE, 'w', 0600) do |f|
    f.write data.to_yaml
  end
end

#each_tab(&block) ⇒ Object



66
67
68
69
70
# File 'lib/twterm/tab_manager.rb', line 66

def each_tab(&block)
  @tabs.each do |tab|
    block.call(tab)
  end
end

#open_my_profileObject



82
83
84
85
86
# File 'lib/twterm/tab_manager.rb', line 82

def open_my_profile
  current_user_id = Client.current.user_id
  tab = Tab::UserTab.new(current_user_id)
  add_and_show(tab)
end

#open_newObject



88
89
90
91
# File 'lib/twterm/tab_manager.rb', line 88

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

#recover_tabsObject



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

def recover_tabs
  unless File.exist? DUMPED_TABS_FILE
    tab = Tab::KeyAssignmentsCheatsheet.new
    add(tab)
    return
  end

  data = YAML.load(File.read(DUMPED_TABS_FILE))
  data.each do |klass, title, arg|
    tab = klass.recover(title, arg)
    add(tab)
  end
rescue
  publish(Event::Notification.new(:error, 'Failed to recover tabs'))
end

#refresh_windowObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/twterm/tab_manager.rb', line 109

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/twterm/tab_manager.rb', line 129

def respond_to_key(key)
  case key
  when ?1..?9
    @index = key.to_i - 1 if @tabs.count >= key.to_i
    current_tab.refresh
    refresh_window
  when ?0
    @index = @tabs.count - 1
    current_tab.refresh
    refresh_window
  when ?h, 2, Key::LEFT
    show_previous
  when ?l, 6, Key::RIGHT
    show_next
  when ?P
    open_my_profile
  when ?N
    open_new
  when ?w
    close
  else
    return false
  end
  true
end

#show_nextObject



155
156
157
158
159
# File 'lib/twterm/tab_manager.rb', line 155

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

#show_previousObject



161
162
163
164
165
# File 'lib/twterm/tab_manager.rb', line 161

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

#switch(tab) ⇒ Object



167
168
169
170
# File 'lib/twterm/tab_manager.rb', line 167

def switch(tab)
  close
  add_and_show(tab)
end