Commit 9ca91309 authored by Nils Görs's avatar Nils Görs Committed by Sébastien Helleu

text_item.py 0.8: add option "interval"

parent bceced8b
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
# Copyright (c) 2012-2018 by nils_2 <weechatter@arcor.de>
#
# add a plain text or evaluated content to item bar
#
......@@ -17,6 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# 2018-08-18: nils_2, (freenode.#weechat)
# 0.8 : add new option "interval"
#
# 2017-08-23: nils_2, (freenode.#weechat)
# 0.7.1 : improve /help text
#
......@@ -56,7 +59,7 @@ except Exception:
SCRIPT_NAME = "text_item"
SCRIPT_AUTHOR = "nils_2 <weechatter@arcor.de>"
SCRIPT_VERSION = "0.7.1"
SCRIPT_VERSION = "0.8"
SCRIPT_LICENSE = "GPL"
SCRIPT_DESC = "add a plain text or evaluated content to item bar"
......@@ -64,6 +67,11 @@ SCRIPT_DESC = "add a plain text or evaluated content to item bar"
regex_color=re.compile('\$\{([^\{\}]+)\}')
hooks = {}
TIMER = None
settings = {
'interval': ('0', 'How often (in seconds) to force an update of all items. 0 means deactivated'),
}
# ================================[ hooks ]===============================
def add_hook(signal, item):
......@@ -71,7 +79,7 @@ def add_hook(signal, item):
# signal already exists?
if signal in hooks:
return
hooks[item] = weechat.hook_signal(signal, "bar_item_update", "")
hooks[item] = weechat.hook_signal(signal, "bar_item_update_cb", "")
def unhook(hook):
global hooks
......@@ -79,9 +87,14 @@ def unhook(hook):
weechat.unhook(hooks[hook])
del hooks[hook]
def toggle_refresh(pointer, name, value):
def toggle_refresh_cb(pointer, name, value):
option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
# check for timer hook
if name.endswith(".interval"):
set_timer()
return weechat.WEECHAT_RC_OK
# option was removed? remove bar_item from struct
if not weechat.config_get_plugin(option_name):
ptr_bar = weechat.bar_item_search(option_name)
......@@ -96,6 +109,19 @@ def toggle_refresh(pointer, name, value):
weechat.bar_item_update(option_name)
return weechat.WEECHAT_RC_OK
def set_timer():
# Update timer hook with new interval. 0 means deactivated
global TIMER
if TIMER:
weechat.unhook(TIMER)
if int(weechat.config_get_plugin('interval')) >= 1:
TIMER = weechat.hook_timer(int(weechat.config_get_plugin('interval')) * 1000,0, 0, "timer_dummy_cb", '')
def timer_dummy_cb(data, remaining_calls):
# hook_timer() has two arguments, hook_signal() needs three arguments
bar_item_update_cb("","","")
return weechat.WEECHAT_RC_OK
# ================================[ items ]===============================
def create_bar_items():
ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
......@@ -132,7 +158,7 @@ def update_item (data, item, window):
return substitute_colors(value,window)
# update item from weechat.hook_signal()
def bar_item_update(signal, callback, callback_data):
def bar_item_update_cb(signal, callback, callback_data):
ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
if not ptr_infolist_option:
......@@ -141,12 +167,15 @@ def bar_item_update(signal, callback, callback_data):
while weechat.infolist_next(ptr_infolist_option):
option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname
if option_name == "interval":
continue
# check if item exists in a bar and if we have a hook for it
if weechat.bar_item_search(option_name) and option_name in hooks:
weechat.bar_item_update(option_name)
weechat.infolist_free(ptr_infolist_option)
return weechat.WEECHAT_RC_OK
......@@ -200,7 +229,11 @@ if __name__ == "__main__":
' type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
' (see: /buffer localvar)\n\n'
' signal (eg.): buffer_switch, buffer_closing, print, mouse_enabled\n'
' (for a list of all possible signals, see API doc weechat_hook_signal())\n\n'
' (for a list of all possible signals, see API doc weechat_hook_signal())\n'
'\n'
'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'
'updated every second (e.g. watch)\n'
'\n'
'Examples:\n'
'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
......@@ -215,5 +248,14 @@ if __name__ == "__main__":
'',
'')
version = weechat.info_get("version_number", "") or 0
for option, default_desc in settings.items():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, default_desc[0])
if int(version) >= 0x00030500:
weechat.config_set_desc_plugin(option, default_desc[1])
set_timer()
create_bar_items()
weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )
weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh_cb', '' )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment