]> git.rmz.io Git - dotfiles.git/blob - weechat/python/text_item.py
weechat: update plugins
[dotfiles.git] / weechat / python / text_item.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
4 #
5 # add a plain text or evaluated content to item bar
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # 2017-08-23: nils_2, (freenode.#weechat)
21 # 0.7.1 : improve /help text
22 #
23 # 2017-08-19: nils_2, (freenode.#weechat)
24 # 0.7 : add type "!all", internal changes
25 #
26 # 2016-12-12: nils_2, (freenode.#weechat)
27 # 0.6 : fix problem with multiple windows (reported by Ram-Z)
28 #
29 # 2016-09-15: nils_2, (freenode.#weechat)
30 # 0.5 : add /help text (suggested by gb)
31 #
32 # 2014-05-19: nils_2, (freenode.#weechat)
33 # 0.4 : evaluate content of item (suggested by FlashCode)
34 #
35 # 2013-06-27: nils_2, (freenode.#weechat)
36 # 0.3 : fix: bug with root bar
37 #
38 # 2013-01-25: nils_2, (freenode.#weechat)
39 # 0.2 : make script compatible with Python 3.x
40 #
41 # 2012-12-23: nils_2, (freenode.#weechat)
42 # 0.1 : initial release
43 #
44 # requires: WeeChat version 0.3.0
45 #
46 # Development is currently hosted at
47 # https://github.com/weechatter/weechat-scripts
48
49 try:
50 import weechat,re
51
52 except Exception:
53 print("This script must be run under WeeChat.")
54 print("Get WeeChat now at: http://www.weechat.org/")
55 quit()
56
57 SCRIPT_NAME = "text_item"
58 SCRIPT_AUTHOR = "nils_2 <weechatter@arcor.de>"
59 SCRIPT_VERSION = "0.7.1"
60 SCRIPT_LICENSE = "GPL"
61 SCRIPT_DESC = "add a plain text or evaluated content to item bar"
62
63 # regexp to match ${color} tags
64 regex_color=re.compile('\$\{([^\{\}]+)\}')
65
66 hooks = {}
67
68 # ================================[ hooks ]===============================
69 def add_hook(signal, item):
70 global hooks
71 # signal already exists?
72 if signal in hooks:
73 return
74 hooks[item] = weechat.hook_signal(signal, "bar_item_update", "")
75
76 def unhook(hook):
77 global hooks
78 if hook in hooks:
79 weechat.unhook(hooks[hook])
80 del hooks[hook]
81
82 def toggle_refresh(pointer, name, value):
83 option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
84
85 # option was removed? remove bar_item from struct
86 if not weechat.config_get_plugin(option_name):
87 ptr_bar = weechat.bar_item_search(option_name)
88 if ptr_bar:
89 weechat.bar_item_remove(ptr_bar)
90 return weechat.WEECHAT_RC_OK
91
92 # check if option is new or changed
93 if not weechat.bar_item_search(option_name):
94 weechat.bar_item_new(option_name,'update_item',option_name)
95
96 weechat.bar_item_update(option_name)
97 return weechat.WEECHAT_RC_OK
98
99 # ================================[ items ]===============================
100 def create_bar_items():
101 ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
102
103 if not ptr_infolist_option:
104 return
105
106 while weechat.infolist_next(ptr_infolist_option):
107 option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
108 option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
109
110 if weechat.bar_item_search(option_name):
111 weechat.bar_item_update(option_name)
112 else:
113 weechat.bar_item_new(option_name,'update_item',option_name)
114 weechat.bar_item_update(option_name)
115
116 weechat.infolist_free(ptr_infolist_option)
117
118 def update_item (data, item, window):
119 if not data:
120 return ""
121
122 # window empty? root bar!
123 if not window:
124 window = weechat.current_window()
125
126 value = weechat.config_get_plugin(data)
127 if not value:
128 return ""
129
130 value = check_buffer_type(window, data, value)
131
132 return substitute_colors(value,window)
133
134 # update item from weechat.hook_signal()
135 def bar_item_update(signal, callback, callback_data):
136 ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
137
138 if not ptr_infolist_option:
139 return
140
141 while weechat.infolist_next(ptr_infolist_option):
142 option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
143 option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
144
145 # check if item exists in a bar and if we have a hook for it
146 if weechat.bar_item_search(option_name) and option_name in hooks:
147 weechat.bar_item_update(option_name)
148
149 weechat.infolist_free(ptr_infolist_option)
150 return weechat.WEECHAT_RC_OK
151
152
153 # ================================[ subroutines ]===============================
154 def substitute_colors(text,window):
155 if int(version) >= 0x00040200:
156 bufpointer = weechat.window_get_pointer(window,"buffer")
157 return weechat.string_eval_expression(text, {"buffer": bufpointer}, {}, {})
158 # return weechat.string_eval_expression(text,{},{},{})
159 # substitute colors in output
160 return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text)
161
162 def check_buffer_type(window, data, value):
163 bufpointer = weechat.window_get_pointer(window,"buffer")
164 if bufpointer == "":
165 return ""
166
167 value = value.split(' ', 1)
168 if len(value) <= 1:
169 return ""
170
171 # format is : buffer_type (channel,server,private,all) | signal (e.g: buffer_switch)
172 channel_type_and_signal = value[0]
173 if channel_type_and_signal.find('|') >= 0:
174 channel_type = channel_type_and_signal[0:channel_type_and_signal.find("|")]
175 signal_type = channel_type_and_signal[channel_type_and_signal.find("|")+1:]
176 unhook(data)
177 add_hook(signal_type, data)
178 else:
179 channel_type = value[0]
180
181 value = value[1]
182
183 if channel_type == 'all' or weechat.buffer_get_string(bufpointer,'localvar_type') == channel_type:
184 return value
185 if channel_type == '!all':
186 a = ["channel","server","private"]
187 if weechat.buffer_get_string(bufpointer,'localvar_type') in a:
188 return value
189 return ""
190
191 # ================================[ main ]===============================
192 if __name__ == "__main__":
193 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC,'',''):
194 weechat.hook_command(SCRIPT_NAME,SCRIPT_DESC,
195 '',
196 'How to use:\n'
197 '===========\n'
198 'Template:\n'
199 '/set plugins.var.python.text_item.<item_name> <type>|<signal> <${color:name/number}><text>\n\n'
200 ' type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
201 ' (see: /buffer localvar)\n\n'
202 ' signal (eg.): buffer_switch, buffer_closing, print, mouse_enabled\n'
203 ' (for a list of all possible signals, see API doc weechat_hook_signal())\n\n'
204 'Examples:\n'
205 'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
206 'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
207 ' /set plugins.var.python.text_item.nick_text "channel ${color:yellow}Nicks:"\n\n'
208 'now you have to add the item "nick_text" to the bar.items (use auto-completion or iset.pl!)\n'
209 ' /set weechat.bar.status.items nick_text\n\n'
210 'creates an option to display the terminal width and height in an item bar. item will be updated on signal "signal_sigwinch":\n'
211 ' /set plugins.var.python.text_item.dimension "all|signal_sigwinch width: ${info:term_width} height: ${info:term_height}"\n'
212 'creates an option to display the status from "/filter toggle" and "/filter toggle @" command, item name is "filter_item":\n'
213 ' /set plugins.var.python.text_item.filter_item "!all|*filters* ${if:${info:filters_enabled}==1?${color:yellow}F:${color:243}F}${if:${buffer.filter}==1?${color:yellow}@:${color:243}@}"\n',
214 '',
215 '',
216 '')
217 version = weechat.info_get("version_number", "") or 0
218 create_bar_items()
219 weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )