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

[Help] Making a list

Started by remiX, 29 October 2012 - 08:52 AM
remiX #1
Posted 29 October 2012 - 09:52 AM
Hey everyone ;D

I have perfectly working function that list's all the programs in a floppy disk which displays when you click enter for 'List left' or 'List right'. Everything works great but the code for listing will take too much if I do it the way I've done it now. So I went to check the the 'list' file because the list command works awesome.

function listFiles()
	cX,cY = term.getSize()
	for i =1, cX-1 do cp(i,2) write('-') end
	cp(1,1)
	local selection = menuW("OK", "List left", "List right")
	if selection == "OK" then lol()
	elseif selection == "List left" then
		term.clear()
		if disk.isPresent(side_CopyFrom) then
			fileList = fs.list("disk")
			if fileList[1] then
				status = #fileList.." files"
				for i = 1, #fileList do
					if i < 14 then
						cp(1,i+4)
						print(" - "..fileList[i])
					elseif i >= 14 then
						cp(15,i-13+4)
						print(" - "..fileList[i])
					end
				end
			else
				print(err["NoDataFound"])
				status = "No Data"
			end
		else
			print(err["NoDiskFound"])
			status = "No Disk"
		end
		cp(1,3) print("Left disk ["..status.."]")
	elseif selection == "List right" then
		term.clear()
		if disk.isPresent(side_CopyTo) then
			if disk.isPresent(side_CopyFrom) then fileList = fs.list("disk2") else fileList = fs.list("disk") end
			cp(30,1) print(#fileList)
			if fileList[1] then
				status = #fileList.." files"
				for i = 1, #fileList do
					if i < 14 then
						cp(1,i+4)
						print(" - "..fileList[i])
					elseif i >= 14 then
						cp(15,i-13+4)
						print(" - "..fileList[i])
					end
				end
			else
				print(err["NoDataFound"])
				status = "No Data"
			end
		else
			print(err["NoDiskFound"])
			status = "No Disk"
		end
		cp(1,3) if diskToLabel ~= "No label" then print("Left disk ["..status.."]".." - "..diskToLabel) else print("Left disk ["..status.."]") end
	end
	return listFiles()
end

I have no clue what's happening within the list file and have no clue how it does all the sorting, getting file names etc. Can someone show me how to change this function so it sort's it by itself? Thanks.

Pictures:
SpoilerRight Disk:
Spoiler

Left Disk
Spoiler
BigSHinyToys #2
Posted 29 October 2012 - 10:36 AM
table.sort()

do that to your list before displaying it.
remiX #3
Posted 29 October 2012 - 05:05 PM
I changed the for loop to this but it doesn't work


  table.sort(fileList)
 for i = 1, #fileList do
  print(fileList[i])
 end
ChunLing #4
Posted 29 October 2012 - 08:20 PM
Do you mean that it doesn't sort the list in alphabetical order or that it throws an error?
remiX #5
Posted 29 October 2012 - 08:41 PM
Oh should've said what happens, woops :P/>/>

It like only shows half the files, and only in one column.

Like, if there were 20 files, for some reason it would only show file number 6-14 for some reason
ChunLing #6
Posted 29 October 2012 - 08:44 PM
Well, the above code snippet removed your code for printing in two columns.