1+ using Term
2+
3+ const TRACE_COLORS = (" bright_green" , " hot_pink" , " bright_blue" , " light_coral" , " bright_cyan" , " sandy_brown" , " violet" )
4+
5+ Base. show (io:: IO , :: MIME"text/plain" , t:: Union{Trace,Traces,Episode,Episodes,Trajectory} ) = tprint (io, convert (Term. AbstractRenderable, t; width= displaysize (io)[2 ]) |> string)
6+
7+ inner_convert (:: Type{Term.AbstractRenderable} , s:: String ; style= " gray1" , width= 88 ) = Panel (s, width= width, style= style, justify= :center )
8+ inner_convert (t:: Type{Term.AbstractRenderable} , x:: Union{Symbol,Number} ; kw... ) = inner_convert (t, string (x); kw... )
9+
10+ function inner_convert (:: Type{Term.AbstractRenderable} , x:: AbstractArray ; style= " gray1" , width= 88 )
11+ t = string (nameof (typeof (x)))
12+ s = replace (string (size (x)), " " => " " )
13+ Panel (t * " \n " * s, style= style, justify= :center , width= width)
14+ end
15+
16+ function inner_convert (:: Type{Term.AbstractRenderable} , x; style= " gray1" , width= 88 )
17+ s = string (nameof (typeof (x)))
18+ Panel (s, style= style, justify= :center , width= width)
19+ end
20+
21+ Base. convert (T:: Type{Term.AbstractRenderable} , t:: Trace{<:AbstractArray} ; kw... ) = convert (T, Trace (collect (eachslice (t. x, dims= ndims (t. x)))); kw... , type= typeof (t), subtitle= " size: $(size (t. x)) " )
22+
23+ function Base. convert (
24+ :: Type{Term.AbstractRenderable} ,
25+ t:: Trace{<:AbstractVector} ;
26+ width= 88 ,
27+ n_head= 2 ,
28+ n_tail= 1 ,
29+ name= " Trace" ,
30+ style= TRACE_COLORS[mod1 (hash (name), length (TRACE_COLORS))],
31+ type= typeof (t),
32+ subtitle= " size: $(size (t. x)) "
33+ )
34+ title = " $name : [italic]$type [/italic] "
35+ min_width = min (width, length (title) - 4 )
36+
37+ n = length (t. x)
38+ if n == 0
39+ content = " "
40+ elseif 1 <= n <= n_head + n_tail
41+ content = mapreduce (x -> inner_convert (Term. AbstractRenderable, x, style= style, width= min_width - 6 ), / , t. x)
42+ else
43+ content = mapreduce (x -> inner_convert (Term. AbstractRenderable, x, style= style, width= min_width - 6 ), / , t. x[1 : n_head]) /
44+ TextBox (" ..." , justify= :center , width= min_width - 6 ) /
45+ mapreduce (x -> inner_convert (Term. AbstractRenderable, x, style= style, width= min_width - 6 ), / , t. x[end - n_tail+ 1 : end ])
46+ end
47+ Panel (content, width= min_width, title= title, subtitle= subtitle, subtitle_justify= :right , style= style, subtitle_style= " yellow" )
48+ end
49+
50+ function Base. convert (:: Type{Term.AbstractRenderable} , t:: Traces ; width= 88 )
51+ max_len = mapreduce (length, max, t. traces)
52+ min_len = mapreduce (length, min, t. traces)
53+ if max_len - min_len == 1
54+ n_tails = [length (x) == max_len ? 2 : 1 for x in t. traces]
55+ else
56+ n_tails = [1 for x in t. traces]
57+ end
58+ N = length (t. traces)
59+ max_inner_width = ceil (Int, (width - 6 * 2 ) / N)
60+ Panel (
61+ mapreduce (((i, x),) -> convert (Term. AbstractRenderable, t[x]; width= max_inner_width, name= x, n_tail= n_tails[i], style= TRACE_COLORS[mod1 (i, length (TRACE_COLORS))]), * , enumerate (keys (t))),
62+ title= " Traces" ,
63+ style= " yellow3" ,
64+ subtitle= " $N traces in total" ,
65+ subtitle_justify= :right ,
66+ width= width,
67+ fit= true
68+ )
69+ end
70+
71+ function Base. convert (:: Type{Term.AbstractRenderable} , e:: Episode ; width= 88 )
72+ Panel (
73+ convert (Term. AbstractRenderable, e. traces; width= width - 6 ),
74+ title= " Episode" ,
75+ style= " green_yellow" ,
76+ subtitle= e[] ? " Episode END" : " Episode growing..." ,
77+ subtitle_justify= :right ,
78+ width= width,
79+ fit= true
80+ )
81+ end
82+
83+ function Base. convert (:: Type{Term.AbstractRenderable} , e:: Episodes ; width= 88 )
84+ n = length (e)
85+ if n == 0
86+ content = " "
87+ elseif n == 1
88+ content = convert (Term. AbstractRenderable, e[1 ], width= width - 6 )
89+ elseif n == 2
90+ content = convert (Term. AbstractRenderable, e[1 ], width= width - 6 ) /
91+ convert (Term. AbstractRenderable, e[end ], width= width - 6 )
92+ else
93+ content = convert (Term. AbstractRenderable, e[1 ], width= width - 6 ) /
94+ TextBox (" ..." , justify= :center , width= width - 6 ) /
95+ convert (Term. AbstractRenderable, e[end ], width= width - 6 )
96+ end
97+
98+ Panel (
99+ content,
100+ title= " Episodes" ,
101+ subtitle= " $n episodes in total" ,
102+ subtitle_justify= :right ,
103+ width= width,
104+ fit= true ,
105+ style= " wheat1"
106+ )
107+ end
108+
109+ function Base. convert (r:: Type{Term.AbstractRenderable} , t:: Trajectory ; width= 88 )
110+ Panel (
111+ convert (r, t. container; width= width - 8 ) /
112+ Panel (convert (Term. Tree, t. sampler); title= " sampler" , style= " yellow3" , fit= true , width= width - 8 ) /
113+ Panel (convert (Term. Tree, t. controller); title= " controller" , style= " yellow3" , fit= true , width= width - 8 );
114+ title= " Trajectory" ,
115+ style= " yellow3" ,
116+ width= width,
117+ fit= true
118+ )
119+ end
120+
121+ # general converter
122+
123+ Base. convert (:: Type{Term.Tree} , x) = Tree (to_tree_body (x); title= to_tree_title (x))
124+ Base. convert (:: Type{Term.Tree} , x:: Tree ) = x
125+
126+ function to_tree_body (x)
127+ pts = propertynames (x)
128+ if length (pts) > 0
129+ Dict (" $p => $(summary (getproperty (x, p))) " => to_tree_body (getproperty (x, p)) for p in pts)
130+ else
131+ x
132+ end
133+ end
134+
135+ to_tree_title (x) = " $(summary (x)) "
0 commit comments