]> git.rmz.io Git - dotfiles.git/blob - weechat/python/text_item.py
vim: use system clangd and local compile db
[dotfiles.git] / weechat / python / text_item.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2012-2019 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 # 2019-05-23: FlashCode, (freenode.#weechat)
21 # 0.9 : fix eval_expression() for split windows
22 #
23 # 2018-08-18: nils_2, (freenode.#weechat)
24 # 0.8 : add new option "interval"
25 #
26 # 2017-08-23: nils_2, (freenode.#weechat)
27 # 0.7.1 : improve /help text
28 #
29 # 2017-08-19: nils_2, (freenode.#weechat)
30 # 0.7 : add type "!all", internal changes
31 #
32 # 2016-12-12: nils_2, (freenode.#weechat)
33 # 0.6 : fix problem with multiple windows (reported by Ram-Z)
34 #
35 # 2016-09-15: nils_2, (freenode.#weechat)
36 # 0.5 : add /help text (suggested by gb)
37 #
38 # 2014-05-19: nils_2, (freenode.#weechat)
39 # 0.4 : evaluate content of item (suggested by FlashCode)
40 #
41 # 2013-06-27: nils_2, (freenode.#weechat)
42 # 0.3 : fix: bug with root bar
43 #
44 # 2013-01-25: nils_2, (freenode.#weechat)
45 # 0.2 : make script compatible with Python 3.x
46 #
47 # 2012-12-23: nils_2, (freenode.#weechat)
48 # 0.1 : initial release
49 #
50 # requires: WeeChat version 0.3.0
51 #
52 # Development is currently hosted at
53 # https://github.com/weechatter/weechat-scripts
54
55 # TODO
56 # plugins.var.python.text_item.<item_name>.enabled
57 # plugins.var.python.text_item.<item_name>.type
58 # plugins.var.python.text_item.<item_name>.signal
59 # plugins.var.python.text_item.<item_name>.text
60 # plugins.var.python.text_item.<item_name>.interval
61
62 try:
63 import weechat,re
64
65 except Exception:
66 print("This script must be run under WeeChat.")
67 print("Get WeeChat now at: http://www.weechat.org/")
68 quit()
69
70 SCRIPT_NAME = "text_item"
71 SCRIPT_AUTHOR = "nils_2 <weechatter@arcor.de>"
72 SCRIPT_VERSION = "0.9"
73 SCRIPT_LICENSE = "GPL"
74 SCRIPT_DESC = "add a plain text or evaluated content to item bar"
75
76 # regexp to match ${color} tags
77 regex_color=re.compile('\$\{([^\{\}]+)\}')
78
79 hooks = {}
80 TIMER = None
81
82 settings = {
83 'interval': ('0', 'How often (in seconds) to force an update of all items. 0 means deactivated'),
84 }
85 # ================================[ hooks ]===============================
86 def add_hook(signal, item):
87 global hooks
88 # signal already exists?
89 if signal in hooks:
90 return
91 hooks[item] = weechat.hook_signal(signal, "bar_item_update_cb", "")
92
93 def unhook(hook):
94 global hooks
95 if hook in hooks:
96 weechat.unhook(hooks[hook])
97 del hooks[hook]
98
99 def toggle_refresh_cb(pointer, name, value):
100 option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
101
102 # check for timer hook
103 if name.endswith(".interval"):
104 set_timer()
105 return weechat.WEECHAT_RC_OK
106
107 # option was removed? remove bar_item from struct
108 if not weechat.config_get_plugin(option_name):
109 ptr_bar = weechat.bar_item_search(option_name)
110 if ptr_bar:
111 weechat.bar_item_remove(ptr_bar)
112 return weechat.WEECHAT_RC_OK
113
114 # check if option is new or changed
115 if not weechat.bar_item_search(option_name):
116 weechat.bar_item_new(option_name,'update_item',option_name)
117
118 weechat.bar_item_update(option_name)
119 return weechat.WEECHAT_RC_OK
120
121 def set_timer():
122 # Update timer hook with new interval. 0 means deactivated
123 global TIMER
124 if TIMER:
125 weechat.unhook(TIMER)
126 if int(weechat.config_get_plugin('interval')) >= 1:
127 TIMER = weechat.hook_timer(int(weechat.config_get_plugin('interval')) * 1000,0, 0, "timer_dummy_cb", '')
128
129 def timer_dummy_cb(data, remaining_calls):
130 # hook_timer() has two arguments, hook_signal() needs three arguments
131 bar_item_update_cb("","","")
132 return weechat.WEECHAT_RC_OK
133
134 # ================================[ items ]===============================
135 def create_bar_items():
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 if weechat.bar_item_search(option_name):
146 weechat.bar_item_update(option_name)
147 else:
148 weechat.bar_item_new(option_name,'update_item',option_name)
149 weechat.bar_item_update(option_name)
150
151 weechat.infolist_free(ptr_infolist_option)
152
153 def update_item (data, item, window):
154 if not data:
155 return ""
156
157 # window empty? root bar!
158 if not window:
159 window = weechat.current_window()
160
161 value = weechat.config_get_plugin(data)
162 if not value:
163 return ""
164
165 value = check_buffer_type(window, data, value)
166
167 return substitute_colors(value,window)
168
169 # update item from weechat.hook_signal()
170 def bar_item_update_cb(signal, callback, callback_data):
171 ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
172
173 if not ptr_infolist_option:
174 return
175
176 while weechat.infolist_next(ptr_infolist_option):
177 option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
178 option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
179 if option_name == "interval":
180 continue
181
182 # check if item exists in a bar and if we have a hook for it
183 if weechat.bar_item_search(option_name) and option_name in hooks:
184 weechat.bar_item_update(option_name)
185
186 weechat.infolist_free(ptr_infolist_option)
187
188 return weechat.WEECHAT_RC_OK
189
190
191 # ================================[ subroutines ]===============================
192 def substitute_colors(text,window):
193 if int(version) >= 0x00040200:
194 bufpointer = weechat.window_get_pointer(window,"buffer")
195 return weechat.string_eval_expression(text, {"window": window, "buffer": bufpointer}, {}, {})
196 # substitute colors in output
197 return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text)
198
199 def check_buffer_type(window, data, value):
200 bufpointer = weechat.window_get_pointer(window,"buffer")
201 if bufpointer == "":
202 return ""
203
204 value = value.split(' ', 1)
205 if len(value) <= 1:
206 return ""
207
208 # format is : buffer_type (channel,server,private,all) | signal (e.g: buffer_switch)
209 channel_type_and_signal = value[0]
210 if channel_type_and_signal.find('|') >= 0:
211 channel_type = channel_type_and_signal[0:channel_type_and_signal.find("|")]
212 signal_type = channel_type_and_signal[channel_type_and_signal.find("|")+1:]
213 unhook(data)
214 add_hook(signal_type, data)
215 else:
216 channel_type = value[0]
217
218 value = value[1]
219
220 if channel_type == 'all' or weechat.buffer_get_string(bufpointer,'localvar_type') == channel_type:
221 return value
222 if channel_type == '!all':
223 a = ["channel","server","private"]
224 if weechat.buffer_get_string(bufpointer,'localvar_type') in a:
225 return value
226 return ""
227
228 # ================================[ main ]===============================
229 if __name__ == "__main__":
230 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC,'',''):
231 weechat.hook_command(SCRIPT_NAME,SCRIPT_DESC,
232 '',
233 'How to use:\n'
234 '===========\n'
235 'Template:\n'
236 '/set plugins.var.python.text_item.<item_name> <type>|<signal> <${color:name/number}><text>\n\n'
237 ' type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
238 ' (see: /buffer localvar)\n\n'
239 ' signal (eg.): buffer_switch, buffer_closing, print, mouse_enabled\n'
240 ' (for a list of all possible signals, see API doc weechat_hook_signal())\n'
241 '\n'
242 'You can activate a timer hook() to force an upgrade of all items in a given period of time, for example using an item that have to be\n'
243 'updated every second (e.g. watch)\n'
244 '\n'
245 'Examples:\n'
246 'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
247 'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
248 ' /set plugins.var.python.text_item.nick_text "channel ${color:yellow}Nicks:"\n\n'
249 'now you have to add the item "nick_text" to the bar.items (use auto-completion or iset.pl!)\n'
250 ' /set weechat.bar.status.items nick_text\n\n'
251 'creates an option to display the terminal width and height in an item bar. item will be updated on signal "signal_sigwinch":\n'
252 ' /set plugins.var.python.text_item.dimension "all|signal_sigwinch width: ${info:term_width} height: ${info:term_height}"\n'
253 'creates an option to display the status from "/filter toggle" and "/filter toggle @" command, item name is "filter_item":\n'
254 ' /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',
255 '',
256 '',
257 '')
258 version = weechat.info_get("version_number", "") or 0
259
260 for option, default_desc in settings.items():
261 if not weechat.config_is_set_plugin(option):
262 weechat.config_set_plugin(option, default_desc[0])
263 if int(version) >= 0x00030500:
264 weechat.config_set_desc_plugin(option, default_desc[1])
265
266 set_timer()
267 create_bar_items()
268
269 weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh_cb', '' )