Class: Tmux::Session

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tmux/session.rb

Overview

A session is a single collection of pseudo terminals under the management of tmux. Each session has one or more windows linked to it. A window occupies the entire screen and may be split into rectangular 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 windows may be present in the same session. Once all sessions are killed, tmux exits.

Instance Attribute Summary collapse

Selecting collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, name) ⇒ Session

Returns a new instance of Session.



122
123
124
125
126
# File 'lib/tmux/session.rb', line 122

def initialize(server, name)
  @server, @name = server, name
  @status_bar = StatusBar.new(self)
  @options = OptionsList.new(:session, self, false)
end

Instance Attribute Details

#attachedBoolean (readonly) Also known as: attached?

Returns:

  • (Boolean)


187
188
189
# File 'lib/tmux/session.rb', line 187

def attached
  @attached
end

#buffersArray<Buffer> (readonly)

Returns All buffers.

Returns:

tmux command:

  • list-buffers



272
273
274
# File 'lib/tmux/session.rb', line 272

def buffers
  @buffers
end

#clientsArray<Client> (readonly)

Returns All clients.

Returns:



195
196
197
# File 'lib/tmux/session.rb', line 195

def clients
  @clients
end

#creation_timeTime (readonly) Also known as: created_at

Returns:

  • (Time)


165
166
167
# File 'lib/tmux/session.rb', line 165

def creation_time
  @creation_time
end

#current_panePane (readonly)

Returns The currently displayed pane.

Returns:

  • (Pane)

    The currently displayed pane.

See Also:



72
73
74
# File 'lib/tmux/session.rb', line 72

def current_pane
  @current_pane
end

#current_windowWindow (readonly)

Returns The currently displayed window.

Returns:

See Also:



64
65
66
# File 'lib/tmux/session.rb', line 64

def current_window
  @current_window
end

#heightInteger (readonly)

Returns:

  • (Integer)


180
181
182
# File 'lib/tmux/session.rb', line 180

def height
  @height
end

#identifierString (readonly)

Returns:

  • (String)


140
141
142
# File 'lib/tmux/session.rb', line 140

def identifier
  @identifier
end

#nameString #name=(new_name) ⇒ String

Overloads:

  • #nameString

    Returns:

    • (String)
  • #name=(new_name) ⇒ String
    TODO:

    escape name

    Renames the session.

    Returns:

    • (String)

    tmux command:

    • rename-session

Returns:

  • (String)


114
115
116
# File 'lib/tmux/session.rb', line 114

def name
  @name
end

#num_windowsInteger (readonly)

Returns:

  • (Integer)


158
159
160
# File 'lib/tmux/session.rb', line 158

def num_windows
  @num_windows
end

#optionsOptionsList (readonly)

Returns:



119
120
121
# File 'lib/tmux/session.rb', line 119

def options
  @options
end

#serverServer (readonly)

Returns:



117
118
119
# File 'lib/tmux/session.rb', line 117

def server
  @server
end

#status_barStatusBar (readonly)

Returns:



121
122
123
# File 'lib/tmux/session.rb', line 121

def status_bar
  @status_bar
end

#widthInteger (readonly)

Returns:

  • (Integer)


173
174
175
# File 'lib/tmux/session.rb', line 173

def width
  @width
end

#windowsHash{Number => Window} (readonly)

Returns All windows.

Returns:

tmux command:

  • list-windows

Required tmux version:

  • >=1.1



244
245
246
# File 'lib/tmux/session.rb', line 244

def windows
  @windows
end

Class Method Details

.options(session) ⇒ Options

Returns:



15
16
17
# File 'lib/tmux/session.rb', line 15

def self.options(session)
  OptionsList.new(:session, session, true)
end

Instance Method Details

#<=>(other) ⇒ Undefined



100
101
102
103
# File 'lib/tmux/session.rb', line 100

def <=>(other)
  return nil unless other.is_a?(Session)
  [@server, @name] <=> [other.server, other.name]
end

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/tmux/session.rb', line 86

def ==(other)
  self.class == other.class && @server == other.server && @name == other.name
end

#any_clientClient?

Returns a client that is displaying the session.

Returns:

  • (Client, nil)

    A client that is displaying the session.



