2727stamp = start_time
2828
2929
30- def base_activity (started = False ):
30+ def base_activity (started = False ):
3131 activity = {
3232 'assets' : {
3333 'small_image' : 'afk' ,
@@ -80,7 +80,7 @@ def base_activity(started = False):
8080 'png,jpg,jpeg,jfif,gif,webp' : 'image' ,
8181 'py,pyx' : 'python' ,
8282 'p,sp' : 'pawn' ,
83- 'r,rda,rdata,rds,rhistory' : 'r' ,
83+ 'r,rda,rdata,rds,rhistory' : 'r' ,
8484 'rb' : 'ruby' ,
8585 'rs' : 'rust' ,
8686 'sh,bat' : 'shell' ,
@@ -142,10 +142,9 @@ def base_activity(started = False):
142142
143143def get_icon (file , ext , _scope ):
144144 main_scope = _scope .split ()[0 ]
145- base_scope = main_scope .split ('.' )[0 ]
146145 try :
147146 sub_scope = '.' .join (main_scope .split ()[0 ].split ('.' )[1 ::])
148- except :
147+ except Exception :
149148 sub_scope = ''
150149
151150 for _icon in ICONS :
@@ -160,7 +159,8 @@ def get_icon(file, ext, _scope):
160159 else :
161160 icon = 'unknown'
162161
163- if file == 'LICENSE' : icon = 'license'
162+ if file == 'LICENSE' :
163+ icon = 'license'
164164 logger .debug ('Using icon "%s" for file %s (scope: %s)' , icon , file , main_scope )
165165
166166 return 'https://raw.githubusercontent.com/Snazzah/SublimeDiscordRP/master/icons/lang-%s.png' % icon
@@ -181,7 +181,7 @@ def sizehf(num):
181181 return "%.1f%s%s" % (num , 'Yi' , 'B' )
182182
183183
184- def handle_activity (view , is_write = False , idle = False ):
184+ def handle_activity (view ):
185185 window = view .window ()
186186 entity = view .file_name ()
187187 if not (ipc and window and entity ):
@@ -199,8 +199,10 @@ def handle_activity(view, is_write=False, idle=False):
199199
200200 logger .info ('Updating activity' )
201201
202- try : extension = entity .split ('.' )[len (entity .split ('.' )) - 1 ]
203- except : extension = ''
202+ try :
203+ extension = entity .split ('.' )[len (entity .split ('.' )) - 1 ]
204+ except Exception :
205+ extension = ''
204206
205207 language = os .path .splitext (os .path .basename (view .settings ().get ('syntax' )))[0 ]
206208 if len (language ) < 2 :
@@ -229,7 +231,6 @@ def handle_activity(view, is_write=False, idle=False):
229231 main_scope = view .scope_name (0 )
230232 icon = get_icon (format_dict ['file' ], format_dict ['extension' ], main_scope )
231233 if settings .get ('big_icon' ):
232- act ['assets' ]['small_image' ] = 'afk' if idle == True else act ['assets' ]['small_image' ]
233234 act ['assets' ]['small_text' ] = act ['assets' ]['small_text' ]
234235 act ['assets' ]['large_image' ] = icon
235236 act ['assets' ]['large_text' ] = language
@@ -257,6 +258,7 @@ def handle_activity(view, is_write=False, idle=False):
257258def reset_activity (started = False ):
258259 if not ipc :
259260 return
261+ global last_file
260262 last_file = ''
261263 try : ipc .set_activity (base_activity (started ))
262264 except OSError as e : handle_error (e )
@@ -288,20 +290,20 @@ def git_config_parser(path):
288290 if line .startswith ("[" ):
289291 res = re .search ('"(.*)"' , line )
290292 if res is not None :
291- sec_name = re .sub ('\[|"(.*)"|\]' , "" , line )
293+ sec_name = re .sub (r '\[|"(.*)"|\]' , "" , line )
292294 subsec_name = res .group (1 )
293295 if sec_name not in obj :
294296 obj [sec_name ] = {}
295297
296298 obj [sec_name ][subsec_name ] = {}
297299 current_section = [sec_name , subsec_name ]
298300 else :
299- sec_name = re .sub ("\[|\]" , "" , line )
301+ sec_name = re .sub (r "\[|\]" , "" , line )
300302 obj [sec_name ] = {}
301303 current_section = [sec_name ]
302304
303305 else :
304- parts = re .sub ("\t |\0 " , "" ,line ).split ("=" )
306+ parts = re .sub ("\t |\0 " , "" , line ).split ("=" )
305307 if len (current_section ) < 2 :
306308 obj [current_section [0 ]][parts [0 ]] = parts [1 ]
307309 else :
@@ -311,7 +313,7 @@ def git_config_parser(path):
311313
312314
313315def get_git_url_from_config (folder ):
314- gitcfg_path = folder + "/.git/config"
316+ gitcfg_path = folder + "/.git/config"
315317 if os .path .exists (gitcfg_path ):
316318 cfg = git_config_parser (gitcfg_path )
317319 if "remote" in cfg and "origin" in cfg ["remote" ]:
@@ -321,7 +323,7 @@ def get_git_url_from_config(folder):
321323
322324
323325def parse_git_url (url ):
324- url = re .sub ("\.git\n ?$" , "" , url )
326+ url = re .sub (r "\.git\n?$" , "" , url )
325327 if url .startswith ("https" ):
326328 return url
327329
@@ -342,7 +344,7 @@ def get_git_url(entity):
342344 si = subprocess .STARTUPINFO ()
343345 si .dwFlags = subprocess .SW_HIDE | subprocess .STARTF_USESHOWWINDOW
344346 url = subprocess .check_output (["git" , "-C" , folder , "remote" , "get-url" , "origin" ], universal_newlines = True , startupinfo = si )
345- except :
347+ except Exception :
346348 url = get_git_url_from_config (folder )
347349
348350 if url is not None :
@@ -383,12 +385,18 @@ def find_folder_containing_file(folders, current_file):
383385
384386
385387def is_view_active (view ):
386- if view :
387- active_window = sublime .active_window ()
388- if active_window :
389- active_view = active_window .active_view ()
390- if active_view :
391- return active_view .buffer_id () == view .buffer_id ()
388+ if not view :
389+ return False
390+
391+ if int (sublime .version ()) > 4000 :
392+ return view .element () is None
393+
394+ active_window = sublime .active_window ()
395+ if active_window :
396+ active_view = active_window .active_view ()
397+ if active_view :
398+ return active_view .buffer_id () == view .buffer_id ()
399+
392400 return False
393401
394402
@@ -448,27 +456,38 @@ def disconnect():
448456 ipc = None
449457
450458
451- class DRPListener ( sublime_plugin . EventListener ):
459+ deactivate_bounce_count = 0
452460
453- def on_post_save_async (self , view ):
454- handle_activity (view , is_write = True )
455461
456- def on_modified_async (self , view ):
457- if is_view_active (view ):
458- if view .file_name () != last_file :
459- logger .info ("Setting presence to file %r from %r" , view .file_name (), last_file )
460- handle_activity (view )
462+ def _bounce_deactivate (expected_bounce_count ):
463+ if deactivate_bounce_count == expected_bounce_count :
464+ logger .debug ("Idle timeout reached" )
465+ reset_activity ()
466+
467+
468+ class DRPListener (sublime_plugin .EventListener ):
461469
462470 def on_activated_async (self , view ):
471+ global last_file
472+ global deactivate_bounce_count
473+ deactivate_bounce_count += 1
474+
475+ if not is_view_active (view ) or view .file_name () == last_file :
476+ return
477+ logger .debug ("Setting presence to file %r from %r" , view .file_name (), last_file )
463478 handle_activity (view )
464479
465- def on_close (self , view ):
466- active_window = sublime .active_window ()
467- if active_window :
468- active_view = active_window .active_view ()
469- if active_view : handle_activity (active_view )
470- else : reset_activity ()
471- else : reset_activity ()
480+ def on_post_save_async (self , view ):
481+ # Refresh template variables
482+ handle_activity (view )
483+
484+ def on_deactivated_async (self , _view ):
485+ global deactivate_bounce_count
486+ deactivate_bounce_count += 1
487+
488+ timeout = settings .get ('idle_timeout' , 0 ) * 1000
489+ if timeout :
490+ sublime .set_timeout_async (partial (_bounce_deactivate , deactivate_bounce_count ), timeout )
472491
473492
474493class DiscordrpConnectCommand (sublime_plugin .ApplicationCommand ):
0 commit comments