Class: Twterm::TabManager
- Inherits:
-
Object
- Object
- Twterm::TabManager
- Includes:
- Curses, Singleton
- 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
-
#initialize ⇒ TabManager
constructor
A new instance of TabManager.
- #open_my_profile ⇒ Object
- #open_new ⇒ Object
- #recover_tabs ⇒ Object
- #refresh_window ⇒ Object
- #resize ⇒ Object
- #respond_to_key(key) ⇒ Object
- #show_next ⇒ Object
- #show_previous ⇒ Object
- #switch(tab) ⇒ Object
Constructor Details
#initialize ⇒ TabManager
Returns a new instance of TabManager.
64 65 66 67 68 69 70 |
# File 'lib/twterm/tab_manager.rb', line 64 def initialize @tabs = [] @index = 0 @history = [] @window = stdscr.subwin(3, stdscr.maxx, 0, 0) end |
Instance Method Details
#add(tab_to_add) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/twterm/tab_manager.rb', line 8 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
23 24 25 26 27 28 29 |
# File 'lib/twterm/tab_manager.rb', line 23 def add_and_show(tab) result = add(tab) @index = @tabs.count - 1 if result current_tab.refresh refresh_window result end |
#close ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/twterm/tab_manager.rb', line 31 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_tab ⇒ Object
43 44 45 46 |
# File 'lib/twterm/tab_manager.rb', line 43 def current_tab @history.unshift(@index).uniq! @tabs[@index] end |
#dump_tabs ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/twterm/tab_manager.rb', line 48 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
58 59 60 61 62 |
# File 'lib/twterm/tab_manager.rb', line 58 def each_tab(&block) @tabs.each do |tab| block.call(tab) end end |
#open_my_profile ⇒ Object
72 73 74 75 76 |
# File 'lib/twterm/tab_manager.rb', line 72 def open_my_profile current_user_id = Client.current.user_id tab = Tab::UserTab.new(current_user_id) add_and_show(tab) end |
#open_new ⇒ Object
78 79 80 81 |
# File 'lib/twterm/tab_manager.rb', line 78 def open_new tab = Tab::New::Start.new add_and_show(tab) end |
#recover_tabs ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twterm/tab_manager.rb', line 83 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 Notifier.instance.show_error 'Failed to recover tabs' end |
#refresh_window ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/twterm/tab_manager.rb', line 99 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 |
#resize ⇒ Object
119 120 121 122 |
# File 'lib/twterm/tab_manager.rb', line 119 def resize @window.resize(3, stdscr.maxx) @window.move(0, 0) end |
#respond_to_key(key) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/twterm/tab_manager.rb', line 124 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_next ⇒ Object
150 151 152 153 154 |
# File 'lib/twterm/tab_manager.rb', line 150 def show_next @index = (@index + 1) % @tabs.count current_tab.refresh refresh_window end |
#show_previous ⇒ Object
156 157 158 159 160 |
# File 'lib/twterm/tab_manager.rb', line 156 def show_previous @index = (@index - 1) % @tabs.count current_tab.refresh refresh_window end |
#switch(tab) ⇒ Object
162 163 164 165 |
# File 'lib/twterm/tab_manager.rb', line 162 def switch(tab) close add_and_show(tab) end |