-
Notifications
You must be signed in to change notification settings - Fork 2
/
aligner_command.py
64 lines (51 loc) · 1.68 KB
/
aligner_command.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sublime, sublime_plugin
from subprocess import Popen, PIPE, STDOUT
import os, sys
DEBUG = False
def log(s):
if DEBUG:
print(s)
class AlignerCommand(sublime_plugin.TextCommand):
def run(self, edit, lang='default'):
#self.view.insert(edit, 0, "HW")
lines = []
log(lang)
#path = "/home/generall/Dropbox/code/Ruby/aligner"
path = sublime.packages_path() + "/AutoAligner"
prev_dir = os.getcwd();
os.chdir(path)
for line in self.view.sel():
lines_region = self.view.line (line );
txt_lines = self.view.substr(lines_region);
lines = txt_lines.split("\n");
for l in lines:
log(l)
log("---")
try:
slave = Popen(['ruby', path + '/pipe_launch.rb', lang], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
if sys.version_info >= (3,):
slave.stdin.write(bytes(str(len(lines)) + "\n" , "UTF-8"));
slave.stdin.write(bytes(txt_lines + "\n" , "UTF-8"));
slave.stdin.close()
else:
slave.stdin.write(bytes(str(len(lines)) + "\n" ));
slave.stdin.write(bytes(txt_lines + "\n" ));
log("patch: " + path)
result = []
while True:
# check if slave has terminated:
if slave.poll() is not None:
break
# read one line, remove newline chars and trailing spaces:
line = slave.stdout.readline().rstrip()
log(line)
result.append(line.decode("utf-8"))
responce = '\n'.join(result)
responce = responce.rstrip('\n')
self.view.replace(edit, lines_region, responce);
self.view.sel().clear()
except OSError as e:
if e.errno == os.errno.ENOENT:
sublime.error_message("ruby is not installed")
os.chdir(prev_dir)
##self.view.insert(edit, 0, '\n'.join(result))