| 
									
										
										
										
											2015-04-01 17:37:39 -04:00
										 |  |  | ! Copyright (C) 2011 John Benediktsson | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | USING: accessors command-line continuations formatting io | 
					
						
							| 
									
										
										
										
											2015-08-06 00:39:51 -04:00
										 |  |  | io.directories io.files.info io.pathnames kernel locals math | 
					
						
							| 
									
										
										
										
											2015-04-01 17:37:39 -04:00
										 |  |  | namespaces sequences sorting ;
 | 
					
						
							|  |  |  | IN: tools.tree | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: #files | 
					
						
							|  |  |  | SYMBOL: #directories | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : indent ( indents -- )
 | 
					
						
							|  |  |  |     unclip-last-slice
 | 
					
						
							|  |  |  |     [ [ "    " "|   " ? write ] each ] | 
					
						
							|  |  |  |     [ "└── " "├── " ? write ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write-name ( entry indents -- )
 | 
					
						
							|  |  |  |     indent name>> write ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write-file ( entry indents -- )
 | 
					
						
							|  |  |  |     write-name #files [ 1 + ] change-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: write-tree | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write-dir ( entry indents -- )
 | 
					
						
							|  |  |  |     [ write-name ] [ | 
					
						
							|  |  |  |         [ [ name>> ] dip write-tree ] | 
					
						
							|  |  |  |         [ 3drop " [error opening dir]" write ] recover
 | 
					
						
							|  |  |  |     ] 2bi #directories [ 1 + ] change-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write-entry ( entry indents -- )
 | 
					
						
							| 
									
										
										
										
											2015-08-05 21:33:35 -04:00
										 |  |  |     nl over directory? [ write-dir ] [ write-file ] if ;
 | 
					
						
							| 
									
										
										
										
											2015-04-01 17:37:39 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: write-tree ( path indents -- )
 | 
					
						
							|  |  |  |     path [ | 
					
						
							|  |  |  |         [ name>> ] sort-with [ ] [ | 
					
						
							|  |  |  |             unclip-last [ | 
					
						
							|  |  |  |                 f indents push
 | 
					
						
							|  |  |  |                 [ indents write-entry ] each
 | 
					
						
							|  |  |  |             ] [ | 
					
						
							|  |  |  |                 indents pop* t indents push
 | 
					
						
							|  |  |  |                 indents write-entry | 
					
						
							|  |  |  |             ] bi* indents pop*
 | 
					
						
							|  |  |  |         ] if-empty
 | 
					
						
							|  |  |  |     ] with-directory-entries ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tree ( path -- )
 | 
					
						
							|  |  |  |     0 #directories set-global 0 #files set-global
 | 
					
						
							|  |  |  |     [ write ] [ V{ } clone write-tree ] bi nl
 | 
					
						
							|  |  |  |     #directories get-global #files get-global
 | 
					
						
							|  |  |  |     "\n%d directories, %d files\n" printf ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : run-tree ( -- )
 | 
					
						
							|  |  |  |     command-line get [ | 
					
						
							| 
									
										
										
										
											2016-03-18 20:04:05 -04:00
										 |  |  |         "." tree | 
					
						
							| 
									
										
										
										
											2015-04-01 17:37:39 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         [ tree ] each
 | 
					
						
							|  |  |  |     ] if-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MAIN: run-tree |