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

Monitor text, make new lines?

Started by Tusillody, 15 July 2012 - 04:50 PM
Tusillody #1
Posted 15 July 2012 - 06:50 PM
So I'm working on a computer program for a server I play on, and its going to be in the spawn. So im setting up a rules function, and everything's working except for the fact, I cant use mon.print(" ")

All I can use for text is mon.write("") and I cant figure out how to make the text go to a new line. Keep in mind, this is a function in an ongoing program, not monitor right blah.

Can anyone help me?
Ponder #2
Posted 15 July 2012 - 06:57 PM
I haven't read that much about monitors, but shouldn't mon.write ('n') do the trick?
Tusillody #3
Posted 15 July 2012 - 07:03 PM
I thought it would, but when I have

mon.write ("Rule 1: n")

It shows a question mark. And when I have it outside of quotations, it throws an error.
1lann #4
Posted 15 July 2012 - 07:14 PM
I thought it would, but when I have

mon.write ("Rule 1: n")

It shows a question mark. And when I have it outside of quotations, it throws an error.
I believe it's /n. Also why don't you use mon.setCursorPos?

Edit: dammit I was wrong again :P/>/> keep on getting the / and mixed up
Tusillody #5
Posted 15 July 2012 - 07:17 PM
/n inside quotations just displays Rule 1: /n.
And /n outside, gives an error for trying to use arithmetic on a string.

And Because i have over 10 lines of rules, i was trying to make it seem a bit neater. But If nothing else works, Ill just try that :P/>/>
MysticT #6
Posted 15 July 2012 - 08:14 PM
The easier way would be to redirect the terminal output and then use print:

local mon = peripheral.wrap("<side>") -- I guess you already know this
term.redirect(mon) -- redirect the output to the monitor
print("Hello World!!") -- use print, write, and any function that uses the terminal, it will show in the minitor
term.restore() -- after printing everything, restore the output to the terminal

BTW, this is how the monitor program works.
Tusillody #7
Posted 15 July 2012 - 08:26 PM
The easier way would be to redirect the terminal output and then use print:

local mon = peripheral.wrap("<side>") -- I guess you already know this
term.redirect(mon) -- redirect the output to the monitor
print("Hello World!!") -- use print, write, and any function that uses the terminal, it will show in the minitor
term.restore() -- after printing everything, restore the output to the terminal

BTW, this is how the monitor program works.

Awesome! This will work perfect, thank you!
bloodless2010 #8
Posted 15 July 2012 - 08:43 PM
If you want to make a new line, you could also use setCursorPos,
so for example, you would have

local mon = peripheral.wrap("<side>") – I guess you already know this
term.redirect(mon) – redirect the output to the monitor
print("Hello World!!") – use print, write, and any function that uses the terminal, it will show in the minitor
term
.restore() – after printing everything, restore the output to the terminal
as MysticT said, But if you want to make another line for rules example it would be like this:

local mon = peripheral.wrap("<side>")
term.redirect(mon)
mon.setCursorPos(1,1)
print("Rules:")
mon.setCursorPos(1,2) – this sets it to the second line
print("1: No Griefing!")
term.restore()

and so forth, it should work, it's how I did it with mine. Goodluck!!
MysticT #9
Posted 15 July 2012 - 08:47 PM
If you want to make a new line, you could also use setCursorPos,
so for example, you would have

local mon = peripheral.wrap("<side>") – I guess you already know this
term.redirect(mon) – redirect the output to the monitor
print("Hello World!!") – use print, write, and any function that uses the terminal, it will show in the minitor
term
.restore() – after printing everything, restore the output to the terminal
as MysticT said, But if you want to make another line for rules example it would be like this:

local mon = peripheral.wrap("<side>")
term.redirect(mon)
mon.setCursorPos(1,1)
print("Rules:")
mon.setCursorPos(1,2) – this sets it to the second line
print("1: No Griefing!")
term.restore()

and so forth, it should work, it's how I did it with mine. Goodluck!!

And why don't you just do:

local mon = peripheral.wrap("<side>")
term.redirect(mon)
print("Rules:")
print("1: No Griefing!")
-- All the prints you want here
term.restore()
It would have the same effect, since print already moves the cursor to the next line.
bloodless2010 #10
Posted 15 July 2012 - 08:49 PM
If you want to make a new line, you could also use setCursorPos,
so for example, you would have

local mon = peripheral.wrap("<side>") – I guess you already know this
term.redirect(mon) – redirect the output to the monitor
print("Hello World!!") – use print, write, and any function that uses the terminal, it will show in the minitor
term
.restore() – after printing everything, restore the output to the terminal
as MysticT said, But if you want to make another line for rules example it would be like this:

local mon = peripheral.wrap("<side>")
term.redirect(mon)
mon.setCursorPos(1,1)
print("Rules:")
mon.setCursorPos(1,2) – this sets it to the second line
print("1: No Griefing!")
term.restore()

and so forth, it should work, it's how I did it with mine. Goodluck!!

And why don't you just do:

local mon = peripheral.wrap("<side>")
term.redirect(mon)
print("Rules:")
print("1: No Griefing!")
-- All the prints you want here
term.restore()
It would have the same effect, since print already moves the cursor to the next line.
Oh ok, I'm new to this I didn't know! Thanks! :P/>/>
KaoS #11
Posted 19 September 2012 - 06:57 PM
I have a question: can you do the following?


local function redirector(side)
  term.redrect(peripheral.wrap(side))
  local temp={}
  for k,v in pairs(term) do
    temp[k]=v
  end
  term.restore()
  return temp
end

and that will return a table of functions including the print one
Cranium #12
Posted 19 September 2012 - 07:15 PM
If you feel really abitious, you could write a whole new write function just for your program that automatically adds a new line break into your strings as you call back to it.
GopherAtl #13
Posted 19 September 2012 - 07:16 PM
there's no print function in the term, it's a global that just uses term's write and get/setCursorPos functions, so… nope, sorry

the print function isn't magic, though. You could implement your own easily enough…

function printTo(device,text)
  local lines={}
  --break into lines on ns
  --we add a n at the end, so it behaves like print and so the match can find the last line
  string.gsub(text.."n","(.-)n",function(v) lines[#lines+1]=v end)
  local _,row=device.getCursorPos()
  local _,height=device.getSize()
  for i=1,#lines do
	--write the line
	device.write(lines[i])
	--move to next row
	row=row+1
	--if this went off the screen, scroll
	if row>height then
	  row=height
	  device.scroll(1)
	end
	--set to start of next line
	device.setCursorPos(1,row)
  end
end

:edit2: note that this doesn't support tabs (t).
MysticT #14
Posted 19 September 2012 - 07:24 PM
Also, it would return the term api functions, wich don't change when you redirect, they are always the same.
d0min8r3 #15
Posted 20 October 2012 - 04:14 AM
You can actually just type;

x = 1
y = 1
function setPoint()
mon.setCursorPos(x,y)
end

setPoint()
mon.write"Hello, User!"
y = y+1
setPoint()
mon.write"This is the Next line!"
Kingdaro #16
Posted 20 October 2012 - 04:46 AM
What about using multiline strings?

mon.write [[
Rules
---------
1. Follow rule #2
2. Follow rule #3
3. Follow rule #1
]]