Advertisement
Guest User

ccfm

a guest
Oct 31st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. --Chaos Console File Manager
  2. --By Metallist | Under GNU GPLv3
  3. --Version 1.0
  4. --Author's Skype : metalgamestudio
  5. --For ComputerCraft
  6.  
  7. local dir = "/home"
  8. local pos = 1
  9. local w,h = term.getSize()
  10. local filename = ""
  11. local screen = 0
  12. local ex = false
  13.  
  14. local col = {
  15.   ["bg"] = colours.red,
  16.   ["bar"] = colours.gray,
  17.   ["btn"] = colours.black,
  18.   ["txt"] = colours.black,
  19.   ["bt"] = colours.white
  20. }
  21.  
  22. local ucc = {
  23.   ["bg"] = colours.white,
  24.   ["bar"] = colours.white,
  25.   ["btn"] = colours.black,
  26.   ["txt"] = colours.black,
  27.   ["bt"] = colours.white
  28. }
  29. if not term.isColor() then col = ucc end
  30. local tfiles = {}
  31.  
  32. function redraw()
  33.   tfiles = fs.list(dir)
  34.   term.setBackgroundColor(col["bg"])
  35.   term.clear()
  36.   term.setCursorPos(1,1)
  37.   term.setBackgroundColor(col["bar"])
  38.   write(dir.." ")
  39.   term.setCursorPos(w-5,1)
  40.   term.setBackgroundCol(col["btn"])
  41.   term.setTextColor(col["bt"])
  42.   write("X")
  43.   term.setTextColor(col["txt"])
  44.   if screen == 0 then
  45.     term.setCursorPos(w-1,1)
  46.     term.setBackgroundColor(col["btn"])
  47.     term.setTextColor(col["bt"])
  48.     term.write("^")
  49.     term.setTextColor(col["txt"])
  50.     term.setBackgroundColor(col["bg"])
  51.     for i = pos, h - 2 do
  52.       if tfiles[i] == nil then break end
  53.       term.setCursorPos(1,i+1)
  54.       write(tfiles[i])
  55.       local type = ""
  56.       if fs.isDir(tfiles[i]) then
  57.         type = "folder"
  58.       else
  59.         local sp = split(tfiles[i], ".")
  60.         if #sp == 1 then
  61.           type = "executable"
  62.         else
  63.           type = sp[#sp] .. " file"
  64.         end
  65.       end
  66.       term.setCursorPos(20,i+1)
  67.       write(type)
  68.     end
  69.     term.setCursorPos(w - 1, h)
  70.     term.setbackgroundColor(col["btn"])
  71.     write(" ")
  72.   elseif screen == 1 then
  73.     term.setCursorPos(1,2)
  74.     term.setTextColor(col["txt"])
  75.     term.setBackgroundColor(col["bg"])
  76.     print("Name: " .. filename)
  77.     local fns = astapi.split(filename, ".")
  78.     local type
  79.     if #fns == 1 then
  80.       type = "executable"
  81.     else
  82.       type = fns[#fns]
  83.     end
  84.     print("Type: "..type)
  85.     term.setCursorPos(1,4)
  86.     term.setBackgroundColor(col["btn"])
  87.     write("Open")
  88.     term.setBackgroundColor(col["bg"])
  89.     write(" ")
  90.     term.setBackgroundColor(col["btn"])
  91.     write("Exit")
  92.     term.setBackgroundColor(col["bg"])
  93.     write(" ")
  94.     if type == "executable" or type == "sh" then
  95.       term.setBackgroundColor(col["btn"])
  96.       write("Run")
  97.       ex = true
  98.     else
  99.       ex = false
  100.     end
  101.   end
  102. end
  103. local works = true
  104. function increase()
  105.   if pos < #tfiles - h + 3 then pos = pos + 1 end
  106. end
  107.  
  108. function decrease()
  109.   if pos > 1 then pos = pos -1 end
  110. end
  111.  
  112. function OnMouseClick(cx,cy)
  113.   if cx == w -5 and cy == 1 then works = false end  --Exit
  114.   if screen == 0 then
  115.     if cx < w -2 then
  116.       if cy == 2 then
  117.         local d = astapi.split(dir,"/")
  118.         local dd = ""
  119.         for a = 1, #d -1 do
  120.           dd = dd.."/"..d[a]
  121.         end
  122.         dir = dd.. "/"
  123.       elseif cy > 2 then
  124.         if fs.isDir(dir..tfiles[cy - 2 + pos]) then
  125.           dir = dir .. tfiles[cy -2 +pos] .. "/"
  126.         else
  127.           filename = tfiles[cy - 2 + pos]
  128.           screen = 1
  129.         end
  130.       end
  131.     else
  132.       if cy == 1 then
  133.         increase()
  134.       elseif cy == h -1 then
  135.         decrease()
  136.       end
  137.     end
  138.   elseif screen == 1 then
  139.     if cy == 4 then
  140.       if cx >= 1 and cx <= 4 then
  141.         shell.run("edit "..filename)
  142.       elseif cx > 5 and cx <= 9 then
  143.         screen = 0
  144.       elseif ex == true and cx > 10 and cx <= 13 then
  145.         shell.run(filename)
  146.       end
  147.     end
  148.   end
  149. end
  150.  
  151. function mouseEvent()
  152.   local e,b,x,y = os.pullEvent("mouse_click")
  153.   if b == 0 then OnMouseClick(x,y) end
  154. end
  155.  
  156. function msEvent()
  157.   local e,d,sx,sy = os.pullEvent("mouse_scroll")
  158.   if d == 1 then
  159.     increase()
  160.   elseif d == -1 then
  161.     decrease()
  162.   end
  163. end
  164.  
  165. while works == true do
  166.   parallel.waitForAll(redraw,mouseEvent,msEvent)
  167.   sleep(1)
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement