Directory command


;
; Dir command, written in Little.
;
(package demo)

(using
	little.io.File
	little.io.FileAttributes
	little.io.Directory
	little.io.TextOutputStream)

(class Dir ()

	(method indent (out n) :static
		(let ((i 0))
			(while (i:lt n)
				(out:print " "c)
				(set i (i:succ)))))

	(method isdot (file) :static
		(((file:name):getchar (((file:name):size):sub 1)):equals "."c))

	(method list (dir out indent) :static
		(try
			(dir:open)
			(let ((files (dir:files)) (file nil) (attr nil))
				(while (files:hasMoreElements)
					(self:indent out indent)
					(set file (files:nextElement))
					(cond
						((file:instanceof Directory)
							(out:println "" (file:name))
							(if (self:isdot file)
								nil
								(self:list file out (indent:add 5))))
						(true
							(out:println "   " (file:name))))))
			(dir:close)
		 catch (e)
			(dir:close)
			((TextOutputStream:err):println "Error: " (e:tostring))))

	(method main () :static
		(try
		(let ((dir nil) (arguments (System:arguments)) (dirname ".") (n 0))
			(while (arguments:hasMoreElements)
				(set dirname (arguments:nextElement))
				((TextOutputStream:out):println dirname ":")
				(set dir (Directory:new dirname))
				(self:list dir (TextOutputStream:out) 5))
			(if n
				nil
				(prog
					(set dir (Directory:new dirname))
					(self:list dir (TextOutputStream:out) 5))))
		 catch (e)
			((TextOutputStream:err):println "Error: " (e:tostring))))

)