Paste: Words to print gadget component hierarchy
Author: | bitli |
Mode: | factor |
Date: | Tue, 14 Nov 2023 21:28:34 |
Plain Text |
USING: accessors classes combinators formatting io io.styles
kernel math namespaces prettyprint sequences strings ui.gadgets
ui.gadgets.labels ui.gadgets.worlds vocabs ;
IN: introspection.gadgets
CONSTANT: max_depth 10
CONSTANT: max_length 8
DEFER: component-hierarchy.
<PRIVATE
: each-child-index ( gadget quot -- ) [ children>> ] dip each-index ; inline
: write-leader ( n -- )
[ "| " ] replicate "" concat-as write
;
: hierarchy-depth ( g -- l )
parents length 1 -
;
: dump-name ( g -- )
[ hierarchy-depth write-leader ] keep
[ class-of dup dup name>> swap write-object
vocabulary>> lookup-vocab name>>
".private" tail? [ " P " ] [ " " ] if write
] keep
dup label instance? [ string>> pprint-short ] [ drop ] if
nl
;
: dump-with-children-cond ( gadget idx -- )
1 +
{
{ [ dup max_length < ] [ drop component-hierarchy. ] }
{ [ dup max_length = ] [
drop
[ hierarchy-depth write-leader ] keep
parent>> children>> length
max_length
"* COUNT %d > %d, skipped\n" printf
] }
[ drop drop ]
} cond
;
PRIVATE>
: component-hierarchy. ( gadget -- )
gadget check-instance
[ dump-name ] keep
dup hierarchy-depth max_depth <
[
[ dump-with-children-cond ] each-child-index
]
[
hierarchy-depth write-leader max_depth "| * DEPTH > %d (max_depth), ignored \n" printf
]
if
;
: hierarchy. ( -- ) world get component-hierarchy. ;
New Annotation