1 ! Copyright (C) 2011 John Benediktsson
2 ! See https://factorcode.org/license.txt for BSD license
4 USING: accessors command-line continuations formatting io
5 io.directories io.files.info io.pathnames kernel locals math
6 namespaces sequences sorting ;
12 : indent ( indents -- )
14 [ [ " " "| " ? write ] each ]
15 [ "└── " "├── " ? write ] bi* ;
17 : write-name ( entry indents -- )
20 : write-file ( entry indents -- )
21 write-name #files [ 1 + ] change-global ;
25 : write-dir ( entry indents -- )
27 [ [ name>> ] dip write-tree ]
28 [ 3drop " [error opening dir]" write ] recover
29 ] 2bi #directories [ 1 + ] change-global ;
31 : write-entry ( entry indents -- )
32 nl over directory? [ write-dir ] [ write-file ] if ;
34 :: write-tree ( path indents -- )
36 [ name>> ] sort-with [ ] [
39 [ indents write-entry ] each
41 indents pop* t indents push
45 ] with-directory-entries ;
48 0 #directories set-global 0 #files set-global
49 [ write ] [ V{ } clone write-tree ] bi nl
50 #directories get-global #files get-global
51 "\n%d directories, %d files\n" printf ;