forked from ggordan/GutterColor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutter_color.py
58 lines (45 loc) · 1.71 KB
/
gutter_color.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
from .file import File
from sublime_plugin import EventListener, WindowCommand
from sublime import load_settings
def clear_cache(force = False):
"""
If the folder exists, and has more than 5MB of icons in the cache, delete
it to clear all the icons then recreate it.
"""
from os.path import getsize, join, isfile, exists
from os import makedirs, listdir
from sublime import cache_path
from shutil import rmtree
# The icon cache path
icon_path = join(cache_path(), "GutterColor")
# The maximum amount of space to take up
limit = 5242880 # 5 MB
if exists(icon_path):
size = sum(getsize(join(icon_path, f)) for f in listdir(icon_path) if isfile(join(icon_path, f)))
if force or (size > limit): rmtree(icon_path)
if not exists(icon_path): makedirs(icon_path)
def plugin_loaded():
clear_cache()
class GutterColorClearCacheCommand(WindowCommand):
def run(self):
clear_cache(True)
class GutterColorEventListener(EventListener):
"""Scan the view when it gains focus, and when it is saved."""
def on_activated_async(self, view):
"""Scan file when it gets focus"""
if syntax(view) in settings().get('supported_syntax'):
File(view)
def on_modified(self, view):
"""Scan file when it is modified"""
if syntax(view) in settings().get('supported_syntax'):
File(view, 'update')
def on_pre_save_async(self, view):
"""Scan file before it is saved"""
if syntax(view) in settings().get('supported_syntax'):
File(view, 'update')
def settings():
"""Shortcut to the settings"""
return load_settings("GutterColor.sublime-settings")
def syntax(view):
"""Return the view syntax"""
return view.settings().get('syntax').split('/')[-1].split('.')[0].lower()