├── README.md └── theme-switcher.py /README.md: -------------------------------------------------------------------------------- 1 | ## Theme Switcher 2 | 3 | This is a Sublime Text 3 Plugin to switch Themes directly from the GUI like how Color Schemes can be changed. 4 | 5 | This Plugin is in BETA status currently. It has been tested and developed on Sublime Text 3065 Linux x64 on Ubuntu 14.04. 6 | 7 | ### Why Theme Switcher? 8 | 9 | Sublime Text has many themes available for download from Package Control and many of them are very cool looking. But, there is a slight problem with the entire theme system on activating themes. 10 | 11 | For example, let's say I am browsing for Themes on Package Control. 12 | 13 | I searched for Themes and I found the "Flatland" theme. I installed it directly from Sublime Text and then I want to try the theme out. 14 | 15 | Now the only way to do so, is to open my Preferences file, and modify it to activate Flatland. 16 | 17 | So, in my prefernces file, I modified the `theme` key to become `Flatland.sublime-theme`. But the Theme did not activate. 18 | 19 | Wait, why? Because the theme file is named `Flatland Dark.sublime-theme`. So to activate the Theme, I have to go to the Flatland GitHub page and then search for Instructions. 20 | 21 | So, to just activate a Theme, I have to do a lot of work. 22 | 23 | That's where Theme Switcher jumps in. Theme Switcher automatically adds your Themes to the Menu from which you can easily Activate your Themes. Theme Switcher also displays multiple variants of Themes so that you can use them all at ease. For example, all variants of the "Afterglow" Theme can be accessed and activated from the GUI itself. 24 | 25 | ### Usage 26 | 27 | After Installing the plugin, Go to Preferences - Theme - Your Desired Theme. 28 | 29 | For example to activate the Flatland theme, after installation, Go to Preferences - Theme - Theme Flatland - and then click on Flatland Dark. 30 | 31 | Note that you might have to reload Sublime Text (by closing and reopening) to get completely activate the Theme. This is native Sublime Text behaviour and cannot be changed. 32 | 33 | ###For Package Developers 34 | 35 | If you are developing a Theme for Sublime Text, then your Package name (as given in the Package Control Repoistory) must have the word "Theme" (with a capital T ) in it so that Theme Switcher can recognise your Theme. 36 | 37 | Also, the current version only supports ST3 and Themes installed by Package Control, although "User" themes support is coming too. 38 | 39 | ### About 40 | 41 | Created by Pradipta (geekpradd). Copyright 2015. MIT Licensed. 42 | -------------------------------------------------------------------------------- /theme-switcher.py: -------------------------------------------------------------------------------- 1 | import sublime 2 | import sublime_plugin 3 | import os 4 | import zipfile 5 | import json 6 | import sys 7 | import shutil 8 | import threading 9 | MENU_BASE = """ 10 | [ 11 | { 12 | "id": "preferences", 13 | "children": 14 | [{ 15 | "caption": "Themes", 16 | "mnemonic": "t", 17 | "id": "themes" 18 | 19 | }] 20 | } 21 | ] 22 | """.strip() 23 | 24 | def create_menu(): 25 | PACKAGE_FOLDER = os.path.join(sublime.packages_path(), "theme switch") 26 | if not os.path.isdir(PACKAGE_FOLDER): 27 | os.makedirs(PACKAGE_FOLDER) 28 | MENU_LOCATION = os.path.join(PACKAGE_FOLDER, "Main.sublime-menu") 29 | if not os.path.isfile(MENU_LOCATION): 30 | open(MENU_LOCATION, "w").close() 31 | modify_menu(MENU_LOCATION) 32 | 33 | 34 | def plugin_loaded(): 35 | create_menu() 36 | 37 | 38 | def plugin_unloaded(): 39 | PACKAGE_FOLDER = os.path.join(sublime.packages_path(), "theme switch") 40 | MENU_LOCATION = os.path.join(PACKAGE_FOLDER, "Main.sublime-menu") 41 | os.remove(MENU_LOCATION) 42 | 43 | def get_path(): 44 | return os.path.join(os.path.dirname(sublime.packages_path()), "Installed Packages") 45 | 46 | def is_theme_file(name): 47 | return "sublime-theme" == name.split('.')[-1] 48 | 49 | def sanitized(n): 50 | return n.replace("." + n.split('.')[-1],"") 51 | 52 | def read(file): 53 | with open(file, 'r') as f: 54 | return f.read() 55 | 56 | def menufy(dic): 57 | new = [] 58 | for key in dic: 59 | temp = {} 60 | temp['caption'] = key 61 | temp['children'] = [{"caption":a, "command":"themeswitch", "args": {"name":a+".sublime-theme"}} for a in dic[key]] 62 | new.append(temp) 63 | new.append({"caption": "Refresh Theme Cache", "command": "refreshthemes"}) 64 | print(new) 65 | return new 66 | 67 | def get_files(): 68 | theme_files = list(filter(lambda a: "Theme " in a, os.listdir(get_path()))) 69 | collection = {} 70 | for theme in theme_files: 71 | Zip = zipfile.ZipFile(os.path.join(get_path(),theme)) 72 | files = list(map(sanitized, list(filter(is_theme_file, Zip.namelist())))) 73 | if len(files): 74 | collection[sanitized(theme)] = files 75 | user_path = os.path.join(sublime.packages_path(), "User") 76 | user_themes = list(filter(is_theme_file, os.listdir(user_path))) 77 | if len(user_themes): 78 | collection["User"] = user_themes 79 | return collection 80 | 81 | def modify_menu(LOCATION): 82 | files = get_files() 83 | 84 | menu = json.loads(MENU_BASE) 85 | menu[0]['children'][0]['children'] = menufy(files) 86 | with open(LOCATION,"w") as f: 87 | f.write(json.dumps(menu,indent=4, sort_keys=True)) 88 | 89 | class themeswitchCommand(sublime_plugin.WindowCommand): 90 | def run(self,name): 91 | settings = sublime.load_settings("Preferences.sublime-settings") 92 | settings.set('theme',name) 93 | sublime.save_settings("Preferences.sublime-settings") 94 | 95 | class refreshthemesCommand(sublime_plugin.WindowCommand): 96 | def run(self): 97 | create_menu() --------------------------------------------------------------------------------