Advertisement
mypal125

CWCOS Installer

May 22nd, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 KB | None | 0 0
  1. -- http://www.computercraft.info/forums2/index.php?/topic/18815-customizable-installer-program on CC Forums
  2. -- CWCOS and its installer is licensed under the BSD 3-Clause License.
  3. -- Copyright (c) 2014 ClassCoder
  4. -- See the license at http://opensource.org/licenses/BSD-3-Clause.
  5. -- Various functions with minor indentation modification
  6. -- http://www.computercraft.info/forums2/index.php?/topic/460-how-to-center-text/page__view__findpost__p__2948
  7. local function centerText(text)
  8.   local x,y = term.getSize()
  9.   local x2,y2 = term.getCursorPos()
  10.   term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  11.   write(text)
  12. end
  13. -- http://www.computercraft.info/forums2/index.php?/topic/460-how-to-center-text/page__view__findpost__p__70667
  14. local function centerTextXY(text)
  15.   local w, h = term.getSize()
  16.   term.setCursorPos(math.floor(w / 2 - text:len() / 2 + .5), math.floor(h / 2 + .5))
  17.   io.write(text)
  18. end
  19. -- OS Config
  20. os_name = "CWCOS"
  21. os_version_major = 0
  22. os_version_minor = 1
  23. os_version_revision = 0
  24. os_version_abr = "alpha"
  25. license_url = "http://opensource.org/licenses/BSD-3-Clause"
  26. github_repo = "CodingWithClass/CWCOS/master"
  27. -- Terminal clearers
  28. function clearTermWName(fg, bg)
  29.   colorTerm(fg, bg)
  30.   term.clear()
  31.   term.setCursorPos(1, 1)
  32.   centerText(os_name.." Installer")
  33.   term.setCursorPos(1, 2)
  34. end
  35. function clearTerm()
  36.   term.clear()
  37.   term.setCursorPos(1, 1)
  38. end
  39. function resetTerm()
  40.   colorTerm(colors.white, colors.black)
  41.   clearTerm()
  42.   term.setCursorPos(1, 1)
  43. end
  44. -- Terminal colorer
  45. function colorTerm(fg, bg)
  46.   term.setTextColor(fg)
  47.   term.setBackgroundColor(bg)
  48. end
  49. -- Wait for a scancode
  50. function waitForScanCode(code)
  51.   local waiting = true
  52.   while waiting do
  53.     local event, scancode = os.pullEvent("key")
  54.     if scancode == code then
  55.       waiting = false
  56.     end
  57.   end
  58. end
  59.  
  60. -- Loading screen - black fg and gray bg
  61. colorTerm(colors.black, colors.gray)
  62. clearTerm()
  63. centerTextXY(os_name.." Installer")
  64. local sizex, sizey = term.getSize()
  65. term.setCursorPos(1, sizey)
  66. centerText("Loading...")
  67. term.setCursorPos(1, 1)
  68. sleep(3)
  69.  
  70. clearTermWName(colors.white, colors.cyan)
  71. print("Hello, user!")
  72. print("Welcome to the "..os_name.." version "..os_version_major.."."..os_version_minor.."."..os_version_revision.." "..os_version_abr.." installer.")
  73. print("You will first need to accept the license at")
  74. print(license_url)
  75. print("before installation.")
  76. term.setCursorPos(1, sizey-2)
  77. centerText("Press enter to select")
  78. term.setCursorPos(1, sizey)
  79. colorTerm(colors.white, colors.green)
  80. centerText("   Accept   ")
  81. waitForScanCode(28)
  82. clearTermWName(colors.white, colors.cyan)
  83. centerText("Select a Disk")
  84. print(" ")
  85. if fs.exists("/") then
  86.   if fs.getFreeSpace("/") then
  87.     print("/: Free space: "..fs.getFreeSpace("/"))
  88.   else
  89.     print("/")
  90.   end
  91. end
  92. if fs.exists("/disk") then
  93.   if fs.getFreeSpace("/disk") then
  94.     print("/disk: Free space: "..fs.getFreeSpace("/disk"))
  95.   else
  96.     print("/disk")
  97.   end
  98. end
  99. term.setCursorPos(1, sizey-2)
  100. centerText("Type the name of one (/ or /disk)")
  101. term.setCursorPos(1, sizey)
  102. write(": ")
  103. install_to = read()
  104. if not fs.exists(install_to) then
  105.   termReset()
  106.   print("ERROR: That location does not exist.")
  107.   os.exit()
  108. end
  109. clearTermWName(colors.white, colors.red)
  110. centerText("ARE YOU SURE YOU WISH TO INSTALL TO")
  111. print(" ")
  112. centerText(install_to)
  113. term.setCursorPos(1, sizey-2)
  114. centerText("Press enter to select")
  115. term.setCursorPos(1, sizey)
  116. colorTerm(colors.white, colors.green)
  117. centerText("   Install   ")
  118. waitForScanCode(28)
  119. clearTermWName(colors.white, colors.green)
  120. centerTextXY("Downloading file list from GitHub")
  121. local install_files = textutils.unserialize(http.get("https://raw.githubusercontent.com/"..github_repo.."/install_files.textutils").readAll())
  122. for topath, url in pairs(install_files) do
  123.   clearTermWName(colors.white, colors.green)
  124.   centerTextXY("Downloading "..url)
  125.   local downloader = http.get(url)
  126.   local download = downloader.readAll()
  127.   downloader.close()
  128.   fs.makeDir(install_to.."/"..topath)
  129.   fs.delete(install_to.."/"..topath)
  130.   if fs.exists(install_to.."/"..topath) then
  131.     fs.delete(install_to.."/"..topath)
  132.   end
  133.   local file = fs.open(install_to.."/"..topath, "w")
  134.   file.write(download)
  135.   file.close()
  136. end
  137. resetTerm()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement