function allColour(tColour, bColour)
if term.isColour() then
term.setTextColour(colours[tColour])
term.setBackgroundColour(colours[bColour])
else
term.setTextColour(colours.white)
term.setBackgroundColour(colours.black)
end
end
function indented(str, indent, height, tColour, bColour)
term.setCursorPos(indent, height)
if tColour and bColour then
allColour(tColour, bColour)
elseif tColour then
textColour(tColour)
elseif bColour then
bgColour(bColour)
end
term.write(str)
end
function drawBorder(xMin, yMin, xMax, yMax, colour)
if term.isColour() then
paintutils.drawBox(xMin, yMin, xMax, yMax, colours[colour])
end
end
function drawBox(xMin, yMin, xMax, yMax, colour)
if term.isColour() then
paintutils.drawFilledBox(xMin, yMin, xMax, yMax, colours[colour])
end
end
function drawAlertBox(xMin, yMin, xMax, yMax, title, titleColour, txt, txtColour)
X = xMin + 2
Y = yMin + 2
-- Draws the box
drawBorder(xMin, yMin, xMax, yMax, "grey")
drawBox(xMin + 1, yMin + 1, xMax - 1, yMax - 1, "lightGrey")
-- Prints the title
allColour(titleColour, "grey")
term.setCursorPos(xMax / 2 - #title / 2, yMin)
term.write(title)
-- Prints the text
indented(txt, X, Y, txtColour, "lightGrey")
indented(" OK ", xMax - 4, yMax - 1)
while true do
local event = { os.pullEvent() }
if event[1] == "mouse_click" then
if event[2] == 1 and event[3] >= xMax - 4 and event[3] <= xMax - 1 and event[4] == yMax - 1 then
printAPI.drawBox(xMin, yMin, xMax, yMax, "black")
end
end
end
end
How to shorten a string and print it
Started by _removed, Feb 22 2015 07:29 PM
9 replies to this topic
#1
Posted 22 February 2015 - 07:29 PM
For RetroOS, I am making an API for designing. One of the functions is to create a popup box that tells the user information. However, the text on the popup goes over the boundaries of the box and makes it look scruffy and ugly to look at. I want it so that if the text goes over a certain amount, it will extend the popup and print it on the next line. How would I achieve this? This is the code i have so far:
#2
Posted 22 February 2015 - 07:34 PM
It sounds like you are looking for something like the 'print' function, which makes sure that text fits on the screen
I believe the print function is in the bios, you can take a look at how it works.
I believe the print function is in the bios, you can take a look at how it works.
#3
Posted 22 February 2015 - 07:41 PM
HPWebcamAble, on 22 February 2015 - 07:34 PM, said:
It sounds like you are looking for something like the 'print' function, which makes sure that text fits on the screen
I believe the print function is in the bios, you can take a look at how it works.
I believe the print function is in the bios, you can take a look at how it works.
I know how the print function works, but the popup isn't the size of the terminal. It is smaller.
#4
Posted 22 February 2015 - 07:45 PM
smigger22, on 22 February 2015 - 07:41 PM, said:
I know how the print function works, but the popup isn't the size of the terminal. It is smaller.
You could just make the popup longer to get around that.
Otherwise, you'll need to make a modified version of the print function
#5
Posted 22 February 2015 - 08:35 PM
Also how would I make the X coordinate set to a certain place? This example
drawAlertBox(5, 5, x - 4, y - 4, "Error", "red", "We have experienced\n an error and are\n trying to fix this.", "black") -- How do i make the \n go to a certain X coordinate?
#6
Posted 22 February 2015 - 10:58 PM
smigger22, on 22 February 2015 - 08:35 PM, said:
Also how would I make the X coordinate set to a certain place? This example
drawAlertBox(5, 5, x - 4, y - 4, "Error", "red", "We have experienced\n an error and are\n trying to fix this.", "black") -- How do i make the \n go to a certain X coordinate?
Maybe I could have said this earlier but I actually wrote a function for this purpose a while ago
local toWrite = "The string to write"
local lText
local space
local lOffset = 0
local pX,pY = 16,7 --#The x and y of the first character
while string.len(toWrite) > 0 do
if lOffset > 12 then break end
if string.len(toWrite) < 36 then --# 36 was how many characters of space I had between 16 and the edge
cp(pX,pY+lOffset)
tw(toWrite)
break
end
space = 0
for i = 1, 36 do
temp = string.sub(toWrite,36-i,36-i)
if temp == " " then
space = i
break
end
end
lText = string.sub(toWrite,1,36-space)
toWrite = string.sub(toWrite,36-space+1)
cp(pX,pY+lOffset)
tw(lText)
lOffset = lOffset+1
end
It might be kind of difficult to adapt to your needs, but I'm sure it can be done.
First try changing the pX and pY, then change all the 36's to the length of the box.
It doesn't care about the \n, it places a word at a time until a word goes off the edge, then it goes to the next line. It uses spaces to determine where words are
The print function actually looks for the \n character I believe.
#7
Posted 23 February 2015 - 03:22 AM
This sounds like a job for string.gmatch() - use that to pull out all the individual lines into a table, and as you go, pay attention to the length of each.
#8
Posted 25 February 2015 - 08:52 PM
HPWebcamAble, on 22 February 2015 - 10:58 PM, said:
smigger22, on 22 February 2015 - 08:35 PM, said:
Also how would I make the X coordinate set to a certain place? This example
drawAlertBox(5, 5, x - 4, y - 4, "Error", "red", "We have experienced\n an error and are\n trying to fix this.", "black") -- How do i make the \n go to a certain X coordinate?
Maybe I could have said this earlier but I actually wrote a function for this purpose a while ago
local toWrite = "The string to write" local lText local space local lOffset = 0 local pX,pY = 16,7 --#The x and y of the first character while string.len(toWrite) > 0 do if lOffset > 12 then break end if string.len(toWrite) < 36 then --# 36 was how many characters of space I had between 16 and the edge cp(pX,pY+lOffset) tw(toWrite) break end space = 0 for i = 1, 36 do temp = string.sub(toWrite,36-i,36-i) if temp == " " then space = i break end end lText = string.sub(toWrite,1,36-space) toWrite = string.sub(toWrite,36-space+1) cp(pX,pY+lOffset) tw(lText) lOffset = lOffset+1 end
It might be kind of difficult to adapt to your needs, but I'm sure it can be done.
First try changing the pX and pY, then change all the 36's to the length of the box.
It doesn't care about the \n, it places a word at a time until a word goes off the edge, then it goes to the next line. It uses spaces to determine where words are
The print function actually looks for the \n character I believe.
What are the cw and tw functions for?
#9
Posted 26 February 2015 - 12:15 AM
I've wrapped up an example of BombBloke's suggestion about using string.gmatch
Example
Edited by TheOddByte, 26 February 2015 - 12:15 AM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











