1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://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 ;
7 FROM: namespaces => change-global ;
13 : indent ( indents -- )
15 [ [ " " "| " ? write ] each ]
16 [ "└── " "├── " ? write ] bi* ;
18 : write-name ( entry indents -- )
21 : write-file ( entry indents -- )
22 write-name #files [ 1 + ] change-global ;
26 : write-dir ( entry indents -- )
28 [ [ name>> ] dip write-tree ]
29 [ 3drop " [error opening dir]" write ] recover
30 ] 2bi #directories [ 1 + ] change-global ;
32 : write-entry ( entry indents -- )
33 nl over directory? [ write-dir ] [ write-file ] if ;
35 :: write-tree ( path indents -- )
37 [ name>> ] sort-with [ ] [
40 [ indents write-entry ] each
42 indents pop* t indents push
46 ] with-directory-entries ;
49 0 #directories set-global 0 #files set-global
50 [ write ] [ V{ } clone write-tree ] bi nl
51 #directories get-global #files get-global
52 "\n%d directories, %d files\n" printf ;
56 current-directory get tree