Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from luaposix/luaposix
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.lua
More file actions
Latest commit
56 lines (50 loc) · 1.87 KB
/
Copy pathtree.lua
File metadata and controls
56 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- tree view of the file system like the "tree" unix utility
-- John Belmonte <jvb@prairienet.org>
localposix=require'posix'
localleaf_indent='| '
localtail_leaf_indent=''
localleaf_prefix='|-- '
localtail_leaf_prefix='`-- '
locallink_prefix=' -> '
localfunctionprintf(...)
io.write(string.format(unpack({...})))
end
localfunctiondo_directory(directory, level, prefix)
localnum_dirs=0
localnum_files=0
localfiles=posix.dir(directory)
locallast_file_index=#files
table.sort(files)
fori, nameinipairs(files) do
ifname~='.' andname~='..' then
localfull_name=string.format('%s/%s', directory, name)
localinfo=assert(posix.stat(full_name))
localis_tail= (i==last_file_index)
localprefix2=is_tailandtail_leaf_prefixorleaf_prefix
locallink=''
ifinfo.type=='link' then
linked_name=assert(posix.readlink(full_name))
link=string.format('%s%s', link_prefix, linked_name)
end
printf('%s%s%s%s\n', prefix, prefix2, name, link)
ifinfo.type=='directory' then
localindent=is_tailandtail_leaf_indentorleaf_indent
-- TODO: cache string concatination
sub_dirs, sub_files=do_directory(full_name, level+1,
prefix..indent)
num_dirs=num_dirs+sub_dirs+1
num_files=num_files+sub_files
else
num_files=num_files+1
end
end
end
returnnum_dirs, num_files
end
localfunctionfore(directory)
print(directory)
num_dirs, num_files=do_directory(directory, 0, '')
printf('\n%d directories, %d files\n', num_dirs, num_files)
end
directory= (argand#arg>0) andarg[1] or'.'
fore(directory)