-
Notifications
You must be signed in to change notification settings - Fork 0
/
make3Dindent.py
executable file
·102 lines (88 loc) · 2.43 KB
/
make3Dindent.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
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
#!/usr/bin/env python3
import sys
import os
start = '{' # ネストの起点
end = '}' # ネストの終点
width = 20 # 改行幅
def make3Dindent(stream, reverse=False):
depth = 0 # いまの浮かび具合(右側のテキストに適用)
depthList = [0] # 行ごとの浮かび具合の管理
leftLines = []
rightLines = []
for line in stream:
charcount = 0
left = ''
right = ''
for c in line:
charcount += 1
if(c == start):
# 浮かび具合を上げる
depth += 1
left += c
right += ' ' + c
elif(c == end):
# 浮かび具合を下げる
depth -= 1
left += c + ' '
right += c
elif c == "\n": # 改行処理
pad = (width - charcount) * ' '
leftLines.append(left + pad)
rightLines.append(right + pad)
depthList.append(depth)
left = ''
right = ''
charcount = 0
continue
else:
left += c
right += c
if charcount >= width:
leftLines.append(left)
rightLines.append(right)
depthList.append(depth)
left = ''
right = ''
charcount = 0
if c != "\n" and charcount > 0:
pad = (width - charcount) * ' '
leftLines.append(left + pad)
rightLines.append(right + pad)
depthList.append(depth)
depthList.pop()
maxDepth = max(depthList)
maxLength = max([len(line) for line in leftLines])
retRight = []
retLeft = []
baseWidth = maxDepth + maxLength
for (l, r, d) in zip(leftLines, rightLines, depthList):
retLeft.append((' ' * (maxDepth - d)) + l + (' ' * (baseWidth - (len(l) + (maxDepth - d)))))
retRight.append((' ' * d) + r + (' ' * (baseWidth - (len(r) + d))))
# 焦点補助のドット
pad = ' ' * int(baseWidth / 2)
dotLine = pad + '●' + pad
retLeft.append(dotLine)
retRight.append(dotLine)
if reverse:
# 沈む
return (retRight, retLeft)
else:
# 浮かぶ
return (retLeft, retRight)
# 出力用
def printLeftRight(left, right):
for (l, r) in zip(left, right):
print (l + ' ' + r)
if __name__ == '__main__':
if len(sys.argv) == 1:
(left, right) = make3Dindent(sys.stdin)
printLeftRight(left, right)
else:
input_file = sys.argv[1]
if not os.path.isfile(input_file):
print(input_file + ': not found')
sys.exit(1)
f = open(input_file)
(left, right) = make3Dindent(f)
f.close()
printLeftRight(left, right)