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

File Navigator [CC1.46]

Started by sirdabalot, 27 October 2012 - 03:31 PM
sirdabalot #1
Posted 27 October 2012 - 05:31 PM
Okay, this is just a simple file browser, it runs programs, I think, and navigates through directories with ease.

You need an advanced computer for this. Also it only supports 30 files/directories. To go back to the root folder put 0 as your selection.

GUI API you need:
Spoiler

function printCentered(per, str, ypos)
	pw, pl = per.getSize()
	per.setCursorPos(((pw/2) - (#str/2)), ypos)
	per.write(str)
end

function drawHeader(per, str)
	pw, pl = per.getSize()
	printCentered(per, str, 1)
	per.setCursorPos(1,2)
	write(string.rep("=", pw))
end

function addSpaces(str, len)
	str = str .. string.rep(" ", len - #str)
end

function clearScreen(per)
	per.clear()
	per.setCursorPos(1,1)
end

function fillBackground(per, str, col)
	pw, pl = per.getSize()
	for x = 1, pw do
		for y = 1, pl do
			per.setCursorPos(x, y)
			per.setBackgroundColour(col)
			write(str)
		end
	end
end

function drawWindow(per, str, col, x1, y1, x2, y2)
	per.setCursorPos(x1, y1)
	per.setBackgroundColour(col)
	for x = x1, x2 do
		for y = y1, y2 do
			per.setCursorPos(x, y)
			per.write(" ")
		end
	end
	per.setCursorPos(x1,y1)
	per.write(str)
end

function drawIMG(per, img, x, y)
	for i = 1, #img do
		per.setCursorPos(x,y-1+i)
		per.write(img[i])
	end
end

function errMsg(per, str, dtime)
	pw, pl = per.getSize()
	fillBackground(per, " ", colours.blue)
	printCentered(per, str, (pl/2) + 1)
	for i = 1, dtime do
		printCentered(per, "		", pl/2)
		sleep(0.5)
		printCentered(per, "SYS.FLR:", pl/2)
		sleep(0.5)
	end
	clearScreen(per)
end

The actual program:
Spoiler

sgui.clearScreen(term)

ver = "1"

files = {}

logo = {
"Sirdabalots Navigator,",
"  the file browser.",
"			   ",
"		oooooo ",
"	   o  .   o",
"	   o   .  o",
"	   o	. o",
"	   o	  o",
"	  XXoooooo ",
"	 XXX	   ",
"	XXX		",
"   XXX		 ",
"  XXX		  "
}

tw, tl = term.getSize()

files = fs.list(shell.dir())

function drawFiles()
	files = fs.list(shell.dir())
	sgui.drawWindow(term, "Files:", colours.blue, 1,1, 28, tl - 1)
	term.setBackgroundColour(colours.blue)
	n = 1
	for i = 1, #files do
		if i < tl - 3 then
			term.setCursorPos(1,2+i)
			term.write(i .. ": " .. files[i])
		else
			n = i - (tl - 4)
			term.setCursorPos(17,2+n)
			term.write(i .. ": " .. files[i])
		end
	end
end

function getSel()
	term.setBackgroundColour(colours.lime)
	sel = tonumber(read())
	if sel == 0 then
		shell.setDir("")
	elseif sel < 0 or sel > #files then
		sgui.errMsg(term, "INVALID SELECTION", 2)
	elseif fs.isDir(shell.dir() .. "/" .. files[sel]) then
		shell.setDir(shell.dir() .. "/" .. files[sel])
	else
		shell.run(shell.dir() .. "/" .. files[sel])
	end
end

while true do
	sgui.clearScreen(term)
	sgui.fillBackground(term, " ", colours.black)
	term.setCursorPos(tw + 1 - #"version: " - #ver, 1)
	term.write("Version: " .. ver)
	sgui.drawIMG(term, logo, 30, 3)
	drawFiles()
	sgui.drawWindow(term, "Selection: ", colours.lime, 1, tl, tw, tl)
	getSel()
end
Laserman34170 #2
Posted 11 November 2012 - 05:18 PM
You mispelled the code tag in your program code. Otherwise, nice program.
sirdabalot #3
Posted 17 November 2012 - 07:03 AM
You mispelled the code tag in your program code. Otherwise, nice program.

Fixed, mah bad. :)/>/>
Tiin57 #4
Posted 17 November 2012 - 10:30 AM
Is the GUI API yours or someone else's? I shall show you some code.

if (!gui.getOwner().equals("sirdabalot")) {
 gui.integrateIn(filebrowser); // No need for it to be separate, right?
}
else {
 gui.getOffThisPost(); // IT'S NOT YOUR CODE!
}
You know Java, right? Good. :)/>/>
Sammich Lord #5
Posted 17 November 2012 - 11:37 AM
Is the GUI API yours or someone else's? I shall show you some code.

if (!gui.getOwner().equals("sirdabalot")) {
gui.integrateIn(filebrowser); // No need for it to be separate, right?
}
else {
gui.getOffThisPost(); // IT'S NOT YOUR CODE!
}
You know Java, right? Good. :)/>/>
LMFAO, I know how to read some simple Java code. I can get a pretty good idea of what a set of code does by looking at it,
sirdabalot #6
Posted 20 November 2012 - 05:48 AM
Is the GUI API yours or someone else's? I shall show you some code.

if (!gui.getOwner().equals("sirdabalot")) {
gui.integrateIn(filebrowser); // No need for it to be separate, right?
}
else {
gui.getOffThisPost(); // IT'S NOT YOUR CODE!
}
You know Java, right? Good. :(/>/>

Do not fear, it is mine. I created a separate topic which I will update regularly which contains it as well. As for integration, will do later. :(/>/>