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#
4649# 1.0 <nils_2@freenode>:
4750# make script python3 compatible
4851# add regular expression for format option
52+ # 1.0.1:
53+ # 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
4957#
5058# usage:
5159# add [tc] to your weechat.bar.status.items
7381# - buffer whitelist/blacklist
7482# - max chars per buffer (ie, bar item will turn red when count > 140 for identica buffer)
7583
76- from __future__ import print_function
77-
7884SCRIPT_NAME = "typing_counter"
7985SCRIPT_AUTHOR = "fauno <[email protected] >" 80- SCRIPT_VERSION = "1.0"
86+ SCRIPT_VERSION = "1.0.2 "
8187SCRIPT_LICENSE = "GPL3"
8288SCRIPT_DESC = "Bar item showing typing count and cursor position. Add 'tc' to a bar."
8389
8490import_ok = True
8591
8692try :
8793 import weechat as w
88-
89- except Exception :
94+ except ImportError :
9095 print ("This script must be run under WeeChat." )
9196 print ("Get WeeChat now at: https://weechat.org/" )
9297 import_ok = False
93- try :
94- import os , sys , re
9598
99+ try :
100+ import os
101+ import re
96102except ImportError as message :
97- print (( 'Missing package(s) for %s: %s' % (SCRIPT_NAME , message ) ))
103+ print ('Missing package(s) for %s: %s' % (SCRIPT_NAME , message ))
98104 import_ok = False
99105
100106tc_input_text = ''
121127regex_optional_tags = re .compile (r'%\{[^\{\}]+\}' )
122128
123129def command_run_cb (data , signal , signal_data ):
130+ """Callback for /input xxx commands."""
124131 if tc_options ['warn_command' ] == '' :
125132 return w .WEECHAT_RC_OK
126133 global length , cursor_pos , tc_input_text
@@ -131,16 +138,20 @@ def command_run_cb (data, signal, signal_data):
131138 tc_action_cb ()
132139 return w .WEECHAT_RC_OK
133140
141+
134142def tc_bar_item_update (data = None , signal = None , signal_data = None ):
135- '''Updates bar item'''
136- '''May be used as a callback or standalone call.'''
143+ """Update bar item.
144+
145+ May be used as a callback or standalone call.
146+ """
137147 global length , cursor_pos , tc_input_text
138148
139149 w .bar_item_update ('tc' )
140150 return w .WEECHAT_RC_OK
141151
152+
142153def tc_bar_item (data , item , window ):
143- ''' Item constructor'''
154+ """ Item constructor."""
144155 # window empty? root bar!
145156 if not window :
146157 window = w .current_window ()
@@ -169,18 +180,14 @@ def tc_bar_item (data, item, window):
169180 name = w .buffer_get_string (ptr_buffer , 'localvar_name' )
170181 input_line = w .buffer_get_string (ptr_buffer , 'input' )
171182 mynick = w .info_get ('irc_nick' , servername )
172- nick_ptr = w .nicklist_search_nick (ptr_buffer , '' , mynick )
173183
174184 # check for a sms message
175185 if channel_type == 'private' and name in tc_options ['sms_buffer' ].split ("," ):
176186 # 160 chars for a sms
177187 # 'sms:name:text'
178188 get_sms_text = re .match (r'(s|sms):(.*?:)(.*)' , input_line )
179189 if get_sms_text :
180- # if get_sms_text.group(2):
181190 sms_len = len (get_sms_text .group (3 ))
182- # input_length = len(input_line)
183- # sms_prefix = input_length - sms_len
184191 sms = 160 - sms_len
185192 reverse_chars = sms
186193 else :
@@ -202,14 +209,12 @@ def tc_bar_item (data, item, window):
202209 # get host and length from host
203210 elif servername != channelname :
204211 infolist = w .infolist_get ('irc_nick' , '' , '%s,%s,%s' % (servername ,channelname ,mynick ))
205- # w.prnt("","%s.%s.%s.%s" % (servername,channelname,mynick,nick_ptr))
206212 while w .infolist_next (infolist ):
207213 host = w .infolist_string (infolist , 'host' )
208214 w .infolist_free (infolist )
209215 if host != '' :
210216 host = ':%s!%s PRIVMSG %s :' % (mynick ,host ,channelname )
211217 host_length = len (host )
212- # w.prnt("","%d" % host_length)
213218 reverse_chars = (475 - int (host_length ) - length - 1 ) # -1 = return
214219 else :
215220 reverse_chars = (int (tc_options ['max_chars' ]) - length )
@@ -221,13 +226,12 @@ def tc_bar_item (data, item, window):
221226
222227 if reverse_chars == 0 :
223228 reverse_chars = "%s" % ("0" )
229+ elif reverse_chars < 0 :
230+ count_over = "%s%s%s" % (w .color (tc_options ['warn_colour' ]),str (reverse_chars * - 1 ), w .color ('default' ))
231+ reverse_chars = "%s" % ("0" )
232+ tc_action_cb ()
224233 else :
225- if reverse_chars < 0 :
226- count_over = "%s%s%s" % (w .color (tc_options ['warn_colour' ]),str (reverse_chars * - 1 ), w .color ('default' ))
227- reverse_chars = "%s" % ("0" )
228- tc_action_cb ()
229- else :
230- reverse_chars = str (reverse_chars )
234+ reverse_chars = str (reverse_chars )
231235
232236 out_format = tc_options ['format' ]
233237 if tc_options ['warn' ]:
@@ -247,12 +251,11 @@ def tc_bar_item (data, item, window):
247251 else :
248252 out_format = out_format .replace ('%R' , reverse_chars )
249253 out_format = out_format .replace ('%C' , count_over )
250- # out_format = out_format.replace('%T', str(tweet))
251- # out_format = out_format.replace('%S', str(sms))
252254 tc_input_text = out_format
253255
254256 return substitute_colors (tc_input_text )
255257
258+
256259def substitute_colors (text ):
257260 if int (version ) >= 0x00040200 :
258261 return w .string_eval_expression (text ,{},{},{})
@@ -271,33 +274,34 @@ def init_config():
271274 else :
272275 tc_options [option ] = w .config_get_plugin (option )
273276
277+
274278def config_changed (data , option , value ):
275279 init_config ()
276280 return w .WEECHAT_RC_OK
277281
282+
278283def tc_action_cb ():
279284 global tc_options
280285 if tc_options ['warn_command' ]:
281286 if tc_options ['warn_command' ] == '$bell' :
282- f = open ('/dev/tty' , 'w' )
283- f .write ('\a ' )
284- f .close ()
287+ with open ('/dev/tty' , 'w' ) as f :
288+ f .write ('\a ' )
285289 else :
286290 os .system (tc_options ['warn_command' ])
287291 return w .WEECHAT_RC_OK
288292
289- if __name__ == "__main__" and import_ok :
290- if w . register ( SCRIPT_NAME , SCRIPT_AUTHOR , SCRIPT_VERSION ,
291- SCRIPT_LICENSE , SCRIPT_DESC ,
292- "" , "" ):
293- version = w .info_get ("version_number" , "" ) or 0
294- init_config () # read configuration
295- tc_bar_item_update () # update status bar display
296-
297- w .hook_signal ('input_text_changed' , 'tc_bar_item_update' , '' )
298- w .hook_signal ('input_text_cursor_moved' ,'tc_bar_item_update' ,'' )
299- w .hook_command_run ('/input move_previous_char' ,'command_run_cb' ,'' )
300- w .hook_command_run ('/input delete_previous_char' ,'command_run_cb' ,'' )
301- w .hook_signal ('buffer_switch' ,'tc_bar_item_update' ,'' )
302- w .hook_config ('plugins.var.python.' + SCRIPT_NAME + ".*" , "config_changed" , "" )
303- w .bar_item_new ('tc' , 'tc_bar_item' , '' )
293+
294+ if ( __name__ == "__main__"
295+ and import_ok
296+ and w . register ( SCRIPT_NAME , SCRIPT_AUTHOR , SCRIPT_VERSION , SCRIPT_LICENSE , SCRIPT_DESC , "" , "" ) ):
297+ version = w .info_get ("version_number" , "" ) or 0
298+ init_config () # read configuration
299+ tc_bar_item_update () # update status bar display
300+
301+ w .hook_signal ('input_text_changed' , 'tc_bar_item_update' , '' )
302+ w .hook_signal ('input_text_cursor_moved' ,'tc_bar_item_update' ,'' )
303+ w .hook_command_run ('/input move_previous_char' ,'command_run_cb' ,'' )
304+ w .hook_command_run ('/input delete_previous_char' ,'command_run_cb' ,'' )
305+ w .hook_signal ('buffer_switch' ,'tc_bar_item_update' ,'' )
306+ w .hook_config ('plugins.var.python.' + SCRIPT_NAME + ".*" , "config_changed" , "" )
307+ w .bar_item_new ('tc' , 'tc_bar_item' , '' )
0 commit comments