Ebbe422

TextToDisplay_WithOverflow(Final)

Jun 27th, 2024 (edited)
673
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | Gaming | 0 0
  1. local displayLink = peripheral.wrap("right")
  2. local chatBox = peripheral.wrap("left")
  3.  
  4. local displaySizeY, displaySizeX-- Stores the display size
  5.  
  6. local displayedText = "" -- This is the input text
  7. local currentChunkRVRS = "" -- This is the reversed input text
  8. local conditionedInput = "" -- This is the final substring
  9.  
  10. local stringStartpoint = 1 -- This is the start point of the previous chunk check
  11. local stringEndpoint = 1 -- This is the end point of the previous chunk check
  12. local finalStartpoint = 1 -- This is the start point of the current chunk check
  13. local finalEndpoint = 1 -- This is the end point of the current chunk check
  14.  
  15. local spaceDistance = 0 -- This is the number of spaces from the end point, to the location of the space
  16. local numStrings = 1 -- This is the number of loops the cycle has gone through
  17. local currentChunk = "" -- This is the currently worked on text chunk
  18. local currentChunkWidth = 0 -- This is the maximum length of the string search
  19. local displayCommand = ".display" -- This is the command we use to run the code
  20. local spaceToFind = "%s" -- This is the space chararacter we are looking for
  21.  
  22. local finalStrings = {} -- This is the array of completed strings
  23. local finalStringsLenTot = 0 -- This is the length of all completed strings
  24. local removedSpacesNum = 0 -- This is the number of spaces we have removed from the original string
  25.  
  26. -- Clears all current text on the display
  27. local function ClearScreen()
  28.     for i = 1, displaySizeY, 1 do
  29.         displayLink.setCursorPos(1, i)
  30.         displayLink.clearLine()
  31.         --print("Cleared Line")
  32.     end
  33.     displayLink.setCursorPos(1, 1)
  34. end
  35.  
  36. -- Resets all required values to their defaults
  37. local function ResetValues()
  38.     displayLink = peripheral.wrap("right")
  39.     finalStrings = {}
  40.     displaySizeY, displaySizeX = displayLink.getSize() -- Gets the display size
  41.     stringStartpoint = 1
  42.     finalStartpoint = 1
  43.     stringEndpoint = displaySizeX
  44.     finalEndpoint = displaySizeX
  45.     displayLink.setCursorPos(1,1)
  46.     numStrings = 1
  47.     finalStringsLenTot = 0
  48.     removedSpacesNum = 0
  49.     spaceDistance = 0
  50. end
  51.  
  52. --Updates the Display Board
  53. local function Update()
  54.     displayLink.update()
  55. end
  56. --Runs initial Display Link update (On server launch the displaylink requires a redstone output)
  57. --Could be potentially solved by placing a clock underneath the link instead
  58. local function StartLink()
  59.     redstone.setAnalogOutput("back", 15)
  60.     sleep(0.2)
  61.     redstone.setAnalogOutput("back", 0)
  62.     ResetValues()
  63. end
  64. --Writes the single line string to the board.
  65. local function WriteToDisplaySingle()
  66.     displayLink.write(displayedText)
  67.     Update()
  68. end
  69. --Writes the most recently submitted final string to the board.
  70. local function WriteToDisplay()
  71.     displayLink.write(finalStrings[numStrings])
  72.     Update()
  73.     local cursorX, cursorY = displayLink.getCursorPos()
  74.     displayLink.setCursorPos(1, cursorY + 1)
  75. end
  76.  
  77. --Removes uneccessary spaces at the begining of a string by adjusting the start index and
  78. --running the conditioned input again.
  79. local function RemoveSpaces()
  80.     numSpaces = conditionedInput:find(spaceToFind)
  81.     if numSpaces == 1 then
  82.         finalStartpoint = finalStartpoint + numSpaces
  83.         print("Conditioned Text: ", conditionedInput )
  84.         conditionedInput = displayedText:sub(finalStartpoint, finalEndpoint)
  85.     end
  86.     removedSpacesNum = removedSpacesNum + 1
  87.     return displayedText:sub(stringStartpoint, stringEndpoint)
  88. end
  89.  
  90. --Finds the start and end range of the Output string based on where the closest
  91. --space is to the end of our display
  92. local function FindRange()
  93.     finalStartpoint = stringStartpoint
  94.  
  95.     currentChunkWidth = numStrings * displaySizeX
  96.     print("CurrentChunk width: ", currentChunkWidth)
  97.     print("StartIndex: ", stringStartpoint, " EndIndex: ", stringEndpoint)
  98.  
  99.     currentChunk = displayedText:sub(stringStartpoint, stringEndpoint)
  100.  
  101.     currentChunkRVRS = currentChunk:reverse()
  102.     spaceDistance = currentChunkRVRS:find(spaceToFind)
  103.  
  104.     finalEndpoint = stringEndpoint - spaceDistance
  105.     print("Space Distance: ", spaceDistance, " Final start point: ", finalStartpoint, " Final end point ", finalEndpoint)
  106.     conditionedInput = displayedText:sub(finalStartpoint, finalEndpoint)
  107.     --print("Conditioned Text: ", conditionedInput )
  108. end
  109.  
  110. --Updates the start and endpoint to reflect the new display line
  111. local function LoopDataUpdate()
  112.     finalStringsLenTot = finalStringsLenTot + #conditionedInput --+ spaceDistance
  113.     print("Length of finalstrings", finalStringsLenTot, "Displayed Text Len: ", #displayedText)
  114.  
  115.     stringStartpoint = finalEndpoint + 1
  116.     stringEndpoint = finalEndpoint + displaySizeX
  117.  
  118.     numStrings = numStrings + 1
  119. end
  120. -- Waits for an incoming command, clears the display, then runs the through the functions,
  121. --until we have a final string length equal to the original - removed chars
  122. local function WaitForMessage()
  123.     local event, username, message, uuid, isHidden = os.pullEvent("chat")
  124.     if string.find(message, displayCommand) then
  125.         ClearScreen()
  126.         displayedText = message:sub(#displayCommand + 2) -- displayed text is the entire typed message
  127.         print("Displayed message: ", displayedText)
  128.         if #displayedText < displaySizeX then
  129.             WriteToDisplaySingle()
  130.         else
  131.             while finalStringsLenTot + removedSpacesNum < #displayedText do
  132.                 FindRange()
  133.                 RemoveSpaces()
  134.                 table.insert(finalStrings, conditionedInput)
  135.                 WriteToDisplay()
  136.                 LoopDataUpdate()
  137.                 print(" ")
  138.             end
  139.         end
  140.         print("Length of the array: ", #finalStrings)
  141.         spaceDistance = 1
  142.     end
  143. end
  144.  
  145. --Core loop
  146. StartLink()
  147. sleep(1)
  148. print("Display Width: ", displaySizeX)
  149. while true do
  150.     WaitForMessage()
  151.     ResetValues()
  152. end
Advertisement
Comments
  • Ebbe422
    1 year
    # text 0.65 KB | 0 0
    1. This is a program to take a text input with command .display and output the text onto a Create Display Board, with overflow handling, and dynamic code for different display sizes.
    2. You will need to attach a Display Link on the right side of the computer and build a small redstone circuit to lead from the back of the computer, to the Display Link.
    3. You will also need the Advanced Peripherals ChatBox on the left side of the computer.
    4. The command can be adjusted by editing the "displayCommand" variable at the top of the program.
    5. The sides of the components can be adjusted via:
    6. local displayLink = peripheral.wrap("right")
    7. local chatBox = peripheral.wrap("left")
Add Comment
Please, Sign In to add comment