X-Git-Url: https://git.rmz.io/dotfiles.git/blobdiff_plain/d607af1e5d9c66d254e6b46af0718ddb067add80..refs/heads/lazyvim:/weechat/python/text_item.py?ds=sidebyside diff --git a/weechat/python/text_item.py b/weechat/python/text_item.py index 4b63a81..975878a 100644 --- a/weechat/python/text_item.py +++ b/weechat/python/text_item.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-2016 by nils_2 +# Copyright (c) 2012-2019 by nils_2 # # add a plain text or evaluated content to item bar # @@ -17,6 +17,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +# 2019-05-23: FlashCode, (freenode.#weechat) +# 0.9 : fix eval_expression() for split windows +# +# 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 +# +# 2017-08-19: nils_2, (freenode.#weechat) +# 0.7 : add type "!all", internal changes +# # 2016-12-12: nils_2, (freenode.#weechat) # 0.6 : fix problem with multiple windows (reported by Ram-Z) # @@ -40,6 +52,13 @@ # Development is currently hosted at # https://github.com/weechatter/weechat-scripts +# TODO +# plugins.var.python.text_item..enabled +# plugins.var.python.text_item..type +# plugins.var.python.text_item..signal +# plugins.var.python.text_item..text +# plugins.var.python.text_item..interval + try: import weechat,re @@ -50,7 +69,7 @@ except Exception: SCRIPT_NAME = "text_item" SCRIPT_AUTHOR = "nils_2 " -SCRIPT_VERSION = "0.6" +SCRIPT_VERSION = "0.9" SCRIPT_LICENSE = "GPL" SCRIPT_DESC = "add a plain text or evaluated content to item bar" @@ -58,14 +77,18 @@ 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): global hooks # 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 @@ -73,27 +96,41 @@ 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 - # option was removed? remove bar_item from struct! + # 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) if ptr_bar: weechat.bar_item_remove(ptr_bar) - return weechat.WEECHAT_RC_OK - else: - return weechat.WEECHAT_RC_OK + return weechat.WEECHAT_RC_OK - # check if option is new or simply changed - if weechat.bar_item_search(option_name): - weechat.bar_item_update(option_name) - else: + # check if option is new or changed + if not weechat.bar_item_search(option_name): weechat.bar_item_new(option_name,'update_item',option_name) 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 + '.*') @@ -122,19 +159,15 @@ def update_item (data, item, window): window = weechat.current_window() value = weechat.config_get_plugin(data) - - if value: - value = check_buffer_type(window, data, value) - else: - return "" - if not value: return "" + value = check_buffer_type(window, data, value) + return substitute_colors(value,window) -# update item -def bar_item_update(signal, callback, callback_data): +# update item from weechat.hook_signal() +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: @@ -143,12 +176,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 @@ -156,8 +192,7 @@ def bar_item_update(signal, callback, callback_data): def substitute_colors(text,window): if int(version) >= 0x00040200: bufpointer = weechat.window_get_pointer(window,"buffer") - return weechat.string_eval_expression(text, {"buffer": bufpointer}, {}, {}) -# return weechat.string_eval_expression(text,{},{},{}) + return weechat.string_eval_expression(text, {"window": window, "buffer": bufpointer}, {}, {}) # substitute colors in output return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text) @@ -184,6 +219,10 @@ def check_buffer_type(window, data, value): if channel_type == 'all' or weechat.buffer_get_string(bufpointer,'localvar_type') == channel_type: return value + if channel_type == '!all': + a = ["channel","server","private"] + if weechat.buffer_get_string(bufpointer,'localvar_type') in a: + return value return "" # ================================[ main ]=============================== @@ -195,22 +234,36 @@ if __name__ == "__main__": '===========\n' 'Template:\n' '/set plugins.var.python.text_item. | <${color:name/number}>\n\n' - ' type : all, channel, server, private\n' - ' (you can use: /buffer localvar)\n\n' - ' signal (eg.): buffer_switch, buffer_closing, print, \n' - ' (for a list of all possible signals, see API doc weechat_hook_signal())\n\n' - 'Example:\n' - '=======\n' + ' 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' + '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' ' /set plugins.var.python.text_item.nick_text "channel ${color:yellow}Nicks:"\n\n' 'now you have to add the item "nick_text" to the bar.items (use auto-completion or iset.pl!)\n' ' /set weechat.bar.status.items nick_text\n\n' - 'creates an option to display the terminal width and height in an item bar. item will be updated on signal "signal_sigwinch"\n' - ' /set plugins.var.python.text_item.dimension "all|signal_sigwinch width: ${info:term_width} height: ${info:term_height}"\n', + 'creates an option to display the terminal width and height in an item bar. item will be updated on signal "signal_sigwinch":\n' + ' /set plugins.var.python.text_item.dimension "all|signal_sigwinch width: ${info:term_width} height: ${info:term_height}"\n' + 'creates an option to display the status from "/filter toggle" and "/filter toggle @" command, item name is "filter_item":\n' + ' /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', '', '', '') 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', '' )