-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlignColumns.py
More file actions
132 lines (112 loc) · 3.73 KB
/
Copy pathAlignColumns.py
File metadata and controls
132 lines (112 loc) · 3.73 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
'''
https://github.com/duzun/nppPyAlignColumn
Align text columns by given column delimiter.
A Script for Notepad++ Python Script Plugin.
@author Dumitru Uzun (DUzun.Me)
@license (http://licence.visualidiot.com/)
@version 1.0.2
'''
if not 'alignColumnDelimiter' in globals():
alignColumnDelimiter = u'='
delim = notepad.prompt('Input the column delimiter:', 'Align Columns delimiter string', alignColumnDelimiter);
if delim != None:
alignColumnDelimiter = delim
def utf8len(s):
r = 0;
for c in s: r += (ord(c) & 0xC0) != 0xC0; ''' Don't count sync bytes '''
return r;
# end utf8len()
def alignColumns(right = 0, trim = 0, sticky = 0):
if sticky:
trim = 1;
right = 0;
# endif
(startLine, endLine) = editor.getUserLineSelection();
caretPos = editor.getCurrentPos();
modified = 0;
if editor.getCodePage() == 65001:
charLen = utf8len;
else:
charLen = len;
# endif
''' Compute the column widths from all the selected lines '''
colWidths = []; # colWidths
for ln in range(startLine, endLine+1):
i = editor.getLine(ln).rstrip(); ''' ignore trailing spaces '''
''' Replace each tab with 4 spaces '''
line = i.replace("\t", ' ');
if line != i:
editor.replaceLine(ln, line);
modified += 1;
# endif
''' Split line into columns '''
cols = line.split(delim);
lc = len(cols);
if lc < 2: continue; ''' Ignore lines that do not contain delimiter '''
for i in range(0,lc):
c = cols[i];
if trim: c = c.strip();
l = charLen(c);
if i >= len(colWidths):
colWidths.append(l);
elif l > colWidths[i]:
colWidths[i] = l;
# endfor
# endfor
c = [];
for i in colWidths: c.append(str(i));
v = ":".join(c);
''' Align the columns '''
for ln in range(startLine, endLine+1):
line = editor.getLine(ln).rstrip();
cols = line.split(delim); ''' tabs have already been replaced at this point '''
lc = len(cols);
if lc < 2: continue; ''' Ignore lines that do not contain delimiter '''
chg = 0;
memc = "";
for i in range(0,lc-1):
c = cols[i];
if trim:
c = c.strip();
if charLen(c) < charLen(cols[i]):
cols[i] = c;
chg = 1;
# endif
# endif
if(charLen(memc)):
cols[i] = memc + c;
memc = "";
chg = 1;
# endif
dif = colWidths[i] - charLen(c);
if dif > 0:
if sticky:
memc = " " * dif;
else:
if right:
cols[i] = " " * dif + c;
else:
cols[i] = c + " " * dif;
# endif
chg = 1;
# endif
# endif
# endfor
if chg:
line = delim.join(cols);
editor.replaceLine(ln, line);
modified += 1
# endif
# endfor
if modified:
editor.setEmptySelection(caretPos);
# endif
# end of alignColumns()
if delim != None:
''' First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script '''
editor.beginUndoAction();
alignColumns(right = 0, trim = 0);
''' End the undo action, so Ctrl-Z will undo the above two actions '''
editor.endUndoAction();
# endif