Skip to content

Commit

Permalink
Select new theme works, using a small hack though. General theme meth…
Browse files Browse the repository at this point in the history
…ods moved to functions.
  • Loading branch information
a-sk committed Feb 12, 2012
1 parent 1b00031 commit d59b5f1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 deletions.
7 changes: 6 additions & 1 deletion css-colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from livecss.state import State
from livecss.theme import theme

from livecss.debug import log


class CssColorizeCommand(sublime_plugin.TextCommand):
def run(self, edit, erase_state=False):
Expand All @@ -22,10 +24,12 @@ def run(self, edit):
class EventManager(sublime_plugin.EventListener):
def __init__(self):
# before anything
# theme.set(theme.uncolorized_path)
clean_junk()
# print "settings", uncolorized_path

def on_load(self, view):
# theme.on_select_new_theme(lambda: colorize_on_change(view))
theme.on_select_new_theme(lambda: selected_new_theme(view))
if need_colorization(view):
colorize_file(view, True)

Expand All @@ -38,6 +42,7 @@ def on_modified(self, view):

def on_activated(self, view):
if file_is_css(view):
log("on_activated was called")
generate_menu(view)

# set file's own theme path
Expand Down
19 changes: 6 additions & 13 deletions livecss/colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fast_theme_generation import generate_theme_file
from helpers import escape
from state import State
from theme import theme
from theme import *
from file_operatios import rm_theme
# from debug import profile

Expand All @@ -23,21 +23,15 @@
def colorize_file(view, erase_state=False):
"""Highlight color definition regions
by it's real colors
@param {sublime.view} view
@param {bool} erase_state: use saved state for this file
"""
#TODO: add erase_state handling
print "Colorize was called"

colored_regions = get_colored_regions(view)
colors = get_colors(view, colored_regions)
if not colors:
return

state = State(view, colors, colored_regions)
if erase_state:
print "State was errased"
state.erase()

if not state.is_dirty:
Expand All @@ -47,7 +41,7 @@ def colorize_file(view, erase_state=False):

if state.need_generate_theme_file:

colorized_theme = generate_theme(theme.uncolorized_path, colors)
colorized_theme = generate_theme(uncolorized_path(theme.abspath), colors)
theme.set(colorized_theme)

# remove previously used theme if any
Expand All @@ -65,7 +59,7 @@ def uncolorize_file(view):
"""

clear_css_regions(view)
theme.set(theme.uncolorized_path)
theme.set(uncolorized_path(theme.abspath))
state = State(view)

rm_theme(state.theme_path)
Expand Down Expand Up @@ -118,12 +112,11 @@ def generate_theme(theme_path, colors):
"""

colorized_theme_path = theme.colorized_path
colorized_theme_path = colorized_path(theme.abspath)

new_colors = (template(color) for color in set(colors))
generate_theme_file(theme_path, new_colors, colorized_theme_path)
from os.path import basename
print "Theme was generated ", basename(colorized_theme_path)

return colorized_theme_path


Expand Down
15 changes: 15 additions & 0 deletions livecss/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ def __contains__(self, attr):
escape = lambda s: "\'" + s + "\'"

make_eq = lambda x: lambda y: x == y


## {{{ http://code.activestate.com/recipes/576693/ (r9)
# Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
# Passes Python2.7's test suite and incorporates all the latest updates.

try:
from thread import get_ident as _get_ident
except ImportError:
from dummy_thread import get_ident as _get_ident

try:
from _abcoll import KeysView, ValuesView, ItemsView
except ImportError:
pass
62 changes: 32 additions & 30 deletions livecss/theme.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from os.path import join, basename, dirname, normpath, relpath, exists
from os.path import basename, normpath, relpath, exists
import os.path
from random import randint
import re

import sublime

PACKAGES_PATH = sublime.packages_path()
SUBLIME_PATH = dirname(PACKAGES_PATH)
SUBLIME_PATH = os.path.dirname(PACKAGES_PATH)


def print_(msg):
print msg

#TODO: add fallbacks on errors
class theme(object):
"""Global object represents ST color scheme """

_settings = sublime.load_settings('Base File.sublime-settings')
_settings_file = 'Preferences.sublime-settings'
_settings = sublime.load_settings(_settings_file)
prefix = 'Colorized-'

class __metaclass__(type):
Expand All @@ -24,7 +23,7 @@ def abspath(cls):
theme_path = cls._settings.get('color_scheme') or ""

if theme_path.startswith('Packages'):
theme_path = join(SUBLIME_PATH, theme_path)
theme_path = os.path.join(SUBLIME_PATH, theme_path)

return normpath(theme_path)

Expand All @@ -34,7 +33,7 @@ def relpath(cls):

@property
def dirname(cls):
return dirname(cls.abspath)
return os.path.dirname(cls.abspath)

@property
def name(cls):
Expand All @@ -43,34 +42,37 @@ def name(cls):
def set(cls, theme_path):
"""theme: abs or relpath to PACKAGES_PATH"""
if exists(theme_path):
print "Setting ", theme_path
cls._settings.set('color_scheme', theme_path)

def on_select_new_theme(cls, callback):
cls._settings.add_on_change('color_scheme', callback)

@property
def is_colorized(self):
if self.name.startswith(self.prefix):
return True

@property
def colorized_path(self):
return join(self.dirname, self.colorized_name)
def is_colorized(name):
if name.startswith(theme.prefix):
return True

@property
def colorized_name(self):
random = str(randint(1, 10 ** 15)) + '-'
return self.prefix + random + self.uncolorized_name

@property
def uncolorized_name(self):
if self.is_colorized:
s = re.search(self.prefix + "(\d+-)?(?P<Name>.*)", self.name)
self_name = s.group('Name')
return self_name
return self.name
def colorized_path(path):
dirname = os.path.dirname(path)
name = basename(path)
return os.path.join(dirname, colorized_name(name))

@property
def uncolorized_path(self):
return join(self.dirname, self.uncolorized_name)

def colorized_name(name):
random = str(randint(1, 10 ** 15)) + '-'
return theme.prefix + random + uncolorized_name(name)


def uncolorized_name(name):
if is_colorized(name):
s = re.search(theme.prefix + "(\d+-)?(?P<Name>.*)", name)
self_name = s.group('Name')
return self_name
return name


def uncolorized_path(path):
dirname = os.path.dirname(path)
name = basename(path)
return os.path.join(dirname, uncolorized_name(name))
14 changes: 12 additions & 2 deletions livecss/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from config import Config
from state import State
from theme import theme
from theme import *
from colorizer import colorize_file
from menu import create_menu

from os.path import join, dirname, basename
import sublime


def selected_new_theme(view):
state = State(view)
if not state.theme_path:
return
if uncolorized_path(state.theme_path) != uncolorized_path(theme.abspath):
# here is small hack to colorize after we change the theme
# TODO: find out better solution
sublime.set_timeout(lambda: colorize_file(view, True), 200)


def generate_menu(view):
Expand Down

0 comments on commit d59b5f1

Please sign in to comment.