Skip to content

Commit

Permalink
Non-UTF console workaround.
Browse files Browse the repository at this point in the history
+ Added console_encoding parameter to settings
+ Modific.py now uses this parameter to encode command line arguments for
popen
+ Updated README.md
+ Minor refactoring of Modific.py
  • Loading branch information
stdk committed Jun 12, 2012
1 parent d65e1f8 commit 9071593
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Modific.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import os
Expand All @@ -12,13 +13,19 @@

settings = sublime.load_settings("Modific.sublime-settings")

#helper function to test for vcs existence in given folder
#uses closure to lock cycle variable value inside lambda
vcs_check = [ (lambda vcs: lambda dir: os.path.exists(os.path.join(dir,'.'+vcs))
and {'root': dir, 'name': vcs}) (vcs)
for vcs,_ in settings.get('vcs') ]

def get_vcs(directory):
test = lambda vcs: lambda dir: os.path.exists(os.path.join(dir,'.'+vcs)) and {'root': dir, 'name': vcs}
checkers = [ test(vcs) for vcs,_ in settings.get('vcs') ]

"""
Determines, which of VCS systems we should use for given folder.
Currently, uses priority of definitions in settings.get('vcs')
"""
while directory:
available = filter(lambda x:x,[check(directory) for check in checkers ])
available = filter(lambda x:x,[check(directory) for check in vcs_check])
if available: return available[0]
parent = os.path.realpath(os.path.join(directory, os.path.pardir))
if parent == directory:
Expand Down Expand Up @@ -51,13 +58,14 @@ def do_when(conditional, callback, *args, **kwargs):


class CommandThread(threading.Thread):
def __init__(self, command, on_done, working_dir="", fallback_encoding="", **kwargs):
def __init__(self, command, on_done, working_dir="", fallback_encoding="", console_encoding="", **kwargs):
threading.Thread.__init__(self)
self.command = command
self.on_done = on_done
self.working_dir = working_dir
self.stdin = kwargs.get('stdin',None)
self.stdout = kwargs.get('stdout',subprocess.PIPE)
self.console_encoding = console_encoding
self.fallback_encoding = fallback_encoding
self.kwargs = kwargs

Expand All @@ -69,6 +77,9 @@ def run(self):
if self.working_dir != "":
os.chdir(self.working_dir)

if self.console_encoding:
self.command = [s.encode(self.console_encoding) for s in self.command]

proc = subprocess.Popen(self.command,
stdout=self.stdout, stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
Expand Down Expand Up @@ -101,6 +112,7 @@ def run_command(self, command, callback=None, show_status=True,
kwargs['working_dir'] = self.get_working_dir()
if 'fallback_encoding' not in kwargs and self.active_view() and self.active_view().settings().get('fallback_encoding'):
kwargs['fallback_encoding'] = self.active_view().settings().get('fallback_encoding').rpartition('(')[2].rpartition(')')[0]
kwargs['console_encoding'] = settings.get('console_encoding')

if self.active_view() and self.active_view().is_dirty() and not no_save:
self.active_view().run_command('save')
Expand Down
4 changes: 4 additions & 0 deletions Modific.sublime-settings
Expand Up @@ -12,6 +12,10 @@
["hg" , "hg"]
],

//if you have some weird OS, that has non-unicode console
//place its console encoding here
"console_encoding" : "cp1251",

// if true, plugin prints some debug information to the console window
"debug": false
}
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -65,6 +65,7 @@ If you want to change something, don't do it in this file. Open `Preferences ->

You can configure is a type of icon (dot, circle or bookmark) and path for your VCS binaries (or leave them as is, if you have them in your PATH). It's also possible to set priority for VCS used (when you have more than one simultaneously) by reordering their definitions.

If some sacred punishment has been bestowed upon you, and you have no other choice but to use OS, where console has non-UTF8 encoding, you can set console_encoding parameter to the name of your beloved encoding. This parameter is specifically designed for Windows XP users, who have their git repositories in folders with cyrillic path. Since russian XP uses CP1251 as default encoding (including console), VCS diff commands will be encoded appropriately, when using this parameter.


License
Expand Down

0 comments on commit 9071593

Please sign in to comment.