Class: Weechat::Buffer
Overview
This class provides a wrapper around WeeChat buffers.
Creating new buffers
Using Buffer.create, one can create new buffers which even respond to input and closing using hooks (procs or methods or anything that responds to #call).
Buffer.create("my buffer",
lambda {|b, i|
# work with input
},
lambda {|b|
# respond to the closing of a buffer
}
)
The input line
While internally the input line is managed by two properties (input
and input_get_unknown_commands
), the WeeChat Ruby abstraction uses one instance of the Input class per buffer (see #input). The content of the input line thus can be read using Input#text and set using Input#text= (or using the shorthand #input=). To turn on/off the receiving of unknown commands, use Input#get_unknown_commands=.
Key binds
Buffer local key binds can be set/unset using #bind_keys and #unbind_keys. Note, however, that key binds can only invoke commands, not callbacks (you can, however, pass in existing Command instances).
Closed buffers
The library is NOT doing any checks if the pointer points at a valid/still existing buffer. That means, if you do not take care of this yourself (by keeping your variables up to date or calling #valid? yourself), you might risk crashes.
List of getters
- plugin
-
The plugin which created the buffer
- name
-
The name of the buffer
- short_name
-
The short name of the buffer
- title
-
The title of the buffer
- number
-
The number (position) of the buffer
- num_displayed
-
How many windows are displaying this buffer
- notify
-
The buffer’s notify level. Can be
:never
,:highlights
,:messages
and:always
- lines_hidden?
-
true if at least one line is hidden (filtered), otherwise false
- prefix_max_length
-
“max length for prefix align”
- show_times?
-
true if timestamps are shown, false otherwise (also called show_times?)
- text_search
-
The type of search. Can be
:none
,:backward
and:forward
- text_search_exact?
-
true if search is case sensitive
- text_search_found?
-
true if text was found, false otherwise
- text_search_input
-
The content of the input buffer before the search was started
- active?
-
Whether this is the current buffer
- highlight_words
-
An array of words that trigger highlighting
- highlight_tags
-
An array of tags that trigger highlighting
- type
-
The type of the buffer, can be either
:formatted
or:free
List of gettable properties using the infolist
:print_hooks_enabled=>1, :first_line_not_read=>0, :prefix_max_length=>0, :nicklist_case_sensitive=>0, :nicklist_display_groups=>1,
List of setters
- hotlist
-
(not implemented yet)
- name
-
The name of the buffer
- short_name
-
The short name of the buffer
- type
-
The type of the buffer, can be either
:formatted
or:free
- notify
-
The buffer’s notify level. Can be
:never
,:highlights
,:messages
and:everything
- title
-
The title of the buffer
- show_times
-
Whether to display times or not
- nicklist
-
(not implemented yet)
- nicklist_case_sensitive
-
(not implemented yet)
- nicklist_display_groups
-
(not implemented yet)
- highlight_words
-
The words to highlight in the buffer, expects an array
- highlight_tags
-
The tags to highlight in the buffer, expects an array
- input
-
Sets the content of the input line (See Input#text=)
Notify levels
- :never
-
Don’t notify at all
- :highlights
-
Only notify on highlights
- :messages
-
Notify on highlights and messages
- :everything
-
Notify on everything
Constant Summary collapse
- NOTIFY_LEVELS =
TODO:
localvar_set_xxx
TODO:localvar_del_xxx
[:never, :highlights, :messages, :always].freeze
Instance Attribute Summary collapse
Attributes included from Pointer
Class Method Summary collapse
-
.buffers ⇒ Array<Buffer>
(also: all)
Returns a list of all buffers.
-
.call_close_callback(id, buffer) ⇒ void
This method manages all close callbacks, resolving the callback to use by an ID which gets supplied by Helper#close_callback.
-
.call_input_callback(id, buffer, input) ⇒ void
This method manages all input callbacks, resolving the callback to use by an ID which gets supplied by Helper#input_callback.
-
.callbacks ⇒ Array<Hash{Symbol => String, #call}>
Returns all callbacks.
-
.create(name, input_callback, close_callback) ⇒ Buffer
Creates a new buffer.
-
.current ⇒ Buffer
Returns the current buffer.
-
.find_by_name(name, plugin = "ruby") ⇒ Buffer?
(also: find)
Finds a buffer by its name and its plugin.
- .from_ptr(ptr) ⇒ Buffer
-
.search(pattern, properties = {}) ⇒ Array<Buffer>
Returns all buffers with a certain name.
Instance Method Summary collapse
-
#bind_keys(*args) ⇒ String
Bind keys to a command.
-
#callbacks ⇒ Array<Hash{Symbol => String, #call}>
Returns the callbacks assigned to the buffer.
-
#channel ⇒ IRC::Channel
Returns the channel associated with the buffer.
-
#channel? ⇒ Boolean
Check if the buffer represents a channel.
-
#clear ⇒ void
Clears the buffer.
-
#close ⇒ void
Closes the buffer.
-
#close_callback ⇒ #call
The close callback assigned to the buffer.
-
#command(*parts) ⇒ String
(also: #send_command, #exec, #execute)
Send a command to the current buffer.
-
#display(auto = false) ⇒ void
(also: #show)
Displays the buffer in the current window.
-
#initialize(ptr) ⇒ Buffer
constructor
A new instance of Buffer.
-
#input_callback ⇒ #call
The input callback assigned to the buffer.
-
#key_binds ⇒ Hash{String => String}
Returns all key binds.
-
#lines(strip_colors = false) ⇒ Array<Line>
Returns an array with all lines of the buffer.
-
#move(n) ⇒ Number
(also: #move_to)
Moves the buffer.
- #plugin ⇒ Object
-
#print(text) ⇒ void
(also: #puts)
Writes to the buffer.
-
#send(*text) ⇒ String
(also: #privmsg, #say)
Send a text to the buffer.
-
#size ⇒ Number
(also: #count, #length)
Returns the number of lines in the buffer.
-
#text(strip_colors = false) ⇒ String
(also: #content)
Returns the content of the buffer.
-
#unbind_keys(*keys) ⇒ String
Unbind keys.
-
#update_marker ⇒ void
(also: #update_read_marker)
Moves the read marker to the bottom.
-
#valid? ⇒ Boolean
(also: #exist?)
Checks if the buffer is valid, that is if the pointer represents an existing buffer.
-
#windows ⇒ Array<Window>
Returns all windows that are displaying this buffer.
Methods included from Properties::ClassMethods
#apply_transformation, #init_properties, #known_integer_properties, #known_properties, #known_string_properties, #mappings, #rtransformations, #settable_properties, #transformations, #type
Methods included from Pointer
Constructor Details
Instance Attribute Details
#input ⇒ Weechat::Input #input=(val) ⇒ void
108 109 110 |
# File 'lib/weechat/buffer.rb', line 108 def input @input end |
Class Method Details
.buffers ⇒ Array<Buffer> Also known as: all
move into own module
Returns a list of all buffers
205 206 207 208 209 210 211 |
# File 'lib/weechat/buffer.rb', line 205 def buffers buffers = [] Weechat::Infolist.parse("buffer").each do |buffer| buffers << Buffer.new(buffer[:pointer]) end buffers end |
.call_close_callback(id, buffer) ⇒ void
This method returns an undefined value.
This method manages all close callbacks, resolving the callback to use by an ID which gets supplied by Helper#close_callback. This shouldn’t be called directly by the user.
279 280 281 282 |
# File 'lib/weechat/buffer.rb', line 279 def call_close_callback(id, buffer) buffer = Buffer.new(buffer) return @callbacks[id.to_i][:close_callback].call(buffer) end |
.call_input_callback(id, buffer, input) ⇒ void
This method returns an undefined value.
This method manages all input callbacks, resolving the callback to use by an ID which gets supplied by Helper#input_callback. This shouldn’t be called directly by the user.
266 267 268 269 |
# File 'lib/weechat/buffer.rb', line 266 def call_input_callback(id, buffer, input) buffer = Buffer.new(buffer) return @callbacks[id.to_i][:input_callback].call(buffer, input) end |
.callbacks ⇒ Array<Hash{Symbol => String, #call}>
Returns all callbacks
290 291 292 |
# File 'lib/weechat/buffer.rb', line 290 def callbacks @callbacks end |
.create(name, input_callback, close_callback) ⇒ Buffer
Creates a new buffer.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/weechat/buffer.rb', line 319 def create(name, input_callback, close_callback) @callbacks << { :input_callback => EvaluatedCallback.new(input_callback), :close_callback => EvaluatedCallback.new(close_callback), } id = @callbacks.size - 1 ptr = Weechat.buffer_new(name.to_s, "input_callback", id.to_s, "close_callback", id.to_s) if ptr.empty? raise Exception::DuplicateBufferName, name.to_s else @callbacks[-1][:ptr] = ptr Buffer.new(ptr) end end |
.current ⇒ Buffer
Returns the current buffer.
297 298 299 |
# File 'lib/weechat/buffer.rb', line 297 def current Buffer.new(Weechat.current_buffer) end |
.find_by_name(name, plugin = "ruby") ⇒ Buffer? Also known as: find
Finds a buffer by its name and its plugin.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/weechat/buffer.rb', line 219 def find_by_name(name, plugin = "ruby") plugin = case plugin when Plugin plugin.name else plugin.to_s end ptr = Weechat.buffer_search(plugin, name) if ptr == "" nil else Buffer.new(ptr) end end |
.from_ptr(ptr) ⇒ Buffer
190 191 192 |
# File 'lib/weechat/buffer.rb', line 190 def from_ptr(ptr) self.new(ptr) end |
.search(pattern, properties = {}) ⇒ Array<Buffer>
Returns all buffers with a certain name
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/weechat/buffer.rb', line 242 def search(pattern, properties={}) if pattern.is_a? String pattern = Regexp.new("^#{pattern}$") end Weechat::Infolist.parse("buffer").select {|h| h[:name] =~ pattern }.map {|h| Buffer.new(h[:pointer]) }.select {|b| properties.all? {|key, value| b.__send__(key) == value } } end |
Instance Method Details
#bind_keys(*args) ⇒ String
Bind keys to a command.
528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/weechat/buffer.rb', line 528 def bind_keys(*args) keys = args[0..-2] command = args[-1] keychain = keys.join("-") if command.is_a? Command command = command.command end set("key_bind_#{keychain}", command) @keybinds[keys] = command keychain end |
#callbacks ⇒ Array<Hash{Symbol => String, #call}>
Returns the callbacks assigned to the buffer.
453 454 455 |
# File 'lib/weechat/buffer.rb', line 453 def callbacks self.class.callbacks.find {|c| c.ptr == @ptr} end |
#channel ⇒ IRC::Channel
Returns the channel associated with the buffer.
375 376 377 |
# File 'lib/weechat/buffer.rb', line 375 def channel IRC::Channel.new(self) end |
#channel? ⇒ Boolean
Check if the buffer represents a channel.
367 368 369 |
# File 'lib/weechat/buffer.rb', line 367 def channel? self.localvar_type == "channel" end |
#clear ⇒ void
This method returns an undefined value.
Clears the buffer.
445 446 447 |
# File 'lib/weechat/buffer.rb', line 445 def clear Weechat.buffer_clear(@ptr) end |
#close ⇒ void
This method returns an undefined value.
Closes the buffer.
Note: After a buffer has been closed, it shouldn’t be used anymore as that might lead to segfaults.
419 420 421 422 423 |
# File 'lib/weechat/buffer.rb', line 419 def close # TODO add check if a buffer is closed, to all methods Weechat.buffer_close(@ptr) @closed = true end |
#close_callback ⇒ #call
The close callback assigned to the buffer.
471 472 473 |
# File 'lib/weechat/buffer.rb', line 471 def close_callback callbacks[:close_callback] end |
#command(*parts) ⇒ String Also known as: send_command, exec, execute
Send a command to the current buffer.
Note: If the given command does not start with a slash, one will be added.
387 388 389 390 391 392 |
# File 'lib/weechat/buffer.rb', line 387 def command(*parts) parts[0][0,0] = '/' unless parts[0][0..0] == '/' line = parts.join(" ") Weechat.exec(line, self) line end |
#display(auto = false) ⇒ void Also known as: show
This method returns an undefined value.
Displays the buffer in the current window.
350 351 352 353 |
# File 'lib/weechat/buffer.rb', line 350 def display(auto = false) auto = auto ? "auto" : 1 set_property("display", auto) end |
#input_callback ⇒ #call
The input callback assigned to the buffer.
462 463 464 |
# File 'lib/weechat/buffer.rb', line 462 def input_callback callbacks[:input_callback] end |
#key_binds ⇒ Hash{String => String}
Returns all key binds
555 556 557 |
# File 'lib/weechat/buffer.rb', line 555 def key_binds @keybinds end |
#lines(strip_colors = false) ⇒ Array<Line>
Returns an array with all lines of the buffer.
488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/weechat/buffer.rb', line 488 def lines(strip_colors = false) lines = [] Weechat::Infolist.parse("buffer_lines", @ptr).each do |line| line = Weechat::Line.from_hash(line) if strip_colors line.prefix.strip_colors! line..strip_colors! end lines << line end lines end |
#move(n) ⇒ Number Also known as: move_to
Moves the buffer.
429 430 431 |
# File 'lib/weechat/buffer.rb', line 429 def move(n) self.number = (n) end |
#plugin ⇒ Object
566 567 568 |
# File 'lib/weechat/buffer.rb', line 566 def plugin Plugin.new(Weechat.buffer_get_pointer(@ptr, "plugin")) end |
#print(text) ⇒ void Also known as: puts
This method returns an undefined value.
Writes to the buffer.
478 479 480 |
# File 'lib/weechat/buffer.rb', line 478 def print(text) Weechat.puts(text, @ptr) end |
#send(*text) ⇒ String Also known as: privmsg, say
Send a text to the buffer. If the buffer represents a channel, the text will be send as a message to the channel.
Note: this method will automatically escape a leading slash, if present.
404 405 406 407 408 409 |
# File 'lib/weechat/buffer.rb', line 404 def send(*text) text[0][0,0] = '/' if text[0][0..0] == '/' line = text.join(" ") Weechat.exec(line) line end |
#size ⇒ Number Also known as: count, length
Returns the number of lines in the buffer.
515 516 517 518 |
# File 'lib/weechat/buffer.rb', line 515 def size # TODO check if there is a property for that Weechat::Infolist.parse("buffer_lines", @ptr).size end |
#text(strip_colors = false) ⇒ String Also known as: content
Returns the content of the buffer.
507 508 509 |
# File 'lib/weechat/buffer.rb', line 507 def text(strip_colors = false) lines(strip_colors).join("\n") end |
#unbind_keys(*keys) ⇒ String
Unbind keys.
@param keys An array of keys which will be used to build a keychain
546 547 548 549 550 |
# File 'lib/weechat/buffer.rb', line 546 def unbind_keys(*keys) keychain = keys.join("-") set("key_unbind_#{keychain}", "") @keybinds.delete keys end |
#update_marker ⇒ void Also known as: update_read_marker
This method returns an undefined value.
Moves the read marker to the bottom
437 438 439 |
# File 'lib/weechat/buffer.rb', line 437 def update_marker self.unread = true end |