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

Can't reset cursorPos on a monitor

Started by Desslink, 09 May 2014 - 12:08 AM
Desslink #1
Posted 09 May 2014 - 02:08 AM
Hi everyone, first I want to apologize for any mistake made with my English because I'm French.

So want I want to do is a to do list on my computer which can be displayed on a monitor above it. Here is my code:


while true do
term.clear()
term.setCursorPos(1, 1)
print("Type your sentence")
input = read()
paripheral.call("top", "clear")
sleep(1)
peripheral.call("top", "write", input)
end
end

When I type something in, it works well but when i type another thing it clear the screen and write the new sentence like if it was the old sentence but invisible. Example:


1st: just a test
2nd:            another one

The second thing is that the program doesn't make auto line breaks (my monitor is 3X6)

So can you at least help me with this, even better would be to add a button next to the computer (left) that can clear the screen so that we can enter multiple times something in the computer and that will not erase the previous sentence.

Thanks you, Desslink
Edited on 09 May 2014 - 12:09 AM
CometWolf #2
Posted 09 May 2014 - 05:27 AM
The thing is, write dosen't move the cursor to a new line when it's done writing. You have to move the cursor yourself, or use term.redirect so you get acess to print. And obviously not clear the monitor everytime you write to it, which is what causes the first text to be "invisible".
TheOddByte #3
Posted 09 May 2014 - 02:45 PM
To make it easier for yourself you could also do this

local mon = peripheral.wrap( "top" ) --# Wrap the monitor

while true do
    local input = read()
    mon.clear()  --# Clear the screen
    mon.setCursorPos( 1, 1 )  --# Set the position
    mon.write( input )
end
As you see you can use the term functions with the variable mon as it's wrapped to the monitor
I feel like it's easier to wrap the peripheral and then calling functions instead of using peripheral.call

Also, if you're doing a todo list it would be smart to store everything in a table

local list = {}
local mon = peripheral.wrap( "top" )

while true do
    local input = read()
    table.insert( list, input ) --# Insert the input into the table
    mon.clear()
    for i = 1, #list do -- # Loop through the table
        mon.setCursorPos( 1, i )
        mon.write( list[i] ) --# Write the text in the index of the table 
    end
end
These are just some examples though, mess around with them and the code and see where it leads you.

If you haven't already then here's some useful links
Tables
Peripheral API
Term API
and even if you have checked them out it's still good to do it again if you feel unsure about something.