weechat 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,5 +1,63 @@
1
+ 2010-01-03 Dominik Honnef <dominikho@gmx.net>
2
+
3
+ * lib/weechat/bar.rb: added support for bars and bar items
4
+ * lib/weechat.rb: added support for bars and bar items
5
+
6
+ * lib/weechat.rb: bumped version to 0.0.5
7
+
8
+ 2010-01-02 Dominik Honnef <dominikho@gmx.net>
9
+
10
+ * lib/weechat/server.rb (Weechat::IRC::Server.find): unitized find/from_name
11
+ (Weechat::IRC::Server#get_infolist): removed
12
+
13
+ * lib/weechat/plugin.rb (Weechat::Plugin.find): unitized find/from_name
14
+
15
+ * lib/weechat/properties.rb (Weechat::Properties::ClassMethods#all):
16
+ centralised the procedure of getting all instances of a buffer/plugin/etc
17
+
18
+ 2009-12-31 Dominik Honnef <dominikho@gmx.net>
19
+
20
+ * lib/weechat/properties.rb (Weechat::Properties::ClassMethods#apply_rtransformation):
21
+ added method for applying reverse transformations
22
+
23
+ * lib/weechat/input.rb (Weechat::Input#length): added alias #length to #size
24
+
25
+ * Rakefile: added missing gem dependency on json
26
+
27
+ * lib/weechat/command.rb (Weechat::Command#initialize): dup
28
+ arguments passed to Command.new
29
+
30
+ * lib/weechat/callbacks.rb (Weechat::Callbacks): made callback
31
+ handling more centralized; changed all uses
32
+
33
+ * lib/weechat.rb: do not serialize arbitrary objects anymore
34
+ * lib/weechat/rubyext/object.rb: deleted file
35
+
36
+ * lib/weechat/rubyext/hash.rb (Hash#to_weechat_config)
37
+ (Hash::from_weechat_config.): use JSON for storing hashes
38
+ * lib/weechat.rb: use JSON for storing hashes
39
+
40
+ * lib/weechat/color.rb (Weechat::Color#color): made name to color
41
+ lookup dynamic
42
+
43
+ * lib/weechat/rubyext/string.rb (String#irc_downcase)
44
+ (String#irc_downcase): added RFC 1459 up/downcasing to strings
45
+
46
+ * lib/weechat/color.rb (Weechat::Color.const_missing): added
47
+ dynamic color lookup
48
+
1
49
  2009-12-24 Dominik Honnef <dominikho@gmx.net>
2
50
 
51
+ * *: substituted #initialize with #from_ptr and #from_name where
52
+ appropriate
53
+
54
+ * lib/weechat.rb (Weechat::Helper#process_callback): small performance tweak
55
+
56
+ * lib/weechat.rb (Weechat::Helper#process_callback): added alias collect? for collect
57
+ * lib/weechat/process.rb (Weechat::Process): added alias collect? for collect
58
+
59
+ * lib/weechat/hooks/signal.rb (Weechat::Hooks::Signal.send): implemented weechat_hook_signal_send
60
+
3
61
  * lib/weechat.rb (Weechat): bumped version to 0.0.4
4
62
 
5
63
  * lib/weechat/properties.rb (Weechat::Properties::InstanceMethods#get_infolist)
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
 
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = "weechat"
@@ -20,7 +20,7 @@ spec = Gem::Specification.new do |s|
20
20
  # s.required_ruby_version = '>= 1.9.1'
21
21
 
22
22
  # s.add_dependency "keyword_arguments"
23
-
23
+ s.add_dependency "json"
24
24
  # s.add_development_dependency "baretest"
25
25
  # s.add_development_dependency "mocha"
26
26
 
data/TODO CHANGED
@@ -91,7 +91,7 @@ hooks:
91
91
  [ ] hook_connect
92
92
  [x] hook_print
93
93
  [x] hook_signal
94
- [ ] hook_signal_send
94
+ [x] hook_signal_send
95
95
  [x] hook_config
96
96
  [ ] hook_completion
97
97
  [ ] hook_completion_list_add
@@ -130,15 +130,15 @@ nicklist:
130
130
  [ ] nicklist_remove_all
131
131
 
132
132
  bars:
133
- [ ] bar_item_search
134
- [ ] bar_item_new
135
- [ ] bar_item_update
136
- [ ] bar_item_remove
137
- [ ] bar_search
138
- [ ] bar_new
139
- [ ] bar_set
140
- [ ] bar_update
141
- [ ] bar_remove
133
+ [x] bar_item_search
134
+ [x] bar_item_new
135
+ [x] bar_item_update
136
+ [x] bar_item_remove
137
+ [x] bar_search
138
+ [x] bar_new
139
+ [x] bar_set
140
+ [x] bar_update
141
+ [x] bar_remove
142
142
 
143
143
  commands:
144
144
  [x] command
@@ -186,3 +186,6 @@ upgrade:
186
186
 
187
187
  Functionality:
188
188
  [x] if a buffer is a channel, get nicks, usercount, topic, etc
189
+
190
+ Known limitations:
191
+ - on /upgrade, the library forgets all callbacks
@@ -6,14 +6,14 @@ end
6
6
  require 'pp'
7
7
 
8
8
  module Weechat
9
- VERSION = "0.0.4"
9
+ VERSION = "0.0.5"
10
10
  module Helper
11
11
  def command_callback(id, buffer, args)
12
- Weechat::Command.find_by_id(id).call(Weechat::Buffer.new(buffer), args)
12
+ Weechat::Command.find_by_id(id).call(Weechat::Buffer.from_ptr(buffer), args)
13
13
  end
14
14
 
15
15
  def command_run_callback(id, buffer, command)
16
- Weechat::Hooks::CommandRunHook.find_by_id(id).call(Weechat::Buffer.new(buffer), command)
16
+ Weechat::Hooks::CommandRunHook.find_by_id(id).call(Weechat::Buffer.from_ptr(buffer), command)
17
17
  end
18
18
 
19
19
  def timer_callback(id, remaining)
@@ -28,12 +28,16 @@ module Weechat
28
28
  Weechat::Buffer.call_close_callback(method, buffer)
29
29
  end
30
30
 
31
+ def bar_build_callback(id, item, window)
32
+ Weechat::Bar::Item.call_build_callback(id, window)
33
+ end
34
+
31
35
  def info_callback(id, info, arguments)
32
36
  Weechat::Info.find_by_id(id).call(arguments).to_s
33
37
  end
34
38
 
35
39
  def print_callback(id, buffer, date, tags, displayed, highlight, prefix, message)
36
- buffer = Weechat::Buffer.new(buffer)
40
+ buffer = Weechat::Buffer.from_ptr(buffer)
37
41
  date = Time.at(date.to_i)
38
42
  tags = tags.split(",")
39
43
  displayed = Weechat.integer_to_bool(displayed)
@@ -50,10 +54,10 @@ module Weechat
50
54
  [/irc_(channel|pv)_opened/, /^logger_(start|stop|backlog)$/,
51
55
  /^buffer_(closing|closed|lines_hidden|moved|opened|renamed|switch)$/,
52
56
  /^buffer_(title|type)_changed$/,
53
- /^buffer_localvar_(added|changed|removed)$/] => lambda { |v| Weechat::Buffer.new(v) },
54
- [/irc_server_(connecting|connected|disconnected)/] => lambda { |v| Weechat::Server.new(v) },
57
+ /^buffer_localvar_(added|changed|removed)$/] => lambda { |v| Weechat::Buffer.from_ptr(v) },
58
+ [/irc_server_(connecting|connected|disconnected)/] => lambda { |v| Weechat::Server.from_name(v) },
55
59
  [/weechat_(highlight|pv)/] => lambda { |v| Weechat::Line.parse(v) },
56
- [/window_(scrolled|unzooming|unzoomed|zooming|zoomed)/] => lambda { |v| Weechat::Window.new(v) },
60
+ [/window_(scrolled|unzooming|unzoomed|zooming|zoomed)/] => lambda { |v| Weechat::Window.from_ptr(v) },
57
61
  }
58
62
 
59
63
  def signal_callback(id, signal, data)
@@ -76,21 +80,22 @@ module Weechat
76
80
  end
77
81
 
78
82
  process = Weechat::Process.find_by_id(id)
79
- if process.collect
83
+ if process.collect?
80
84
  process.buffer(stdout, stderr)
81
85
  if code == :error || code != :running
82
86
  process.call(code, process.stdout, process.stderr)
83
87
  end
84
88
  else
85
- Weechat::Process.find_by_id(id).call(code, stdout, stderr)
89
+ process.call(code, stdout, stderr)
86
90
  end
87
91
  end
88
92
 
89
93
  ModifierCallbackTransformations = {
94
+ [/^irc_(in|out)_.+$/] => lambda { |v| Weechat::IRC::Server.from_name(v) },
90
95
  ['irc_color_decode', 'irc_color_encode'] => lambda { |v| Weechat.integer_to_bool(v) },
91
- [/^bar_condition_.+$/] => lambda { |v| Weechat::Window.new(v) },
96
+ [/^bar_condition_.+$/] => lambda { |v| Weechat::Window.from_ptr(v) },
92
97
  ["input_text_content", "input_text_display",
93
- "input_text_display_with_cursor"] => lambda { |v| Weechat::Buffer.new(v) },
98
+ "input_text_display_with_cursor"] => lambda { |v| Weechat::Buffer.from_ptr(v) },
94
99
  ["weechat_print"] => lambda { |v|
95
100
  parts = v.split(";")
96
101
  parts[0] = Weechat::Plugin.find(parts[0])
@@ -111,6 +116,7 @@ module Weechat
111
116
  def modifier_callback(id, modifier, modifier_data, s)
112
117
  klass = Weechat::Modifiers::Mappings[modifier] || Weechat::Modifier
113
118
  modifier_data = Weechat::Utilities.apply_transformation(modifier, modifier_data, ModifierCallbackTransformations)
119
+ modifier_data = [modifier_data] unless modifier_data.is_a?(Array)
114
120
  args = modifier_data + [Weechat::Line.parse(s)]
115
121
  callback = klass.find_by_id(id)
116
122
  callback ||= Weechat::Modifier.find_by_id(id)
@@ -172,6 +178,11 @@ module Weechat
172
178
  bool ? 1 : 0
173
179
  end
174
180
 
181
+ alias_method :old_bar_update, :bar_update
182
+ def bar_update(name)
183
+ old_bar_update(name.to_s)
184
+ end
185
+
175
186
  alias_method :old_mkdir_home, :mkdir_home
176
187
  alias_method :old_mkdir, :mkdir
177
188
  alias_method :old_mkdir_parents, :mkdir_parents
@@ -232,6 +243,7 @@ require 'weechat/blankslate.rb'
232
243
  require 'weechat/line.rb'
233
244
  require 'weechat/terminal.rb'
234
245
  require 'weechat/callback.rb'
246
+ require 'weechat/callbacks.rb'
235
247
  require 'weechat/property.rb'
236
248
  require 'weechat/properties.rb'
237
249
  require 'weechat/exceptions.rb'
@@ -244,6 +256,7 @@ require 'weechat/modifier.rb'
244
256
  require 'weechat/input.rb'
245
257
  require 'weechat/buffer.rb'
246
258
  require 'weechat/window.rb'
259
+ require 'weechat/bar.rb'
247
260
  require 'weechat/server.rb'
248
261
  require 'weechat/channel.rb'
249
262
  require 'weechat/user.rb'
@@ -253,8 +266,8 @@ require 'weechat/color.rb'
253
266
  require 'weechat/plugin.rb'
254
267
  require 'weechat/rubyext/string.rb'
255
268
  require 'weechat/rubyext/boolean.rb'
256
- require 'weechat/rubyext/object.rb'
257
269
  require 'weechat/rubyext/array.rb'
270
+ require 'weechat/rubyext/hash.rb'
258
271
  require 'weechat/rubyext/integer.rb'
259
272
  require 'weechat/rubyext/float.rb'
260
273
  require 'weechat/hooks.rb'
@@ -0,0 +1,167 @@
1
+ module Weechat
2
+ class Bar
3
+ class Item
4
+ include Weechat::Pointer
5
+ extend Weechat::Properties
6
+ extend Weechat::Callbacks
7
+
8
+ @transformations = {
9
+ [:plugin] => lambda { |v| Weechat::Plugin.from_ptr(v) },
10
+ }
11
+
12
+ @mappings = {}
13
+ @rtransformations = {}
14
+ @settable_properties = %w()
15
+
16
+ init_properties
17
+ @type = "bar_item"
18
+
19
+ class << self
20
+ def items
21
+ items = []
22
+ Weechat::Infolist.parse("bar_item").each do |item|
23
+ items << Item.find(item[:name])
24
+ end
25
+ items
26
+ end
27
+ alias_method :all, :items
28
+
29
+ def find(name)
30
+ ptr = Weechat.bar_item_search(name)
31
+ if !ptr.empty?
32
+ from_ptr(ptr)
33
+ else
34
+ nil
35
+ end
36
+ end
37
+ alias_method :from_name, :find
38
+ end # eigenclass
39
+
40
+ def initialize(name, &build_callback)
41
+ id = self.class.compute_free_id
42
+ @ptr = Weechat.bar_item_new(name, "bar_build_callback", id.to_s)
43
+ if @ptr.empty?
44
+ raise "Could not create bar item"
45
+ end
46
+
47
+ self.class.register_callback(
48
+ :build_callback => Callback.new(build_callback),
49
+ :ptr => @ptr
50
+ )
51
+ end
52
+
53
+ def update
54
+ Weechat.bar_item_update(@name)
55
+ end
56
+
57
+ def delete
58
+ # TODO mark deletion status
59
+ Weechat.bar_item_remove(@ptr)
60
+ end
61
+ alias_method :remove, :delete
62
+
63
+ class << self
64
+ def call_build_callback(id, window)
65
+ window = Window.from_ptr(window)
66
+ call_callback(id, :build_callback, window).to_s
67
+ end
68
+ end # eigenclass
69
+ end # Item
70
+
71
+ include Weechat::Pointer
72
+ extend Weechat::Properties
73
+
74
+ @transformations = {
75
+ [:hidden, :separator] => lambda { |v| Weechat.integer_to_bool(v) },
76
+ [:type] => lambda { |v| [:root, :window][v] },
77
+ [:position] => lambda { |v| [:top, :bottom, :left, :right][v] },
78
+ [:filling_top_bottom, :filling_left_right] => lambda { |v|
79
+ [:horizontal, :vertical, :columns_horizontal, :columns_vertical][v]
80
+ },
81
+ [:color_fg, :color_delim, :color_bg] => lambda { |v| Weechat::Color.new(v) },
82
+ [:bar_window] => lambda { |v| Weechat::Window.from_ptr(v) },
83
+ [:items] => lambda { |v|
84
+ items = v.split(",")
85
+ items.map { |item|
86
+ item = item.split("+")
87
+ item.size == 1 ? item.first : item
88
+ }
89
+ },
90
+ }
91
+
92
+ @mappings = {
93
+ :hidden? => :hidden,
94
+ :separator? => :separator,
95
+ :has_separator? => :separator,
96
+ }
97
+
98
+ @rtransformations = {
99
+ [:hidden, :separator] => lambda { |v|
100
+ v.to_weechat_config
101
+ },
102
+ [:color_fg, :color_delim, :color_bg] => lambda { |v|
103
+ if v.respond_to?(:name)
104
+ v.name
105
+ else
106
+ v
107
+ end
108
+ },
109
+ [:name, :type, :condition, :position, :filling_top_bottom,
110
+ :filling_left_right, :size, :size_max, :priority] => lambda { |v| v.to_s},
111
+ [:items] => lambda { |v|
112
+ v.map {|item|
113
+ if item.is_a?(Array)
114
+ item.join("+")
115
+ else
116
+ item.to_s
117
+ end
118
+ }.join(",")
119
+ },
120
+ }
121
+
122
+ @settable_properties = %w(name hidden priority conditions position filling_top_bottom
123
+ filling_left_right size size_max color_fg color_delim color_bg separator items)
124
+
125
+ init_properties
126
+
127
+ class << self
128
+ def find(name)
129
+ ptr = Weechat.bar_search(name)
130
+ if !ptr.empty?
131
+ from_ptr(ptr)
132
+ else
133
+ nil
134
+ end
135
+ end
136
+ alias_method :from_name, :find
137
+ end # eigenclass
138
+
139
+ def initialize(args = {})
140
+ mapped_args = {}
141
+ args.each do |key, value|
142
+ mapped_args[key] = self.class.apply_rtransformation(key, value)
143
+ end
144
+
145
+ @ptr = Weechat.bar_new(*mapped_args.values_at(:name, :hidden, :priority, :type,
146
+ :condition, :position, :filling_top_bottom,
147
+ :filling_left_right, :size, :size_max,
148
+ :color_fg, :color_delim, :color_bg, :separator,
149
+ :items))
150
+ if @ptr.empty?
151
+ raise "Could not create bar"
152
+ end
153
+
154
+ @name = args[:name]
155
+ end
156
+
157
+ def update
158
+ Weechat.bar_update(self.name)
159
+ end
160
+
161
+ def delete
162
+ # TODO mark deletion state
163
+ Weechat.bar_remove(@ptr)
164
+ end
165
+ alias_method :remove, :delete
166
+ end # Bar
167
+ end # Weechat
@@ -3,10 +3,10 @@ module Weechat
3
3
  #
4
4
  # == Creating new buffers
5
5
  #
6
- # Using {Buffer.create}, one can create new buffers which even
6
+ # Using {Buffer.new}, one can create new buffers which even
7
7
  # respond to input and closing using hooks (procs or methods or
8
8
  # anything that responds to #call).
9
- # Buffer.create("my buffer",
9
+ # Buffer.new("my buffer",
10
10
  # lambda {|b, i|
11
11
  # # work with input
12
12
  # },
@@ -97,17 +97,7 @@ module Weechat
97
97
  class Buffer
98
98
  include Weechat::Pointer
99
99
  extend Weechat::Properties
100
-
101
- # @overload input
102
- # @return [Weechat::Input]
103
- # @overload input=(val)
104
- # Sets the content of the input line.
105
- #
106
- # @return [void]
107
- # @see Input#text=
108
- attr_accessor :input
109
-
110
- @callbacks = []
100
+ extend Weechat::Callbacks
111
101
 
112
102
  # A list of all properties that can be retrieved using {#get_string_property}.
113
103
  #
@@ -143,7 +133,7 @@ module Weechat
143
133
  [:notify] => lambda {|v| NOTIFY_LEVELS[v] },
144
134
  [:text_search] => lambda {|v| [:none, :backward, :foward][v] },
145
135
  [:type] => lambda {|v| [:formatted, :free][v]},
146
- [:plugin] => lambda {|v| Weechat::Plugin.new(v)},
136
+ [:plugin] => lambda {|v| Weechat::Plugin.from_ptr(v)},
147
137
  }.freeze
148
138
 
149
139
  # The transformation procedures that get applied to values before they
@@ -180,37 +170,19 @@ module Weechat
180
170
  :position => :number,
181
171
  :position= => :number=,
182
172
  :active? => :current_buffer,
173
+ :current? => :current_buffer,
183
174
  }.freeze
184
175
 
185
176
  init_properties
186
177
 
187
178
  class << self
188
- # @return [Buffer]
189
- # @see #initialize
190
- def from_ptr(ptr)
191
- self.new(ptr)
192
- end
193
-
194
179
  def find(plugin, name)
195
180
  plugin = Weechat::Plugin.from_name(plugin) if plugin.is_a?(String)
196
- buffers.find {|buffer|
181
+ all.find {|buffer|
197
182
  buffer.name == name && buffer.plugin == plugin
198
183
  }
199
184
  end
200
185
 
201
- # Returns a list of all buffers
202
- #
203
- # @return [Array<Buffer>]
204
- # @todo move into own module
205
- def buffers
206
- buffers = []
207
- Weechat::Infolist.parse("buffer").each do |buffer|
208
- buffers << Buffer.new(buffer[:pointer])
209
- end
210
- buffers
211
- end
212
- alias_method :all, :buffers
213
-
214
186
  # Finds a buffer by its name and its plugin.
215
187
  #
216
188
  # @param [String] name The name of the buffer to find
@@ -227,7 +199,7 @@ module Weechat
227
199
  if ptr == ""
228
200
  nil
229
201
  else
230
- Buffer.new(ptr)
202
+ Buffer.from_ptr(ptr)
231
203
  end
232
204
  end
233
205
  alias_method :find, :find_by_name
@@ -244,14 +216,10 @@ module Weechat
244
216
  pattern = Regexp.new("^#{pattern}$")
245
217
  end
246
218
 
247
- Weechat::Infolist.parse("buffer").select {|h|
219
+ Weechat::Infolist.parse("buffer", "", "", properties, :name, :pointer).select {|h|
248
220
  h[:name] =~ pattern
249
221
  }.map {|h|
250
- Buffer.new(h[:pointer])
251
- }.select {|b|
252
- properties.all? {|key, value|
253
- b.__send__(key) == value
254
- }
222
+ Buffer.from_ptr(h[:pointer])
255
223
  }
256
224
  end
257
225
 
@@ -264,8 +232,8 @@ module Weechat
264
232
  # @see .call_close_callback
265
233
  # @private
266
234
  def call_input_callback(id, buffer, input)
267
- buffer = Buffer.new(buffer)
268
- return @callbacks[id.to_i][:input_callback].call(buffer, input)
235
+ buffer = Buffer.from_ptr(buffer)
236
+ call_callback(id, :input_callback, buffer, input)
269
237
  end
270
238
 
271
239
  # This method manages all close callbacks, resolving the
@@ -277,63 +245,67 @@ module Weechat
277
245
  # @see .call_input_callback
278
246
  # @private
279
247
  def call_close_callback(id, buffer)
280
- buffer = Buffer.new(buffer)
281
- return @callbacks[id.to_i][:close_callback].call(buffer)
282
- end
283
-
284
- # Returns all callbacks
285
- #
286
- # @return [Array<Hash{Symbol => String, #call}>] An array of hashes containing
287
- # the callbacks and pointers of the buffers to which the callbacks are assigned to
288
- # @see #input_callback
289
- # @see #close_callback
290
- def callbacks
291
- @callbacks
248
+ buffer = Buffer.from_ptr(buffer)
249
+ call_callback(id, :close_callback, buffer)
292
250
  end
293
251
 
294
252
  # Returns the current buffer.
295
253
  #
296
254
  # @return [Buffer] The current buffer
297
255
  def current
298
- Buffer.new(Weechat.current_buffer)
256
+ Buffer.from_ptr(Weechat.current_buffer)
299
257
  end
300
258
 
301
- # Creates a new buffer.
302
- #
303
- # @param [#to_s] name The name of the new buffer
304
- # @param [#call] input_callback The callback to be called when something
305
- # is entered in the new buffer's input line
306
- # @param [#call] close_callback The callback to be called when the new buffer
307
- # is being closed
308
- # @example
309
- # Buffer.create("my buffer",
310
- # lambda {|b, i|
311
- # # work with input
312
- # },
313
- # lambda {|b|
314
- # # respond to the closing of a buffer
315
- # }
316
- # )
317
- # @return [Buffer] The new buffer
318
- # @raise [Exception::DuplicateBufferName] In case a buffer with that name already exists
319
- def create(name, input_callback, close_callback)
320
- @callbacks << {
321
- :input_callback => EvaluatedCallback.new(input_callback),
322
- :close_callback => EvaluatedCallback.new(close_callback),
323
- }
324
- id = @callbacks.size - 1
325
- ptr = Weechat.buffer_new(name.to_s, "input_callback", id.to_s, "close_callback", id.to_s)
326
- if ptr.empty?
327
- raise Exception::DuplicateBufferName, name.to_s
328
- else
329
- @callbacks[-1][:ptr] = ptr
330
- Buffer.new(ptr)
331
- end
259
+ def from_ptr(ptr)
260
+ o = super
261
+ o.instance_variable_set(:@input, Weechat::Input.new(o))
262
+ o.instance_variable_set(:@keybinds, {})
263
+ o
332
264
  end
333
265
  end
334
266
 
335
- def initialize(ptr)
336
- super
267
+
268
+ # @overload input
269
+ # @return [Weechat::Input]
270
+ # @overload input=(val)
271
+ # Sets the content of the input line.
272
+ #
273
+ # @return [void]
274
+ # @see Input#text=
275
+ attr_accessor :input
276
+
277
+ # Creates a new buffer.
278
+ #
279
+ # @param [#to_s] name The name of the new buffer
280
+ # @param [#call] input_callback The callback to be called when something
281
+ # is entered in the new buffer's input line
282
+ # @param [#call] close_callback The callback to be called when the new buffer
283
+ # is being closed
284
+ # @example
285
+ # Buffer.create("my buffer",
286
+ # lambda {|b, i|
287
+ # # work with input
288
+ # },
289
+ # lambda {|b|
290
+ # # respond to the closing of a buffer
291
+ # }
292
+ # )
293
+ # @return [Buffer] The new buffer
294
+ # @raise [Exception::DuplicateBufferName] In case a buffer with that name already exists
295
+ def initialize(name, input_callback, close_callback)
296
+ id = self.class.compute_free_id
297
+
298
+ @ptr = Weechat.buffer_new(name.to_s, "input_callback", id.to_s, "close_callback", id.to_s)
299
+ if @ptr.empty?
300
+ raise Exception::DuplicateBufferName, name.to_s
301
+ end
302
+
303
+ self.class.register_callback(
304
+ :input_callback => EvaluatedCallback.new(input_callback),
305
+ :close_callback => EvaluatedCallback.new(close_callback),
306
+ :ptr => @ptr
307
+ )
308
+
337
309
  @input = Weechat::Input.new(self)
338
310
  @keybinds = {}
339
311
  end
@@ -357,7 +329,7 @@ module Weechat
357
329
  #
358
330
  # @return [Boolean]
359
331
  def valid?
360
- Buffer.buffers.map{|b|b.pointer}.include?(@ptr)
332
+ Buffer.all.map{|b|b.pointer}.include?(@ptr)
361
333
  end
362
334
  alias_method :exist?, :valid?
363
335
 
@@ -376,6 +348,23 @@ module Weechat
376
348
  IRC::Channel.new(self)
377
349
  end
378
350
 
351
+ def server
352
+ return nil unless ["core", "irc"].include? self.plugin.name
353
+ parts = self.name.split(".")
354
+ name1, name2 = parts[0], parts[1..-1].join(",")
355
+
356
+ server = begin
357
+ IRC::Server.from_name(name1)
358
+ rescue Exception::UnknownServer
359
+ begin
360
+ raise Exception::UnknownServer if name2.empty?
361
+ IRC::Server.from_name(name2)
362
+ rescue Exception::UnknownServer
363
+ nil
364
+ end
365
+ end
366
+ end
367
+
379
368
  # Send a command to the current buffer.
380
369
  #
381
370
  # Note: If the given command does not start with a slash, one will be added.
@@ -446,14 +435,6 @@ module Weechat
446
435
  Weechat.buffer_clear(@ptr)
447
436
  end
448
437
 
449
- # Returns the callbacks assigned to the buffer.
450
- #
451
- # @return (see Weechat::Buffer.callbacks)
452
- # @private
453
- def callbacks
454
- self.class.callbacks.find {|c| c.ptr == @ptr}
455
- end
456
-
457
438
  # The input callback assigned to the buffer.
458
439
  #
459
440
  # @return [#call]
@@ -562,12 +543,8 @@ module Weechat
562
543
  def windows
563
544
  Window.all.select {|window| window.buffer == self }
564
545
  end
565
-
566
- def plugin
567
- Plugin.new(Weechat.buffer_get_pointer(@ptr, "plugin"))
568
- end
569
546
  end
570
547
 
571
548
  # The core buffer
572
- Core = Buffer.new("")
549
+ Core = Buffer.from_ptr("")
573
550
  end
@@ -0,0 +1,35 @@
1
+ module Weechat
2
+ module Callbacks
3
+ @unique_id = 0
4
+ @callbacks = {}
5
+
6
+ class << self
7
+ attr_reader :callbacks
8
+ attr_reader :unique_id
9
+
10
+ def compute_free_id
11
+ @unique_id += 1
12
+ end
13
+ end
14
+
15
+ def call_callback(id, type, *args)
16
+ return callbacks[id.to_i][type].call(*args)
17
+ end
18
+
19
+ def register_callback(args = {})
20
+ callbacks[unique_id] = args
21
+ end
22
+
23
+ def compute_free_id
24
+ Callbacks.compute_free_id
25
+ end
26
+
27
+ def callbacks
28
+ Callbacks.callbacks
29
+ end
30
+
31
+ def unique_id
32
+ Callbacks.unique_id
33
+ end
34
+ end
35
+ end
@@ -13,10 +13,9 @@ module Weechat
13
13
  init_properties
14
14
 
15
15
  class << self
16
- def channels
16
+ def all
17
17
  Weechat::Buffer.all.select {|b| b.channel?}.map{|b| b.channel}
18
18
  end
19
- alias_method :all, :channels
20
19
 
21
20
  def find(server, channel)
22
21
  server = server.name if server.respond_to?(:name)
@@ -27,7 +26,7 @@ module Weechat
27
26
 
28
27
  attr_reader :buffer
29
28
  def initialize(buffer)
30
- @buffer = Buffer.new(buffer.to_s)
29
+ @buffer = Buffer.from_ptr(buffer.to_s)
31
30
  @ptr = @buffer.ptr
32
31
  if not ["channel"].include?(@buffer.localvar_type)
33
32
  raise Exception::NotAChannel, buffer.ptr
@@ -40,7 +39,7 @@ module Weechat
40
39
  end
41
40
 
42
41
  def server
43
- IRC::Server.new(@buffer.localvar_server)
42
+ IRC::Server.from_name(@buffer.localvar_server)
44
43
  end
45
44
 
46
45
  def part(reason="")
@@ -8,18 +8,22 @@ module Weechat
8
8
  new(v)
9
9
  end
10
10
 
11
+ def self.const_missing(c)
12
+ self.new(c.to_s.downcase)
13
+ end
14
+
11
15
  # @param [String] name Name of the color
12
16
  attr_reader :name
13
17
  attr_reader :color
14
18
  def initialize(name)
15
19
  @name = name
16
- @color = Weechat.color(name)
17
20
  end
18
21
 
19
- def to_s
20
- @color
22
+ def color
23
+ Weechat.color(name)
21
24
  end
22
- alias_method :to_str, :to_s
25
+ alias_method :to_s, :color
26
+ alias_method :to_str, :color
23
27
 
24
28
  def to_weechat_config
25
29
  @name
@@ -6,6 +6,7 @@ module Weechat
6
6
  attr_reader :args_description
7
7
  attr_reader :completion
8
8
  def initialize(*args, &callback)
9
+ args = args.map{|e| e.dup}
9
10
  raise "No callback specified" if callback.nil?
10
11
  super
11
12
 
@@ -56,6 +57,7 @@ module Weechat
56
57
 
57
58
  class << self
58
59
  def find_by_command(name)
60
+ name[0..0] = '' if name[0..0] == '/'
59
61
  @hooks.values.find {|h| h.command == name}
60
62
  end
61
63
  alias_method :find_by_name, :find_by_command
@@ -12,8 +12,7 @@ module Weechat
12
12
  @hook_classes << by
13
13
  end
14
14
 
15
- def hooks; @hooks; end
16
- alias_method :all, :hooks
15
+ def all; @hooks; end
17
16
 
18
17
  def init
19
18
  @hooks = {}
@@ -6,6 +6,13 @@ module Weechat
6
6
  @callback = EvaluatedCallback.new(callback)
7
7
  @ptr = Weechat.hook_signal(signal, "signal_callback", id.to_s)
8
8
  end
9
+
10
+ class << self
11
+ def send(signal, type, data)
12
+ Weechat.hook_signal_send(signal.to_s, type.to_s, data.to_s)
13
+ end
14
+ alias_method :exec, :send
15
+ end
9
16
  end
10
17
  end
11
18
  end
@@ -25,6 +25,7 @@ module Weechat
25
25
  def size
26
26
  @buffer.input_buffer_size
27
27
  end
28
+ alias_method :length, :size
28
29
 
29
30
  def pos
30
31
  @buffer.input_buffer_pos
@@ -26,22 +26,19 @@ module Weechat
26
26
  init_properties
27
27
 
28
28
  class << self
29
- def find_by_name(name)
29
+ def find(name)
30
30
  if name.nil? or name.empty? or name == "core"
31
- return Plugin.new("")
31
+ return Plugin.from_ptr("")
32
32
  end
33
- plugins.find {|plugin| plugin.name == name}
33
+ all.find {|plugin| plugin.name == name}
34
34
  end
35
- alias_method :find, :find_by_name
35
+ alias_method :from_name, :find
36
36
 
37
- def plugins
38
- plugins = [Plugin.new("")]
39
- Weechat::Infolist.parse("plugin").each do |plugin|
40
- plugins << Plugin.new(plugin[:pointer])
41
- end
42
- plugins
37
+ def all
38
+ items = super
39
+ items[0,0] = Plugin.find("")
40
+ items
43
41
  end
44
- alias_method :all, :plugins
45
42
 
46
43
  # Loads a plugin.
47
44
  #
@@ -1,5 +1,17 @@
1
1
  module Weechat
2
2
  module Pointer
3
+ module ClassMethods
4
+ def from_ptr(ptr)
5
+ o = allocate
6
+ o.instance_variable_set(:@ptr, ptr)
7
+ o
8
+ end
9
+ end
10
+
11
+ def self.included(by)
12
+ by.extend Weechat::Pointer::ClassMethods
13
+ end
14
+
3
15
  attr_reader :ptr
4
16
  alias_method :pointer, :ptr
5
17
 
@@ -7,10 +19,6 @@ module Weechat
7
19
  @ptr
8
20
  end
9
21
 
10
- def initialize(ptr)
11
- @ptr = ptr
12
- end
13
-
14
22
  def ==(other)
15
23
  other.respond_to?(:ptr) and @ptr == other.ptr
16
24
  end
@@ -15,6 +15,7 @@ module Weechat
15
15
  @callback = EvaluatedCallback.new(callback)
16
16
  @ptr = Weechat.hook_process(command, timeout, "process_callback", id.to_s)
17
17
  end
18
+ alias_method :collect?, :collect
18
19
 
19
20
  def stdout
20
21
  @stdout.join("")
@@ -36,6 +36,14 @@ module Weechat
36
36
  @type
37
37
  end
38
38
 
39
+ def all
40
+ items = []
41
+ Weechat::Infolist.parse(@type).each do |item|
42
+ items << from_ptr(item[:pointer])
43
+ end
44
+ items
45
+ end
46
+
39
47
  def init_properties
40
48
  @known_string_properties ||= [].freeze
41
49
  @known_integer_properties ||= [].freeze
@@ -77,6 +85,10 @@ module Weechat
77
85
  def apply_transformation(property, value)
78
86
  Utilities.apply_transformation(property, value, @transformations)
79
87
  end
88
+
89
+ def apply_rtransformation(property, value)
90
+ Utilities.apply_transformation(property, value, @rtransformations)
91
+ end
80
92
  end
81
93
 
82
94
  module InstanceMethods
@@ -168,7 +180,7 @@ module Weechat
168
180
  #
169
181
  # @return [Hash{Symbol => Object}] All properties in the infolist
170
182
  def get_infolist(*fields)
171
- Weechat::Infolist.parse(self.class.type, @ptr, {}, fields)
183
+ Weechat::Infolist.parse(self.class.type, @ptr, "", {}, *fields)
172
184
  end
173
185
 
174
186
  # Returns a property obtained by an infolist.
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+ class Hash
3
+ def to_weechat_config
4
+ to_json
5
+ end
6
+
7
+ def self.from_weechat_config(v)
8
+ JSON.load(v)
9
+ end
10
+ end
@@ -139,4 +139,32 @@ class String
139
139
  end
140
140
  out
141
141
  end
142
+
143
+ # Downcase a string (nickname) according to RFC 1459.
144
+ #
145
+ # @return [String]
146
+ def irc_downcase
147
+ downcase.tr("[]\\~", "{}|^")
148
+ end
149
+
150
+ # Same as #irc_downcase, but modifying the string in place.
151
+ #
152
+ # @return [String] self
153
+ def irc_downcase!
154
+ replace(irc_downcase)
155
+ end
156
+
157
+ # Upcases a string (nickname) according to RFC 1459.
158
+ #
159
+ # @return [String]
160
+ def irc_upcase
161
+ upcase.tr("{}|^", "[]\\~")
162
+ end
163
+
164
+ # Same as #irc_upcase, but modifying the string in place.
165
+ #
166
+ # @return [String] self
167
+ def irc_upcase!
168
+ replace(irc_upcase)
169
+ end
142
170
  end
@@ -67,10 +67,9 @@ module Weechat
67
67
  init_properties
68
68
 
69
69
  class << self
70
- def scripts(plugin = nil)
70
+ def all(plugin = nil)
71
71
  Plugin.all.map {|plugin| plugin.scripts}.flatten
72
72
  end
73
- alias_method :all, :scripts
74
73
  end
75
74
 
76
75
  def initialize(ptr, plugin)
@@ -12,7 +12,7 @@ module Weechat
12
12
  if b.empty?
13
13
  nil
14
14
  else
15
- Weechat::Buffer.new(b)
15
+ Weechat::Buffer.from_ptr(b)
16
16
  end
17
17
  },
18
18
  [:ipv6, :ssl, :ssl_verify, :autoconnect, :autoreconnect,
@@ -39,28 +39,44 @@ module Weechat
39
39
  }.freeze
40
40
 
41
41
  init_properties
42
+ @type = "irc_server"
42
43
 
43
44
  def autojoin?
44
45
  !disable_autojoin
45
46
  end
46
47
 
47
48
  attr_reader :ptr
48
- def initialize(name)
49
- @ptr = @name = name.to_s
50
- raise Exception::UnknownServer, name if get_infolist.empty?
49
+ def initialize(*args)
50
+ # TODO allow the creation of new channels using commands
51
51
  end
52
52
 
53
- class << self
54
- alias_method :from_name, :new
53
+ def ==(other)
54
+ @ptr == other.ptr
55
+ end
56
+ alias_method :eql?, "=="
57
+ alias_method :equal?, "=="
55
58
 
56
- def servers
59
+ def hash
60
+ @ptr.hash
61
+ end
62
+
63
+ class << self
64
+ def all
57
65
  servers = []
58
66
  Weechat::Infolist.parse("irc_server").each do |server|
59
- servers << Server.new(server[:name])
67
+ servers << Server.from_name(server[:name])
60
68
  end
61
69
  servers
62
70
  end
63
- alias_method :all, :servers
71
+
72
+ def find(name)
73
+ o = allocate
74
+ o.instance_variable_set(:@ptr, name)
75
+ o.instance_variable_set(:@name, name.to_s)
76
+ raise Exception::UnknownServer, name if o.get_infolist.empty?
77
+ o
78
+ end
79
+ alias_method :from_name, :find
64
80
  end
65
81
 
66
82
  def channels
@@ -82,10 +98,6 @@ module Weechat
82
98
  end
83
99
 
84
100
  # TODO method for creating a new server
85
-
86
- def get_infolist
87
- Weechat::Infolist.parse("irc_server", "", @name)
88
- end
89
101
  end
90
102
  end
91
103
  end
@@ -58,7 +58,7 @@ module Weechat
58
58
  # @private
59
59
  @transformations = {
60
60
  [:first_line_displayed, :scroll] => lambda {|v| Weechat.integer_to_bool(v) },
61
- [:buffer] => lambda {|v| Buffer.new(v) },
61
+ [:buffer] => lambda {|v| Buffer.from_ptr(v) },
62
62
  }.freeze
63
63
 
64
64
  # @private
@@ -78,26 +78,18 @@ module Weechat
78
78
 
79
79
  class << self
80
80
  def current
81
- Window.new(Weechat.current_window)
81
+ Window.from_ptr(Weechat.current_window)
82
82
  end
83
83
 
84
- # @todo TODO move into own module
85
- def windows
86
- windows = []
87
- Weechat::Infolist.parse("window").each do |window|
88
- windows << Window.new(window[:pointer])
89
- end
90
- windows
84
+ def from_ptr(*args)
85
+ o = super
86
+ o.instance_variable_set(:@chat, Chat.new(o))
87
+ o
91
88
  end
92
- alias_method :all, :windows
93
89
  end
94
90
 
95
91
  init_properties
96
92
 
97
93
  attr_reader :chat
98
- def initialize(*args)
99
- super
100
- @chat = Chat.new(self)
101
- end
102
94
  end
103
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weechat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Honnef
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-24 00:00:00 +01:00
12
+ date: 2010-01-03 00:00:00 +01:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description: An abstraction layer on top of the WeeChat API, allowing a cleaner and more intuitive way of writing Ruby scripts for WeeChat.
17
26
  email: dominikho@gmx.net
18
27
  executables: []
@@ -23,6 +32,7 @@ extra_rdoc_files: []
23
32
 
24
33
  files:
25
34
  - lib/weechat/terminal.rb
35
+ - lib/weechat/bar.rb
26
36
  - lib/weechat/option.rb
27
37
  - lib/weechat/infolist.rb
28
38
  - lib/weechat/script.rb
@@ -36,10 +46,10 @@ files:
36
46
  - lib/weechat/pointer.rb
37
47
  - lib/weechat/buffer.rb
38
48
  - lib/weechat/color.rb
39
- - lib/weechat/rubyext/object.rb
40
49
  - lib/weechat/rubyext/float.rb
41
50
  - lib/weechat/rubyext/array.rb
42
51
  - lib/weechat/rubyext/integer.rb
52
+ - lib/weechat/rubyext/hash.rb
43
53
  - lib/weechat/rubyext/string.rb
44
54
  - lib/weechat/rubyext/boolean.rb
45
55
  - lib/weechat/hook.rb
@@ -51,6 +61,7 @@ files:
51
61
  - lib/weechat/line.rb
52
62
  - lib/weechat/exceptions.rb
53
63
  - lib/weechat/command.rb
64
+ - lib/weechat/callbacks.rb
54
65
  - lib/weechat/channel.rb
55
66
  - lib/weechat/info.rb
56
67
  - lib/weechat/process.rb
@@ -1,12 +0,0 @@
1
- require 'yaml'
2
- class Object
3
- def to_weechat_config
4
- to_yaml
5
- end
6
-
7
- def self.from_weechat_config(v)
8
- YAML.load(v)
9
- end
10
-
11
- alias_method :__class__, :class
12
- end