1- # Copyright (c) 2010-2013 by fauno <
[email protected] >
2- # Copyright (c) 2018 by nils_2 <freenode.#weechat>
1+ #
2+ # SPDX-FileCopyrightText: 2010-2013 fauno <
[email protected] >
3+ # SPDX-FileCopyrightText: 2018 Nils Görs <
[email protected] >
4+ #
5+ # SPDX-License-Identifier: GPL-3.0-or-later
36#
47# Bar item showing typing count. Add 'tc' to a bar.
58#
4851# add regular expression for format option
4952# 1.0.1:
5053# fix warning messages when loading script
54+ # 1.0.2:
55+ # drop Python 2 support, remove commented code, fix linter errors
56+ # add SPDX copyright and license tags
5157#
5258# usage:
5359# add [tc] to your weechat.bar.status.items
7581# - buffer whitelist/blacklist
7682# - max chars per buffer (ie, bar item will turn red when count > 140 for identica buffer)
7783
78- from __future__ import print_function
79-
8084SCRIPT_NAME = "typing_counter"
8185SCRIPT_AUTHOR = "fauno <
[email protected] >"
82- SCRIPT_VERSION = "1.0.1 "
86+ SCRIPT_VERSION = "1.0.2 "
8387SCRIPT_LICENSE = "GPL3"
8488SCRIPT_DESC = "Bar item showing typing count and cursor position. Add 'tc' to a bar."
8589
8690import_ok = True
8791
8892try:
8993 import weechat as w
90-
91- except Exception:
94+ except ImportError:
9295 print("This script must be run under WeeChat.")
9396 print("Get WeeChat now at: https://weechat.org/")
9497 import_ok = False
95- try:
96- import os, sys, re
9798
99+ try:
100+ import os
101+ import re
98102except ImportError as message:
99- print(( 'Missing package(s) for %s: %s' % (SCRIPT_NAME, message) ))
103+ print('Missing package(s) for %s: %s' % (SCRIPT_NAME, message))
100104 import_ok = False
101105
102106tc_input_text = ''
122126# regexp to match ${optional string} tags
123127regex_optional_tags=re.compile(r'%\{[^\{\}]+\}')
124128
129+
125130def command_run_cb (data, signal, signal_data):
131+ """Callback for /input xxx commands."""
126132 if tc_options['warn_command'] == '':
127133 return w.WEECHAT_RC_OK
128134 global length, cursor_pos, tc_input_text
@@ -133,16 +139,20 @@ def command_run_cb (data, signal, signal_data):
133139 tc_action_cb()
134140 return w.WEECHAT_RC_OK
135141
142+
136143def tc_bar_item_update (data=None, signal=None, signal_data=None):
137- '''Updates bar item'''
138- '''May be used as a callback or standalone call.'''
144+ """Update bar item.
145+
146+ May be used as a callback or standalone call.
147+ """
139148 global length, cursor_pos, tc_input_text
140149
141150 w.bar_item_update('tc')
142151 return w.WEECHAT_RC_OK
143152
153+
144154def tc_bar_item (data, item, window):
145- ''' Item constructor'''
155+ """ Item constructor."""
146156 # window empty? root bar!
147157 if not window:
148158 window = w.current_window()
@@ -171,18 +181,14 @@ def tc_bar_item (data, item, window):
171181 name = w.buffer_get_string(ptr_buffer, 'localvar_name')
172182 input_line = w.buffer_get_string(ptr_buffer, 'input')
173183 mynick = w.info_get('irc_nick', servername)
174- nick_ptr = w.nicklist_search_nick(ptr_buffer, '', mynick)
175184
176185 # check for a sms message
177186 if channel_type == 'private' and name in tc_options['sms_buffer'].split(","):
178187 # 160 chars for a sms
179188 # 'sms:name:text'
180189 get_sms_text = re.match(r'(s|sms):(.*?:)(.*)', input_line)
181190 if get_sms_text:
182- # if get_sms_text.group(2):
183191 sms_len = len(get_sms_text.group(3))
184- # input_length = len(input_line)
185- # sms_prefix = input_length - sms_len
186192 sms = 160-sms_len
187193 reverse_chars = sms
188194 else:
@@ -204,14 +210,12 @@ def tc_bar_item (data, item, window):
204210 # get host and length from host
205211 elif servername != channelname:
206212 infolist = w.infolist_get('irc_nick', '', '%s,%s,%s' % (servername,channelname,mynick))
207- # w.prnt("","%s.%s.%s.%s" % (servername,channelname,mynick,nick_ptr))
208213 while w.infolist_next(infolist):
209214 host = w.infolist_string(infolist, 'host')
210215 w.infolist_free(infolist)
211216 if host != '':
212217 host = ':%s!%s PRIVMSG %s :' % (mynick,host,channelname)
213218 host_length = len(host)
214- # w.prnt("","%d" % host_length)
215219 reverse_chars = (475 - int(host_length) - length -1) # -1 = return
216220 else:
217221 reverse_chars = (int(tc_options['max_chars']) - length)
@@ -223,13 +227,12 @@ def tc_bar_item (data, item, window):
223227
224228 if reverse_chars == 0:
225229 reverse_chars = "%s" % ("0")
230+ elif reverse_chars < 0:
231+ count_over = "%s%s%s" % (w.color(tc_options['warn_colour']),str(reverse_chars*-1), w.color('default'))
232+ reverse_chars = "%s" % ("0")
233+ tc_action_cb()
226234 else:
227- if reverse_chars < 0:
228- count_over = "%s%s%s" % (w.color(tc_options['warn_colour']),str(reverse_chars*-1), w.color('default'))
229- reverse_chars = "%s" % ("0")
230- tc_action_cb()
231- else:
232- reverse_chars = str(reverse_chars)
235+ reverse_chars = str(reverse_chars)
233236
234237 out_format = tc_options['format']
235238 if tc_options['warn']:
@@ -249,12 +252,11 @@ def tc_bar_item (data, item, window):
249252 else:
250253 out_format = out_format.replace('%R', reverse_chars)
251254 out_format = out_format.replace('%C', count_over)
252- # out_format = out_format.replace('%T', str(tweet))
253- # out_format = out_format.replace('%S', str(sms))
254255 tc_input_text = out_format
255256
256257 return substitute_colors(tc_input_text)
257258
259+
258260def substitute_colors(text):
259261 if int(version) >= 0x00040200:
260262 return w.string_eval_expression(text,{},{},{})
@@ -273,33 +275,34 @@ def init_config():
273275 else:
274276 tc_options[option] = w.config_get_plugin(option)
275277
278+
276279def config_changed(data, option, value):
277280 init_config()
278281 return w.WEECHAT_RC_OK
279282
283+
280284def tc_action_cb():
281285 global tc_options
282286 if tc_options['warn_command']:
283287 if tc_options['warn_command'] == '$bell':
284- f = open('/dev/tty', 'w')
285- f.write('\a')
286- f.close()
288+ with open('/dev/tty', 'w') as f:
289+ f.write('\a')
287290 else:
288291 os.system(tc_options['warn_command'])
289292 return w.WEECHAT_RC_OK
290293
291- if __name__ == "__main__" and import_ok:
292- if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
293- SCRIPT_LICENSE, SCRIPT_DESC,
294- "", ""):
295- version = w.info_get("version_number", "") or 0
296- init_config() # read configuration
297- tc_bar_item_update() # update status bar display
298-
299- w.hook_signal('input_text_changed', 'tc_bar_item_update', '')
300- w.hook_signal('input_text_cursor_moved','tc_bar_item_update','')
301- w.hook_command_run('/input move_previous_char','command_run_cb','')
302- w.hook_command_run('/input delete_previous_char','command_run_cb','')
303- w.hook_signal('buffer_switch','tc_bar_item_update','')
304- w.hook_config('plugins.var.python.' + SCRIPT_NAME + ".*", "config_changed", "")
305- w.bar_item_new('tc', 'tc_bar_item', '')
294+
295+ if (__name__ == "__main__"
296+ and import_ok
297+ and w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "") ):
298+ version = w.info_get("version_number", "") or 0
299+ init_config() # read configuration
300+ tc_bar_item_update() # update status bar display
301+
302+ w.hook_signal('input_text_changed', 'tc_bar_item_update', '')
303+ w.hook_signal('input_text_cursor_moved','tc_bar_item_update','')
304+ w.hook_command_run('/input move_previous_char','command_run_cb','')
305+ w.hook_command_run('/input delete_previous_char','command_run_cb','')
306+ w.hook_signal('buffer_switch','tc_bar_item_update','')
307+ w.hook_config('plugins.var.python.' + SCRIPT_NAME + ".*", "config_changed", "")
308+ w.bar_item_new('tc', 'tc_bar_item', '')
0 commit comments