Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local function SettingParam(id, name, default, parser, validator)
- return {
- id = id,
- name = name,
- default = default,
- type = type(default),
- parser = parser,
- validator = validator
- }
- end
- local function ModuleReq(name, code, target)
- return {
- name = name,
- code = code,
- target = target
- }
- end
- local pastebinCode = 'ATgPXEFH'
- local programName = 'Train Platform Controller'
- local modules = {
- ModuleReq('Util Library', 'fE5Cb2WN', 'utils.lua')
- }
- local settingFields = {
- SettingParam(
- 'train_direction',
- 'Travel Direction',
- 'NB',
- function(v) return v end,
- function (v)
- return (
- v == 'NB' or
- v == 'SB' or
- v == 'WB' or
- v == 'EB'
- )
- end
- )
- }
- local function getInput(prefix, parser)
- term.write(('%s> '):format(prefix))
- term.setTextColor(colors.yellow)
- local input = parser(read())
- term.setTextColor(colors.white)
- return input
- end
- -- Get the starting x value to perfectly center the provided string.
- local function getHorizontalAlignment(value)
- return ((w / 2) - (string.len(value) / 2)) + 1
- end
- -- Build a header divider to write to the terminal.
- local function buildDivider(length)
- local divider = ''
- for i = 1, (length + 2) do
- divider = divider .. (i % 2 == 0 and '-' or '=')
- end
- return divider
- end
- -- Write the header to the terminal at the current y-level.
- local function writeHeader(title, titleColor, subtitle, subtitleColor, includeDividers)
- if not titleColor then titleColor = colors.white end
- if not subtitleColor then subtitleColor = colors.lightGray end
- local x, y = term.getCursorPos()
- local increment = 0
- local length = string.len(subtitle) > string.len(title) and string.len(subtitle) or string.len(title)
- -- Write the first divider if enabled
- if includeDividers then
- term.setCursorPos(getHorizontalAlignment(buildDivider(length)), y + increment)
- term.setTextColor(colors.white)
- term.write(buildDivider(length))
- increment = increment + 1
- end
- -- Write the title
- term.setCursorPos(getHorizontalAlignment(title), y + increment)
- term.setTextColor(titleColor)
- term.write(title)
- term.setTextColor(colors.white)
- increment = increment + 1
- -- Write the subtitle, if enabled.
- if subtitle then
- term.setCursorPos(getHorizontalAlignment(subtitle), y + increment)
- term.setTextColor(subtitleColor)
- term.write(subtitle)
- term.setTextColor(colors.white)
- if includeDividers then
- increment = increment + 1
- else
- increment = increment + 2
- end
- end
- -- Write the second divider if enabled
- if includeDividers then
- term.setCursorPos(getHorizontalAlignment(buildDivider(length)), y + increment)
- term.setTextColor(colors.white)
- term.write(buildDivider(length))
- increment = increment + 2
- end
- term.setCursorPos(1, y + increment)
- end
- local function resetTerminal()
- term.clear()
- term.setCursorPos(1, 1)
- writeHeader('Software Installer', colors.white, programName, colors.lightGray, true)
- end
- for i, setting in ipairs(settingFields) do
- settings.define(setting.id, {
- default = setting.default,
- type = setting.type
- });
- local isValid = false
- repeat
- resetTerminal()
- local v = getInput(setting.name, setting.parser)
- if setting.validator(v) then
- settings.set(setting.id, v)
- isValid = true
- end
- until isValid
- settings.save()
- end
- if #modules > 0 then
- for i = 1, #modules do
- print(('Downloading required module: %s'):format(modules[i].name))
- shell.run('pastebin', 'get', modules[i].code, modules[i].target)
- end
- end
- print(('Required settings configured. Installing %s software.'):format(programName))
- shell.run('pastebin', 'get', pastebinCode, 'startup.lua')
- print(('%s software installed successfully. Rebooting...'):format(programName))
- sleep(3)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement