-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_line_tree.py
executable file
·121 lines (102 loc) · 3.19 KB
/
one_line_tree.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
###
# Copyright 2013 Tomasz Wyderka <wyderkat@cofoh.com>
# www.cofoh.com
# Licensed under GPL-2
##
from collections import OrderedDict as OD
from code_upstairs_core import Function, FunctionDB
css = {
"sym" : {
"left" : "(",
"right" : ")",
"middle" : "",
"sel_l" : "*",
"sel_r" : "*",
},
"col": {
},
"names": {
"parents" : "P",
"siblings" : "S",
"childs" : "C",
"status" : "-",
}
}
def main( current_sel, width ):
if not len( current_sel ):
current_sel = ["childs", "acc"]
stub = Function("stub")
stub.find_strong_layers()
db = FunctionDB( stub )
db.D = OD ([ ("parents", OD([ ("main",0) ]) ) ,
("siblings", OD([ ("init",0), ("update",0) ]) ) ,
("childs", OD([ ("calc",0), ("precalc",0), ("acc",0) ]) ),
("3", OD([ ("ola",0), ("ma",0) ]) ),
("4", OD([ ("kota",0) ]) ),
(">4", OD([ ("fx",0), ("xx123456789"*6,0) ]) )
])
db.prepend_text_layer("-", "Ola i kot sa w domu")
db.select( current_sel[0], current_sel[1] )
return render_line( db, width, css )
def render_line( db, width, css ):
rendered_elements = OD( )
current_selected = db.get_selected()[0]
for l in db.get_all_layers():
if l == current_selected:
# skip now, just placeholder
render = ""
else:
render = render_layer( db, l, 0, css )
rendered_elements[ l ] = render
totalwidth = 0
for s in rendered_elements.values():
totalwidth += len( s )
if totalwidth >= width:
print "not enough space to render"
return "-" * width
# missing selected element
rendered_elements[ current_selected ] = \
render_layer( db, current_selected, width - totalwidth , css )
#put it back together
return "".join( rendered_elements.values() )
def render_layer( db, layer, width, css ):
out = css["sym"]["left"]
try:
out += css["names"][layer]
except KeyError:
out += layer
if db.is_selected( layer=layer ):
rendered_subelements = OD( )
# TODO
if db.is_text_layer( layer ):
# in case of flat text element, just print it
rendered_subelements[ layer ] = db.get_text_layer( layer )
else:
for f in db.get_fnames_in_layer( layer ):
render = render_fname( db, layer, f, 0, css )
rendered_subelements[ f ] = render
totalwidth = 0
for s in rendered_subelements.values():
totalwidth += len( s )
#put it back together
out += " " + " ".join( rendered_subelements.values() ) + " "
right = css["sym"]["right"]
space = width - len(out) - len(right)
if space >= 0:
out += " " * space + right
else:
print "width %d space %d" % ( width, space )
out = out[ : width-len(right) ] + right
else:
# not selected layer
out += css["sym"]["right"]
return out
def render_fname( db, layer, fname, width, css ):
if db.is_selected( layer=layer, fname=fname ):
return css["sym"]["sel_l"] + fname + css["sym"]["sel_r"]
else:
return fname
if __name__ == "__main__":
import sys
print main( sys.argv[1:], 80 )