Class: Tmux::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket = "default") ⇒ Server

Returns a new instance of Server

Parameters:

  • socket (String) (defaults to: "default")

    A socket name.



59
60
61
62
# File 'lib/tmux/server.rb', line 59

def initialize(socket = "default")
  @socket = socket
  @options = OptionsList.new(:server, self, false)
end

Instance Attribute Details

#clientsArray<Client> (readonly)

Returns:

tmux command:

  • list-clients



185
186
187
188
189
# File 'lib/tmux/server.rb', line 185

def clients(search = {})
  clients_information(search).map { |device, information|
    Client.new(self, device)
  }
end

#infoString (readonly)

Returns Information about the server

Returns:

  • (String)

    Information about the server

tmux command:

  • server-info



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

def info
  invoke_command "server-info"
end

#optionsOptionsList (readonly)

Returns:



57
58
59
# File 'lib/tmux/server.rb', line 57

def options
  @options
end

#serverServer (readonly)

Returns self. This is useful for other classes which can operate on Server, Tmux::Session, Window, Pane and so on

Returns:



75
76
77
# File 'lib/tmux/server.rb', line 75

def server
  self
end

#sessionSession (readonly)

Returns The first session. This is especially useful if working with a server that only has one session.

Returns:

  • (Session)

    The first session. This is especially useful if working with a server that only has one session.



149
150
151
# File 'lib/tmux/server.rb', line 149

def session
  sessions.first
end

#sessionsArray<Session> (readonly)

Returns All sessions

Returns:

tmux command:

  • list-sessions



138
139
140
141
142
# File 'lib/tmux/server.rb', line 138

def sessions(search = {})
  sessions_information(search).map do |name, information|
    Session.new(self, name)
  end
end

#socketString (readonly)

Returns:

  • (String)


55
56
57
# File 'lib/tmux/server.rb', line 55

def socket
  @socket
end

#versionString (readonly)

Returns Version of the tmux server

Returns:

  • (String)

    Version of the tmux server



201
202
203
# File 'lib/tmux/server.rb', line 201

def version
  @version ||= info.lines.first.split(",").first[/([.\d]+)/]
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Returns:

  • (-1, 0, 1)


65
66
67
68
# File 'lib/tmux/server.rb', line 65

def <=>(other)
  return nil unless other.is_a?(Server)
  @socket <=> other.socket
end

#buffersArray<Buffer>

Returns all buffers of all sessions.

Returns:

See Also:

tmux command:

  • list-buffers



210
211
212
213
214
215
216
217
218
# File 'lib/tmux/server.rb', line 210

def buffers
  if @server.version < "1.5"
    # Manually walk all sessions
    sessions.flat_map {|s| s.buffers}
  else
    # There's only a global stack, so any session will do
    session.buffers
  end
end

#check_for_version!(required)

This method returns an undefined value.

Checks if a version requirement is being met

Parameters:

  • required (String)

    The version at least required

Raises:



225
226
227
228
229
# File 'lib/tmux/server.rb', line 225

def check_for_version!(required)
  if required > version
    raise Exception::UnsupportedVersion, required
  end
end

#clients_information(search = {}) ⇒ Hash

Returns A hash with information for all clients

Parameters:

Returns:

  • (Hash)

    A hash with information for all clients

tmux command:

  • list-clients



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tmux/server.rb', line 156

def clients_information(search = {})
  clients = invoke_command "list-clients"
  hash = {}
  clients.each_line do |client|
    params  = client.strip.match(/^(?<device>.+?): (?<session>.+?) \[(?<width>\d+)x(?<height>\d+) (?<term>.+?)\](?: \((?<utf8>utf8)\))?$/)
    device  = params[:device]
    session = sessions(:name => params[:session]).first
    width   = params[:width].to_i
    height  = params[:height].to_i
    term    = params[:term]
    utf8    = !!params[:utf8]

    hash[device] = {
      :device => device,
      :session => session,
      :width => width,
      :height => height,
      :term => term,
      :utf8 => utf8,
    }
  end
  hash.extend FilterableHash
  hash.filter(search)
end

#create_session(args = {}) ⇒ Session?

Creates a new session

Parameters:

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

    a customizable set of options

Options Hash (args):

  • :attach (Boolean) — default: false

    Attach to the new session?

  • :name (String) — default: nil

    Name of the new session. Will be automatically generated of nil.

  • :window_name (String) — default: nil

    Name of the initial window. Cannot be used when grouping sessions.

  • :group_with (Session) — default: nil

    Group with this session, sharing all windows.

  • :command (String) — default: nil

    Execute this command in the initial window. Cannot be used when grouping sessions.

Returns:

  • (Session, nil)

    Returns the new session if a :name has been given and if :attach is false

Raises:

  • (ArgumentError)

    if combining :group_with and :window_name or :command

Required tmux version:

  • >=1.4



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
# File 'lib/tmux/server.rb', line 25

def create_session(args = {})
  check_for_version!("1.4")

  if args[:group_with] && (args[:window_name] || args[:command])
    raise ArgumentError, "Cannot combine :group_with and :window_name or :command"
  end

  # FIXME shell escape names
  flags = []
  flags << "-d" unless args[:attach]
  flags << "-n '#{args[:window_name]}'"     if args[:window_name]
  flags << "-s '#{args[:name]}'"            if args[:name]
  flags << "-t '#{args[:group_with].name}'" if args[:group_with]
  flags << args[:command]                   if args[:command]

  command = "new-session #{flags.join(" ")}"

  ret = invoke_command(command, true)
  if ret.start_with?("duplicate session:")
    raise RuntimeError, ret
  elsif ret.start_with?("sessions should be nested with care.")
    raise Exception::InTmux("new-session")
  else
    if args[:name] and !args[:attach]
      return Session.new(self, args[:name])
    end
  end
end

#invoke_command(command, unset_tmux = false)

This method returns an undefined value.

Invokes a tmux command.

Parameters:

  • command (String)

    The command to invoke



83
84
85
# File 'lib/tmux/server.rb', line 83

def invoke_command(command, unset_tmux = false)
  Tmux.invoke_command("-L #@socket #{command}", unset_tmux)
end

#kill

This method returns an undefined value.

Kills a server and thus all sessions, windows and clients.

tmux command:

  • kill-server



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

def kill
  invoke_command "kill-server"
end

#sessions_information(search = {}) ⇒ Hash

Returns A hash with information for all sessions

Parameters:

Returns:

  • (Hash)

    A hash with information for all sessions

tmux command:

  • list-sessions



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
# File 'lib/tmux/server.rb', line 108

def sessions_information(search = {})
  hash = {}
  output = invoke_command "list-sessions"
  output.each_line do |session|
    params = session.match(/^(?<name>[\w-]+?): (?<num_windows>\d+) windows \(created (?<creation_time>.+?)\) \[(?<width>\d+)x(?<height>\d+)\](?: \((?<attached>attached)\))?$/)

    name          = params[:name]
    num_windows   = params[:num_windows].to_i
    creation_time = Date.parse(params[:creation_time])
    width         = params[:width].to_i
    height        = params[:height].to_i
    attached      = !!params[:attached]

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

#source_file(file) Also known as: load

This method returns an undefined value.

Sources a file, that is load and evaluate it in tmux.

Parameters:

  • file (String)

    Name of the file to source

tmux command:

  • source-file



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

def source_file(file)
  invoke_command "source-file #{file}"
end