]> git.rmz.io Git - dotfiles.git/blobdiff - weechat/python/text_item.py
nvim: disable inlay_hints
[dotfiles.git] / weechat / python / text_item.py
index 47b284fb16a69ce5519bb1e11ba9c3770f74140f..975878a1e62bd7065280b520601cfa2ffe2b2d72 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
+# Copyright (c) 2012-2019 by nils_2 <weechatter@arcor.de>
 #
 # add a plain text or evaluated content to item bar
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+# 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
 #
 # Development is currently hosted at
 # https://github.com/weechatter/weechat-scripts
 
+# TODO
+# plugins.var.python.text_item.<item_name>.enabled
+# plugins.var.python.text_item.<item_name>.type
+# plugins.var.python.text_item.<item_name>.signal
+# plugins.var.python.text_item.<item_name>.text
+# plugins.var.python.text_item.<item_name>.interval
+
 try:
     import weechat,re
 
@@ -56,7 +69,7 @@ except Exception:
 
 SCRIPT_NAME     = "text_item"
 SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
-SCRIPT_VERSION  = "0.7.1"
+SCRIPT_VERSION  = "0.9"
 SCRIPT_LICENSE  = "GPL"
 SCRIPT_DESC     = "add a plain text or evaluated content to item bar"
 
@@ -64,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
@@ -79,9 +96,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 +118,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 +167,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 +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
 
 
@@ -154,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)
 
@@ -200,7 +237,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 +256,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', '' )