81
82
83
# File 'lib/tmux/session.rb', line 81

def any_client
  @server.clients({:session => self}).first
end

#attach

This method returns an undefined value.

Attach to a session. Replaces the ruby process.

tmux command:

  • attach



205
206
207
# File 'lib/tmux/session.rb', line 205

def attach
  exec "#{Tmux::BINARY} attach -t #{identifier}"
end

#buffers_information(search = {}) ⇒ Hash

Returns A hash with information for all buffers.

Parameters:

Returns:

  • (Hash)

    A hash with information for all buffers

tmux command:

  • list-buffers



259
260
261
262
263
264
265
266
267
268
# File 'lib/tmux/session.rb', line 259

def buffers_information(search = {})
  hash = {}
  buffers = @server.invoke_command "list-buffers -t #{identifier}"
  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

#create_window(args = {}) ⇒ Window

Creates a new window.

Parameters:

  • args (Hash) (defaults to: {})

    a customizable set of options

Options Hash (args):

  • :after_number (Boolean) — default: false

    If true, the new window will be inserted at the next index up from the specified number (or the current window), moving windows up if necessary

  • :kill_existing (Boolean) — default: false

    Kill an existing window if it conflicts with a desired number

  • :make_active (Boolean) — default: true

    Switch to the newly generated window

  • :name (String)

    Name of the new window (optional)

  • :number (Number)

    Number of the new window (optional)

  • :command (String)

    Command to run in the new window (optional)

Returns:

tmux command:

  • new-window



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tmux/session.rb', line 38

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 << "-t #{args[:number]}" if args[:number]
  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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tmux/session.rb', line 96

def eql?(other)
  self == other
end

#hashNumber

Returns:

  • (Number)


91
92
93
# File 'lib/tmux/session.rb', line 91

def hash
  [@server.hash, @number].hash
end

#kill

This method returns an undefined value.

Kills the session.

tmux command:

  • kill-session



213
214
215
# File 'lib/tmux/session.rb', line 213

def kill
  @server.invoke_command "kill-session -t #{identifier}"
end

#lock

This method returns an undefined value.

Locks the session.

tmux command:

  • lock-session

Required tmux version:

  • >=1.1



151
152
153
154
155
# File 'lib/tmux/session.rb', line 151

def lock
  @server.check_for_version!("1.1")

  @server.invoke_command "lock-session -t #{identifier}"
end

#select_last_windowWindow

Select the last (previously selected) window.

Returns:



285
286
287
288
# File 'lib/tmux/session.rb', line 285

def select_last_window
  @server.invoke_command "last-window -t #{identifier}"
  current_window
end

#select_next_window(num = 1) ⇒ Window

Selects the next (higher index) window

Parameters:

  • num (Number) (defaults to: 1)

    How many windows to move

Returns:

Required tmux version:

  • >=1.3



295
296
297
298
# File 'lib/tmux/session.rb', line 295

def select_next_window(num = 1)
  @server.invoke_command "select-window -t #{identifier}:+#{num}"
  current_window
end

#select_previous_window(num = 1) ⇒ Window

Selects the previous (lower index) window

Parameters:

  • num (Number) (defaults to: 1)

    How many windows to move

Returns:

Required tmux version:

  • >=1.3



305
306
307
308
# File 'lib/tmux/session.rb', line 305

def select_previous_window(num = 1)
  @server.invoke_command "select-window -t:-#{num}"
  current_window
end

#windows_information(search = {}) ⇒ Hash

Parameters:

Returns:

  • (Hash)

    A hash with information for all windows

  • (Hash)

tmux command:

  • list-windows

Required tmux version:

  • >=1.1



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/tmux/session.rb', line 222

def windows_information(search = {})
  @server.check_for_version!("1.1")

  hash = {}
  output = @server.invoke_command "list-windows -t #{identifier}"
  output.each_line do |session|
    params = session.match(/^(?<num>\d+): (?<name>.+?) \[(?<width>\d+)x(?<height>\d+)\]$/)
    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

    hash[num] = {:num => num, :name => name, :width => width, :height => height}
  end
  hash.extend FilterableHash
  hash.filter(search)
end