Home

Script: text_item.py

Add bar items with plain text. (for WeeChat ≥ 0.3.0)
Author: nils_2, version 0.6, GPL3 — added: 2013-01-02, updated: 2016-12-13
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2016 by nils_2 <weechatter@arcor.de>
#
# add a plain text or evaluated content to item bar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 2016-12-12: nils_2, (freenode.#weechat)
#       0.6 : fix problem with multiple windows (reported by Ram-Z)
#
# 2016-09-15: nils_2, (freenode.#weechat)
#       0.5 : add /help text (suggested by gb)
#
# 2014-05-19: nils_2, (freenode.#weechat)
#       0.4 : evaluate content of item (suggested by FlashCode)
#
# 2013-06-27: nils_2, (freenode.#weechat)
#       0.3 : fix: bug with root bar
#
# 2013-01-25: nils_2, (freenode.#weechat)
#       0.2 : make script compatible with Python 3.x
#
# 2012-12-23: nils_2, (freenode.#weechat)
#       0.1 : initial release
#
# requires: WeeChat version 0.3.0
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts

try:
    import weechat,re

except Exception:
    print("This script must be run under WeeChat.")
    print("Get WeeChat now at: http://www.weechat.org/")
    quit()

SCRIPT_NAME     = "text_item"
SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
SCRIPT_VERSION  = "0.6"
SCRIPT_LICENSE  = "GPL"
SCRIPT_DESC     = "add a plain text or evaluated content to item bar"

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

hooks = {}

# ================================[ 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", "")

def unhook(hook):
    global hooks
    if hook in hooks:
        weechat.unhook(hooks[hook])
        del hooks[hook]

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

    # 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

    # check if option is new or simply changed
    if weechat.bar_item_search(option_name):
        weechat.bar_item_update(option_name)
    else:
        weechat.bar_item_new(option_name,'update_item',option_name)

    weechat.bar_item_update(option_name)
    return weechat.WEECHAT_RC_OK

# ================================[ items ]===============================
def create_bar_items():
    ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')

    if not ptr_infolist_option:
        return

    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 weechat.bar_item_search(option_name):
            weechat.bar_item_update(option_name)
        else:
            weechat.bar_item_new(option_name,'update_item',option_name)
        weechat.bar_item_update(option_name)

    weechat.infolist_free(ptr_infolist_option)

def update_item (data, item, window):
    if not data:
        return ""

    # window empty? root bar!
    if not 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 ""

    return substitute_colors(value,window)

# update item
def bar_item_update(signal, callback, callback_data):
    ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')

    if not ptr_infolist_option:
        return

    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

        # 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


# ================================[ subroutines ]===============================
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,{},{},{})
    # substitute colors in output
    return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text)

def check_buffer_type(window, data, value):
    bufpointer = weechat.window_get_pointer(window,"buffer")
    if bufpointer == "":
        return ""

    value = value.split(' ', 1)
    if len(value) <= 1:
        return ""

    # format is : buffer_type (channel,server,private,all) | signal (e.g: buffer_switch)
    channel_type_and_signal = value[0]
    if channel_type_and_signal.find('|') >= 0:
        channel_type = channel_type_and_signal[0:channel_type_and_signal.find("|")]
        signal_type = channel_type_and_signal[channel_type_and_signal.find("|")+1:]
        unhook(data)
        add_hook(signal_type, data)
    else:
        channel_type = value[0]

    value = value[1]

    if channel_type == 'all' or weechat.buffer_get_string(bufpointer,'localvar_type') == channel_type:
        return value
    return ""

# ================================[ main ]===============================
if __name__ == "__main__":
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC,'',''):
            weechat.hook_command(SCRIPT_NAME,SCRIPT_DESC,
                        '',
                        'How to use:\n'
                        '===========\n'
                        'Template:\n'
                        '/set plugins.var.python.text_item.<item_name> <type>|<signal> <${color:name/number}><text>\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'
                        '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',
                        '',
                        '',
                        '')
            version = weechat.info_get("version_number", "") or 0
            create_bar_items()
            weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )