Miki_Tellurium

Text API

Jan 2nd, 2023 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | Gaming | 0 0
  1. -- API for writing text by Miki_Tellurium
  2. -- Version: 1.0.0
  3.  
  4. --- Simple API to write fast monochromatic text
  5. ---@class text
  6. text = {}
  7.  
  8. --- Print a line and then move cursor to next line
  9. ---@param text string The text to write
  10. ---@param xPos number The x coordinate of the text
  11. ---@param yPos number The y coordinate of the text
  12. ---@param textColor number What color the text will be
  13. ---@param backgroundColor number What color the background will be
  14. function text.println(text, xPos, yPos, textColor, backgroundColor)
  15.  
  16.  local dText = term.getTextColor()               --Store the current text color
  17.  local dBack = term.getBackgroundColor()         --Store the current background color
  18.  local currentX, currentY = term.getCursorPos()  --Store the current cursor position
  19.  
  20.  if xPos == nil then
  21.   xPos = currentX
  22.  end
  23.  
  24.  if yPos == nil then
  25.   yPos = currentY
  26.  end
  27.  
  28.  if backgroundColor ~= nil then
  29.   term.setBackgroundColor(backgroundColor)
  30.  else
  31.   term.setBackgroundColor(dBack)
  32.  end
  33.  
  34.  if textColor ~= nil then
  35.   term.setTextColor(textColor)
  36.  else
  37.   term.setTextColor(dText)
  38.  end
  39.  
  40.  term.setCursorPos(xPos, yPos)
  41.  print(text)
  42.  
  43.  term.setTextColor(dText)
  44.  term.setBackgroundColor(dBack)
  45. end
  46.  
  47. --- Print a line and move the cursor at the end of the written text
  48. ---@param text string The text to write
  49. ---@param xPos number The x coordinate of the text
  50. ---@param yPos number The y coordinate of the text
  51. ---@param textColor number What color the text will be
  52. ---@param backgroundColor number What color the background will be
  53. function text.print(text, xPos, yPos, textColor, backgroundColor)
  54.  
  55.  local dText = term.getTextColor()               --Store the current text color
  56.  local dBack = term.getBackgroundColor()         --Store the current background color
  57.  local currentX, currentY = term.getCursorPos()  --Store the current cursor position
  58.  
  59.  if xPos == nil then
  60.   xPos = currentX
  61.  end
  62.  
  63.  if yPos == nil then
  64.   yPos = currentY
  65.  end
  66.  
  67.  if backgroundColor ~= nil then
  68.   term.setBackgroundColor(backgroundColor)
  69.  else
  70.   term.setBackgroundColor(dBack)
  71.  end
  72.  
  73.  if textColor ~= nil then
  74.   term.setTextColor(textColor)
  75.  else
  76.   term.setTextColor(dText)
  77.  end
  78.  
  79.  term.setCursorPos(xPos, yPos)
  80.  term.write(text)
  81.  
  82.  term.setTextColor(dText)
  83.  term.setBackgroundColor(dBack)
  84. end
Advertisement
Add Comment
Please, Sign In to add comment