1 # vim: set noet nosta sw=4 ts=4 :
4 # Michael B. Hix <m@hix.io>
5 # http://code.hix.io/projects/colorizer
7 # Color certain parts of text in certain buffers based on rules.
13 # plugins.var.ruby.colorizer.buffer_regex
14 # Buffers with names matching this regex are colorized. All buffers are
15 # colorized if this option is empty.
17 # plugins.var.ruby.colorizer.rule.count
18 # This is the maximum number of rules to load.
20 # plugins.var.ruby.colorizer.rule.X
21 # X is zero or a positive integer. Rules are strings consisting of a regular
22 # expression followed immediately by a slash and a Weechat color name. The
23 # regular expressions are case-insensitive.
25 # Text matching the regular expression is colored with the given color. The
26 # last match "wins" and overlapping matches are not detected.
28 # For example: "strelka|mongrel2/lightgreen"
34 # 0.1: Initial release.
37 SCRIPT_NAME
= 'colorizer'
38 SCRIPT_AUTHOR
= 'Michael B. Hix'
39 SCRIPT_DESC
= 'Colorize text in buffers based on rules.'
40 SCRIPT_VERSION
= '0.1'
41 SCRIPT_LICENSE
= 'BSD'
43 # A default coloring rule.
47 :description => 'A colorizing rule of the form: <regular_expression>/<weechat_color_name> Empty rules are ignored.',
50 # Configuration defaults are supplied and set for the user if they're not already set.
53 'rule.0' => DEFAULT_RULE
,
54 'rule.1' => DEFAULT_RULE
,
55 'rule.2' => DEFAULT_RULE
,
56 'rule.3' => DEFAULT_RULE
,
57 'rule.4' => DEFAULT_RULE
,
60 :description => 'The maximum number of rules to look for in your config.',
64 :description => 'Only colorize text in buffers with names that match this regex. Leaving this empty matches all buffer names.',
68 ########################################################################
70 ########################################################################
73 Weechat
.register SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
, SCRIPT_DESC
, '', ''
75 Weechat
.hook_modifier( 'weechat_print', 'colorize_cb', '' )
77 DEFAULTS
.each_pair
do |option
, opts
|
79 description
= opts
[:description]
81 cur_value
= Weechat
.config_get_plugin( option
)
83 if cur_value
.nil? || cur_value
.empty
?
84 Weechat
.config_set_plugin( option
, value
.to_s
)
87 Weechat
.config_set_desc_plugin( option
, description
)
92 Weechat
.hook_config( "plugins.var.ruby.#{SCRIPT_NAME}.*", 'config_cb', '' )
94 return Weechat
::WEECHAT_RC_OK
97 ################################################################################
99 ################################################################################
101 # Provide a way to print legible stack traces.
103 def pp_error( e
, message
= '' )
104 return unless e
.is_a
? Exception
105 unless message
.nil? or message
.empty
?
106 Weechat
.print( '', '%s%s' % [Weechat
.prefix('error'), message
] )
108 Weechat
.print( '', '%s%s: %s' % [Weechat
.prefix( 'error' ), SCRIPT_NAME
, e
.to_s
] )
109 e
.backtrace
.each
do |line
|
110 Weechat
.print( '', '%s%s' % [Weechat
.prefix( 'error' ), line
] )
114 # Re-build rules and any regular expressions when the config changes.
118 count
= Weechat
::config_get_plugin( 'rule.count' ).to_i
||
119 DEFAULTS
['rule.count']
123 next unless Weechat
::config_is_set_plugin( key
)
125 conf
= Weechat
::config_get_plugin( key
)
126 regex
,color
,_
= conf
.split( /(?<!\\)\//, 3 )
128 next if regex
.nil? or regex
.empty
? or color
.nil? or color
.empty
?
131 rules
[/(#{regex})/i
] = color
132 rescue Exception
=> e
133 pp_error( e
, 'There was a problem with rule %d:' % [i
] )
140 @buffer_regex = /#{Weechat::config_get_plugin( 'buffer_regex' )}/i
141 rescue Exception
=> e
142 pp_error( e
, 'There was a problem with buffer_regex:' )
146 ################################################################################
147 ### C A L L B A C K S
148 ################################################################################
150 # Handle configuration changes.
152 def config_cb( data, option
, value
)
154 return Weechat
::WEECHAT_RC_OK
157 # Handle message printing.
159 def colorize_cb( data, modifier
, modifier_data
, message
)
160 _
,buffer
,_
= modifier_data
.split( ';' )
161 return message
unless @buffer_regex =~ buffer
163 reset
= Weechat
.color( 'reset' )
164 @rules.each
do |reg
, color_str
|
165 color
= Weechat
.color( color_str
)
166 message
.gsub!
( reg
, '%s\1%s' % [color
,reset
] )
175 Copyright (c
) 2014 Michael B
. Hix
178 Redistribution
and use
in source
and binary forms
, with
or without
179 modification
, are permitted provided that the following conditions are met
:
181 1. Redistributions of source code must retain the above copyright notice
,
182 this list of conditions
and the following disclaimer
.
184 2. Redistributions
in binary form must reproduce the above copyright notice
,
185 this list of conditions
and the following disclaimer
in the documentation
186 and/or other materials provided with the distribution
.
188 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS"
189 AND ANY EXPRESS OR IMPLIED WARRANTIES
, INCLUDING
, BUT NOT LIMITED TO
, THE
190 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191 ARE DISCLAIMED
. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
192 LIABLE FOR ANY DIRECT
, INDIRECT
, INCIDENTAL
, SPECIAL
, EXEMPLARY
, OR
193 CONSEQUENTIAL
DAMAGES (INCLUDING
, BUT NOT LIMITED TO
, PROCUREMENT OF
194 SUBSTITUTE GOODS OR SERVICES
; LOSS OF USE
, DATA, OR PROFITS
; OR BUSINESS
195 INTERRUPTION
) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY
, WHETHER IN
196 CONTRACT
, STRICT LIABILITY
, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE
)
197 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE
, EVEN IF ADVISED OF THE
198 POSSIBILITY OF SUCH DAMAGE
.