weechat 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ module Weechat
2
+ class Callback
3
+ def initialize(callback)
4
+ @callback = callback
5
+ end
6
+
7
+ def call(*args)
8
+ return Weechat::Utilities.evaluate_call {@callback.call(*args)}
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ module Weechat
2
+ # This class represents a Weechat color.
3
+ #
4
+ # It is created from a color name and saves that name and the color
5
+ # representation.
6
+ class Color
7
+ def self.from_weechat_config(v)
8
+ new(v)
9
+ end
10
+
11
+ # @param [String] name Name of the color
12
+ attr_reader :name
13
+ attr_reader :color
14
+ def initialize(name)
15
+ @name = name
16
+ @color = Weechat.color(name)
17
+ end
18
+
19
+ def to_s
20
+ @color
21
+ end
22
+ alias_method :to_str, :to_s
23
+
24
+ def to_weechat_config
25
+ @name
26
+ end
27
+
28
+ def ==(other)
29
+ @name == other.name
30
+ end
31
+ end
32
+
33
+ class << self
34
+ alias_method :old_color, :color
35
+ def color(name)
36
+ color = Weechat.old_color(name)
37
+ color.empty? ? nil : color
38
+ end
39
+ alias_method :get_color, :color
40
+ end
41
+ end
@@ -0,0 +1,66 @@
1
+ module Weechat
2
+ # Note: As opposed to plain WeeChat, we properly parse arguments
3
+ # given to a command, like a shell would.
4
+ class Command < Hook
5
+ attr_reader :command
6
+ attr_reader :description
7
+ attr_reader :args
8
+ attr_reader :args_description
9
+ attr_reader :completion
10
+ def initialize(*args, &callback)
11
+ raise "No callback specified" if callback.nil?
12
+ super
13
+
14
+ if args.size == 2
15
+ @command, @description = args
16
+ elsif args.size == 1 && args[0].is_a?(Hash)
17
+ @command, @description, @args, =
18
+ args[0].values_at(:command, :description, :args)
19
+
20
+ @completion = case args[0][:completion]
21
+ when Array
22
+ args[0][:completion].join(" || ")
23
+ else
24
+ args[0][:completion].to_s
25
+ end
26
+
27
+ case args[0][:args_description]
28
+ when Hash
29
+ lines = []
30
+ color = Weechat.color("white")
31
+ reset = Weechat.color("reset")
32
+ max_length = args[0][:args_description].keys.map {|k| k.size}.max
33
+ args[0][:args_description].each do |key, value|
34
+ key = (" " * (max_length - key.size)) + key
35
+ lines << "#{color}#{key}: #{reset}#{value}"
36
+ end
37
+ @args_description = lines.join("\n")
38
+ when Array
39
+ @args_description = args[0][:args_description].join("\n")
40
+ else
41
+ @args_description = args[0][:args_description].to_s
42
+ end
43
+
44
+ else
45
+ raise "Please supply two arguments or a hash"
46
+ end
47
+
48
+ @command[0..0] = '' if @command[0..0] == '/'
49
+ @callback = Callback.new(callback)
50
+ @ptr = Weechat.hook_command(@command,
51
+ @description.to_s,
52
+ @args.to_s,
53
+ @args_description.to_s,
54
+ @completion.to_s,
55
+ "command_callback",
56
+ id.to_s)
57
+ end
58
+
59
+ class << self
60
+ def find_by_command(name)
61
+ @hooks.values.find {|h| h.command == name}
62
+ end
63
+ alias_method :find_by_name, :find_by_command
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,31 @@
1
+ module Weechat
2
+ module Exception
3
+ # This exception gets raised whenever one tries to read a property
4
+ # that doesn't exist.
5
+ #
6
+ # @see Buffer#get_property
7
+ # @see Window#get_property
8
+ class UnknownProperty < RuntimeError; end
9
+
10
+ # This exception gets raised whenever one tries to set a property
11
+ # that cannot be set.
12
+ #
13
+ # @see Buffer#set_property
14
+ # @see Window#set_property
15
+ class UnsettableProperty < RuntimeError; end
16
+
17
+ # This exception gets raised whenever one tries to set a property,
18
+ # supplying a value not suiting it.
19
+ class InvalidPropertyValue < RuntimeError; end
20
+
21
+ # This exception gets raised when one tries to create a buffer
22
+ # with the name of an already existing one.
23
+ #
24
+ # @see Buffer.create
25
+ class DuplicateBufferName < RuntimeError; end
26
+
27
+ class WEECHAT_RC_OK < ::Exception; end
28
+ class WEECHAT_RC_ERROR < ::Exception; end
29
+ class WEECHAT_RC_OK_EAT < ::Exception; end
30
+ end
31
+ end
@@ -0,0 +1,93 @@
1
+ module Weechat
2
+ # Each hook as an unique ID, which is passed to the middle-man
3
+ # callback, which then calls the appropriate callback.
4
+ class Hook
5
+ include Weechat::Pointer
6
+
7
+ @hook_classes = [self]
8
+ def self.inherited(by)
9
+ by.init
10
+ @hook_classes << by
11
+ end
12
+
13
+ def self.hooks; @hooks; end
14
+
15
+ def self.init
16
+ @hooks = {}
17
+ end
18
+
19
+ init
20
+
21
+ attr_reader :id
22
+ attr_reader :callback
23
+ def initialize(*args)
24
+ @id = self.class.compute_free_id
25
+ @ptr = nil
26
+ @callback = nil
27
+ @hooked = true
28
+ self.class.register(self)
29
+ end
30
+
31
+ def callback=(callback)
32
+ @callback = Callback.new(callback)
33
+ end
34
+
35
+ class << self
36
+ alias_method :hook, :new
37
+ end
38
+
39
+ # def to_s
40
+ # @ptr
41
+ # end
42
+
43
+ def hooked?
44
+ @hooked
45
+ end
46
+
47
+ # low level unhooking, no checks whatsoever. Basically used for
48
+ # unhooking foreign hooks.
49
+ def self.unhook(ptr)
50
+ Weechat.unhook(ptr)
51
+ end
52
+
53
+ # Note: this also unhooks all hooks that were made using the API
54
+ def self.unhook_all
55
+ @hook_classes.each do |hook_class|
56
+ hook_class.hooks.values.each {|hook| hook.unhook}
57
+ end
58
+ Weechat.unhook_all
59
+ end
60
+
61
+ def unhook(_raise = true)
62
+ if _raise and !hooked?
63
+ raise "not hooked"
64
+ end
65
+
66
+ self.class.unhook(@ptr)
67
+ self.class.unregister(self)
68
+ @callback = nil
69
+ @hooked = false
70
+ true
71
+ end
72
+
73
+ def call(*args)
74
+ return @callback.call(*args)
75
+ end
76
+
77
+ def self.find_by_id(id)
78
+ @hooks[id.to_i]
79
+ end
80
+
81
+ def self.compute_free_id
82
+ (@hooks.keys.max || -1) + 1
83
+ end
84
+
85
+ def self.register(hook)
86
+ @hooks[hook.id] = hook
87
+ end
88
+
89
+ def self.unregister(hook)
90
+ @hooks.delete hook.id
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,8 @@
1
+ module Weechat
2
+ module Hooks
3
+ end
4
+ end
5
+
6
+ require 'weechat/hooks/command_run.rb'
7
+ require 'weechat/hooks/print.rb'
8
+ require 'weechat/hooks/config.rb'
@@ -0,0 +1,30 @@
1
+ module Weechat
2
+ module Hooks
3
+ class CommandRun < Hook
4
+ # Returns a new instance of CommandRunHook
5
+ #
6
+ # @param [Boolean] also_arguments If true, two hooks will be
7
+ # made, one for "$command" and one for "$command *", matching
8
+ # both calls with and without arguments
9
+ def initialize(command, also_arguments = false, &callback)
10
+ super
11
+ @command = if command.is_a? Command
12
+ command.command
13
+ else
14
+ command.to_s
15
+ end
16
+
17
+ @callback = Callback.new(callback)
18
+ @ptr = Weechat.hook_command_run(@command, "command_run_callback", id.to_s)
19
+ if also_arguments
20
+ @ptr2 = Weechat.hook_command_run("#@command *", "command_run_callback", id.to_s)
21
+ end
22
+ end
23
+
24
+ def unhook(*args)
25
+ super
26
+ self.class.unhook(@ptr2) if @ptr2
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Weechat
2
+ module Hooks
3
+ class Config < Hook
4
+ def initialize(option, &callback)
5
+ super
6
+ @callback = Callback.new(callback)
7
+ @ptr = Weechat.hook_config(option, "config_callback", id.to_s)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Weechat
2
+ module Hooks
3
+ class Print < Hook
4
+ def initialize(buffer='*', tags = [], message = '', strip_colors = false, &callback)
5
+ super
6
+ buffer = buffer.ptr if buffer.respond_to?(:ptr)
7
+ tags = tags.join(",")
8
+ strip_colors = Weechat.bool_to_integer(strip_colors)
9
+ @callback = Callback.new(callback)
10
+ @ptr = Weechat.hook_print(buffer, tags, message, strip_colors, "print_callback", id.to_s)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Weechat
2
+ # Note: As opposed to plain WeeChat, we properly parse arguments
3
+ # given to a command, like a shell would.
4
+ class Info < Hook
5
+ attr_reader :name
6
+ attr_reader :description
7
+ def initialize(name, description, &callback)
8
+ super
9
+ @name, @description = name, description
10
+ @callback = callback
11
+ @ptr = Weechat.hook_info(name, description, "info_callback", id.to_s)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module Weechat
2
+ class Infolist
3
+ def self.parse(type, ptr="", arguments="")
4
+ infolist_ptr = Weechat.infolist_get(type, ptr, arguments)
5
+ ret = []
6
+ while Weechat.infolist_next(infolist_ptr) > 0
7
+ h = { }
8
+ str = Weechat.infolist_fields(infolist_ptr)
9
+ str.split(/,/).each do |item|
10
+ type, name = item.split(/:/)
11
+ h[name.to_sym] = case type
12
+ when 'p'
13
+ Weechat.infolist_pointer(infolist_ptr, name)
14
+ when 'i'
15
+ Weechat.infolist_integer(infolist_ptr, name)
16
+ when 's'
17
+ Weechat.infolist_string(infolist_ptr, name)
18
+ when 'b'
19
+ # FIXME: not exposed to script API yet.
20
+ # Weechat.infolist_buffer(infolist_ptr, name)
21
+ when 't'
22
+ Weechat.infolist_time(infolist_ptr, name)
23
+ end
24
+ end
25
+
26
+ ret.push h
27
+ end
28
+ Weechat.infolist_free(infolist_ptr)
29
+ return ret
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ # :input=>1,
2
+ # :input_buffer_alloc=>256,
3
+ # :input_buffer_length=>0,
4
+ # :input_buffer_1st_display=>0,
5
+
6
+ module Weechat
7
+ class Input
8
+ def initialize(buffer)
9
+ @buffer = buffer
10
+ end
11
+
12
+ # Returns the current content of the input line.
13
+ def to_s
14
+ @buffer.get_property("input")
15
+ end
16
+ alias_method :text, :to_s
17
+ alias_method :content, :to_s
18
+
19
+ # Sets the content of the input line.
20
+ def text=(val)
21
+ @buffer.set_property("input", val)
22
+ end
23
+ alias_method :content=, "text="
24
+
25
+ def size
26
+ @buffer.input_buffer_size
27
+ end
28
+
29
+ def pos
30
+ @buffer.input_buffer_pos
31
+ end
32
+
33
+ def get_unknown_commands=(val)
34
+ @buffer.set_property("input_get_unknown_commands", val)
35
+ end
36
+
37
+ # Returns true if unknown commands are being sent as plain text to the buffer.
38
+ #
39
+ # @return [Boolean]
40
+ def get_unknown_commands?
41
+ @buffer.get_property("input_get_unknown_commands")
42
+ end
43
+ alias_method :get_unknown_commands, :get_unknown_commands?
44
+ end
45
+ end
@@ -0,0 +1,60 @@
1
+ module Weechat
2
+ # This class encapsulates lines like they're printed in WeeChat.
3
+ #
4
+ # A line usually consists of a prefix (doesn't have to) and a text.
5
+ # One can access both parts individually, or call methods on both
6
+ # combined, which means that the method will be first called on the
7
+ # prefix and then on the text part.
8
+ class Line
9
+ class << self
10
+ def parse(line)
11
+ parts = line.split("\t")
12
+ case parts.size
13
+ when 0
14
+ parts = ["", ""]
15
+ when 1
16
+ parts.unshift ""
17
+ when 2
18
+ else
19
+ parts = [parts[0], parts[1..-1].join("\t")]
20
+ end
21
+
22
+ new(*parts)
23
+ end
24
+
25
+ def from_hash(h)
26
+ prefix = h.delete(:prefix)
27
+ message = h.delete(:message)
28
+ new(prefix, message, h)
29
+ end
30
+ end
31
+
32
+ %w(y date date_printed str_time tags_count tags displayed highlight last_read_line).each do |prop|
33
+ define_method(prop) { details[prop] }
34
+ define_method("#{prop}=") {|v| details[prop] = v }
35
+ end
36
+
37
+ attr_accessor :prefix
38
+ attr_accessor :message
39
+ attr_reader :details
40
+ def initialize(prefix, message, details = {})
41
+ @prefix, @message, @details = prefix, message, details
42
+ end
43
+
44
+ def to_s
45
+ join("\t")
46
+ end
47
+
48
+ def join(delimiter)
49
+ [prefix.empty? ? nil : prefix, message].compact.join(delimiter)
50
+ end
51
+
52
+ def method_missing(m, *args)
53
+ rets = []
54
+ [prefix, message].each do |var|
55
+ rets << var.__send__(m, *args) rescue var
56
+ end
57
+ [rets[0].empty? ? nil : rets[0], rets[1]].compact.join("\t")
58
+ end
59
+ end
60
+ end