File: bufsize.py

package info (click to toggle)
weechat-scripts 20120603-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,804 kB
  • sloc: python: 23,181; perl: 16,968; ruby: 1,166; tcl: 199; sh: 8; makefile: 7
file content (148 lines) | stat: -rw-r--r-- 6,707 bytes parent folder | download
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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 by nils_2 <weechatter@arcor.de>
#
# scroll indicator; displaying number of lines below last line, overall lines in buffer, number of current line and percent displayed
#
# 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/>.
#
# This script deletes weechatlog-files by age or size
# YOU ARE USING THIS SCRIPT AT YOUR OWN RISK!
#
# 2012-01-11: nils_2, nesthib (freenode.#weechat)
#       0.1 : initial release
#

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     = "bufsize"
SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
SCRIPT_VERSION  = "0.1"
SCRIPT_LICENSE  = "GPL"
SCRIPT_DESC     = "scroll indicator; displaying number of lines below last line, overall lines in buffer, number of current line and percent displayed"

OPTIONS         = { 'format'            : ('${yellow}%P${default}⋅%{${yellow}%A${default}⇵${yellow}%C${default}/}${yellow}%L',
                                           'format for items to display in bar, possible items: %P = percent indicator, %A = number of lines below last line, %L = lines counter, %C = current line'),                                           # %P = percent, %A = lines_after, %L = lines_count, %C = current
                   }
# ================================[ weechat item ]===============================
# regexp to match ${color} tags
regex_color=re.compile('\$\{[^\{\}]+\}')
# regexp to match ${optional string} tags
regex_optional_tags=re.compile('%\{[^\{\}]+\}')

def show_item (data, item, window):
    bufpointer = weechat.current_buffer()
    if weechat.buffer_get_string(bufpointer,'name') != 'weechat':                         # not weechat core buffer
        if weechat.buffer_get_string(bufpointer,'localvar_type') == '':                     # buffer with free content?
          return ""

    lines_after, lines_count, percent, current_line = count_lines("")
    tags = {'%C': str(current_line),
            '%A': str(lines_after),
            '%L': str(lines_count),
            '%P': str(percent)+"%"}

    bufsize_item = OPTIONS['format']

    # substitute colors in output
    for color_tag in regex_color.findall(bufsize_item):
        bufsize_item = bufsize_item.replace(color_tag, weechat.color(color_tag.lstrip('${').rstrip('}')))

    # replace mandatory tags
    for tag in tags.keys():
        bufsize_item = bufsize_item.replace(tag, tags[tag])

    # replace optional tags
    # %{…} only if lines after (e.g. %A > 0)
    if lines_after > 0:
        for regex_tag in regex_optional_tags.findall(bufsize_item):
            bufsize_item = bufsize_item.replace(regex_tag, regex_tag.lstrip('%{').rstrip('}'))
    else:
        bufsize_item = regex_optional_tags.sub('', bufsize_item)

    return bufsize_item


def count_lines(bufpointer):
    if bufpointer == "":
        bufpointer = weechat.current_buffer()
    hdata_buf = weechat.hdata_get('buffer')
    hdata_lines = weechat.hdata_get('lines')
    lines = weechat.hdata_pointer(hdata_buf, bufpointer, 'lines') # own_lines, mixed_lines
    lines_count = weechat.hdata_integer(hdata_lines, lines, 'lines_count')

    winpointer = weechat.current_window()
    hdata_window = weechat.hdata_get('window')
    hdata_winscroll = weechat.hdata_get('window_scroll')
    window_scroll = weechat.hdata_pointer(hdata_window, winpointer, 'scroll')
    lines_after = weechat.hdata_integer(hdata_winscroll, window_scroll, 'lines_after')
    window_height = weechat.window_get_integer(weechat.current_window(), 'win_chat_height')
    if lines_count > window_height:
        differential = lines_count - window_height
        percent = max(int(round(100. * (differential - lines_after) / differential)), 0)
    else:
        percent = 100
    #weechat.prnt('', " : lines_count "+str(lines_count)+" window_height "+str(window_height)+" lines after "+str(lines_after))
    current_line = lines_count - lines_after
    return lines_after,lines_count,percent, current_line

def update_cb(data, signal, signal_data):
    weechat.bar_item_update(SCRIPT_NAME)
    return weechat.WEECHAT_RC_OK

def clear_cb(data, buffer, command):
    weechat.bar_item_update(SCRIPT_NAME)
    return weechat.WEECHAT_RC_OK

def window_page_cb(data, buffer, command):
    weechat.bar_item_update(SCRIPT_NAME)
    return weechat.WEECHAT_RC_OK

# ================================[ weechat options and description ]===============================
def init_options():
    for option,value in OPTIONS.items():
        if not weechat.config_get_plugin(option):
          weechat.config_set_plugin(option, value[0])
    else:
        OPTIONS[option] = weechat.config_get_plugin(option)
    weechat.config_set_desc_plugin(option, '%s (default: "%s")' % (value[1], value[0]))

def toggle_refresh(pointer, name, value):
    global OPTIONS
    option = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]        # get optionname
    OPTIONS[option] = value                                               # save new value
    weechat.bar_item_update(SCRIPT_NAME)
    return weechat.WEECHAT_RC_OK
# ================================[ main ]===============================
if __name__ == "__main__":
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
        version = weechat.info_get("version_number", "") or 0

        if int(version) >= 0x00030600:
            bar_item = weechat.bar_item_new(SCRIPT_NAME, 'show_item','')
            weechat.bar_item_update(SCRIPT_NAME)
            weechat.hook_signal("buffer_line_added","update_cb","")
            weechat.hook_signal("window_scrolled","window_page_cb","")
            weechat.hook_command_run("/buffer clear*","clear_cb","")
            weechat.hook_command_run("/window page*","window_page_cb","")
            weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )
            init_options()
        else:
            weechat.prnt("","%s%s %s" % (weechat.prefix("error"),SCRIPT_NAME,": needs version 0.3.6 or higher"))