Advertisement
eniallator

EniOS

Feb 9th, 2016
3,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 36.03 KB | None | 0 0
  1. -- This is eniallator's OS that is in the form of a GUI.
  2. -- The way it works is through left click navigation right click drop down menu's so to get started just right click in the black space to create new folders/apps
  3. -- If you want to try to get a better understanding of how it works, then i have completely commented my code so feel free to read it.
  4. -- You can also search for folders/files by simply typing, This will only handle lowercase a-z characters, numbers and also backspaces. It's lowercase because it will search with the apps as lowercase.
  5. -- For full details on features/changelogs i have a computercraft forums link that i update whenever i make a change - http://www.computercraft.info/forums2/index.php?/topic/25954-enios-the-interactive-gui-os/
  6. --
  7. -- ================================================================================
  8. --
  9. -- To make your own apps, you can either run this program once (it will create a folder from which you can use pastebin/ even edit in to make apps). Or you can right click on the screen (not on an app and when the program is running) and you can choose App from the drop down menu.
  10. -- This has alot of features and configurable options e.g app icon size or even the colour theme, so be sure to look below at the variables and config to your liking.
  11.  
  12. -- =============================== User Config Below ==============================
  13.  
  14. local folderDir = "eniOSApps"
  15. local osVersion = "1.4.7"
  16. -- Defining folderDir as the folder you keep your apps in and the osVersion is the version of the os you are using
  17.  
  18. local appMaxHeight = 3
  19. local appMaxNameLength = 11
  20. -- appMaxHeight is the height of the icons on the page and namelength will not only concatenate the apps to that value, it will also be the width of the icons on the page
  21.  
  22. local pageNumber = 1
  23. -- Setting the page number that the program loads with
  24.  
  25. local colours = {
  26.   app = colors.blue,
  27.   folder = colors.lightBlue,
  28.   addNew = colors.cyan,
  29.   page = colors.green,
  30.   dropMenuTop = colors.gray,
  31.   dropMenuBottom = colors.lightGray,
  32.   infoText = colors.lightBlue,
  33.   decoText = colors.yellow,
  34.   searchBackground = colors.cyan,
  35.   filterBackground = colors.purple,
  36.   filterOn = colors.lime,
  37.   filterOff = colors.red
  38. }
  39. -- Colour theme for the apps and the page icons
  40.  
  41. -- ============================== End Of User Config ==============================
  42.  
  43. if fs.exists(folderDir) and not fs.isDir(folderDir) then
  44.  
  45.   error("folderDir variable is assigned to an already existing file.")
  46. end
  47. -- Seeing if the folderDir is a file already on the local computer
  48.  
  49. if not fs.exists(folderDir) then
  50.  
  51.   fs.makeDir(folderDir)
  52. end
  53. -- Making the folderDir if its not already made
  54.  
  55. shell.run("clear")
  56. term.setTextColor(colours.infoText)
  57. print("Setting things up...")
  58. -- The boot up message
  59.  
  60. local maxX,maxY = term.getSize()
  61. -- Getting the max dimensions of the screen
  62.  
  63. local searchFilterName = "Filter"
  64. -- The word that is displayed where the filter is
  65.  
  66. local activeFilterTags = {}
  67. -- Defining a table that i will be keeping the active filter tags in
  68.  
  69. local terminate = false
  70. -- Defining the terminate variable as false to use later on
  71.  
  72. local clipboard = nil
  73. -- Defining the clipBoard variable as nil for use later on
  74.  
  75. local addNewVisible = true
  76. -- Setting the AddNew icon to visible
  77.  
  78. local oldPullEvent = os.pullEvent
  79. os.pullEvent = os.pullEventRaw
  80. -- Defining os.pullEvent as os.pullEventRaw so that the read() function will use raw instead of the normal os.pullEvent
  81.  
  82. local dropMenuPower = {
  83.   Terminate = function() terminate = true end,
  84.   Restart = function() os.reboot() end,
  85.   Shutdown = function() os.shutdown() end
  86. }
  87. -- Functions that are called when the user left clicks the power button on the top right
  88.  
  89. local dropMenuFilterTags = {
  90.   Folders = function() checkFilterTags("Folders") end,
  91.   Files = function() checkFilterTags("Files") end,
  92.   Hidden = function() checkFilterTags("Hidden") end,
  93.   AddNew = function() checkAddNew() end
  94. }
  95. -- Tag Filters that will be displayed visually
  96.  
  97. local dropMenuFolders = {
  98.   Delete = function(name) fs.delete(appFolder .. "/" .. name) end,
  99.   Copy = function(name) clipboard = {appFolder, name} end
  100. }
  101. -- Functions that are called when the user right clicks an option for a folder in the drop menu
  102.  
  103. local dropMenuApps = {
  104.   Edit = function(name) shell.run("edit " .. appFolder .. "/" .. name) end,
  105.   Delete = function(name) fs.delete(appFolder .. "/" .. name) end,
  106.   Copy = function(name) clipboard = {appFolder, name} end,
  107.   Rename = function(name)
  108.  
  109.     clearScreen()
  110.     term.setTextColor(colours.infoText)
  111.     print("Enter the new name of the app below or press enter again to not rename:")
  112.     -- Prompting the user for an input
  113.  
  114.     term.setTextColor(colors.white)
  115.     local newName = read()
  116.     -- Getting user input
  117.  
  118.     term.setTextColor(colours.infoText)
  119.     -- Setting the text colour so i don't have to set it in the if statements
  120.  
  121.     if not fs.exists(appFolder .. "/" .. newName) then
  122.       -- Checking if the name doesn't already exist in the current directory
  123.  
  124.       fs.move(appFolder .. "/" .. name, appFolder .. "/" .. newName)
  125.       print("File successfully renamed")
  126.       sleep(1)
  127.       -- If the directory already doesn't have an app/folder of that name then it will rename the app
  128.  
  129.     elseif newName == "" then
  130.       -- Checking if they have put anything in the read()
  131.  
  132.       print("New name not set")
  133.       sleep(1)
  134.       -- If they haven't put anything in the read() then it won't try to set a new name
  135.     else
  136.  
  137.       print("Name already exists in current directory")
  138.       sleep(1)
  139.       -- Informing the user that the name they chose already exists
  140.     end
  141.   end
  142. }
  143. -- Functions that are called when the user right clicks an option for an app in the drop menu
  144.  
  145. local dropMenuOpts = {
  146.   App = function()
  147.  
  148.     clearScreen()
  149.     eniOS()
  150.     -- Loading the OS version
  151.  
  152.     term.setCursorPos(1,2)
  153.     term.setTextColor(colours.infoText)
  154.     print("Enter name of new app below or leave blank to not create:")
  155.     term.setTextColor(colors.white)
  156.     -- Prompting the user to enter the new app name
  157.  
  158.     local appName = wordSplit(read())
  159.     -- Immediately splitting the user input at the spaces
  160.  
  161.     if appName[1] and not fs.exists(appName[1]) then
  162.       -- Checking if the user inputted a app name and seeing if it exists already or not
  163.  
  164.       shell.run("edit " .. appFolder .. "/" .. appName[1])
  165.       -- Edit the new file
  166.     elseif appName[1] and fs.exists(appName[1]) then
  167.  
  168.       term.setTextColor(colours.infoText)
  169.       print("Name already exists")
  170.       sleep(1)
  171.       -- Notifying the user that the name already exists
  172.     else
  173.  
  174.       term.setTextColor(colours.infoText)
  175.       print("No new file created")
  176.       sleep(1)
  177.       -- If the user didn't enter something then it gives a message, sleeps (so the user can see the message) and then goes back to the app page
  178.     end
  179.   end,
  180.  
  181.   Folder = function()
  182.  
  183.     clearScreen()
  184.     eniOS()
  185.     -- Loading the OS version
  186.  
  187.     term.setCursorPos(1,2)
  188.     term.setTextColor(colours.infoText)
  189.     print("Enter the name of the new folder below or leave blank to not create:")
  190.     term.setTextColor(colors.white)
  191.     -- Prompting the user for the new folder name
  192.  
  193.     local folderName = wordSplit(read())
  194.     -- Immediately splitting the user input into a table
  195.  
  196.     if folderName[1] and not fs.exists(folderName[1]) then
  197.       -- Checking if the user inputted a app name and seeing if it exists already or not
  198.  
  199.       fs.makeDir(appFolder .. "/" .. folderName[1])
  200.       -- Makes the new folder
  201.  
  202.     elseif folderName[1] and fs.exists(folderName[1]) then
  203.       -- Catches if the user has inputted an folder name that already exists
  204.  
  205.       term.setTextColor(colours.infoText)
  206.       print("Name already exists")
  207.       sleep(1)
  208.       -- Notifying the user that the name they have inputted already exists
  209.     else
  210.  
  211.       term.setTextColor(colours.infoText)
  212.       print("No new folder created")
  213.       sleep(1)
  214.       -- If the user didn't enter something then it gives a message, sleeps (so the user can see the message) and then goes back to the app page
  215.     end
  216.   end
  217. }
  218. -- The drop menu you see if you don't right click on an app
  219.  
  220. if http then
  221.   -- First seeing if you have the http api enabled before doing anything
  222.  
  223.   local testFile = http.get("http://pastebin.com/raw.php?i=DSMHN2iF")
  224.  
  225.   if testFile then
  226.  
  227.     testPaste = testFile.readAll()
  228.     testFile.close()
  229.     -- Reading a test file that i have made just to make sure you can access pastebin.com
  230.   end
  231.  
  232.   if testPaste == "test" then
  233.     -- Seeing if the test paste has "test" in it's contents (should always return test)
  234.  
  235.     dropMenuOpts.Pastebin = function()
  236.       -- Adding the Pastebin option to the dropMenuOpts
  237.  
  238.       clearScreen()
  239.       eniOS()
  240.       -- Loading the OS version
  241.  
  242.       term.setCursorPos(1,2)
  243.       term.setTextColor(colours.infoText)
  244.       print("Enter the pastebin url after http://pastebin.com/ below or leave blank to not create:")
  245.       term.setTextColor(colors.white)
  246.       -- Prompting the user for the pastebin url
  247.  
  248.       local pasteURL = wordSplit(read())
  249.       -- Immediately splitting the user input into a table
  250.  
  251.       if pasteURL[1] then
  252.         -- Seeing if the user has entered anything
  253.  
  254.         local pasteFile = http.get("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteURL[1]))
  255.         local pasteContents = pasteFile.readAll()
  256.         pasteFile.close()
  257.         -- Getting the user's file from pastebin
  258.  
  259.         if pasteContents then
  260.           -- Checking if the pastebin url is valid and is actually a file
  261.  
  262.           term.setTextColor(colours.infoText)
  263.           print("Enter the file name you want to download as:")
  264.           term.setTextColor(colors.white)
  265.           -- Prompting the user for the file name to download as
  266.  
  267.           local pasteFile = wordSplit(read())
  268.           -- Getting the file name and splitting the user input into a table
  269.  
  270.           if pasteFile[1] and not fs.exists(appFolder .. "/" .. pasteFile[1]) then
  271.             -- Checking if the user has entered anything and also checking if the file already exists or not
  272.  
  273.             local file = fs.open(appFolder .. "/" .. pasteFile[1], "w")
  274.             file.write(pasteContents)
  275.             file.close()
  276.             -- If everything goes well it will make the file
  277.  
  278.             term.setTextColor(colours.infoText)
  279.             print("Successfully made file")
  280.             sleep(1)
  281.             -- Letting the user know it was successful
  282.  
  283.           elseif pasteFile[1] and fs.exists(appFolder .. pasteFile[1]) then
  284.             -- Catching if the use has entered a file name but it already exists
  285.  
  286.             term.setTextColor(colours.infoText)
  287.             print("File already exists. Press Y to force or enter another key to not force:")
  288.             local event, key, held = os.pullEvent("key")
  289.             -- Seeing if the user wants to force make the file
  290.  
  291.             if key == 21 then
  292.               -- In this case 21 is the key Y
  293.  
  294.               local file = fs.open(appFolder .. "/" .. pasteFile[1], "w")
  295.               file.write(pasteContents)
  296.               file.close()
  297.               -- Writing over the old file with the newly downloaded file
  298.  
  299.               term.setTextColor(colours.infoText)
  300.               print("Successfully force made file")
  301.               sleep(1)
  302.               -- Letting the use know that the file was replaced
  303.             else
  304.  
  305.               term.setTextColor(colours.infoText)
  306.               print("Aborted making the file")
  307.               sleep(1)
  308.               -- If the user hasn't entered a valid name e.g either spaces or nothing
  309.             end
  310.           end
  311.         else
  312.  
  313.           term.setTextColor(infoText)
  314.           print("Invalid paste URL")
  315.           sleep(1)
  316.           -- Letting the user know that the pastebin url's contents aren't there e.g invalid url
  317.         end
  318.       else
  319.  
  320.         term.setTextColor(colours.infoText)
  321.         print("No url specified")
  322.         sleep(1)
  323.         -- If no input was given it returns to the app page
  324.       end
  325.     end
  326.   end
  327. end
  328. -- First checking if the user can access pastebin.com and if they can then will add the pastebin option to dropMenuOpts
  329.  
  330. local filterTags = {
  331.  
  332.   Folders = function (tab)
  333.  
  334.     local out = {}
  335.     -- Defining the out table to be later returned
  336.  
  337.     for i=1, #tab do
  338.       -- Iterating for the amount of values the tab table has
  339.  
  340.       if not fs.isDir(appFolder .. "/" .. tab[i]) then
  341.  
  342.         table.insert(out,#out+1,tab[i])
  343.       end
  344.       -- Removing the folders from the table
  345.     end
  346.  
  347.     return out
  348.     -- Returning the filtered table
  349.   end,
  350.  
  351.   Files = function (tab)
  352.  
  353.     local out = {}
  354.     -- Defining the out table to be later returned
  355.  
  356.     for i=1,#tab do
  357.       -- Iterating for the amount of values the tab table has
  358.  
  359.       if fs.isDir(appFolder .. "/" .. tab[i]) then
  360.  
  361.         table.insert(out,#out+1,tab[i])
  362.       end
  363.       -- Removing the files from the table
  364.     end
  365.  
  366.     return out
  367.     -- Returning the filtered table
  368.   end,
  369.  
  370.   Hidden = function (tab)
  371.  
  372.     local out = {}
  373.     -- Defining the out table to be later returned
  374.  
  375.     for i=1,#tab do
  376.       -- Iterating for the amount of values the tab table has
  377.  
  378.       if tab[i]:sub(1,1) ~= "." then
  379.  
  380.         table.insert(out,#out+1,tab[i])
  381.       end
  382.       -- Adding the Filtered Files to the out table
  383.     end
  384.  
  385.     return out
  386.     -- Returning the filtered table
  387.   end,
  388.  
  389.   AddNew = function(tab) return tab end
  390. }
  391. -- The tags that the directory can be filtered for
  392.  
  393. local numFilterTags = 0
  394.  
  395. for key,val in pairs(dropMenuFilterTags) do
  396.  
  397.   numFilterTags = numFilterTags + 1
  398. end
  399. -- Getting the number of filter tags for later use
  400.  
  401. for key,val in pairs(dropMenuFilterTags) do
  402.   -- Iterating for the number of key/value pairs in dropMenuFilterTags
  403.  
  404.   if key == "Hidden" then
  405.     -- Seeing if the key is "Hidden"
  406.  
  407.     activeFilterTags[key] = true
  408.     -- Setting the key to activated
  409.   else
  410.  
  411.     activeFilterTags[key] = false
  412.     -- Setting the key to deactivated
  413.   end
  414. end
  415. -- Making the activeFilterTags table
  416.  
  417. local directoryBeforeSearch
  418. -- Declaring directoryBeforeSearch variable here so i can compare with it later on
  419.  
  420. function checkFilterTags(name)
  421.   if activeFilterTags[name] then
  422.     -- Seeing what the status of the filter was before
  423.  
  424.     activeFilterTags[name] = false
  425.     -- Removing the filter from the active tags list
  426.   else
  427.  
  428.     activeFilterTags[name] = true
  429.     pageNumber = 1
  430.     -- Adding the filter to the tags list aswell as resetting the page number so it won't error
  431.   end
  432. end
  433.  
  434. function checkAddNew()
  435.   if activeFilterTags.AddNew then
  436.     -- Seeing what the status of AddNew was before
  437.  
  438.     activeFilterTags.AddNew = false
  439.     addNewVisible = true
  440.     -- Removing AddNew from the active tags list and setting it's visibility to true
  441.   else
  442.  
  443.     activeFilterTags.AddNew = true
  444.     pageNumber = 1
  445.     addNewVisible = false
  446.     -- Adding AddNew to the tags list aswell as resetting the page number so it won't error and setting it's visibility to false
  447.   end
  448. end
  449.  
  450.  
  451. function fileSort(fileTab,appFolder,searchWord)
  452.  
  453.   local out = {}
  454.   local files = {}
  455.  
  456.   for key,val in pairs(activeFilterTags) do
  457.     -- iterating over the activeFilterTags table
  458.  
  459.     if val then
  460.  
  461.       fileTab = filterTags[key](fileTab)
  462.       -- Assigning fileTab to the new table that the filterTags tag will return
  463.     end
  464.   end
  465.   -- Applying any filters that the user has put on
  466.  
  467.   directoryBeforeSearch = fileTab
  468.   -- Defining directoryBeforeSearch as fileTab before it has been filtered with the search word
  469.  
  470.   for i=1,#fileTab do
  471.     if fs.isDir(appFolder .. "/" .. fileTab[i])  and fileTab[i]:lower():find(searchWord) then
  472.       -- First iterating for the amount of items in the fileTab table and then checking if its a directory and putting it in the appropriate table
  473.  
  474.       table.insert(out,#out+1,fileTab[i])
  475.     elseif fileTab[i]:lower():find(searchWord) then
  476.  
  477.       table.insert(files,#files+1,fileTab[i])
  478.     end
  479.   end
  480.   -- Sorting the fileTab table into the files and out table
  481.  
  482.   for i=1,#files do
  483.  
  484.     table.insert(out,#out+1,files[i])
  485.   end
  486.   -- merging the files table with the out table
  487.  
  488.   if addNewVisible then
  489.     -- Checking if the visible var is true
  490.  
  491.     out[#out+1] = "+"
  492.     -- Making the addNew icon
  493.   end
  494.  
  495.   return out
  496.   -- Returning the sorted table
  497. end
  498. -- Function for sorting files and folders so that the folders will appear first
  499.  
  500. function wordSplit(string)
  501.  
  502.   local out = {}
  503.  
  504.   for word in string:gmatch("%S+") do
  505.     -- Splitting the string at the spaces
  506.  
  507.     table.insert(out, word)
  508.     -- Inserting the value into a table
  509.   end
  510.  
  511.   return out
  512.   -- Returning the table
  513. end
  514. -- Receives a string and returns a table that's the string split at the spaces
  515.  
  516. function fillArea(tab)
  517.  
  518.   term.setBackgroundColor(tab[5])
  519.   -- Setting the right background colour
  520.  
  521.   for i=0,tab[4]-tab[2] do
  522.     -- Running for how many rows there are (indexes 2,4 == y values and indexes 1,3 == x values in the table)
  523.  
  524.     local j = 0
  525.  
  526.     while j <= tab[3]-tab[1] do
  527.       -- I use a while loop here because i iterate up with variable amounts
  528.  
  529.       term.setCursorPos(tab[1]+j,tab[2]+i)
  530.       -- Positioning the cursor for the next string whether it be a space or the word
  531.  
  532.       if tab[6] and math.floor(j-((tab[3]-tab[1])/2)+#tab[6]/2) == 0 and math.floor(i-(tab[4]-tab[2])/2) == 0 then
  533.         -- If statement seeing whether the iterators have hit the place where it should be writing the word out
  534.  
  535.         term.write(tab[6])
  536.         j = j + #tab[6]
  537.         -- Only runs when the above if statement has found the place where the word will be centered
  538.       else
  539.  
  540.         j = j + 1
  541.         term.write(" ")
  542.         -- Writing " " so the background text colour is visible
  543.       end
  544.     end
  545.   end
  546. end
  547. -- Defining the function for filling an area of the screen with a word centered in it
  548.  
  549. function loadApps(appFolder,appFolderNav,searchWord)
  550.  
  551.   local pages = {}
  552.   local apps = fileSort(fs.list(appFolder),appFolder,searchWord)
  553.   -- Getting the app names from the appFolder
  554.  
  555.   local numberXApps = math.floor(maxX/(appMaxNameLength+1))
  556.   -- Seeing how many apps it can fit on one line
  557.  
  558.   if appFolderNav[2] then
  559.  
  560.     negY = 5
  561.   else
  562.  
  563.     negY = 3
  564.   end
  565.   -- Seeing whether or not there needs to be more space for other icons on the page or not
  566.  
  567.   local appsPerPage = math.floor((maxY-negY)/(appMaxHeight+1))*numberXApps
  568.   -- Seeing the total number of apps per page it can fit
  569.  
  570.   for i=1,math.ceil(#apps/appsPerPage) do
  571.     -- Running for the number of pages it has to create
  572.  
  573.     pages[i] = {}
  574.  
  575.     for j=0,appsPerPage-1 do
  576.       -- Running for the number of apps per page
  577.  
  578.       if not apps[1] then break end
  579.       -- Seeing if it has done all the apps in the table
  580.  
  581.       if fs.isDir(appFolder .. "/" .. apps[1]) then
  582.  
  583.         iconType = "folder"
  584.         colour = colours.folder
  585.         -- Setting the iconType var to folder and setting the colour
  586.  
  587.       elseif fs.exists(appFolder .. "/" .. apps[1]) and not fs.isDir(appFolder .. "/" .. apps[1]) then
  588.  
  589.         iconType = "file"
  590.         colour = colours.app
  591.         -- Setting the iconType var to file and setting the colour
  592.  
  593.       elseif apps[1] == "+" then
  594.  
  595.         iconType = "addNew"
  596.         colour = colours.addNew
  597.         -- Setting the iconType var to addNew and setting the colour
  598.       end
  599.       -- Seeing what icon colour to display for apps[1]
  600.  
  601.       local indent = (j%numberXApps)*appMaxNameLength+(j%numberXApps)+2
  602.       -- Finding what the indent of the X value is
  603.  
  604.       pages[i][j+1] = {indent,negY+math.floor(j/numberXApps)*(appMaxHeight+1),indent+appMaxNameLength-1,appMaxHeight+negY-1+math.floor(j/numberXApps)*(appMaxHeight+1),colour,apps[1]:sub(1,appMaxNameLength),apps[1],iconType}
  605.       -- Making the value for each app in a table in the following format: {x1,y1,x2,y2,background colour, substringed app name (for display purposes), original app name, icon type}
  606.  
  607.       table.remove(apps,1)
  608.       -- Removing the last value from the table
  609.     end
  610.  
  611.     if not apps[1] then break end
  612.     -- Seeing if it has done all the apps in the table
  613.   end
  614.  
  615.   return pages
  616.   -- Returning the pages table after making it
  617. end
  618. -- Making a function to load the apps from the appFolder into a table thats split up into pages
  619.  
  620. function eniOS()
  621.  
  622.   term.setCursorPos(1,1)
  623.   term.setTextColor(colours.decoText)
  624.   term.write("EniOS Version: " .. osVersion)
  625.   term.setTextColor(colors.white)
  626. end
  627. -- Displaying the version number at the top left of the screen
  628.  
  629. function otherArea(pageNumber)
  630.   return {
  631.   plus = {maxX-math.ceil((maxX)/2)+1,maxY-1,maxX,maxY-1,colours.page,"page "..pageNumber+1},
  632.   minus = {1,maxY-1,math.floor((maxX)/2)-1,maxY-1,colours.page,"page "..pageNumber-1},
  633.   power = {maxX-2,1,maxX,1,colors.red,"O"},
  634.   back = {1,3,2,3,colours.page,"<"},
  635.   searchBar = {1,maxY,maxX-#searchFilterName-1,maxY,colours.searchBackground},
  636.   searchFilter = {maxX-#searchFilterName-1,maxY,maxX,maxY,colours.filterBackground,searchFilterName}
  637.   }
  638. end
  639. -- Getting the updated pageNumber for the pages aswell as setting the power button at the top right of the screen
  640.  
  641. function addPaste(tab)
  642.   if clipboard then
  643.  
  644.     dropMenuOpts.paste = function() paste() end
  645.   end
  646. end
  647. -- Function to add the paste option once you have a clipboard from using copy
  648.  
  649. function paste()
  650.   if appFolder ~= clipboard[1] then
  651.  
  652.     fs.copy(clipboard[1] .. "/" .. clipboard[2], appFolder .. "/" .. clipboard[2])
  653.   end
  654. end
  655. -- Function for pasting a file
  656.  
  657. function loadPage(pageNumber,appFolderNav,searchWord)
  658.  
  659.   local otherAreaTab = otherArea(pageNumber)
  660.   -- Getting the otherArea table with the updated pageNumber
  661.  
  662.   local topFolder = fs.list(folderDir)
  663.   -- Defining topFolder as the folders/files in the upper folder to be used later on
  664.  
  665.   eniOS()
  666.   -- displaying the OS Version
  667.  
  668.   if appFolderNav[2] then
  669.  
  670.     term.setBackgroundColor(colors.black)
  671.     term.setTextColor(colours.infoText)
  672.     -- Getting the colours ready
  673.  
  674.     local dirLink = "CC:" .. appFolderNav[2]
  675.     -- Defining the dirLink variable so it won't concatenate with what it was before
  676.  
  677.     for i=1,#appFolderNav-2 do
  678.  
  679.       dirLink = dirLink .. "/" .. appFolderNav[i+2]
  680.     end
  681.     -- Making the dirLink by conatenation without what folderDir is
  682.  
  683.     if #dirLink > maxX-4 then
  684.  
  685.       dirLink = dirLink:sub(#dirLink-maxX+4,#dirLink)
  686.     end
  687.  
  688.     term.setCursorPos(4,3)
  689.     term.write(dirLink)
  690.     term.setTextColor(colors.white)
  691.     -- Seeing if dirLink is longer than the page and making a sub string if it is when writing it out
  692.   end
  693.   -- Seeing if the user is in a folder and then displaying the back button and the directory link
  694.  
  695.   if appPages[1] then
  696.     for i=1,#appPages[pageNumber] do
  697.  
  698.       fillArea(appPages[pageNumber][i])
  699.     end
  700.   end
  701.   -- Displaying the app icons on the screen
  702.  
  703.   for key,val in pairs(otherAreaTab) do
  704.     -- Iterating for all of the values in the otherAreaTab table
  705.  
  706.     local fillCatch = true
  707.     -- Defining a variable so i can use it later on
  708.  
  709.     if key == "searchBar"then
  710.       if directoryBeforeSearch[1] then
  711.  
  712.         fillArea(val)
  713.       end
  714.  
  715.       fillCatch = false
  716.     end
  717.     -- Seeing if there are appPages then it will display the search bar
  718.  
  719.     if key == "minus" then
  720.       if appPages[pageNumber-1] then
  721.  
  722.         fillArea(val)
  723.       end
  724.  
  725.       fillCatch = false
  726.     end
  727.     -- Seeing if theres a table before displaying the previous page icon
  728.  
  729.     if key == "plus" then
  730.       if appPages[pageNumber+1] then
  731.  
  732.         fillArea(val)
  733.       end
  734.  
  735.       fillCatch = false
  736.     end
  737.     -- Seeing if theres a table before displaying the next page icon
  738.  
  739.     if key == "back" then
  740.       if appFolderNav[2] then
  741.  
  742.         fillArea(val)
  743.       end
  744.  
  745.       fillCatch = false
  746.     end
  747.     -- Seeing if the user is in a folder to see if it should display the back icon
  748.  
  749.     if fillCatch then
  750.  
  751.       fillArea(val)
  752.     end
  753.     -- Catching if the other if statements haven't returned true
  754.   end
  755.  
  756.   if #searchWord > 0 then
  757.     -- Checking if the searchWord has characters in it
  758.  
  759.     term.setTextColor(colors.white)
  760.     term.setBackgroundColor(colours.searchBackground)
  761.     term.setCursorPos(2,maxY)
  762.     -- Getting ready for the text to be written
  763.  
  764.     local searchWordWrite = searchWord
  765.     -- Defining another variable as searchWord so that it won't substring searchWord
  766.  
  767.     if #searchWord > maxX-5-#searchFilterName then
  768.       -- Seeing if the searchWord string is longer than the screen
  769.  
  770.       searchWordWrite = searchWord:sub(#searchWord-maxX+5+#searchFilterName,#searchWord)
  771.       -- Making a substring of searchWord so that it will fit on the string
  772.     end
  773.  
  774.     term.write(searchWordWrite)
  775.     term.setCursorBlink(true)
  776.     term.setTextColor(colors.white)
  777.     -- Writing searchWordWrite to the screen and then resetting the text colour
  778.  
  779.   elseif directoryBeforeSearch[1] then
  780.  
  781.     term.setCursorPos(2,maxY)
  782.     term.setBackgroundColor(colours.searchBackground)
  783.     term.setTextColor(colors.white)
  784.     term.write("Type to search")
  785.     -- Making place holder text for the search bar
  786.  
  787.   elseif not topFolder[1] then
  788.  
  789.     term.setCursorPos(1,3)
  790.     term.setBackgroundColor(colors.black)
  791.     term.setTextColor(colours.dropMenuBottom)
  792.     print("Right click to start making apps/folders")
  793.     term.setTextColor(colors.white)
  794.     -- Welcoming the user into the program and telling them how to get started
  795.   end
  796. end
  797. -- Loads the entire page onto the screen using fillArea alot
  798.  
  799. function clickedApp(pageNumber,clickX,clickY)
  800.   if appPages[1] then
  801.     for i=1, #appPages[pageNumber] do
  802.       -- Iterating for the number of apps in the page
  803.  
  804.       if appPages[pageNumber][i][1] <= clickX and appPages[pageNumber][i][3] >= clickX and appPages[pageNumber][i][2] <= clickY and appPages[pageNumber][i][4] >= clickY then
  805.         -- Comparing the clicked x and y values with each of the app's x and y values
  806.  
  807.         return appPages[pageNumber][i]
  808.         -- returning the original app name in the appPages table
  809.       end
  810.     end
  811.   end
  812.  
  813.   return false
  814.   -- Returning false if it hasn't already returned a value
  815. end
  816. -- Detecting what app the user has clicked (if they have clicked one that is)
  817.  
  818. function clickedOther(pageNumber,clickX,clickY)
  819.   for key,val in pairs(otherArea(pageNumber)) do
  820.     -- Iterating for the length of the table the otherArea() function returns
  821.  
  822.     if val[1] <= clickX and val[3] >= clickX and val[2] <= clickY and val[4] >= clickY then
  823.       -- Comparing the clicked x and y values with each of the icon's x and y values
  824.  
  825.       return key
  826.       -- Returning the key of the key/value pair in the otherArea table
  827.     end
  828.   end
  829.  
  830.   return false
  831.   -- Returning false if the func hasn't already returned a value
  832. end
  833. -- Detecting if the user has clicked the page icons or the close icon
  834.  
  835. function clearScreen()
  836.  
  837.   term.setTextColor(colors.white)
  838.   term.setBackgroundColor(colors.black)
  839.   term.setCursorBlink(false)
  840.   shell.run("clear")
  841. end
  842. -- Clearing the screen and resetting the taxt colour and the background colour
  843.  
  844. function loadDropMenu(dropMenuOptionsFuncs,name,x,y,filterBool)
  845.  
  846.   local dropMenuOptions = {}
  847.  
  848.   for key,_ in pairs(dropMenuOptionsFuncs) do
  849.  
  850.     table.insert(dropMenuOptions,1,key)
  851.   end
  852.   -- Making the dropMenuOptions table from the functions's keys
  853.  
  854.   if y + #dropMenuOptions > maxY then
  855.  
  856.     y = y - (y+#dropMenuOptions-maxY)
  857.   end
  858.   -- Seeing if the drop menu will go off the screen and correcting it if it does
  859.  
  860.   if x+appMaxNameLength > maxX then
  861.  
  862.     x = x - (x+appMaxNameLength-maxX)
  863.   end
  864.   -- Seeing if the drop menu will go off the screen and correcting it if it does
  865.  
  866.   if name then
  867.  
  868.     term.setCursorPos(x,y)
  869.     fillArea({x,y,x+appMaxNameLength,y,colours.dropMenuTop})
  870.     term.setCursorPos(x,y)
  871.     term.write(name:sub(1,appMaxNameLength))
  872.     -- Writing the app name in the right colour and at the right place
  873.   end
  874.   -- Seeing if the name argument has been given
  875.  
  876.   fillArea({x,y+1,x+appMaxNameLength,y+#dropMenuOptions,colours.dropMenuBottom})
  877.   -- Filling the below area for the drop menu options
  878.  
  879.   for i=1,#dropMenuOptions do
  880.     -- Iterating through the dropMenuOptions table
  881.  
  882.     if filterBool and activeFilterTags[dropMenuOptions[i]] then
  883.       -- Seeing if filterBool is true and if the filter is active
  884.  
  885.       term.setTextColor(colours.filterOn)
  886.       -- Setting the text colour to filterOn if the filter is on
  887.  
  888.     elseif filterBool then
  889.       -- Seeing if filterBool is true but the filter is not active
  890.  
  891.       term.setTextColor(colours.filterOff)
  892.       -- Setting the text colour to filterOff if the filter is off
  893.     end
  894.  
  895.     term.setCursorPos(x,y+i)
  896.     term.write(dropMenuOptions[i]:sub(1,appMaxNameLength))
  897.     term.setTextColor(colors.white)
  898.   end
  899.   -- Writing the options in their places and making sure they don't go over the appMaxNameLength
  900.  
  901.   return dropMenuOptions,dropMenuOptionsFuncs,name,x,y
  902.   -- Returning the changed coordinates of the drop menu
  903. end
  904. -- Loading the drop menu from where the user has clicked
  905.  
  906. function dropMenu(dropMenuOptions,dropMenuOptionsFuncs,name,x,y)
  907.  
  908.   local event, button, clickX, clickY = os.pullEvent("mouse_click")
  909.   -- Getting a user mouse click input
  910.  
  911.   if button == 1 then
  912.     -- Seeing if the user has left clicked
  913.  
  914.     for i=1,#dropMenuOptions do
  915.       --Iterating for the amount of options in the drop menu
  916.  
  917.       if x <= clickX and x+appMaxNameLength >= clickX and y+i <= clickY and y+i >= clickY then
  918.         -- Detecting whether the user has clicked an option
  919.  
  920.         dropMenuOptionsFuncs[dropMenuOptions[i]](name)
  921.         -- Running the function in the drop menu
  922.       end
  923.     end
  924.   end
  925. end
  926. -- Function for detecting if the user has clicked an option in the drop menu
  927.  
  928. appFolderNav = {folderDir}
  929. -- Making the nav table so its easy to modify the directory navigation
  930.  
  931. local event = "mouse_click"
  932. -- Giving the event var a value
  933.  
  934. local searchWord = ""
  935. -- Defining searchWord as an empty string
  936.  
  937. while not terminate do
  938.   -- Running while the terminate variable is false
  939.  
  940.   appFolder = ""
  941.   -- Defining the appFolder as an empty string before adding strings to it
  942.   for i=1,#appFolderNav do
  943.  
  944.     appFolder = appFolder .. "/" .. appFolderNav[i]
  945.   end
  946.   -- Remaking the appFolder directory navigation
  947.  
  948.   appPages = loadApps(appFolder,appFolderNav,searchWord)
  949.   -- Loading the apps into a sorted table
  950.  
  951.   if event == "mouse_click" or event == "key" then
  952.  
  953.     clearScreen()
  954.     loadPage(pageNumber,appFolderNav,searchWord)
  955.   end
  956.   -- First checking if the last event was a mouse click or a key press (to make it that little bit more efficient) before clearing and then loading the current page
  957.  
  958.   if not dropMenuOpts.paste then
  959.  
  960.     addPaste()
  961.   end
  962.   -- while the paste option is not in the dropMenuOpts table it tries to add paste to dropMenuOpts
  963.  
  964.   local event, button, x, y = os.pullEvent()
  965.   -- Waiting for a mouse click event
  966.  
  967.   local currentFolder = fs.list(appFolder)
  968.   -- Getting whatever is in the current folder for later use
  969.  
  970.   if event == "mouse_click" then
  971.  
  972.     local clickedAppResult = clickedApp(pageNumber,x,y)
  973.     local clickedOtherResult = clickedOther(pageNumber,x,y)
  974.     -- Getting the result of each of the click detection functions
  975.  
  976.     if button == 1 then
  977.       if clickedAppResult and clickedAppResult[8] == "file" then
  978.  
  979.         clearScreen()
  980.         os.pullEvent = oldPullEvent
  981.         shell.run(appFolder .. "/" .. clickedAppResult[7] .. " " .. appFolder)
  982.         clearScreen()
  983.         os.pullEvent = os.pullEventRaw
  984.         -- If the user has clicked an app, it will first clear the screen and then run the app and then clear the screen yet again
  985.  
  986.       elseif clickedAppResult and clickedAppResult[8] == "folder" then
  987.  
  988.         table.insert(appFolderNav,#appFolderNav+1,clickedAppResult[7])
  989.         -- When the user clicks a folder it will add the folder name to the appFolderNav table
  990.  
  991.       elseif clickedAppResult and clickedAppResult[8] == "addNew" then
  992.  
  993.         dropMenu(loadDropMenu(dropMenuOpts,nil,x,y-1))
  994.         -- Loading the create drop menu when the user left clicks the + icon
  995.  
  996.       elseif appFolderNav[2] and clickedOtherResult == "back" then
  997.  
  998.         table.remove(appFolderNav,#appFolderNav)
  999.         -- when the user clicks the back button inside a folder it will remove the last folder name the user clicked
  1000.  
  1001.       elseif appPages[pageNumber-1] and clickedOtherResult == "minus" then
  1002.  
  1003.         pageNumber = pageNumber - 1
  1004.         -- If the user has clicked the left page icon and if the appPages page exists it will minus the current pageNumber
  1005.  
  1006.       elseif appPages[pageNumber+1] and clickedOtherResult == "plus" then
  1007.  
  1008.         pageNumber = pageNumber + 1
  1009.         -- If the user has clicked the right page icon and if the appPages page exists it will plus the current pageNumber
  1010.  
  1011.       elseif clickedOtherResult == "searchFilter" then
  1012.  
  1013.         dropMenu(loadDropMenu(dropMenuFilterTags,nil,x,y-1-numFilterTags,true))
  1014.         -- Loading the filter's drop menu
  1015.  
  1016.       elseif clickedOtherResult == "power" then
  1017.  
  1018.         dropMenu(loadDropMenu(dropMenuPower,nil,x,y))
  1019.         -- If the user has clicked the power icon, it will load the power drop menu
  1020.  
  1021.         if terminate then
  1022.  
  1023.           clearScreen()
  1024.         end
  1025.         -- Clearing the screen when the user terminates the OS to go back to the normal CC OS
  1026.  
  1027.       end
  1028.  
  1029.       if clickedOtherResult ~= "minus" and clickedOtherResult ~= "plus" then
  1030.  
  1031.         searchWord = ""
  1032.       end
  1033.       -- making the searchWord = nothing after you search
  1034.  
  1035.     elseif button == 2 and clickedAppResult and clickedAppResult[8] == "file" then
  1036.  
  1037.       dropMenu(loadDropMenu(dropMenuApps,clickedAppResult[7],x,y))
  1038.       -- First loading the drop menu for apps and then detecting if the user has clicked on it
  1039.  
  1040.     elseif button == 2 and clickedAppResult and clickedAppResult[8] == "folder" then
  1041.  
  1042.       dropMenu(loadDropMenu(dropMenuFolders,clickedAppResult[7],x,y))
  1043.       -- First loading the drop menu for folders and then detecting if the user has clicked on it
  1044.  
  1045.     elseif button == 2 then
  1046.  
  1047.       dropMenu(loadDropMenu(dropMenuOpts,"Create",x,y))
  1048.       -- If the user hasn't right clicked on an app they get this drop down menu
  1049.     end
  1050.   elseif event == "key" and currentFolder[1] then
  1051.     -- Seeing if the event is a key press for searching
  1052.  
  1053.     pageNumber = 1
  1054.     -- Resetting the page number so that the program doesn't error
  1055.  
  1056.     local pressedKey = button
  1057.     local held = x
  1058.     -- Redefining button and x to make sense for the key event
  1059.  
  1060.     if pressedKey == 14 then
  1061.       -- Checking if the user has pressed backspace
  1062.  
  1063.       if #searchWord > 0 then
  1064.         -- Checking if theres any more characters to delete in the searchWord
  1065.  
  1066.         searchWord = searchWord:sub(1,#searchWord-1)
  1067.         -- Removing the last character from the searchWord string
  1068.       end
  1069.  
  1070.     elseif pressedKey <= 10 and pressedKey >= 2 then
  1071.       -- Catching if the user has entered 1-9
  1072.  
  1073.       searchWord = searchWord .. pressedKey - 1
  1074.       -- Adding the correct number to the searchWord
  1075.  
  1076.     elseif pressedKey == 11 then
  1077.       -- Catching if the user has pressed 0
  1078.  
  1079.       searchWord = searchWord .. 0
  1080.       -- Adding 0 to the searchWord
  1081.  
  1082.     elseif pressedKey == 203 and appPages[pageNumber-1] then
  1083.  
  1084.       pageNumber = pageNumber - 1
  1085.       -- If you press the left arrow key it will try to minus 1 from pageNumber
  1086.  
  1087.     elseif pressedKey == 205 and appPages[pageNumber+1] then
  1088.  
  1089.       pageNumber = pageNumber + 1
  1090.       -- If you press the right arrow key it will try to add 1 to pageNumber
  1091.  
  1092.     else
  1093.       for key,val in pairs(keys) do
  1094.         -- Iterating for the amount of keys and values the keys api has
  1095.  
  1096.         if val == pressedKey then
  1097.           -- Checking if what the user entered matched the current val
  1098.  
  1099.           if #key == 1 then
  1100.             -- Seeing if the val is a single character, essentially making this only pick up a-z
  1101.  
  1102.             searchWord = searchWord .. key
  1103.             -- Adding the key to the searchWord string
  1104.           end
  1105.  
  1106.           break
  1107.           -- Breaking the for loop to prevent further unecessary iteration
  1108.         end
  1109.       end
  1110.     end
  1111.   end
  1112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement