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

#clients(search = {}) ⇒ Array<Client> (readonly)

Returns:

tmux command:

  • list-clients



183
184
185
# File 'lib/tmux/server.rb', line 183

def clients
  @clients
end

#infoString (readonly)

Returns Information about the server.

Returns:

  • (String)

    Information about the server

tmux command:

  • server-info



193
194
195
# File 'lib/tmux/server.rb', line 193

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



73
74
75
# File 'lib/tmux/server.rb', line 73

def server
  @server
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.



147
148
149
# File 'lib/tmux/server.rb', line 147

def session
  @session
end

#sessions(search = {}) ⇒ Array<Session> (readonly)

Returns All sessions.

Returns:

tmux command:

  • list-sessions



136
137
138
# File 'lib/tmux/server.rb', line 136

def sessions
  @sessions
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



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

def version
  @version
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

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



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

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.match(/^(?<device>.+?): (?<session>\d+) \[(?<width>\d+)x(?<height>\d+) (?<term>.+?)\](?: \((?<utf8>utf8)\))?$/)
    device  = params[:device]
    session = sessions[params[:session].to_i]
    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