Advertisement
itsjstn

PlatformController - Installer

Oct 14th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. local w, h = term.getSize()
  2.  
  3. local function SettingParam(id, name, default, parser, validator)
  4.     return {
  5.         id = id,
  6.         name = name,
  7.         default = default,
  8.         type = type(default),
  9.         parser = parser,
  10.         validator = validator
  11.     }
  12. end
  13.  
  14. local function ModuleReq(name, code, target)
  15.     return {
  16.         name = name,
  17.         code = code,
  18.         target = target
  19.     }
  20. end
  21.  
  22. local pastebinCode = 'ATgPXEFH'
  23. local programName = 'Train Platform Controller'
  24.  
  25. local modules = {
  26.     ModuleReq('Util Library', 'fE5Cb2WN', 'utils.lua')
  27. }
  28.  
  29. local settingFields = {
  30.     SettingParam(
  31.         'train_direction',
  32.         'Travel Direction',
  33.         'NB',
  34.         function(v) return v end,
  35.         function (v)
  36.             return (
  37.                 v == 'NB' or
  38.                 v == 'SB' or
  39.                 v == 'WB' or
  40.                 v == 'EB'
  41.             )
  42.         end
  43.     )
  44. }
  45.  
  46. local function getInput(prefix, parser)
  47.     term.write(('%s> '):format(prefix))
  48.     term.setTextColor(colors.yellow)
  49.     local input = parser(read())
  50.     term.setTextColor(colors.white)
  51.  
  52.     return input
  53. end
  54.  
  55. -- Get the starting x value to perfectly center the provided string.
  56. local function getHorizontalAlignment(value)
  57.     return ((w / 2) - (string.len(value) / 2)) + 1
  58. end
  59.  
  60. -- Build a header divider to write to the terminal.
  61. local function buildDivider(length)
  62.     local divider = ''
  63.  
  64.     for i = 1, (length + 2) do
  65.         divider = divider .. (i % 2 == 0 and '-' or '=')
  66.     end
  67.  
  68.     return divider
  69. end
  70.  
  71. -- Write the header to the terminal at the current y-level.
  72. local function writeHeader(title, titleColor, subtitle, subtitleColor, includeDividers)
  73.     if not titleColor then titleColor = colors.white end
  74.     if not subtitleColor then subtitleColor = colors.lightGray end
  75.  
  76.     local x, y = term.getCursorPos()
  77.     local increment = 0
  78.     local length = string.len(subtitle) > string.len(title) and string.len(subtitle) or string.len(title)
  79.  
  80.     -- Write the first divider if enabled
  81.     if includeDividers then
  82.         term.setCursorPos(getHorizontalAlignment(buildDivider(length)), y + increment)
  83.         term.setTextColor(colors.white)
  84.         term.write(buildDivider(length))
  85.         increment = increment + 1
  86.     end
  87.  
  88.     -- Write the title
  89.     term.setCursorPos(getHorizontalAlignment(title), y + increment)
  90.     term.setTextColor(titleColor)
  91.     term.write(title)
  92.     term.setTextColor(colors.white)
  93.     increment = increment + 1
  94.  
  95.     -- Write the subtitle, if enabled.
  96.     if subtitle then
  97.         term.setCursorPos(getHorizontalAlignment(subtitle), y + increment)
  98.         term.setTextColor(subtitleColor)
  99.         term.write(subtitle)
  100.         term.setTextColor(colors.white)
  101.  
  102.         if includeDividers then
  103.             increment = increment + 1
  104.         else
  105.             increment = increment + 2
  106.         end
  107.     end
  108.  
  109.     -- Write the second divider if enabled
  110.     if includeDividers then
  111.         term.setCursorPos(getHorizontalAlignment(buildDivider(length)), y + increment)
  112.         term.setTextColor(colors.white)
  113.         term.write(buildDivider(length))
  114.         increment = increment + 2
  115.     end
  116.  
  117.     term.setCursorPos(1, y + increment)
  118. end
  119.  
  120. local function resetTerminal()
  121.     term.clear()
  122.     term.setCursorPos(1, 1)
  123.  
  124.     writeHeader('Software Installer', colors.white, programName, colors.lightGray, true)
  125. end
  126.  
  127. for i, setting in ipairs(settingFields) do
  128.     settings.define(setting.id, {
  129.         default = setting.default,
  130.         type = setting.type
  131.     });
  132.  
  133.     local isValid = false
  134.     repeat
  135.         resetTerminal()
  136.  
  137.         local v = getInput(setting.name, setting.parser)
  138.         if setting.validator(v) then
  139.             settings.set(setting.id, v)
  140.             isValid = true
  141.         end
  142.     until isValid
  143.  
  144.     settings.save()
  145. end
  146.  
  147. if #modules > 0 then
  148.     for i = 1, #modules do
  149.         print(('Downloading required module: %s'):format(modules[i].name))
  150.         shell.run('pastebin', 'get', modules[i].code, modules[i].target)
  151.     end
  152. end
  153.  
  154. print(('Required settings configured. Installing %s software.'):format(programName))
  155. shell.run('pastebin', 'get', pastebinCode, 'startup.lua')
  156.  
  157. print(('%s software installed successfully. Rebooting...'):format(programName))
  158. sleep(3)
  159.  
  160. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement