Class: Twterm::TabManager

Inherits:
Object
  • Object
show all
Includes:
Curses, 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

#initialize(app, client) ⇒ TabManager

Returns a new instance of TabManager.



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

def initialize(app, client)
  @app, @client = app, client

  @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::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



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.render
  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.render
  refresh_window
rescue Tab::NotClosableError
  publish(Event::Message::Warning.new('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



84
85
86
87
88
# File 'lib/twterm/tab_manager.rb', line 84

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_newObject



90
91
92
93
# File 'lib/twterm/tab_manager.rb', line 90

def open_new
  tab = Tab::New::Index.new(app, client)
  add_and_show(tab)
end

#recover_tabsObject



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

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_windowObject



111
112
113
114
115
# File 'lib/twterm/tab_manager.rb', line 111

def refresh_window
  @window.clear
  view.render
  @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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/twterm/tab_manager.rb', line 129

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_nextObject



169
170
171
172
173
# File 'lib/twterm/tab_manager.rb', line 169

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

#show_nth_tab(n) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/twterm/tab_manager.rb', line 175

def show_nth_tab(n)
  return unless n < @tabs.count

  @index = n
  current_tab.render
  refresh_window
end

#show_previousObject



183
184
185
186
187
# File 'lib/twterm/tab_manager.rb', line 183

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

#switch(tab) ⇒ Object



189
190
191
192
# File 'lib/twterm/tab_manager.rb', line 189

def switch(tab)
  close
  add_and_show(tab)
end

#viewObject



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/twterm/tab_manager.rb', line 117

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).at(1, 1)
end