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

Is it possible to make the program print files?

Started by TehTotalPwnage, 11 November 2012 - 02:41 PM
TehTotalPwnage #1
Posted 11 November 2012 - 03:41 PM
Ok, I know SOME lua, but is it possible to make it so while the program is running, the program makes it print files(like os.print(install)) or something like that? I'm making a program thing and want it to print certain files.
Lyqyd #2
Posted 11 November 2012 - 04:08 PM
What sort of print? Do you mean with the printer peripheral, or do you just want to spit the file contents onto the screen? If it's the latter, try:


local handle = io.open("file", "r")
if handle then
  print(handle:read("*a"))
  handle:close()
else
  print("File Not Found")
end
Mads #3
Posted 12 November 2012 - 06:15 AM
God damn, RTFM.
Lyqyd #4
Posted 12 November 2012 - 06:49 AM
We are here to help people learn, and being hostile toward question-askers for no reason isn't how we accomplish that, mad. It wouldn't hurt to provide a link to the manual, either: http://www.lua.org/manual/5.1/
TehTotalPwnage #5
Posted 12 November 2012 - 06:59 AM
What sort of print? Do you mean with the printer peripheral, or do you just want to spit the file contents onto the screen? If it's the latter, try:


local handle = io.open("file", "r")
if handle then
  print(handle:read("*a"))
  handle:close()
else
  print("File Not Found")
end
I mean make it print the file using the printer peripheral.
EDIT: Is it possible to run a program from a program?
Lyqyd #6
Posted 12 November 2012 - 07:45 AM
Yes, you can run another program by using shell.run("path/to/program", "any", "program", "arguments"). For instance, to edit a file named "filename", you could run shell.run("/rom/programs/edit", "filename").
TehTotalPwnage #7
Posted 12 November 2012 - 07:51 AM
I'm assuming you do the same thing for printing files?
Lyqyd #8
Posted 12 November 2012 - 08:04 AM
I don't know of any programs that come with ComputerCraft to automatically print out files. The printer peripheral documentation may be helpful, or you could look through the default edit program to see how it prints files out.
TehTotalPwnage #9
Posted 12 November 2012 - 08:18 AM
I don't know of any programs that come with ComputerCraft to automatically print out files. The printer peripheral documentation may be helpful, or you could look through the default edit program to see how it prints files out.
I'm sorry if I'm acting like a noob right now but, how do I look through the default edit program? It doesn't work with edit edit or edit /rom/programs/edit or anything
Shnupbups #10
Posted 12 November 2012 - 08:29 AM
OK. To print a file follow these steps:

1. Wrap the printer (side is the side the printer is on) :

prnt = peripheral.wrap("side")
2. Type Lyqyd's code, with a couple of tweaks :

local handle = io.open("file", "r")
if handle then
  prnt.write(handle:read("*a"))
  handle:close()
else
  print("File Not Found")
end
3. Save, Exit and run the program. Now, this WILL run off the pages. I had an API on SMP a while back to wrap the text properly, so it IS possible, just I don't know how to do it. (Some other guy gave it to me)
Lyqyd #11
Posted 12 November 2012 - 08:33 AM
I don't know of any programs that come with ComputerCraft to automatically print out files. The printer peripheral documentation may be helpful, or you could look through the default edit program to see how it prints files out.
I'm sorry if I'm acting like a noob right now but, how do I look through the default edit program? It doesn't work with edit edit or edit /rom/programs/edit or anything

edit /rom/programs/edit should have worked. You can look through the mod folder, go into mods/computercraft/lua/rom/programs/ and open up the edit program in your favorite text editor. Computercraft may be either a zip file or a folder, depending on your installation.
TehTotalPwnage #12
Posted 12 November 2012 - 10:07 AM
OK. To print a file follow these steps:

1. Wrap the printer (side is the side the printer is on) :

prnt = peripheral.wrap("side")
2. Type Lyqyd's code, with a couple of tweaks :

local handle = io.open("file", "r")
if handle then
  prnt.write(handle:read("*a"))
  handle:close()
else
  print("File Not Found")
end
3. Save, Exit and run the program. Now, this WILL run off the pages. I had an API on SMP a while back to wrap the text properly, so it IS possible, just I don't know how to do it. (Some other guy gave it to me)
Might work, but I got this error:

bios:338: [string "print"]:7: Function arguments expected
TehTotalPwnage #13
Posted 12 November 2012 - 10:49 AM
I don't know of any programs that come with ComputerCraft to automatically print out files. The printer peripheral documentation may be helpful, or you could look through the default edit program to see how it prints files out.
I'm sorry if I'm acting like a noob right now but, how do I look through the default edit program? It doesn't work with edit edit or edit /rom/programs/edit or anything

