From 798dc7658592734527e921447c13188fcb281fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B5=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=20=D0=94=D0=BC?= =?UTF-8?q?=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 25 Aug 2017 13:01:09 +0300 Subject: [PATCH] add a random picker command --- Default.sublime-commands | 4 ++++ colorsublime-plugin.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Default.sublime-commands b/Default.sublime-commands index c02e661..7649bed 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -10,5 +10,9 @@ { "caption": "Colorsublime: Browse Themes Online", "command": "browse" + }, + { + "caption": "Colorsublime: Install Random Theme", + "command": "install_random" } ] diff --git a/colorsublime-plugin.py b/colorsublime-plugin.py index 21a2b2a..7dd6524 100644 --- a/colorsublime-plugin.py +++ b/colorsublime-plugin.py @@ -1,5 +1,6 @@ import imp import sys +import random import sublime_plugin @@ -8,6 +9,7 @@ from .colorsublime import logger NO_SELECTION = -1 +CONN_ERR_MSG = 'Theme list not found. Please check internet connection or enable debug in settings and report the stack traces.' # Make sure all dependencies are reloaded on upgrade reloader_path = 'Colorsublime.colorsublime.reloader' @@ -29,9 +31,7 @@ def run(self): def display_list(self, themes): self.status.stop() if not themes: - status.error('Theme list not found. Please check internet ' + - 'connection or enable debug in settings and ' + - 'report the stack traces.') + status.error(CONN_ERR_MSG) return self.themes = themes @@ -83,3 +83,27 @@ def uninstall(self, index): class BrowseCommand(sublime_plugin.WindowCommand): def run(self): self.window.run_command('open_url', {'url': 'https://colorsublime.github.io/'}) + +class InstallRandomCommand(sublime_plugin.WindowCommand): + def run(self): + log.info('Running install random command') + self.theme_status = {} + self.status = status.loading('Getting Theme list') + commands.fetch_repo(self.pick_theme) + self.status.stop() + + def pick_theme(self, themes): + if not themes: + status.error(CONN_ERR_MSG) + return + + current_theme = commands.get_current_theme() + while True: + random_theme = random.choice( tuple(themes.values()) ) + if current_theme != random_theme.cache_path.rel: + break + + commands.install_theme(random_theme) + status.message('Theme %s installed!' % random_theme.name) + self.themes = themes +