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

BergeMenu

Started by Berge, 09 April 2013 - 12:55 PM
Berge #1
Posted 09 April 2013 - 02:55 PM
BergeMenu
version 1.0.1
BergeMenu is a simple program to display a menu with selected programs. This program allows you to list any programs you want then be able to click and run them from one simple menu. It's functions can also be copied/pasted in your other programs if you want. To get the installer run
 pastebin get 9Wgc4V3J namehere 

To edit the program list run:
edit progsfile
then follow the format to add or delete programs from the list.

Menu:
Spoiler

progsfile:
Spoiler

Note: ComputerCraft is my only programming experience, so I am NOT an expert. This is not meant to wow anyone, but to provide a nice function for the everyday user. Any constructive criticism would be nice, I'm fairly new to CC and advice would be greatly appreciated.
If you edit and/or redistribute this please give me some credit. I may add to this in the future if requested. Thanks!
BergeMenu's raw code:
Spoiler

local function center(text,Y)
  local c = #text / 2
  local c = 25 - c
  term.setCursorPos(c,Y)
  write(text)
end
local function getdata(file)
  local f = fs.open(file,"r")
  local data = f.readAll()
  f.close()
  return textutils.unserialize(data)
end
local function printT(table,X,Y,B)/>
  for i=1,#table do
	term.setCursorPos(X,Y + i)
	if b then
	  center(table[i],Y)
	else
	  write(table[i])
	end
	if #table[i] < checkLen(table) then
	  local l = #table[i]
	  while checkLen(table) - l ~= 0 do
		write(" ")
		l = l + 1
	  end
	end
  end
end
function checkLen(table)
  local max = 0
  for i=1,#table do
	if #table[i] > max then
	  max = #table[i]
	end
  end
  return max
end
local function desktop()
  while true do
  term.setBackgroundColor(colors.blue)
  term.clear()
  term.setBackgroundColor(colors.gray)
  center(" BergeMenu ",1)
  term.setCursorPos(1,19)
  term.setBackgroundColor(colors.lightBlue)
  write("  ")
  term.setBackgroundColor(colors.gray)
  write("												 ")
  event, button, X, Y = os.pullEvent("mouse_click")
	if X <= 2 and Y==19 then
	  term.setBackgroundColor(colors.lightBlue)
	  printT(progs,1,18 - #progs,false)
	  event, button, X, Y = os.pullEvent("mouse_click")
	if X <= checkLen(progs) and Y >= 19 - #progs and Y < 19 then
	  spot = 18 - #progs
	  term.setBackgroundColor(colors.black)
	  term.clear()
	  term.setCursorPos(1,1)
	  shell.run(progs[Y - spot])
	  break
	end
  end
end
end
progs = getdata("progsfile")
desktop()
If you're going to copy and paste the code please edit a file called
progsfile
and follow the formating in the screenshot example.*On line 13 there is a /> on the end of the line that i cant seem to remove so you can do that manually until i fix it(maybe its just me)*
Changelog:
Spoiler1.0.0- Initial release
1.0.1-fixed bug with clicking one above the menu, added local to all possible functions/variables, added a loop to prevent crashes, updated pastebin.(Thank you BigSHinyToys)
BigSHinyToys #2
Posted 09 April 2013 - 04:36 PM
You have a recursive function that will eventually cause a your program to crash. Might want to change it to a loop
Example :

while true do
write("User : ")
local input = read()
if input == "admin" then
break
end
end
Other than than looks pretty good.
[EDIT]
Also might want to use local variables and functions.
Berge #3
Posted 09 April 2013 - 04:46 PM
You have a recursive function that will eventually cause a your program to crash. Might want to change it to a loop
Example :

while true do
write("User : ")
local input = read()
if input == "admin" then
break
end
end
Other than than looks pretty good.
[EDIT]
Also might want to use local variables and functions.
Thanks for the feedback! But are you referring to the two else statements thet lead back to desktop() ? Im not entirely sure. Fixed
Edited on 09 April 2013 - 03:32 PM