This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Watcher7's profile picture

Alternative to list.

Started by Watcher7, 30 April 2012 - 02:06 AM
Watcher7 #1
Posted 30 April 2012 - 04:06 AM
I was bored for 2 minutes and needed to make something simple, so I made a quick alternative to list.
This alternative provides the ability to see hidden items (Items with a "." infront of them cannot be seen by the normal list command.) and scrolling if there are too many items.
There are indicators for: Access to the item(Read/Write), Visibility of that item(Hidden/Visible), and that item's type(File/Directory) through the use of tokens.
Spoiler
Here are some examples:
Spoiler

And here is the simple messy(Yay 2 mins..) source:
Spoiler
tArgs = { ... }
iCounth, iCountv, iCountd, iCountf, iCountr, iCountw = 0, 0, 0, 0, 0, 0
function spit(key,file,path)
	if fs.isReadOnly(path..file) == true then  rom = "-"; iCountr = iCountr + 1 else rom = "+" ; iCountw = iCountw + 1 end
	if string.match(file,"^.")  == "."  then  vis = "H"; iCounth = iCounth + 1 else vis = "V" ; iCountv = iCountv + 1 end
	if fs.isDir(path..file)			then type = "D"; iCountd = iCountd + 1 else type = "F"; iCountf = iCountf + 1 end
	write(
			" "..
			string.rep("0",(3-string.len(key)))..key..": "..
			string.rep(" ",(34-string.len(file)))..file..
			"["..rom.."]".."["..vis.."]".."["..type.."]".."n"
		)
end
function scan(dir)
	local w,h = term.getSize()
	local nLinesPrinted = 0
	for key,file in ipairs(fs.list(dir)) do
		if nLinesPrinted >= h - 2 then
			term.write(" "..string.rep("-",48))
			x,y=os.pullEvent( "key" )
			term.clearLine()
			term.setCursorPos(1,h)
		end
		spit(key,file,dir.."/")
		nLinesPrinted = nLinesPrinted+1
	end
	write(
		" "..
		"([-]: "..iCountr..")"..
		"([+]: "..iCountw..")"..
		"([H]: "..iCounth..")"..
		"([V]: "..iCountv..")"..
		"([D]: "..iCountd..")"..
		"([F]: "..iCountf..")"..
		"n"
	)
end
function tokens()
	tokens = "[+] = Write accessn[-] = Read onlyn[H] = Hiddenn[V] = Visiblen[D] = Directoryn[F] = Filen"
	write("Usage: scan <dir>nnTokens:n"..tokens)
end

if tArgs[1] == nil then
	scan(shell.dir())
else
	if fs.isDir(tArgs[1]) then
		scan(tArgs[1])
	else
		tokens()
	end
end
Minor code change.
Also, don't scan a dir with a file over 34 chars long.
Hopefully there are no bugs I did not catch on something so short.
MysticT #2
Posted 30 April 2012 - 08:30 PM
Nice work!
But the list program can list hidden files, you just have to do:
> list all
You could add an option like that, so you can choose to see hiden files or not.
I don't like those "arrows" after the numbers, but other than that the display is very nice and easy to read :)/>/>
Really good for a 2 minutes code :)/>/>
Watcher7 #3
Posted 30 April 2012 - 09:17 PM
Nice work!
But the list program can list hidden files, you just have to do:
> list all
You could add an option like that, so you can choose to see hidden files or not.
I don't like those "arrows" after the numbers, but other than that the display is very nice and easy to read :)/>/>
Really good for a 2 minutes code :)/>/>
I'm running on a slightly older version of CC, that's why I didn't know about "list all", sorry.