Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local displayLink = peripheral.wrap("right")
- local chatBox = peripheral.wrap("left")
- local displaySizeY, displaySizeX-- Stores the display size
- local displayedText = "" -- This is the input text
- local currentChunkRVRS = "" -- This is the reversed input text
- local conditionedInput = "" -- This is the final substring
- local stringStartpoint = 1 -- This is the start point of the previous chunk check
- local stringEndpoint = 1 -- This is the end point of the previous chunk check
- local finalStartpoint = 1 -- This is the start point of the current chunk check
- local finalEndpoint = 1 -- This is the end point of the current chunk check
- local spaceDistance = 0 -- This is the number of spaces from the end point, to the location of the space
- local numStrings = 1 -- This is the number of loops the cycle has gone through
- local currentChunk = "" -- This is the currently worked on text chunk
- local currentChunkWidth = 0 -- This is the maximum length of the string search
- local displayCommand = ".display" -- This is the command we use to run the code
- local spaceToFind = "%s" -- This is the space chararacter we are looking for
- local finalStrings = {} -- This is the array of completed strings
- local finalStringsLenTot = 0 -- This is the length of all completed strings
- local removedSpacesNum = 0 -- This is the number of spaces we have removed from the original string
- -- Clears all current text on the display
- local function ClearScreen()
- for i = 1, displaySizeY, 1 do
- displayLink.setCursorPos(1, i)
- displayLink.clearLine()
- --print("Cleared Line")
- end
- displayLink.setCursorPos(1, 1)
- end
- -- Resets all required values to their defaults
- local function ResetValues()
- displayLink = peripheral.wrap("right")
- finalStrings = {}
- displaySizeY, displaySizeX = displayLink.getSize() -- Gets the display size
- stringStartpoint = 1
- finalStartpoint = 1
- stringEndpoint = displaySizeX
- finalEndpoint = displaySizeX
- displayLink.setCursorPos(1,1)
- numStrings = 1
- finalStringsLenTot = 0
- removedSpacesNum = 0
- spaceDistance = 0
- end
- --Updates the Display Board
- local function Update()
- displayLink.update()
- end
- --Runs initial Display Link update (On server launch the displaylink requires a redstone output)
- --Could be potentially solved by placing a clock underneath the link instead
- local function StartLink()
- redstone.setAnalogOutput("back", 15)
- sleep(0.2)
- redstone.setAnalogOutput("back", 0)
- ResetValues()
- end
- --Writes the single line string to the board.
- local function WriteToDisplaySingle()
- displayLink.write(displayedText)
- Update()
- end
- --Writes the most recently submitted final string to the board.
- local function WriteToDisplay()
- displayLink.write(finalStrings[numStrings])
- Update()
- local cursorX, cursorY = displayLink.getCursorPos()
- displayLink.setCursorPos(1, cursorY + 1)
- end
- --Removes uneccessary spaces at the begining of a string by adjusting the start index and
- --running the conditioned input again.
- local function RemoveSpaces()
- numSpaces = conditionedInput:find(spaceToFind)
- if numSpaces == 1 then
- finalStartpoint = finalStartpoint + numSpaces
- print("Conditioned Text: ", conditionedInput )
- conditionedInput = displayedText:sub(finalStartpoint, finalEndpoint)
- end
- removedSpacesNum = removedSpacesNum + 1
- return displayedText:sub(stringStartpoint, stringEndpoint)
- end
- --Finds the start and end range of the Output string based on where the closest
- --space is to the end of our display
- local function FindRange()
- finalStartpoint = stringStartpoint
- currentChunkWidth = numStrings * displaySizeX
- print("CurrentChunk width: ", currentChunkWidth)
- print("StartIndex: ", stringStartpoint, " EndIndex: ", stringEndpoint)
- currentChunk = displayedText:sub(stringStartpoint, stringEndpoint)
- currentChunkRVRS = currentChunk:reverse()
- spaceDistance = currentChunkRVRS:find(spaceToFind)
- finalEndpoint = stringEndpoint - spaceDistance
- print("Space Distance: ", spaceDistance, " Final start point: ", finalStartpoint, " Final end point ", finalEndpoint)
- conditionedInput = displayedText:sub(finalStartpoint, finalEndpoint)
- --print("Conditioned Text: ", conditionedInput )
- end
- --Updates the start and endpoint to reflect the new display line
- local function LoopDataUpdate()
- finalStringsLenTot = finalStringsLenTot + #conditionedInput --+ spaceDistance
- print("Length of finalstrings", finalStringsLenTot, "Displayed Text Len: ", #displayedText)
- stringStartpoint = finalEndpoint + 1
- stringEndpoint = finalEndpoint + displaySizeX
- numStrings = numStrings + 1
- end
- -- Waits for an incoming command, clears the display, then runs the through the functions,
- --until we have a final string length equal to the original - removed chars
- local function WaitForMessage()
- local event, username, message, uuid, isHidden = os.pullEvent("chat")
- if string.find(message, displayCommand) then
- ClearScreen()
- displayedText = message:sub(#displayCommand + 2) -- displayed text is the entire typed message
- print("Displayed message: ", displayedText)
- if #displayedText < displaySizeX then
- WriteToDisplaySingle()
- else
- while finalStringsLenTot + removedSpacesNum < #displayedText do
- FindRange()
- RemoveSpaces()
- table.insert(finalStrings, conditionedInput)
- WriteToDisplay()
- LoopDataUpdate()
- print(" ")
- end
- end
- print("Length of the array: ", #finalStrings)
- spaceDistance = 1
- end
- end
- --Core loop
- StartLink()
- sleep(1)
- print("Display Width: ", displaySizeX)
- while true do
- WaitForMessage()
- ResetValues()
- end
Advertisement
Comments
-
- 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.
- 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.
- You will also need the Advanced Peripherals ChatBox on the left side of the computer.
- The command can be adjusted by editing the "displayCommand" variable at the top of the program.
- The sides of the components can be adjusted via:
- local displayLink = peripheral.wrap("right")
- local chatBox = peripheral.wrap("left")
Add Comment
Please, Sign In to add comment