Advertisement
Nhorr

NhUI v0.5 [ABANDONED] 'desktop'

Sep 15th, 2015
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- future version wish: table implementation?
  2. -- cfg: add nhui auto update preferences [versions, hotfixes] (under new menu under options > preferences > updating > versions/hotfixes)
  3. -- misc: swap uninstall and about
  4. -- .nhuicfg: add variable to detect if config is outdated. give warning before install of a new version
  5. -- desk: add hotfix handling structure (versions fed by legacy startup, handled by desktop)
  6. -- hotfix: detect on startup; have options for more information on the hotfix; option for to never ask again; version list keeps base installs; if multiple hotfixes available, let user select hotfix to view more info/install on startup
  7. -- if auto update is true display message letting user know it's about to apply a hotfix
  8. -- hotfix function: checks for internet, runs hotfix link that detects target nhui programs and deletes them, downloads new versions in their place
  9. -- updates: add warning if version is not compatible
  10. -- add a spoof disclaimer for comedic effect (not responsible for damage caused for virtual computer etc)
  11.  
  12. -- basic variables and functions
  13. nhver,wip="v0.5",true
  14. local nhuiProgs={["login"]="UwwmSJwL",["updater"]="ag7RuWX4",[".nhuicfg"]="mbD7EU2J"}
  15. local nhuiProgList={"startup","desktop","updater","login",".nhuicfg"}
  16.  
  17. local colorList={["red"]=16384,["yellow"]=16,["white"]=1,["lightGrey"]=256,["grey"]=128}
  18. local colorListMono={["red"]=1,["yellow"]=1,["white"]=1,["lightGrey"]=256,["grey"]=128}
  19. local function setTxtColor(color)
  20.   term.setTextColor(term.isColor() and colorList[color] or colorListMono[color])
  21. end
  22.  
  23. local function printAt(text,setX,setY)
  24.   term.setCursorPos(setX,setY) print(text)
  25. end  
  26.  
  27. local function resetScreen()
  28.   term.clear() term.setCursorPos(1,1)
  29. end
  30.  
  31. local function pingPastebin()
  32.   http.request("http://pastebin.com")
  33.   local requesting=true
  34.   while requesting do
  35.     resetScreen()
  36.     print("Waiting for pastebin.com...")
  37.     local event=os.pullEvent()
  38.     if event=="http_success" then internet,requesting=true,false
  39.     elseif event=="http_failure" then internet,requesting=false end
  40.   end
  41.   if internet==false then print("...could not connect.") sleep(1) resetScreen() end
  42. end
  43.  
  44. local function anyKey()
  45.   sleep(.2)
  46.   local event,b,x,y=os.pullEventRaw()
  47.   if (event=="key" and b~=0) or (event=="mouse_click" and x>0 and y>0) then anyKeyReq=nil end
  48. end
  49.  
  50.  
  51. local function openConfig()  -- needs to be formatted to fs.open
  52.   local file=fs.open(".nhuicfg", "r")
  53.   sContents=file:read()
  54.   file:close()
  55.   tContents=textutils.unserialize(sContents)
  56. end
  57.  
  58. local function editConfigLine(line,text)  -- needs to be formatted to fs.open
  59.   table.remove(tContents,line)
  60.   table.insert(tContents,line,text)
  61. end
  62.  
  63. local function saveConfig()  -- needs to be formatted to fs.open
  64.   sContents=textutils.serialize(tContents)
  65.   local file=fs.open(".nhuicfg","w")
  66.   file:write(sContents)
  67.   file:close()
  68. end
  69.  
  70. -- menu-related functions
  71. openConfig()  -- needs to be formatted to fs.open
  72. local deskColors={["menuColor"]=tContents[4],["menuTextColor"]=tContents[5],["backgroundColor"]=tContents[6],["textColor"]=tContents[7],["secondaryTextColor"]=tContents[8],["tertiaryTextColor"]=tContents[9]}
  73. saveConfig()
  74.  
  75. local function menuBar()
  76.   term.setCursorPos(1,1) term.clearLine()
  77.   term.setBackgroundColor(deskColors["menuColor"])
  78.   term.setTextColor(deskColors["menuTextColor"])
  79.   printAt((s==0 or s==nil) and "1|Start   2|Apps   3|Options   4|NhUI" or "-|Start   -|Apps   -|Options   -|NhUI",2,1)
  80. end
  81.  
  82. local function drawMenus()
  83.   term.setTextColor(deskColors["menuTextColor"])
  84.   term.setBackgroundColor(deskColors["menuColor"])
  85.   if s==1 then printAt("1|CraftOS\n ---------\n 2|Shutdown\n 3|Restart",2,2)
  86.   elseif s==2 then printAt("1|Clock",12,2)
  87.   elseif s==3 then printAt("1|Password",21,2)
  88.   elseif s==4 then printAt("1|Versions\n                                2|Uninstall\n                                3|About",33,2) end
  89. end
  90.  
  91. local function drawDesktop()
  92.   term.setBackgroundColor(deskColors["backgroundColor"])
  93.   term.clear()
  94.   if wip==true then setTxtColor("red") printAt("NOTE: WIP BUILD - POSSIBLY UNSTABLE",2,17) end
  95.   setTxtColor("lightGrey") printAt("(Use " .. (term.isColor() and "mouse & " or "") .. "keyboard for selecting.)",2,18)
  96.   setTxtColor("grey") printAt(nhver,47,18)
  97.   menuBar()
  98. end
  99.  
  100. local function displayTime()
  101.   dispTime=true
  102.   repeat
  103.     local inGameTime=textutils.formatTime(os.time(),false)
  104.     local x=term.getSize()
  105.     printAt(inGameTime,23,1)
  106.     sleep(.01)
  107.   until dispTime==false
  108. end
  109.  
  110. local function displayTimeMenu()
  111.   printAt("Current in-game time:\n\n1|(Back)",1,1)
  112.   while true do
  113.     local event,b,x,y=os.pullEventRaw()
  114.     if (event=="key" and b==keys.one) or (event=="mouse_click" and b==1 and x>=1 and x<=8 and y==3) then dispTime=false drawDesktop() return end
  115.   end
  116. end
  117.  
  118. -- executable-related functions
  119. local function runMenu()
  120.   local yLine=2
  121.   repeat
  122.     term.setCursorPos(1,yLine) term.clearLine()
  123.     yLine=yLine+1  
  124.   until yLine==6
  125.   menuBar()
  126.   drawMenus()
  127. end
  128.  
  129. local function runShell()
  130.   s=0 resetScreen()
  131.   setTxtColor("yellow") print(os.version().." (run 'desktop' to return to desktop)")
  132.   setTxtColor("white")
  133.   sleep(.1)
  134. end
  135.  
  136. local function runClock()
  137.   s=0 resetScreen()
  138.   parallel.waitForAny(displayTime,displayTimeMenu)
  139. end
  140.  
  141. local function runPasswordSetup()
  142.   s=0 resetScreen()
  143.   pingPastebin()
  144.   if internet==true then resetScreen()
  145.     openConfig()
  146.     if tContents[2]==true then print("This terminal is password protected.") else print("This terminal is not password protected.") end
  147.     print("Would you like to set/change your password?\n\n1|Yes\n2|No\n3|(Back)")
  148.     while true do
  149.       local event,b,x,y=os.pullEventRaw()
  150.       if (event=="key" and b==keys.one) or (event=="mouse_click" and b==1 and x>=1 and x<=5 and y==4) then
  151.         fs.delete("login")
  152.         shell.run("pastebin get",nhuiProgs["login"],"login")
  153.         resetScreen()
  154.         print("Desired password? (Note: will display input.)")
  155.         editConfigLine(2,"true")
  156.         local sInput=read()
  157.         if sInput~="" then sPass=sInput end
  158.         editConfigLine(3,sPass)
  159.         saveConfig()
  160.         print("\n\n@Preferences saved.\n@Press any key to return to desktop.")
  161.         anyKey() drawDesktop() return
  162.       elseif (event=="key" and b==keys.two) or (event=="mouse_click" and b==1 and x>=1 and x<=4 and y==5) then
  163.         fs.delete("login")
  164.         editConfigLine(2,"")
  165.         editConfigLine(3,"")
  166.         saveConfig()
  167.         print("\n\n@Preferences saved.\n@Press any key to return to desktop.")
  168.         anyKey() drawDesktop() return
  169.       elseif (event=="key" and b==keys.three) or (event=="mouse_click" and b==1 and x>=1 and x<=8 and y==6) then drawDesktop() return end
  170.     end
  171.   elseif internet==false then drawDesktop() return end
  172. end
  173.  
  174. local function runUpdater()
  175.   s=0 resetScreen()
  176.   pingPastebin()
  177.   if internet==true then resetScreen()
  178.     shell.run("pastebin run",nhuiProgs["updater"])
  179.     drawDesktop()
  180.   elseif internet==false then drawDesktop() return end
  181. end
  182.  
  183. local function runUninstallCheck()
  184.   s=0 resetScreen()
  185.   print("Would you like to uninstall NhUI "..nhver.."?")
  186.   setTxtColor("lightGrey") print("(User-created programs will *not* be deleted.)")
  187.   setTxtColor("white") print("\n1|Yes\n2|No (Back)")
  188.   while true do
  189.     local event,b,x,y=os.pullEventRaw()
  190.     if (event=="key" and b==keys.one) or (event=="mouse_click" and b==1 and x>=1 and x<=5 and y==4) then
  191.       for i=1, #nhuiProgList do fs.delete(nhuiProgList[i]) end
  192.       print("\n\n@NhUI "..nhver.." uninstalled successfully.\n@Press any key to reboot your computer.")
  193.       anyKey() os.reboot()
  194.     elseif (event=="key" and b==keys.two) or (event=="mouse_click" and b==1 and x>=1 and x<=11 and y==5) then drawDesktop() return end
  195.   end
  196. end
  197.  
  198. local function runAbout()
  199.   s=0 resetScreen()
  200.   print("NhUI Author: Nhorr (Philip)\n(Pastebin Link: pastebin.com/u/Nhorr)\n\n1|(Back)")
  201.   while true do
  202.     local event,b,x,y=os.pullEventRaw()
  203.     if (event=="key" and b==keys.one) or (event=="mouse_click" and b==1 and x>=1 and x<=8 and y==4) then drawDesktop() return end
  204.   end
  205. end
  206.  
  207. -- desktop structure
  208. local function runDesktop()
  209.   s=0  -- selection rule
  210.   while true do
  211.     local event,b,x,y=os.pullEventRaw()
  212.     -- if selection rule = 0
  213.     if (event=="key" and s==0 and b==keys.one) or (event=="mouse_click" and b==1 and x>=2 and x<=8 and y==1) then s=1 runMenu()
  214.     elseif (event=="key" and s==0 and b==keys.two) or (event=="mouse_click" and b==1 and x>=12 and x<=17 and y==1) then s=2 runMenu()
  215.     elseif (event=="key" and s==0 and b==keys.three) or (event=="mouse_click" and b==1 and x>=21 and x<=29 and y==1) then s=3 runMenu()
  216.     elseif (event=="key" and s==0 and b==keys.four) or (event=="mouse_click" and b==1 and x>=33 and x<=38 and y==1) then s=4 runMenu()
  217.     -- if selection rule = 1
  218.     elseif (event=="key" and s==1 and b==keys.one) or (event=="mouse_click" and s==1 and b==1 and x>=2 and x<=10 and y==2) then runShell() break
  219.     elseif event=="mouse_click" and s==1 and b==1 and x>=2 and x<=10 and y==3 then s=1 drawMenus()
  220.     elseif (event=="key" and s==1 and b==keys.two) or (event=="mouse_click" and s==1 and b==1 and x>=2 and x<=11 and y==4) then os.shutdown()
  221.     elseif (event=="key" and s==1 and b==keys.three) or (event=="mouse_click" and s==1 and b==1 and x>=2 and x<=10 and y==5) then os.reboot()
  222.     -- if selection rule = 2
  223.     elseif (event=="key" and s==2 and b==keys.one) or (event=="mouse_click" and s==2 and b==1 and x>=12 and x<=18 and y==2) then runClock()
  224.     -- if selection rule = 3
  225.     elseif (event=="key" and s==3 and b==keys.one) or (event=="mouse_click" and s==3 and b==1 and x>=21 and x<=30 and y==2) then runPasswordSetup()
  226.     -- if selection rule = 4
  227.     elseif (event=="key" and s==4 and b==keys.one) or (event=="mouse_click" and s==4 and b==1 and x>=33 and x<=42 and y==2) then runUpdater()
  228.     elseif (event=="key" and s==4 and b==keys.two) or (event=="mouse_click" and s==4 and b==1 and x>=33 and x<=43 and y==3) then runUninstallCheck()
  229.     elseif (event=="key" and s==4 and b==keys.three) or (event=="mouse_click" and s==4 and b==1 and x>=33 and x<=39 and y==4) then runAbout()
  230.     elseif event=="key" or event=="mouse_click" then s=0 runMenu() end
  231.   end
  232. end
  233.  
  234. -- run desktop
  235. term.clear()
  236. parallel.waitForAll(drawDesktop,runDesktop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement