-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcross_platform_codecs.py
46 lines (37 loc) · 1.54 KB
/
cross_platform_codecs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sublime
import sys
import re
class CrossPlatformCodecs():
@classmethod
def decode_line(self, line):
line = line.rstrip()
decoded_line = self.force_decode(line) if sys.version_info >= (3, 0) else line
decoded_line = decoded_line.lstrip('\n\r')
decoded_line = re.sub(r'\033\[(\d{1,2}m|\d\w)', '', str(decoded_line))
return decoded_line + "\n"
@classmethod
def force_decode(self, text):
try:
text = text.decode('utf-8')
except UnicodeDecodeError:
if sublime.platform() == "windows":
text = self.decode_windows_line(text)
return text
@classmethod
def decode_windows_line(self, text):
# Import only for Windows
import locale
import subprocess
# STDERR gets the wrong encoding, use chcp to get the real one
proccess = subprocess.Popen(["chcp"], shell=True, stdout=subprocess.PIPE)
(chcp, _) = proccess.communicate()
# Decode using the locale preferred encoding (for example 'cp1251') and remove newlines
chcp = chcp.decode(locale.getpreferredencoding()).strip(".\r\n ")
# Get the actual number
chcp = chcp.split(" ")[-1]
# Actually decode
return text.decode("cp" + chcp)
@classmethod
def encode_process_command(self, command):
is_sublime_2_and_in_windows = sublime.platform() == "windows" and int(sublime.version()) < 3000
return command.encode(sys.getfilesystemencoding()) if is_sublime_2_and_in_windows else command