Advertisement
Gulligagardinen

Ice-Browser

Jan 18th, 2014
1,885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.43 KB | None | 0 0
  1. --[[
  2.         Project info:
  3.        
  4.         Name: Ice-Browser
  5.         Creator: Cutecurtain
  6.         Language: Lua (CC)
  7.         Website: None
  8.         License: GNU GPL
  9.  
  10.         Version: 1.1
  11. ]]
  12.  
  13. --[[
  14.         Changelog:
  15.           1.0:
  16.             Public Release
  17.           1.1:
  18.             Added Double-click support and changed the "are you sure?"-window
  19. ]]
  20.  
  21. --[[
  22.         LICENSE:
  23.        
  24.         Ice-Browser - Graphical User Interface File-Browsing
  25.         Copyright © 2014 Cutecurtain
  26.  
  27.                 This program is free software: you can redistribute it and/or modify
  28.                 it under the terms of the GNU General Public License as published by
  29.                 the Free Software Foundation, either version 3 of the License, or
  30.                 (at your option) any later version.
  31.  
  32.                 This program is distributed in the hope that it will be useful,
  33.                 but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35.                 GNU General Public License for more details.
  36.  
  37.                 You should have received a copy of the GNU General Public License
  38.                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
  39. ]]
  40.  
  41. --Width and Height of screen
  42. local w, h = term.getSize()
  43. --Show hidden files and directories
  44. local hidden = false
  45. --Current directory that is open
  46. local dir = ""
  47. --Refers to the current selected file or directory
  48. local slc = 0
  49. --Indicates if program is running
  50. local running = true
  51. --If time is measured in 24 h time
  52. local mTime = true
  53. --Table with the files
  54. local files = {}
  55. --Scroll the files
  56. local scroll = 0
  57. --Name of file that is copied and if it should be moved
  58. local copy = {}
  59.  
  60. --Set background- and text-colour
  61. local function setColor(bc, tc)
  62.     if not bc then bc = 32768 end
  63.     if not tc then tc = 1 end
  64.     term.setBackgroundColor(bc)
  65.     term.setTextColor(tc)
  66. end
  67.  
  68. --Print at left side on specific line
  69. local function printLeft(y, str, bc, tc)
  70.     setColor(bc, tc)
  71.     term.setCursorPos(1, y)
  72.     term.write(str)
  73. end
  74.  
  75. --Print at right side of specific line
  76. local function printRight(y, str, bc, tc)
  77.     setColor(bc, tc)
  78.     term.setCursorPos(w-#str+1, y)
  79.     term.write(str)
  80. end
  81.  
  82. --Print centred if specific line
  83. local function printCentred(y, str, bc, tc)
  84.     setColor(bc, tc)
  85.     term.setCursorPos(w/2-#str/2, y)
  86.     term.write(str)
  87. end
  88.  
  89. --Print at specific point on screen
  90. local function printHere(x, y, str, bc, tc)
  91.     setColor(bc, tc)
  92.     term.setCursorPos(x, y)
  93.     term.write(str)
  94. end
  95.  
  96. --Get time
  97. local function getTime()
  98.     return textutils.formatTime(os.time(), mTime)
  99. end
  100.  
  101. --Draw time at point
  102. local function printTime(x, y, bc, tc)
  103.     local tTime = getTime()
  104.     if x == "default" then x = w-#tTime+1 end
  105.     printHere(x, y, tTime, bc, tc)
  106. end
  107.  
  108. --Get the computer files
  109. local function getFiles()
  110.     files = {}
  111.     if hidden then
  112.         files = fs.list(dir)
  113.     else
  114.         local temp = fs.list(dir)
  115.         for k, v in pairs(temp) do
  116.             if v:sub(1, 1) ~= '.' then
  117.                 files[#files+1] = v
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. --Draw message if an error or something that is not allowed occurs
  124. local function drawError(msg)
  125.     printCentred(h/2-1, string.rep(' ', #msg), 128)
  126.     printCentred(h/2, msg, 128, 16384)
  127.     printCentred(h/2+1, string.rep(' ', #msg), 128)
  128.     sleep(1.5)
  129. end
  130.  
  131. --Draw header of desktop
  132. local function drawHeader()
  133.     printHere(1, 1, string.rep(' ', w), 128, 1)
  134.     printHere(1, h, string.rep(' ', w), 128, 1)
  135.     printLeft(1, 'X', 16384, 32768)
  136.     printHere(3, 1, dir, 128, 1)
  137.     printTime("default", h, 128, 1)
  138.     if dir ~= "" then
  139.         printLeft(h, "<back> <home>", 128, 1)
  140.     end
  141.     if copy[1] then
  142.         printRight(1, "Copied: "..copy[1]..copy[2], 128, 1)
  143.     end
  144.     for i = 2, h-1 do
  145.         if i == 2 then
  146.             printRight(i, '^', 256, 1)
  147.         elseif i == h-1 then
  148.             printRight(i, 'v', 256, 1)
  149.         else
  150.             printRight(i, ' ', 256, 1)
  151.         end
  152.     end
  153. end
  154.  
  155. --Draw file menu
  156. local function drawMenu()
  157.     for i = 2, h-1 do
  158.         printHere(w-11, i, "           ", 2048, 1)
  159.     end
  160.     if slc == 0 then
  161.         if hidden then
  162.             printHere(w-11, 5, "Hide hidden", 2048, 1)
  163.         else
  164.             printHere(w-11, 5, "Show hidden", 2048, 1)
  165.         end
  166.         if copy[1] then
  167.             printHere(w-11, 6, " De-copy   ", 2048, 1)
  168.         end
  169.         printHere(w-11, 2, " New file  ", 2048, 1)
  170.         printHere(w-11, 3, " New dir   ", 2048, 1)
  171.         printHere(w-11, 4, " Paste     ", 2048, 1)
  172.     elseif slc > 0 then
  173.         if fs.isDir(dir..files[slc]) then
  174.             printHere(w-11, 2, " Open      ", 2048, 1)
  175.         else
  176.             printHere(w-11, 2, " Run       ", 2048, 1)
  177.             printHere(w-11, 8, " Edit      ", 2048, 1)
  178.         end
  179.         printHere(w-11, 3, " Deselect  ", 2048, 1)
  180.         printHere(w-11, 4, " Rename    ", 2048, 1)
  181.         printHere(w-11, 5, " Copy      ", 2048, 1)
  182.         printHere(w-11, 6, " Cut       ", 2048, 1)
  183.         printHere(w-11, 7, " Remove    ", 2048, 1)
  184.     end
  185. end
  186.  
  187. --Click the options in the file menu
  188. local function clickMenu(event)
  189.     if slc == 0 then
  190.         if event[1] == "mouse_click" then
  191.             if event[3] >= w-11 and event[3] <= w-1 and event[4] == 2 then
  192.                 printHere(w-11, 2, "           ", 2048, 1)
  193.                 term.setCursorPos(w-11, 2)
  194.                 local nName = io.read()
  195.                 if nName ~= "" then
  196.                     if fs.exists(dir..nName) then
  197.                         drawError("File already exists")
  198.                     else
  199.                         local f = io.open(dir..nName, 'w')
  200.                         f:close()
  201.                     end
  202.                 end
  203.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 3 then
  204.                 printHere(w-11, 3, "           ", 2048, 1)
  205.                 term.setCursorPos(w-11, 3)
  206.                 local nName = io.read()
  207.                 if nName ~= "" then
  208.                     if fs.exists(dir..nName) then
  209.                         drawError("Dir already exists")
  210.                     else
  211.                         fs.makeDir(dir..nName)
  212.                     end
  213.                 end
  214.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 4 then
  215.                 if copy[1] then
  216.                     local problem = false
  217.                     if dir == copy[1]..copy[2]..'/' then problem = true end
  218.                     if copy[3] == "copy" and not problem then
  219.                         if fs.exists(dir..copy[2]) then
  220.                             drawError("Path already exists")
  221.                         else
  222.                             fs.copy(copy[1]..copy[2], dir..copy[2])
  223.                         end
  224.                     elseif copy[3] == "move" and not problem then
  225.                         if fs.exists(dir..copy[2]) then
  226.                             drawError("Can't move to same dir")
  227.                         else
  228.                             fs.move(copy[1]..copy[2], dir..copy[2])
  229.                             copy = {}
  230.                         end
  231.                     else
  232.                         drawError("copy:13: Can't copy a directory inside it self")
  233.                     end
  234.                 end
  235.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 5 then
  236.                 if hidden then
  237.                     hidden = false
  238.                 else
  239.                     hidden = true
  240.                 end
  241.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 6 then
  242.                 if copy[1] then
  243.                     copy = {}
  244.                 end
  245.             end
  246.         end
  247.     elseif slc > 0 then
  248.         if event[1] == "mouse_click" then
  249.             if event[3] >= w-11 and event[3] <= w-1 and event[4] == 2 then
  250.                 if fs.isDir(dir..files[slc]) then
  251.                     dir = dir..files[slc].."/"
  252.                     slc = 0
  253.                     scroll = 0
  254.                 else
  255.                     setColor(32768, 1)
  256.                     return shell.run(dir..files[slc])
  257.                 end
  258.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 3 then
  259.                 slc = 0
  260.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 4 then
  261.                 local tc = 32768
  262.                 if fs.isDir(dir..files[slc]) then tc = 32 end
  263.                 printHere(2, 2+slc-scroll, string.rep(' ', #files[slc]), 1, tc)
  264.                 term.setCursorPos(2, 2+slc-scroll)
  265.                 local nName = io.read()
  266.                 if nName ~= "" then
  267.                     fs.move(dir..files[slc], dir..nName)
  268.                 end
  269.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 5 then
  270.                 if dir..files[slc] ~= "rom" then
  271.                     copy[1] = dir
  272.                     copy[2] = files[slc]
  273.                     copy[3] = "copy"
  274.                 else
  275.                     drawError("Access Denied")
  276.                 end
  277.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 6 then
  278.                 if dir..files[slc] ~= "rom" and dir..files[slc] ~= "disk" then
  279.                     copy[1] = dir
  280.                     copy[2] = files[slc]
  281.                     copy[3] = "move"
  282.                 else
  283.                     drawError("move:13: Access Denied")
  284.                 end
  285.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 7 then
  286.                 printCentred(h/2, "Are you sure? ", 2048, 32768)
  287.                 printCentred(h/2+1, " Yes       No ", 2048, 32768)
  288.                 local run = true
  289.                 while run do
  290.                     os.startTimer(0.1)
  291.                     local temp = {os.pullEvent()}
  292.                     if temp[1] == "timer" then
  293.                         printTime("default", h, 128, 1)
  294.                     elseif temp[1] == "mouse_click" then
  295.                         if temp[2] == 1 and temp[4] == math.floor(h/2+1) then
  296.                             if temp[3] >= math.floor(w/2-6) and temp[3] <= math.floor(w/2-4) then
  297.                                 if dir..files[slc] ~= "rom" and dir..files[slc] ~= "disk" then
  298.                                     fs.delete(dir..files[slc])
  299.                                     slc = 0
  300.                                 else
  301.                                     drawError("delete:9: Access Denied")
  302.                                 end
  303.                                 run = false
  304.                             elseif temp[3] >= math.floor(w/2+4) and temp[3] <= math.floor(w/2+5) then
  305.                                 run = false
  306.                             end
  307.                         end
  308.                     end
  309.                 end
  310.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 8 then
  311.                 if not fs.isDir(dir..files[slc]) then
  312.                     return shell.run("edit "..dir..files[slc])
  313.                 end
  314.             end
  315.         end
  316.     end
  317.     if event[1] == "mouse_click" and event[2] == 1 and event[3] >= 1 and event[3] <= 6 and event[4] == h and dir ~= "" then
  318.         local slash = 0
  319.         for i = 1, #dir-1 do
  320.             if dir:sub(i, i) == '/' then
  321.                 slash = i
  322.             end
  323.         end
  324.         if slash == 0 then dir = ""
  325.         else dir = dir:sub(1, slash) end
  326.         slc = 0
  327.         scroll = 0
  328.     elseif event[1] == "mouse_click" and event[2] == 1 and event[3] >= 8 and event[3] <= 13 and event[4] == h and dir ~= "" then
  329.         dir = ""
  330.         slc = 0
  331.         scroll = 0
  332.     end
  333. end
  334.  
  335. --Draw the files and directories
  336. local function drawFiles()
  337.     for k, v in pairs(files) do
  338.         local bc, tc = 1, 32768
  339.         if fs.isDir(dir..v) then tc = 32 end
  340.         if k == slc then bc = 256 end
  341.         printHere(2, 2+k-scroll, v, bc, tc)
  342.     end
  343.     drawMenu()
  344. end
  345.  
  346. --Scroll in a large list of files
  347. local function scrollFiles(upDown)
  348.     if upDown == -1 then
  349.         if scroll > 0 then scroll = scroll-1 end
  350.     elseif upDown == 1 then
  351.         if scroll < #files-h/1.2 then scroll = scroll+1 end
  352.     end
  353. end
  354.  
  355. --Click files
  356. local function clickFiles(event)
  357.     if event[1] == "mouse_scroll" then
  358.         scrollFiles(event[2])
  359.     elseif event[1] == "mouse_click" or "mouse_drag" then
  360.         if event[2] == 1 then
  361.             if event[3] == 1 and event[4] == 1 then
  362.                 running = false
  363.                 return nil
  364.             elseif event[3] >= w-#getTime()+1 and event[3] <= w and event[4] == h then
  365.                 if mTime then mTime = false
  366.                 else mTime = true end
  367.             elseif event[3] == w and event[4] >= 2 and event[4] <= h/2 then
  368.                 scrollFiles(-1)
  369.             elseif event[3] == w and event[4] > h/2 and event[4] <= h-1 then
  370.                 scrollFiles(1)
  371.             end
  372.             for k, v in pairs(files) do
  373.                 if event[3] >= 2 and event[3] <= #v+1 and event[4] == 2+k-scroll and event[4] ~= 1 and event[4] ~= h then
  374.                     if slc == k and fs.isDir(dir..files[slc]) then
  375.                         dir = dir..files[slc].."/"
  376.                         slc = 0
  377.                         scroll = 0
  378.                     elseif slc == k and not fs.isDir(dir..files[slc]) then
  379.                         setColor(32768, 1)
  380.                         return shell.run(dir..files[slc])
  381.                     else
  382.                         slc = k
  383.                     end
  384.                 end
  385.             end
  386.         end
  387.     end
  388.     clickMenu(event)
  389. end
  390.  
  391. --Draw the desktop environment
  392. local function drawDesktop()
  393.     if running then
  394.         setColor(1)
  395.         term.clear()
  396.         drawFiles()
  397.         drawHeader()
  398.     else
  399.         setColor(32768, 1)
  400.         term.clear()
  401.         printCentred(1, "Thanks for using Ice-Browser! :D")
  402.         printHere(1, 2, string.rep("-", w), 32768, 1)
  403.         term.setCursorPos(1, 3)
  404.     end
  405. end
  406.  
  407. --Main loop
  408. getFiles()
  409. drawDesktop()
  410. while running do
  411.     os.startTimer(0.1)
  412.     local event = {os.pullEvent()}
  413.     if event[1] == "timer" then
  414.         printTime("default", h, 128, 1)
  415.     else
  416.         clickFiles(event)
  417.         getFiles()
  418.         drawDesktop()
  419.     end
  420. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement