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

Fs.list In A Clickable Menu

Started by mrdawgza, 26 October 2013 - 04:37 PM
mrdawgza #1
Posted 26 October 2013 - 06:37 PM
Hello, I have a question but not the mind to solve it…
The question sorta evolves around my OS.

So to get things straight;
I have a folder in the computer folder called Programs
I have a click-able Programs menu

Menu (Extracted from my OS, Desktop; Cleaned)

print("Program1")
print("Program2")
dmg = 0
while true do
local event, button, X, Y = os.pullEventRaw()
  if dmg == 0 then
	if event == "mouse_click" then

	 if X >=x and X <=x and Y==y and button ==1 --Program 1,
	  shell.run("programs/program1")

	elseif X >=x and X <=x and Y==y and button ==1 --Program 2,
	  shell.run("programs/program2")

   end  
  end
end
end

Now if I were to put say, program3 into programs; how could I get it to print Program3 after Program2 and make another if X> … and shell.run("programs/program3")
and then rename Program3 to hello it would do the same there, the print becomes hello and the if does shell.run("programs/hello"). And if I deleted programs/hello it would
disappear from the print and if.
So I think the finished code, after I do what I just said will look like this;


print("Program1")
print("Program2")
print("hello")
dmg = 0
while true do
local event, button, X, Y = os.pullEventRaw()
  if dmg == 0 then
	if event == "mouse_click" then

	 if X >=x and X <=x and Y==y and button ==1 --Program 1,
	  shell.run("programs/program1")

	elseif X >=x and X <=x and Y==y and button ==1 --Program 2,
	  shell.run("programs/program2")

   elseif X >=x and X <=x and Y==y and button ==1 --hello,
	  shell.run("programs/hello")

   end  
  end
end
end

Also, where I said what I want it to do, I said "and the if does ..", could I also say "and the if carries..", does it mean the same thing or do does and carries mean different things?
Bomb Bloke #2
Posted 26 October 2013 - 11:28 PM
Assuming that's pseudo-code ("x" and "y" are hopefully placeholders for a variety of different values), your proposed method of expanding it will work. Using fs.list per your thread title leads to quite different code, however.

To implement it, you essentially throw the names of the programs into a table, use a "for" loop to draw them on the screen, then another "for" loop to check which has been clicked.

A mock-up example, using two methods of checking what's clicked on (note only one is "needed", but hopefully this'll help your understanding):

local ProgramFolder = "programs/"
local Program = fs.list(ProgramFolder)  -- Puts the names of the programs in a table.
for i=1,#Program do print(Program[i]) end  -- Prints all the programs on the screen.

while true do  -- Start an infinite loop.
  local event, button, X, Y = os.pullEvent()  -- No need for raw events here.

  if event == "mouse_click" and button == 1 then  -- Only one check for the correct "button" is needed.

	-- First check example (with a loop):
	for i=1,#Program do
	  if X > 0 and X <= #Program[i] and Y == i then  -- #Program[i] returns the character
		shell.run(ProgramFolder..Program[i])		 -- count of that program name.
	  end
	end

	-- Or, checking without a loop:
	if Program[Y] ~= nil then
	  if X > 0 and X <= #Program[Y] then shell.run(ProgramFolder..Program[Y]) end
	end  -- Is it even possible for X to be less then 1?

  end
end

To "do" something typically means to perform that action.

To "carry" something typically means to bring it through or along something else.
mrdawgza #3
Posted 27 October 2013 - 03:23 AM
Thanks I'll look into it just now…
TheOddByte #4
Posted 27 October 2013 - 08:39 AM
Here's a quick example for you

Listing function

local function list(folder)
local programs = {}
local x = 1
for _, file in ipairs(fs.list(folder)) do
    local nFile = {}
    nFile.name = file
    nFile.exe = folder.."/"..file
    nFile.sX = x
    nFile.fX = x + #file
    nFile.y = 1
    x = x + ( #file + 1 )
    table.insert(programs, nFile)
end
return programs
end

Then using it


while true do
--# Updating the program list
local programs = list("programs/")

--# Drawing the programs to the screen
for i = 1, #programs do
    term.setCursorPos(programs[i].sX,1)
    term.write(programs[i].name)
end

--# Checking if they were clicked
local evt, p1, mX, mY = os.pullEvent("mouse_click")
if p1 == 1 then
    for i = 1, #programs do
        if mX >= programs[i].sX and mX <= programs[i].fX and mY == programs[i].y then
            shell.run(programs[i].exe)
            break
        end
    end
end

end

I typed this on my phone so sorry if there was something wrong.. ( I have 3% battery left now D: )