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

Double-Screen Redirecting

Started by Ajt86, 15 December 2013 - 10:53 PM
Ajt86 #1
Posted 15 December 2013 - 11:53 PM
I'm trying to redirect the terminal screen so that I can get it to display on the computer and a monitor at the same time, I got it working but there's one problem. My monitor is 8x5 blocks and it's designed to have a two-thick green border except at the bottom where it is one-thick. I'm trying not to get any characters to write on the border, and I managed to do so although… Special characters keep getting drawn on the right side of the monitor even though they shouldn't when I scroll to the left in the program "edit". Is there anything I can do to fix it?

This is the file name config:

monitor = "monitor_4"
title = "Projection"
subtitle = "Projection of Computer"..os.getComputerID()
This is the main program file (the one you run):
Spoiler

os.loadAPI(shell.getRunningProgram().."/../config")
local monitor = peripheral.wrap(config.monitor)
local lastBgColor = colors.black
function getCursorPos()
  return term.native.getCursorPos()
end
function getSize()
  return term.native.getSize()
end
function scroll(qtn)
  monitor.scroll(qtn)
  term.native.scroll(qtn)
end
function clear()
  local x, y = getCursorPos()
  setCursorPos(1, 1)
  local _, h = getSize()
  for i = 1, h + 1 do
	clearLine()
	setCursorPos(1, i)
  end
  setCursorPos(x, y)
  term.native.clear()
end
function clearLine()
  local x, y = getCursorPos()
  setCursorPos(1, y)
  local w = getSize()
  for i = 1, w do
	monitor.write(" ")
  end
  setCursorPos(x, y)
  term.native.clearLine()
end
function write(text)
  local w = getSize()
  local x, y = getCursorPos()
  rtext = string.sub(text, 1, w - x + 1)
  if x < 1 then
	monitor.setCursorPos(1 + 2, y + 2)
	rtext = string.sub(rtext, 2 - x, #rtext)
  end
  monitor.write(rtext)
  term.native.write(text)
end
function setCursorPos(x, y)
  local w, h = getSize()
  local mx, my = x, y
  if y < 1 or y > h then
	my = h + 3
  end
  monitor.setCursorPos(mx + 2, my + 2)
  term.native.setCursorPos(x, y)
end
function setCursorBlink(blink)
  monitor.setCursorBlink(blink)
  term.native.setCursorBlink(blink)
end
function isColor()
  return monitor.isColor()
end
function setTextColor(color)
  monitor.setTextColor(color)
  term.native.setTextColor(color)
end
function setBackgroundColor(color)
  monitor.setBackgroundColor(color)
  term.native.setBackgroundColor(color)
  lastBgColor = color
end
local redirect = {
  getCursorPos = getCursorPos;
  getSize = getSize;
  scroll = scroll;
  clear = clear;
  clearLine = clearLine;
  write = write;
  setCursorPos = setCursorPos;
  setCursorBlink = setCursorBlink;
  isColor = isColor;
  isColour = isColor;
  setTextColor = setTextColor;
  setBackgroundColor = setBackgroundColor;
  setTextColour = setTextColor;
  setBackgroundColour = setBackgroundColor;
}
monitor.setTextScale(1.5)
local width, height = monitor.getSize()
monitor.setTextColor(colors.white)
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setCursorPos(width/2 - #config.title/2 + #config.title%2, 1)
monitor.setBackgroundColor(colors.green)
monitor.clearLine()
monitor.write(config.title)
monitor.setCursorPos(width/2 - #config.subtitle/2 + #config.subtitle%2, 2)
monitor.clearLine()
monitor.write(config.subtitle)
local y = 3
while y < height do
  monitor.setCursorPos(1, y)
  monitor.write("  ")
  monitor.setCursorPos(width - 1, y)
  monitor.write("  ")
  y = y + 1
end
monitor.setCursorPos(1, height)
monitor.clearLine()
redirect.setCursorPos(1, 1)
redirect.setBackgroundColor(colors.black)
shell.setDir("")
redirect.clear()
redirect.setCursorPos(1, 1)
redirect.setTextColor(colors.yellow)
redirect.write("CraftOS 1.5")
redirect.setCursorPos(1, 2)
redirect.setTextColor(colors.white)
term.redirect(redirect)
os.unloadAPI("config")
Edit: The only way I found is to redraw the pixel after writing to the screen, although this could cause screen flicker.
Edit 2: It only happens with magic characters and punctuation

Before:[attachment=1415:2013-12-16_05.00.01.png]

After:[attachment=1416:2013-12-16_05.01.09.png]

Thanks for the help, Ajt86
Edited on 16 December 2013 - 06:36 AM
Ajt86 #2
Posted 16 December 2013 - 07:11 AM
For half a day, nobody's replied. Is that because nobody can? If nobody can reply therefore this is most likely to be a bug with Computercraft.
theoriginalbit #3
Posted 16 December 2013 - 08:08 AM
I'd assume its something to do with the term.getSize call returning a width higher than what you've allowed?
Ajt86 #4
Posted 16 December 2013 - 08:24 AM
But the redirect.getSize() method returns the width and height of the computer, and if that were the case, I would get letters being written on the border, even though I'm only getting magic characters and punctuation.
The redirect.write(text) method is written not to write text on the borders.
The redirect.setCursorPos(x, y) method is written to not allow the user to set the cursor position on the border.
Edited on 16 December 2013 - 07:26 AM
MKlegoman357 #5
Posted 16 December 2013 - 09:06 AM
For half a day, nobody's replied. Is that because nobody can?

Half a day is a small amount of time to wait. There aren't always people online when you need help. Sometimes it takes a few days to find the answer.

If nobody can reply therefore this is most likely to be a bug with Computercraft.

This is most likely to be a bug with your code. Never blame CC if your program doesn't work like it should be.

Before asking questions, debug your program. Put some prints and sleeps in places where you get values, check if they were correct, re-read your code. You can also make a break, watch your favorite movie, read a book or make your homework then come back to your code and maybe you'll see the problem.

It may be a problem with your write function.
Ajt86 #6
Posted 16 December 2013 - 10:35 AM
I have spent a few hours debugging before posting though, I checked, double-checked, triple-checked my code, I found nothing wrong. The write function is mysteriously displaying magic characters and punctuation at the end of the line on the border only when I scroll to the left
Ajt86 #7
Posted 16 December 2013 - 11:12 AM
Well, by following the logic of the write function, there is absolutely NO code that causes it to print ONLY magic characters and punctuation "outside" the screen. Inside the function, I wrote a piece of code designed to change the string into a string that is shortened to prevent it from writing over the border. I temporarily wrote something to redraw the border although it causes it to flicker. I guess I'll have to leave it like that for now…