-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufferlist.py
212 lines (175 loc) · 8.44 KB
/
bufferlist.py
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
# -*- coding: utf-8 -*-
import sys
# Make sure we're running in weechat
try:
import weechat
except ImportError:
print('Please run in weechat')
sys.exit()
SCRIPT_NAME = 'squigzlist'
SCRIPT_AUTHOR = 'squigz'
SCRIPT_DESC = 'Another buffer list script'
# Function for retrieving nick prefix (@, +, etc)
def get_nick_prefix(pointer):
nick = weechat.buffer_get_string(pointer, "localvar_nick")
nick_pointer = weechat.nicklist_search_nick(pointer, "", nick)
prefix = weechat.nicklist_nick_get_string(pointer, nick_pointer, "prefix")
return prefix
# Main function that builds the list, set as the callback for the bar item
def build_list(data, item, window):
# Setup variables
# First retrieve the `hdata`s, then get relevant lists
buffer_hdata = weechat.hdata_get('buffer')
server_hdata = weechat.hdata_get('irc_server')
hotlist_hdata = weechat.hdata_get('hotlist')
buffer_pointer = weechat.hdata_get_list(buffer_hdata, 'gui_buffers')
server_pointer = weechat.hdata_get_list(server_hdata, 'irc_servers')
bar_width = weechat.config_integer(weechat.config_get('weechat.bar.buffers.size'))
buflist = ''
# Start looping through the buffers
while buffer_pointer:
# Grab the hotlist and priority level for the current buffer
hotlist_pointer = weechat.hdata_pointer(buffer_hdata, buffer_pointer, "hotlist")
if hotlist_pointer:
priority = weechat.hdata_integer(hotlist_hdata, hotlist_pointer, 'priority')
else:
priority = 0
# Setup the info variables for the current buffer
nick = weechat.buffer_get_string(buffer_pointer, "localvar_nick")
name = weechat.buffer_get_string(buffer_pointer, "short_name")
full_name = weechat.buffer_get_string(buffer_pointer, "name")
plugin = weechat.buffer_get_string(buffer_pointer, "plugin")
buffer_type = weechat.buffer_get_string(buffer_pointer, "localvar_type")
server = weechat.buffer_get_string(buffer_pointer, 'localvar_server')
icon_color = weechat.buffer_get_string(buffer_pointer, "localvar_icon_color") or 0
current_buffer = 1 if weechat.current_buffer() == buffer_pointer else 0
# Setup info variables for next buffer
next_pointer = weechat.hdata_move(buffer_hdata, buffer_pointer, 1)
next_type = weechat.buffer_get_string(next_pointer, "plugin")
# Start building!
# Draw icons for non-IRC buffers - core, script, fset, etc
# You can set an `icon_color` localvar to override the `color.icon` option for a particular buffer when it's active
# This isn't exactly ideal. Another option would be to use a localvar for each buffer that gets an icon, and then do a check for that
if plugin != 'irc':
if current_buffer:
if icon_color:
buflist += weechat.color(icon_color)
else:
buflist += weechat.color(option_values['color.icon'])
else:
buflist += weechat.color(option_values['color.default_fg'])
buflist += "● "
buflist += weechat.color(option_values['color.default_fg'])
# Add a newline if the next buffer is the start of IRC buffers
if next_type == 'irc':
buflist += '\n'
# Start adding other buffers
else:
# Add separator between servers
if option_values['look.server_separator'] == '1' and buffer_type == 'server':
buflist += '─' * bar_width + '\n'
# Print the appropriate color for the current buffer, as well as an icon for the current buffer
if current_buffer:
buflist += weechat.color(option_values['color.current_fg'])
buflist += "●"
elif priority == 1:
buflist += weechat.color(option_values['color.hotlist_message'])
elif priority == 2:
buflist += weechat.color(option_values['color.hotlist_private'])
elif priority == 3:
buflist += weechat.color(option_values['color.hotlist_highlight'])
else:
buflist += weechat.color(option_values['color.default_fg'])
# Spacing between icon and name
if current_buffer:
buflist += ' '
else:
buflist += ' '
if buffer_type != 'server':
buflist += ' '
if buffer_type == 'private':
buflist += ' '
# Add the nick prefix (@, +, etc)
nick_prefix = get_nick_prefix(buffer_pointer)
buflist += nick_prefix
# Add the buffer name
buflist += name
# Add nick modes next to server buffers, if any are set
if buffer_type == 'server':
# Search for and retrieve a pointer for the server
pointer = weechat.hdata_search(server_hdata, server_pointer, '${irc_server.name} == ' + server, 1)
# Then get the nick modes for that server
nick_modes = weechat.hdata_string(server_hdata, pointer, 'nick_modes')
if nick_modes:
buflist += ' (+{})'.format(nick_modes)
# Add the newline after each buffer
buflist += '\n'
# Move to the next buffer
buffer_pointer = weechat.hdata_move(buffer_hdata, buffer_pointer, 1)
# All done. Return the list
return buflist
# Function for updating the bar on certain signals
def signal_handler(data, signal, signal_data):
weechat.bar_item_update('squigzlist')
return weechat.WEECHAT_RC_OK
# Function for updating variables and reloading bar when config options change
def config_handler(data, option, value):
# Splits the full config option name (plugins.var.python...) to something easier to work with
option = '.'.join(option.split('.')[-2:])
option_values[option] = value
weechat.bar_item_update('squigzlist')
return weechat.WEECHAT_RC_OK
# Register the script
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, '', '', SCRIPT_DESC, '', ''):
# Start configuring options
options = {
'look.display_conditions': (
'',
'Conditions to display buffers (see /help eval)'),
'look.server_separator': (
'1',
'Draw separator between servers'),
'color.default_fg': (
'237',
'Default foreground color for buffers'),
'color.default_bg': (
'default',
'Default background color for buffers'),
'color.current_fg': (
'lightblue',
'Foreground for currently selected buffer'),
'color.current_bg': (
'default',
'Background for currently selected buffer'),
'color.hotlist_low': (
'default',
'Color for buffers in hotlist with \'low\' level'),
'color.hotlist_message': (
'white',
'Color for buffers in hotlist with \'message\' level'),
'color.hotlist_private': (
'red',
'Color for buffers in hotlist with \'private\' level'),
'color.hotlist_highlight': (
'red',
'Color for buffers in hotlist with \'highlight\' priority'),
'color.icon': (
'lightblue',
'Color for icons in list (non-IRC buffers)'),
}
# Loop through and set options with default values and descriptions, and load values into dict to use
option_values = {}
for option, values in options.items():
# First ensure it's not already set
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, values[0])
weechat.config_set_desc_plugin(option, values[1])
option_values[option] = weechat.config_get_plugin(option)
# Setup callback for config option updates
weechat.hook_config('plugins.var.python.' + SCRIPT_NAME + '.*', 'config_handler', '')
# Create bar item
weechat.bar_item_new('squigzlist', 'build_list', '')
# Hook various signals on which to refresh the list
signals = ['buffer_opened', 'buffer_closed', 'buffer_merged', 'buffer_unmerged', 'buffer_moved', 'buffer_renamed', 'buffer_switch', 'buffer_hidden', 'buffer_unhidden', 'buffer_localvar_added', 'buffer_localvar_changed', 'hotlist_changed']
for signal in signals:
weechat.hook_signal(signal, 'signal_handler', '')