Class: Tmux::OptionsList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tmux/options_list.rb

Overview

OptionsList offers an easy way of querying and setting tmux options, taking care of typecasting. Note: You should not have to instantiate this class but use the respective ::options and #options methods instead.

Instance Method Summary collapse

Constructor Details

#initialize(kind, target, global = false) ⇒ OptionsList

Returns a new instance of OptionsList

Parameters:

  • kind (Symbol<:server, :session, :window>)

    Which options to operate on

  • target (Server, Session, Window)

    The target to operate on. Should be an instance of Server for global options

  • global (Boolean) (defaults to: false)

    Operate on global options?



18
19
20
21
22
# File 'lib/tmux/options_list.rb', line 18

def initialize(kind, target, global = false)
  @kind   = kind
  @global = global
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args)

This method returns an undefined value.

Unknown methods will be treated as getters and setters for options. Dashes in option names have to be replaced with underscores.



107
108
109
110
111
112
113
114
115
# File 'lib/tmux/options_list.rb', line 107

def method_missing(m, *args)
  option = m.to_s.tr("_", "-")
  if option[-1..-1] == "="
    option = option[0..-2]
    set(option, args.first)
  else
    get(option)
  end
end

Instance Method Details

#argument_string(global = nil, inject = []) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • global (Boolean, nil) (defaults to: nil)

    Operate on global options? Inherits from @global if nil

  • inject (Array<String>) (defaults to: [])

    Flags to inject into the argument string

Returns:

  • (String)


132
133
134
135
136
137
138
139
140
141
# File 'lib/tmux/options_list.rb', line 132

def argument_string(global = nil, inject = [])
  global = @global if global.nil?
  flags = []
  flags << "-g" if global
  flags << kind_flag
  flags.concat inject
  flags << "-t #{@target.identifier}" if !global && @target && !@target.is_a?(Server)

  flags.compact.join(" ")
end

#each {|option, value| ... } ⇒ OptionsList

Calls block once for each option.

Yields:

  • (option, value)

Yield Parameters:

  • option (String)

    Name of the option

  • value (Object)

    Value of the option

Returns:



30
31
32
33
34
35
# File 'lib/tmux/options_list.rb', line 30

def each
  get_matching(//).each do |key, value|
    yield [key, value]
  end
  self
end

#get(option) ⇒ Object

Returns the value of an option. If the OptionsList does not operate on global options, but the requested option could not be found locally, it will be searched for globally, obeying option inheritance of Tmux.

Parameters:

  • option (String)

    Name of the option

Returns:

  • (Object)


66
67
68
69
70
71
72
73
# File 'lib/tmux/options_list.rb', line 66

def get(option)
  value = get_matching(option).values.first
  if value.nil? && !@global
    return get_matching(option, true).values.first
  else
    value
  end
end

#get_matching(regexp, global = nil) ⇒ Hash<String, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a hash of all options that match regexp, and their values.

Parameters:

  • regexp (Regexp)

    The regexp which all returned option names have to match

  • global (Boolean, nil) (defaults to: nil)

    Operate on global options? Inherits from @global if nil

Returns:

  • (Hash<String, Object>)

    Returns a hash of all options that match regexp, and their values.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tmux/options_list.rb', line 43

def get_matching(regexp, global = nil)
  option_lines = server.invoke_command("show-options #{argument_string(global)}").each_line.select { |line|
    line =~ /^#{regexp}/
  }

  values = {}
  option_lines.each do |option_line|
    option, value   = option_line.chomp.split(" ", 2)
    mapping = Options::Mapping[option]
    value   = mapping ? mapping.from_tmux(value) : value
    values[option] = value
  end

  values
end

#kind_flagString? (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String, nil)


119
120
121
122
123
124
125
# File 'lib/tmux/options_list.rb', line 119

def kind_flag
  {
    :server  => "-s",
    :session => nil,
    :window  => "-w",
  }[@kind]
end

#serverServer (private)

Returns:



145
146
147
# File 'lib/tmux/options_list.rb', line 145

def server
  @target.server
end

#set(option, value) ⇒ Object

Sets an option.

Parameters:

  • option (String)

    Name of the option

  • value (Object)

    New value of the option. Will automatically be converted to a string valid to Tmux.

Returns:

  • (Object)

    value

Raises:

  • (RuntimeError)

    Raised if the new value is invalid



82
83
84
85
86
87
88
89
90
# File 'lib/tmux/options_list.rb', line 82

def set(option, value)
  mapping = Options::Mapping[option]
  value = mapping.to_tmux(value) if mapping
  ret = server.invoke_command "set-option #{argument_string} #{option} \"#{value}\""
  if ret =~ /^value is invalid:/
    raise RuntimeError, ret
  end
  value
end

#unset(option)

This method returns an undefined value.

Unsets an option. Note: global options cannot be unset.

Parameters:

  • option (String)

    Name of the option

Raises:

  • (RuntimeError)

    Raised if you try to unset a global option.



97
98
99
100
# File 'lib/tmux/options_list.rb', line 97

def unset(option)
  raise RuntimeError, "Cannot unset global option" if @global
  server.invoke_command "set-option #{argument_string(nil, ["-u"])} #{option}"
end