Class: Tmux::OptionsList
- Inherits:
-
Object
- Object
- Tmux::OptionsList
- 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
- #argument_string(global = nil, inject = []) ⇒ String private private
-
#each {|option, value| ... } ⇒ OptionsList
Calls block once for each option.
-
#get(option) ⇒ Object
Returns the value of an option.
-
#get_matching(regexp, global = nil) ⇒ Hash<String, Object>
private
Returns a hash of all options that match
regexp
, and their values. -
#initialize(kind, target, global = false) ⇒ OptionsList
constructor
A new instance of OptionsList.
- #kind_flag ⇒ String? private private
- #method_missing(m, *args)
- #server ⇒ Server private
-
#set(option, value) ⇒ Object
Sets an option.
-
#unset(option)
Unsets an option.
Constructor Details
#initialize(kind, target, global = false) ⇒ OptionsList
Returns a new instance of OptionsList.
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)
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.
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.
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.
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.
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_flag ⇒ 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.
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 |
#server ⇒ Server (private)
145 146 147 |
# File 'lib/tmux/options_list.rb', line 145 def server @target.server end |
#set(option, value) ⇒ Object
Sets an option.
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.
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 |