1
1
# -*- coding: utf-8 -*-
2
2
#
3
- # Copyright (c) 2012-2017 by nils_2 <[email protected] >
3
+ # Copyright (c) 2012-2018 by nils_2 <[email protected] >
4
4
#
5
5
# add a plain text or evaluated content to item bar
6
6
#
17
17
# You should have received a copy of the GNU General Public License
18
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
#
20
+ # 2018-08-18: nils_2, (freenode.#weechat)
21
+ # 0.8 : add new option "interval"
22
+ #
20
23
# 2017-08-23: nils_2, (freenode.#weechat)
21
24
# 0.7.1 : improve /help text
22
25
#
56
59
57
60
SCRIPT_NAME = "text_item"
58
61
SCRIPT_AUTHOR = "nils_2 <[email protected] >"
59
- SCRIPT_VERSION = "0.7.1 "
62
+ SCRIPT_VERSION = "0.8 "
60
63
SCRIPT_LICENSE = "GPL"
61
64
SCRIPT_DESC = "add a plain text or evaluated content to item bar"
62
65
63
66
# regexp to match ${color} tags
64
67
regex_color = re .compile ('\$\{([^\{\}]+)\}' )
65
68
66
69
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
+ }
67
75
68
76
# ================================[ hooks ]===============================
69
77
def add_hook (signal , item ):
70
78
global hooks
71
79
# signal already exists?
72
80
if signal in hooks :
73
81
return
74
- hooks [item ] = weechat .hook_signal (signal , "bar_item_update " , "" )
82
+ hooks [item ] = weechat .hook_signal (signal , "bar_item_update_cb " , "" )
75
83
76
84
def unhook (hook ):
77
85
global hooks
78
86
if hook in hooks :
79
87
weechat .unhook (hooks [hook ])
80
88
del hooks [hook ]
81
89
82
- def toggle_refresh (pointer , name , value ):
90
+ def toggle_refresh_cb (pointer , name , value ):
83
91
option_name = name [len ('plugins.var.python.' + SCRIPT_NAME + '.' ):] # get optionname
84
92
93
+ # check for timer hook
94
+ if name .endswith (".interval" ):
95
+ set_timer ()
96
+ return weechat .WEECHAT_RC_OK
97
+
85
98
# option was removed? remove bar_item from struct
86
99
if not weechat .config_get_plugin (option_name ):
87
100
ptr_bar = weechat .bar_item_search (option_name )
@@ -96,6 +109,19 @@ def toggle_refresh(pointer, name, value):
96
109
weechat .bar_item_update (option_name )
97
110
return weechat .WEECHAT_RC_OK
98
111
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
+
99
125
# ================================[ items ]===============================
100
126
def create_bar_items ():
101
127
ptr_infolist_option = weechat .infolist_get ('option' ,'' ,'plugins.var.python.' + SCRIPT_NAME + '.*' )
@@ -132,7 +158,7 @@ def update_item (data, item, window):
132
158
return substitute_colors (value ,window )
133
159
134
160
# 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 ):
136
162
ptr_infolist_option = weechat .infolist_get ('option' ,'' ,'plugins.var.python.' + SCRIPT_NAME + '.*' )
137
163
138
164
if not ptr_infolist_option :
@@ -141,12 +167,15 @@ def bar_item_update(signal, callback, callback_data):
141
167
while weechat .infolist_next (ptr_infolist_option ):
142
168
option_full_name = weechat .infolist_string (ptr_infolist_option , 'full_name' )
143
169
option_name = option_full_name [len ('plugins.var.python.' + SCRIPT_NAME + '.' ):] # get optionname
170
+ if option_name == "interval" :
171
+ continue
144
172
145
173
# check if item exists in a bar and if we have a hook for it
146
174
if weechat .bar_item_search (option_name ) and option_name in hooks :
147
175
weechat .bar_item_update (option_name )
148
176
149
177
weechat .infolist_free (ptr_infolist_option )
178
+
150
179
return weechat .WEECHAT_RC_OK
151
180
152
181
@@ -200,7 +229,11 @@ def check_buffer_type(window, data, value):
200
229
' type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n '
201
230
' (see: /buffer localvar)\n \n '
202
231
' 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 '
204
237
'Examples:\n '
205
238
'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
206
239
'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n '
@@ -215,5 +248,14 @@ def check_buffer_type(window, data, value):
215
248
'' ,
216
249
'' )
217
250
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 ()
218
259
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