Class: Twterm::TabManager
- Inherits:
-
Object
- Object
- Twterm::TabManager
- Includes:
- 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
- #add(tab_to_add) ⇒ Object
- #add_and_show(tab) ⇒ Object
- #close ⇒ Object
- #current_tab ⇒ Object
- #dump_tabs ⇒ Object
- #each_tab(&block) ⇒ Object
-
#enclose?(x, y) ⇒ Boolean
Returns if the given coordinate is enclosed by the window.
-
#handle_left_click(x, _y) ⇒ nil
Open the clicked tab.
-
#handle_scroll_down(_x, _y) ⇒ nil
Open next tab.
-
#handle_scroll_up(_x, _y) ⇒ nil
Open previous tab.
-
#initialize(app, client, window) ⇒ TabManager
constructor
A new instance of TabManager.
- #open_my_profile ⇒ Object
- #open_new ⇒ Object
- #recover_tabs ⇒ Object
- #refresh_window ⇒ Object
- #respond_to_key(key) ⇒ Object
- #show_next ⇒ Object
- #show_nth_tab(n) ⇒ Object
- #show_previous ⇒ Object
- #switch(tab) ⇒ Object
- #view ⇒ Object
Methods included from Utils
Methods included from Subscriber
included, #subscribe, #unsubscribe
Methods included from Publisher
Constructor Details
#initialize(app, client, window) ⇒ TabManager
Returns a new instance of TabManager.
87 88 89 90 91 92 93 94 95 |
# File 'lib/twterm/tab_manager.rb', line 87 def initialize(app, client, window) @app, @client = app, client @tabs = [] @index = 0 @history = [] @window = window end |
Instance Method Details
#add(tab_to_add) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/twterm/tab_manager.rb', line 14 def add(tab_to_add) check_type Tab::AbstractTab, 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
29 30 31 32 33 34 35 |
# File 'lib/twterm/tab_manager.rb', line 29 def add_and_show(tab) result = add(tab) @index = @tabs.count - 1 if result current_tab.render refresh_window result end |
#close ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/twterm/tab_manager.rb', line 37 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.render refresh_window rescue Tab::NotClosableError publish(Event::Message::Warning.new('this tab cannot be closed')) end |
#current_tab ⇒ Object
49 50 51 52 |
# File 'lib/twterm/tab_manager.rb', line 49 def current_tab @history.unshift(@index).uniq! @tabs[@index] end |
#dump_tabs ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/twterm/tab_manager.rb', line 54 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
64 65 66 67 68 |
# File 'lib/twterm/tab_manager.rb', line 64 def each_tab(&block) @tabs.each do |tab| block.call(tab) end end |
#enclose?(x, y) ⇒ Boolean
Returns if the given coordinate is enclosed by the window
75 76 77 78 79 80 81 82 |
# File 'lib/twterm/tab_manager.rb', line 75 def enclose?(x, y) left = @window.begx top = @window.begy right = left + @window.maxx bottom = top + @window.maxy left <= x && x < right && top <= y && y < bottom end |
#handle_left_click(x, _y) ⇒ nil
Open the clicked tab
103 104 105 106 107 108 109 110 |
# File 'lib/twterm/tab_manager.rb', line 103 def handle_left_click(x, _y) n = find_tab_index_on_x(x) return if n.nil? show_nth_tab(n) nil end |
#handle_scroll_down(_x, _y) ⇒ nil
Open next tab
118 119 120 121 122 |
# File 'lib/twterm/tab_manager.rb', line 118 def handle_scroll_down(_x, _y) show_next nil end |
#handle_scroll_up(_x, _y) ⇒ nil
Open previous tab
130 131 132 133 134 |
# File 'lib/twterm/tab_manager.rb', line 130 def handle_scroll_up(_x, _y) show_previous nil end |
#open_my_profile ⇒ Object
136 137 138 139 140 |
# File 'lib/twterm/tab_manager.rb', line 136 def open_my_profile current_user_id = client.user_id tab = Tab::UserTab.new(app, client, current_user_id) add_and_show(tab) end |
#open_new ⇒ Object
142 143 144 145 |
# File 'lib/twterm/tab_manager.rb', line 142 def open_new tab = Tab::New::Index.new(app, client) add_and_show(tab) end |
#recover_tabs ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/twterm/tab_manager.rb', line 147 def recover_tabs unless File.exist? DUMPED_TABS_FILE tab = Tab::KeyAssignmentsCheatsheet.new(app, client) add(tab) return end data = YAML.load(File.read(DUMPED_TABS_FILE)) data.each do |klass, title, arg| tab = klass.recover(app, client, title, arg) add(tab) end rescue StandardError publish(Event::Message::Error.new('Failed to recover tabs')) end |
#refresh_window ⇒ Object
163 164 165 166 167 |
# File 'lib/twterm/tab_manager.rb', line 163 def refresh_window @window.clear view.render @window.refresh end |
#respond_to_key(key) ⇒ Object
181 182 183 184 185 186 187 188 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 |
# File 'lib/twterm/tab_manager.rb', line 181 def respond_to_key(key) k = KeyMapper.instance case key when k[:tab, :'1st'] show_nth_tab(0) when k[:tab, :'2nd'] show_nth_tab(1) when k[:tab, :'3rd'] show_nth_tab(2) when k[:tab, :'4th'] show_nth_tab(3) when k[:tab, :'5th'] show_nth_tab(4) when k[:tab, :'6th'] show_nth_tab(5) when k[:tab, :'7th'] show_nth_tab(6) when k[:tab, :'8th'] show_nth_tab(7) when k[:tab, :'9th'] show_nth_tab(8) when k[:tab, :last] show_nth_tab(@tabs.count - 1) when k[:general, :left], Curses::Key::LEFT show_previous when k[:general, :right], Curses::Key::RIGHT show_next when k[:app, :me] open_my_profile when k[:tab, :new] open_new when k[:tab, :close] close else return false end true end |
#show_next ⇒ Object
221 222 223 224 225 |
# File 'lib/twterm/tab_manager.rb', line 221 def show_next @index = (@index + 1) % @tabs.count current_tab.render refresh_window end |
#show_nth_tab(n) ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/twterm/tab_manager.rb', line 227 def show_nth_tab(n) return unless n < @tabs.count @index = n current_tab.render refresh_window end |
#show_previous ⇒ Object
235 236 237 238 239 |
# File 'lib/twterm/tab_manager.rb', line 235 def show_previous @index = (@index - 1) % @tabs.count current_tab.render refresh_window end |
#switch(tab) ⇒ Object
241 242 243 244 |
# File 'lib/twterm/tab_manager.rb', line 241 def switch(tab) close add_and_show(tab) end |
#view ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/twterm/tab_manager.rb', line 169 def view wss = Image.string(' ') pipe = Image.string('|') image = @tabs .map { |t| [t, Image.string(t.title)] } .map { |t, r| t.equal?(current_tab) ? !r._ : r } .reduce(pipe) { |acc, x| acc - wss - x - wss - pipe } View.new(@window, image) end |