Skip to content

Commit 9ca9130

Browse files
weechatterflashcode
authored andcommitted
text_item.py 0.8: add option "interval"
1 parent bceced8 commit 9ca9130

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

python/text_item.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (c) 2012-2017 by nils_2 <[email protected]>
3+
# Copyright (c) 2012-2018 by nils_2 <[email protected]>
44
#
55
# add a plain text or evaluated content to item bar
66
#
@@ -17,6 +17,9 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
#
20+
# 2018-08-18: nils_2, (freenode.#weechat)
21+
# 0.8 : add new option "interval"
22+
#
2023
# 2017-08-23: nils_2, (freenode.#weechat)
2124
# 0.7.1 : improve /help text
2225
#
@@ -56,32 +59,42 @@
5659

5760
SCRIPT_NAME = "text_item"
5861
SCRIPT_AUTHOR = "nils_2 <[email protected]>"
59-
SCRIPT_VERSION = "0.7.1"
62+
SCRIPT_VERSION = "0.8"
6063
SCRIPT_LICENSE = "GPL"
6164
SCRIPT_DESC = "add a plain text or evaluated content to item bar"
6265

6366
# regexp to match ${color} tags
6467
regex_color=re.compile('\$\{([^\{\}]+)\}')
6568

6669
hooks = {}
70+
TIMER = None
71+
72+
settings = {
73+
'interval': ('0', 'How often (in seconds) to force an update of all items. 0 means deactivated'),
74+
}
6775

6876
# ================================[ hooks ]===============================
6977
def add_hook(signal, item):
7078
global hooks
7179
# signal already exists?
7280
if signal in hooks:
7381
return
74-
hooks[item] = weechat.hook_signal(signal, "bar_item_update", "")
82+
hooks[item] = weechat.hook_signal(signal, "bar_item_update_cb", "")
7583

7684
def unhook(hook):
7785
global hooks
7886
if hook in hooks:
7987
weechat.unhook(hooks[hook])
8088
del hooks[hook]
8189

82-
def toggle_refresh(pointer, name, value):
90+
def toggle_refresh_cb(pointer, name, value):
8391
option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
8492

93+
# check for timer hook
94+
if name.endswith(".interval"):
95+
set_timer()
96+
return weechat.WEECHAT_RC_OK
97+
8598
# option was removed? remove bar_item from struct
8699
if not weechat.config_get_plugin(option_name):
87100
ptr_bar = weechat.bar_item_search(option_name)
@@ -96,6 +109,19 @@ def toggle_refresh(pointer, name, value):
96109
weechat.bar_item_update(option_name)
97110
return weechat.WEECHAT_RC_OK
98111

112+
def set_timer():
113+
# Update timer hook with new interval. 0 means deactivated
114+
global TIMER
115+
if TIMER:
116+
weechat.unhook(TIMER)
117+
if int(weechat.config_get_plugin('interval')) >= 1:
118+
TIMER = weechat.hook_timer(int(weechat.config_get_plugin('interval')) * 1000,0, 0, "timer_dummy_cb", '')
119+
120+
def timer_dummy_cb(data, remaining_calls):
121+
# hook_timer() has two arguments, hook_signal() needs three arguments
122+
bar_item_update_cb("","","")
123+
return weechat.WEECHAT_RC_OK
124+
99125
# ================================[ items ]===============================
100126
def create_bar_items():
101127
ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
@@ -132,7 +158,7 @@ def update_item (data, item, window):
132158
return substitute_colors(value,window)
133159

134160
# update item from weechat.hook_signal()
135-
def bar_item_update(signal, callback, callback_data):
161+
def bar_item_update_cb(signal, callback, callback_data):
136162
ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
137163

138164
if not ptr_infolist_option:
@@ -141,12 +167,15 @@ def bar_item_update(signal, callback, callback_data):
141167
while weechat.infolist_next(ptr_infolist_option):
142168
option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
143169
option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
170+
if option_name == "interval":
171+
continue
144172

145173
# check if item exists in a bar and if we have a hook for it
146174
if weechat.bar_item_search(option_name) and option_name in hooks:
147175
weechat.bar_item_update(option_name)
148176

149177
weechat.infolist_free(ptr_infolist_option)
178+
150179
return weechat.WEECHAT_RC_OK
151180

152181

@@ -200,7 +229,11 @@ def check_buffer_type(window, data, value):
200229
' type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
201230
' (see: /buffer localvar)\n\n'
202231
' 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'
232+
' (for a list of all possible signals, see API doc weechat_hook_signal())\n'
233+
'\n'
234+
'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'
235+
'updated every second (e.g. watch)\n'
236+
'\n'
204237
'Examples:\n'
205238
'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
206239
'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
@@ -215,5 +248,14 @@ def check_buffer_type(window, data, value):
215248
'',
216249
'')
217250
version = weechat.info_get("version_number", "") or 0
251+
252+
for option, default_desc in settings.items():
253+
if not weechat.config_is_set_plugin(option):
254+
weechat.config_set_plugin(option, default_desc[0])
255+
if int(version) >= 0x00030500:
256+
weechat.config_set_desc_plugin(option, default_desc[1])
257+
258+
set_timer()
218259
create_bar_items()
219-
weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )
260+
261+
weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh_cb', '' )

0 commit comments

Comments
 (0)