edit /rom/programs/edit should have worked. You can look through the mod folder, go into mods/computercraft/lua/rom/programs/ and open up the edit program in your favorite text editor. Computercraft may be either a zip file or a folder, depending on your installation.
Thanks! I think I found the function. Here's the code:

    Print=function()
        local sPrinterSide = nil
        for n,sSide in ipairs(rs.getSides()) do
            if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "printer" then
                sPrinterSide = sSide
                break
            end
        end
        
        if not sPrinterSide then
            sStatus = "No printer attached"
            return
        end

        local nPage = 0
        local sName = fs.getName( sPath )
        local printer = peripheral.wrap(sPrinterSide)
        if printer.getInkLevel() < 1 then
            sStatus = "Printer out of ink"
            return
        elseif printer.getPaperLevel() < 1 then
            sStatus = "Printer out of paper"
            return
        end
        
        local terminal = {
            getCursorPos = printer.getCursorPos,
            setCursorPos = printer.setCursorPos,
            getSize = printer.getPageSize,
            write = printer.write,
        }
        terminal.scroll = function()
            if nPage == 1 then
                printer.setPageTitle( sName.." (page "..nPage..")" )            
            end
            
            while not printer.newPage()    do
                if printer.getInkLevel() < 1 then
                    sStatus = "Printer out of ink, please refill"
                elseif printer.getPaperLevel() < 1 then
                    sStatus = "Printer out of paper, please refill"
                else
                    sStatus = "Printer output tray full, please empty"
                end
    
                term.restore()
                redrawMenu()
                term.redirect( terminal )
                
                local timer = os.startTimer(0.5)
                sleep(0.5)
            end

            nPage = nPage + 1
            if nPage == 1 then
                printer.setPageTitle( sName )
            else
                printer.setPageTitle( sName.." (page "..nPage..")" )
            end
        end
        
        bMenu = false
        term.redirect( terminal )
        local ok, error = pcall( function()
            term.scroll()
            for n, sLine in ipairs( tLines ) do
                print( sLine )
            end
        end )
        term.restore()
        if not ok then
            print( error )
        end
        
        while not printer.endPage() do
            sStatus = "Printer output tray full, please empty"
            redrawMenu()
            sleep( 0.5 )
        end
        bMenu = true
            
        if nPage > 1 then
            sStatus = "Printed "..nPage.." Pages"
        else
            sStatus = "Printed 1 Page"
        end
        redrawMenu()
    end,
    Exit=function()
        bRunning = false
    end
}
>.< That's a lot of code
Patey #14
Posted 03 May 2013 - 04:09 PM
I know it's old but I'm trying to do something similar and it saves from having to create a new topic haha- I have a terminal and a printer and I've written a basic code to bring up a selection menu.

print("Welcome to the server!")
print("Here you can print out some useful cheat sheets.")
print("Printouts:")
print("1. Commands list")
print("2. Mod List")
write("Select a number:")
input = read()
if input = "1" then
<need print code here>
os.reboot()
elseif input = "2" then
<need print code here>
os.reboot()
else
os.reboot()
I'll start with making it print a file,but I'd like it to print from a computer in another location, so that I can make this code the startup program and set it to prevent ctrl+T to terminate (if thats possible).
Kingdaro #15
Posted 03 May 2013 - 04:30 PM
This tutorial should help you with printing: http://www.computercraft.info/forums2/index.php?/topic/4375-142-printer-tutorial/

Though there seem to be a couple of problems with your code at the moment. One of them, is when you're comparing two variables to see if they equal one another, you need to use the double equal sign "==". When you're setting variables, use the single equal sign.


input = read()
if input == "1" then

Another problem is that your code lacks an end to its if-elseif-else chain. For every if there is an end, same with "for", "while", "function", and "do".


print("Welcome to the server!")
print("Here you can print out some useful cheat sheets.")
print("Printouts:")
print("1. Commands list")
print("2. Mod List")
write("Select a number:")
input = read()
if input == "1" then
-- print code
os.reboot()
elseif input == "2" then
-- print code
os.reboot()
else
os.reboot()
end

The last problem, is that you're rebooting the computer instead of using a loop, which is a much better and much less hacky way of running your program. the best way of running a loop forever is using "while true do", and ending it off, of course.


while true do
print("Welcome to the server!")
print("Here you can print out some useful cheat sheets.")
print("Printouts:")
print("1. Commands list")
print("2. Mod List")
write("Select a number:")
input = read()
if input == "1" then
-- print code
elseif input == "2" then
-- print code
end
end

This isn't as much of a problem as it is a suggestion. When every if, while, function and do needs an end, things can get confusing when you think about what needs to be ended and where. If you indent your code, it's much easier to see where a block of code ends. You can indent using the tab key.

This is what your code will look like once you've fixed the issues and made it look a little prettier:

while true do
  print("Welcome to the server!")
  print("Here you can print out some useful cheat sheets.")
  print("Printouts:")
  print("1. Commands list")
  print("2. Mod List")
  write("Select a number:")
  input = read()
  if input == "1" then
    -- print code
  elseif input == "2" then
    -- print code
  end
end
Patey #16
Posted 03 May 2013 - 06:26 PM
thanks. so I've got the program to print the file by writing a program that prints using that tutorial and running it using shell.run("commands"),
but any idea how I would store the files on a terminal and access them from the printer terminal when needed?

also, is there a way to make the printer eject the page upon printing? with the protection plugin users can't access the printer to take the page