Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Complete favourites support

  • Loading branch information...
commit 2116626966d79a82e54af6a4dbd19777b679b86f 1 parent 9b42485
@benweier authored
Showing with 55 additions and 23 deletions.
  1. +55 −23 themr.py
View
78 themr.py
@@ -3,8 +3,6 @@
from random import random
class Themr():
- preferences = sublime.load_settings('Preferences.sublime-settings')
-
def load_themes(self):
all_themes = []
@@ -24,7 +22,7 @@ def load_themes(self):
for filename in (filename for filename in zf.namelist() if filename.endswith('.sublime-theme')):
all_themes.append(filename)
- favorite_themes = self.preferences.get('themr_favorites', [])
+ favorite_themes = self.get('themr_favorites', [])
themes = []
for theme in all_themes:
@@ -38,9 +36,8 @@ def load_themes(self):
def list_themes(self, window, theme_list):
themes = [[theme[0], theme[1]] for theme in theme_list]
- the_theme = Themr.get('theme', 'Default.sublime-theme')
- allow_preview = Themr.get('themr_allow_preview', False)
- self.user_selected = False
+ the_theme = self.get('theme', 'Default.sublime-theme')
+ allow_preview = self.get('themr_allow_preview', False)
try:
the_index = [theme[1] for theme in themes].index(the_theme)
except (ValueError):
@@ -48,12 +45,13 @@ def list_themes(self, window, theme_list):
def on_done(index):
if index != -1:
- Themr.set('theme', themes[index][1])
+ self.set('theme', themes[index][1])
sublime.status_message(themes[index][0])
+ self.user_selected = False
def on_select(index):
if self.user_selected == True and allow_preview == True:
- self.preferences.set('theme', themes[index][1]) # use ST's set() so the theme "preview" is not saved to file
+ self.set('theme', themes[index][1])
else:
self.user_selected = True
@@ -62,12 +60,30 @@ def on_select(index):
except:
window.show_quick_panel(themes, on_done)
+ def cycle_themes(self, themes, direction):
+ the_theme = Themr.get('theme', 'Default.sublime-theme')
+ index = 0
+ the_index = [theme[1] for theme in themes].index(the_theme)
+ num_of_themes = len(themes)
+
+ if direction == 'next':
+ index = the_index + 1 if the_index < num_of_themes - 1 else 0
+
+ if direction == 'prev':
+ index = the_index - 1 if the_index > 0 else num_of_themes - 1
+
+ if direction == 'rand':
+ index = int(random() * len(themes))
+
+ Themr.set('theme', themes[index][1])
+ sublime.status_message(themes[index][0])
+
def set(self, setting, value):
- self.preferences.set(setting, value)
+ sublime.load_settings('Preferences.sublime-settings').set(setting, value)
sublime.save_settings('Preferences.sublime-settings')
def get(self, setting, default):
- return self.preferences.get(setting, default)
+ return sublime.load_settings('Preferences.sublime-settings').get(setting, default)
Themr = Themr()
@@ -75,25 +91,41 @@ class ThemrListThemesCommand(sublime_plugin.WindowCommand):
def run(self):
Themr.list_themes(self.window, Themr.load_themes())
+class ThemrListFavoriteThemesCommand(sublime_plugin.WindowCommand):
+ def run(self):
+ Themr.list_themes(self.window, [theme for theme in Themr.load_themes() if theme[2]])
+
+ def is_enabled(self):
+ return len(Themr.get('themr_favorites', [])) >= 1
+
class ThemrCycleThemesCommand(sublime_plugin.WindowCommand):
def run(self, direction):
- themes = Themr.load_themes()
- the_theme = Themr.get('theme', 'Default.sublime-theme')
- index = 0
- the_index = [theme[1] for theme in themes].index(the_theme)
- num_of_themes = len(themes)
+ Themr.cycle_themes(Themr.load_themes(), direction)
- if direction == 'next':
- index = the_index + 1 if the_index < num_of_themes - 1 else 0
+class ThemrCycleFavoriteThemesCommand(sublime_plugin.WindowCommand):
+ def run(self, direction):
+ Themr.cycle_themes([theme for theme in Themr.load_themes() if theme[2]], direction)
- if direction == 'prev':
- index = the_index - 1 if the_index > 0 else num_of_themes - 1
+ def is_enabled(self):
+ return len(Themr.get('themr_favorites', [])) > 1
- if direction == 'rand':
- index = int(random() * len(themes))
+class ThemrFavoriteCurrentThemeCommand(sublime_plugin.WindowCommand):
+ def run(self):
+ favorites = Themr.get('themr_favorites', [])
+ favorites.append(Themr.get('theme', 'Default.sublime-theme'))
+ Themr.set('themr_favorites', favorites)
- Themr.set('theme', themes[index][1])
- sublime.status_message(themes[index][0])
+ def is_enabled(self):
+ return Themr.get('theme', 'Default.sublime-theme') not in Themr.get('themr_favorites', [])
+
+class ThemrUnfavoriteCurrentThemeCommand(sublime_plugin.WindowCommand):
+ def run(self):
+ favorites = Themr.get('themr_favorites', [])
+ favorites.remove(Themr.get('theme', 'Default.sublime-theme'))
+ Themr.set('themr_favorites', favorites)
+
+ def is_enabled(self):
+ return Themr.get('theme', 'Default.sublime-theme') in Themr.get('themr_favorites', [])
class ThemrNextThemeCommand(sublime_plugin.WindowCommand):
def run(self):
Please sign in to comment.
Something went wrong with that request. Please try again.