This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
session.rb
334 lines (291 loc) · 9.22 KB
/
session.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
module Tmux
# A session is a single collection of pseudo terminals under the
# management of {Tmux tmux}. Each session has one or more {Window
# windows} linked to it. A {Window window} occupies the entire
# screen and may be split into rectangular {Pane panes}, each of
# which is a separate pseudo terminal (the pty(4) manual page
# documents the technical details of pseudo terminals). Any number
# of tmux instances may connect to the same session, and any number
# of {Window windows} may be present in the same session. Once all
# sessions are {Session#kill killed}, tmux exits.
class Session
include Comparable
# @return [Options]
def self.options(session)
OptionsList.new(:session, session, true)
end
# Creates a new {Window window}.
#
# @option args [Boolean] :after_number (false) If true, the new
# {Window window} will be inserted at the next index up from the
# specified number (or the {Client#current_window current}
# {Window window}), moving {Window windows} up if necessary
# @option args [Boolean] :kill_existing (false) Kill an existing
# {Window window} if it conflicts with a desired number
# @option args [Boolean] :make_active (true) Switch to the newly
# generated {Window window}
# @option args [String] :name Name of the new {Window window}
# (optional)
# @option args [Number] :number Number of the new {Window window}
# (optional)
# @option args [String] :command Command to run in the new {Window
# window} (optional)
#
# @tmux new-window
# @return [Window] The newly created {Window window}
def create_window(args = {})
args = {
:kill_existing => false,
:make_active => true,
:after_number => false,
}.merge(args)
flags = []
# flags << "-d" unless args[:make_active]
flags << "-a" if args[:after_number]
flags << "-k" if args[:kill_existing]
flags << "-n '#{args[:name]}'" if args[:name] # FIXME escaping
flags << if args[:number]
"-t #{identifier}:#{args[:number]}"
else
"-t #{identifier}"
end
flags << args[:command] if args[:command]
@server.invoke_command "new-window #{flags.join(" ")}"
new_window = current_window
unless args[:make_active]
select_last_window
end
# return Window.new(self, num)
return new_window
end
# @!attribute [r] current_window
#
# @see Client#current_window
# @return (see Client#current_window)
def current_window
# TODO figure out when tmux added "(active)" in list-windows and
# use that when possible
any_client.current_window
end
# @!attribute [r] current_pane
#
# @see Client#current_pane
# @return (see Client#current_pane)
def current_pane
any_client.current_pane
end
# Returns a {Client client} that is displaying the session.
#
# @return [Client, nil] A {Client client} that is displaying the session.
def any_client
@server.clients({:session => self}).first
end
# @return [Boolean]
def ==(other)
self.class == other.class && @server == other.server && @name == other.name
end
# @return [Number]
def hash
[@server.hash, @number].hash
end
# @return [Boolean]
def eql?(other)
self == other
end
def <=>(other)
return nil unless other.is_a?(Session)
[@server, @name] <=> [other.server, other.name]
end
# The session name.
#
# Setting this will rename the session.
#
# @todo escape name
# @tmux rename-session
# @return [String]
attr_reader :name
# @return [Server]
attr_reader :server
# @return [OptionsList]
attr_reader :options
# @return [StatusBar]
attr_reader :status_bar
def initialize(server, name)
@server, @name = server, name
@status_bar = StatusBar.new(self)
@options = OptionsList.new(:session, self, false)
end
# @!attribute name
#
# @return [String]
def name=(new_name)
raise ArgumentError if new_name.to_s.strip.empty?
ret = @server.invoke_command("rename-session -t #{identifier} '#{new_name}'")
if ret.start_with?("duplicate session:")
raise RuntimeError, ret
end
@name = new_name
end
# @!attribute [r] identifier
#
# @return [String]
def identifier
@name
end
# Locks the session.
#
# @tmux lock-session
# @return [void]
# @tmuxver >=1.1
def lock
@server.check_for_version!("1.1")
@server.invoke_command "lock-session -t #{identifier}"
end
# @!attribute [r] num_windows
#
# @return [Integer]
def num_windows
@server.sessions_information[@name][:num_windows]
end
# @!attribute [r] creation_time
#
# @return [Time]
def creation_time
@server.sessions_information[@name][:creation_time]
end
alias_method :created_at, :creation_time
# @!attribute [r] width
#
# @return [Integer]
def width
@server.sessions_information[@name][:width]
end
# @!attribute [r] height
#
# @return [Integer]
def height
@server.sessions_information[@name][:height]
end
# @!attribute [r] attached
#
# @return [Boolean]
def attached
@server.sessions_information[@name][:attached]
end
alias_method :attached?, :attached
# @!attribute [r] clients
#
# @return [Array<Client>] All {Client clients}
def clients
@server.clients({:session => self})
end
# Attach to a session. Replaces the ruby process.
#
# @return [void]
# @tmux attach
def attach
exec "#{Tmux::BINARY} attach -t #{identifier}"
end
# Kills the session.
#
# @tmux kill-session
# @return [void]
def kill
@server.invoke_command "kill-session -t #{identifier}"
end
# @tmux list-windows
# @tmuxver >=1.1
# @param [Hash] search Filters the resulting hash using {FilterableHash#filter}
# @return [Hash] A hash with information for all windows
# @return [Hash]
def windows_information(search = {})
@server.check_for_version!("1.1")
hash = {}
output = @server.invoke_command "list-windows -t #{identifier}"
output.each_line do |session|
# TODO make use of the layout information
# TODO make use of pane count value in later tmux versions
params = session.match(/^(?<num>\d+): (?<name>.*)( \(\d+ panes\))? \[(?<width>\d+)x(?<height>\d+)\](?: \[layout .+?\])?( @\d+)?(?<active> \(active\))?$/)
next if params.nil? # >=1.3 displays layout information in indented lines
num = params[:num].to_i
name = params[:name]
width = params[:width].to_i
height = params[:height].to_i
active = !params[:active].nil?
hash[num] = {:num => num, :name => name, :width => width, :height => height, :active => active}
end
hash.extend FilterableHash
hash.filter(search)
end
# @!attribute [r] windows
#
# @tmux list-windows
# @return [Hash{Number => Window}] All {Window windows}
# @tmuxver >=1.1
def windows
hash = {}
@server.check_for_version!("1.1")
windows_information.each do |num, information|
hash[num] = Window.new(self, num)
end
hash
end
# @param [Hash] search Filters the resulting hash using {FilterableHash#filter}
# @return [Hash] A hash with information for all buffers
# @tmux list-buffers
def buffers_information(search = {})
hash = {}
if @server.version < "1.5"
buffers = @server.invoke_command "list-buffers -t #{identifier}"
else
buffers = @server.invoke_command "list-buffers"
end
buffers.each_line do |buffer|
num, size = buffer.match(/^(\d+): (\d+) bytes/)[1..2]
hash[num] = {:size => size}
end
hash.extend FilterableHash
hash.filter(search)
end
# @!attribute [r] buffers
#
# @tmux list-buffers
# @return [Array<Buffer>] All {Buffer buffers}
# @note Beginning with tmux 1.5, {Buffer buffers} are global and
# not tied to a session anymore. That means that for 1.5 and
# higher, this method will return all buffers of the server,
# making it identical to {Server#buffers}.
def buffers
buffers_information.map do |num, information|
Buffer.new(num, self)
end
end
# @group Selecting
# Select the last (previously selected) window.
#
# @return [Window]
def select_last_window
@server.invoke_command "last-window -t #{identifier}"
current_window
end
# Selects the next (higher index) window
#
# @param [Number] num How many windows to move
# @tmuxver >=1.3
# @return [Window]
def select_next_window(num = 1)
@server.invoke_command "select-window -t #{identifier}:+#{num}"
current_window
end
# Selects the previous (lower index) window
#
# @param [Number] num How many windows to move
# @tmuxver >=1.3
# @return [Window]
def select_previous_window(num = 1)
@server.invoke_command "select-window -t:-#{num}"
current_window
end
# @endgroup
end
end