Advertisement
lxndio

CCC

Feb 1st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.60 KB | None | 0 0
  1. -- Computer Craft Commander by kornichen
  2. -- Recently modified on 04 February 2015
  3.  
  4. local w, h = term.getSize()
  5. local currentScreen = "mainScreen"
  6. local botBarNumberPositions = {1, 6, 11, 16, 21, 26, 31, 35, 39, 46}
  7. local botBarItems = {"Help", "Menu", "View", "Edit", "Copy", "ReMv", "Mkd", "Del", "PullDn", "Quit"}
  8. local grid = {
  9.     {"p", 1, 1, "+"},
  10.     {"p", 1, h - 2, "+"},
  11.     {"p", (w / 2) + 1, 1, "+"},
  12.     {"p", (w / 2) + 1, h - 2, "+"},
  13.     {"p", w, 1, "+"},
  14.     {"p", w, h - 2, "+"},
  15.     {"l", 1, 2, 1, h - 3, "|"},
  16.     {"l", (w / 2) + 1, 2, (w / 2) + 1, h - 3, "|"},
  17.     {"l", w, 2, w, h - 3, "|"},
  18.     {"l", 2, 1, w / 2, 1, "-"},
  19.     {"l", 2, h - 2, w / 2, h - 2, "-"},
  20.     {"l", (w / 2) + 2, 1, w, 1, "-"},
  21.     {"l", (w / 2) + 2, h - 2, w , h - 2, "-"},
  22.     {"p", 2, 2, "FILE"},
  23.     {"p", (w / 2) - 3, 2, "SIZE"},
  24.     {"p", (w / 2) + 2, 2, "FILE"},
  25.     {"p", w - 4, 2, "SIZE"},
  26. }
  27. local currentPathLeft = "/"
  28. local currentPathRight = "/"
  29. local fileListLeft = fs.list(currentPathLeft)
  30. local fileListRight = fs.list(currentPathRight)
  31. local activeColumn = "left"
  32. local oldActiveItemLeft = 0
  33. local oldActiveItemRight = 0
  34. local activeItemLeft = 1
  35. local activeItemRight = 1
  36. local pageLeft = 1
  37. local pageRight = 1
  38. local pagesLeft = 1
  39. local pagesRight = 1
  40. local currentCommand = ""
  41. local nextCharIsCapital = false
  42. local fileListLength = h - 5
  43.  
  44. local function drawMainScreen()
  45.     term.setTextColor(colors.cyan)
  46.     term.setBackgroundColor(colors.blue)
  47.     term.clear()
  48.     for i = 1, #grid do
  49.         if grid[i][1] == "p" then
  50.             term.setCursorPos(grid[i][2], grid[i][3])
  51.             term.write(grid[i][4])
  52.         elseif grid[i][1] == "l" then
  53.             if grid[i][2] == grid[i][4] then
  54.                 for j = grid[i][3], grid[i][5] do
  55.                     term.setCursorPos(grid[i][2], j)
  56.                     term.write(grid[i][6])
  57.                 end
  58.             elseif grid[i][3] == grid[i][5] then
  59.                 for j = grid[i][2], grid[i][4] do
  60.                     term.setCursorPos(j, grid[i][3])
  61.                     term.write(grid[i][6])
  62.                 end
  63.             end
  64.         end
  65.     end
  66.     term.setBackgroundColor(colors.black)
  67.     term.setCursorPos(1, h - 1)
  68.     term.clearLine()
  69.     term.setCursorPos(1, h)
  70.     term.setBackgroundColor(colors.cyan)
  71.     term.clearLine()
  72.     for i = 1, #botBarNumberPositions do
  73.         term.setTextColor(colors.lightGray)
  74.         term.setBackgroundColor(colors.black)
  75.         term.setCursorPos(botBarNumberPositions[i], h)
  76.         term.write(tostring(i))
  77.         term.setTextColor(colors.black)
  78.         term.setBackgroundColor(colors.cyan)
  79.         term.write(botBarItems[i])
  80.     end
  81.  
  82.     if fs.list(currentPathLeft) ~= fileListLeft or oldActiveItemLeft ~= activeItemLeft then
  83.         fileListLeft = fs.list(currentPathLeft)
  84.         table.insert(fileListLeft, 1, "..")
  85.         pageLeft = math.ceil(activeItemLeft / fileListLength)
  86.         pagesLeft = math.ceil(#fileListLeft / fileListLength)
  87.         local startItem = (fileListLength * (pageLeft - 1)) + 1
  88.         local endItem = 0
  89.         if pageLeft == pages then
  90.             endItem = #fileListLeft % pages
  91.         else
  92.             endItem = fileListLength * pageLeft
  93.         end
  94.         for i = startItem, endItem do
  95.             if i == activeItemLeft then
  96.                 term.setTextColor(colors.blue)
  97.                 term.setBackgroundColor(colors.cyan)
  98.                 term.setCursorPos(2, i - (fileListLength * (pageLeft - 1)) + 2)
  99.                 for j = 2, w / 2 do
  100.                     write(" ")
  101.                 end
  102.             else
  103.                 term.setTextColor(colors.cyan)
  104.                 term.setBackgroundColor(colors.blue)
  105.             end
  106.             term.setCursorPos(2, i - (fileListLength * (pageLeft - 1)) + 2)
  107.             term.write(fileListLeft[i] or "")
  108.             term.setCursorPos((w / 2) - 5, i - (fileListLength * (pageLeft - 1)) + 2)
  109.             if fileListLeft[i] ~= ".." then
  110.                 if currentPathLeft == "/" then
  111.                     if i <= #fileListLeft then
  112.                         term.write(" " .. fs.getSize(currentPathLeft .. fileListLeft[i]))
  113.                     end
  114.                 else
  115.                     if i <= #fileListLeft then
  116.                         term.write(" " .. fs.getSize(currentPathLeft .. "/" .. fileListLeft[i]))
  117.                     end
  118.                 end
  119.             end
  120.         end
  121.     end
  122.     if fs.list(currentPathRight) ~= fileListRight or oldActiveItemRight ~= activeItemRight then
  123.         fileListRight = fs.list(currentPathRight)
  124.         table.insert(fileListRight, 1, "..")
  125.         pageRight = math.ceil(activeItemRight / fileListLength)
  126.         pagesRight = math.ceil(#fileListRight / fileListLength)
  127.         local startItem = (fileListLength * (pageRight - 1)) + 1
  128.         local endItem = 0
  129.         if pageRight == pages then
  130.             endItem = #fileListRight % pages
  131.         else
  132.             endItem = fileListLength * pageRight
  133.         end
  134.         for i = startItem, endItem do
  135.             if i == activeItemRight then
  136.                 term.setTextColor(colors.blue)
  137.                 term.setBackgroundColor(colors.cyan)
  138.                 term.setCursorPos((w / 2) + 2, i - (fileListLength * (pageRight - 1)) + 2)
  139.                 for j = 2, w / 2 do
  140.                     write(" ")
  141.                 end
  142.             else
  143.                 term.setTextColor(colors.cyan)
  144.                 term.setBackgroundColor(colors.blue)
  145.             end
  146.             term.setCursorPos((w / 2) + 2, i - (fileListLength * (pageRight - 1)) + 2)
  147.             term.write(fileListRight[i] or "")
  148.             term.setCursorPos(w - 6, i - (fileListLength * (pageRight - 1)) + 2)
  149.             if fileListRight[i] ~= ".." then
  150.                 if currentPathRight == "/" then
  151.                     if i <= #fileListRight then
  152.                         term.write(" " .. fs.getSize(currentPathRight .. fileListRight[i]))
  153.                     end
  154.                 else
  155.                     if i <= #fileListRight then
  156.                         term.write(" " .. fs.getSize(currentPathRight .. "/" .. fileListRight[i]))
  157.                     end
  158.                 end
  159.             end
  160.         end
  161.     end
  162.     term.setTextColor(colors.lightGray)
  163.     term.setBackgroundColor(colors.black)
  164.     term.setCursorPos(1, h - 1)
  165.     if activeColumn == "left" then
  166.         term.write(currentPathLeft .. "> " .. currentCommand)
  167.     elseif activeColumn == "right" then
  168.         term.write(currentPathRight .. "> " .. currentCommand)
  169.     end
  170. end
  171.  
  172. local function drawInputBox(title, text)
  173.     term.setTextColor(colors.black)
  174.     term.setBackgroundColor(colors.lightGray)
  175.     for y = (h / 2) - 4, (h / 2) + 4 do
  176.         for x = (w / 2) - 15, (w / 2) + 15 do
  177.             term.setCursorPos(x, y)
  178.             if y == (h / 2) - 4 then
  179.                 term.write(" ")
  180.             elseif y == (h / 2) + 4 then
  181.                 term.write(" ")
  182.             elseif y == (h / 2) - 3 then
  183.                 if x == (w / 2) - 15 then
  184.                     term.write(" ")
  185.                 elseif x == (w / 2) + 15 then
  186.                     term.write(" ")
  187.                 elseif x == (w / 2) - 14 then
  188.                     term.write("+")
  189.                 elseif x == (w / 2) + 14 then
  190.                     term.write("+")
  191.                 else
  192.                     term.write("-")
  193.                 end
  194.             elseif y == (h / 2) + 1 then
  195.                 if x == (w / 2) - 14 then
  196.                     term.write("|")
  197.                 elseif x == (w / 2) + 14 then
  198.                     term.write("|")
  199.                 else
  200.                     if x > (w / 2) - 13 and x < (w / 2) + 13 then
  201.                         term.setBackgroundColor(colors.black)
  202.                         term.write(" ")
  203.                         term.setBackgroundColor(colors.lightGray)
  204.                     else
  205.                         term.write(" ")
  206.                     end
  207.                 end
  208.             elseif y == (h / 2) + 3 then
  209.                 if x == (w / 2) - 15 then
  210.                     term.write(" ")
  211.                 elseif x == (w / 2) + 15 then
  212.                     term.write(" ")
  213.                 elseif x == (w / 2) - 14 then
  214.                     term.write("+")
  215.                 elseif x == (w / 2) + 14 then
  216.                     term.write("+")
  217.                 else
  218.                     term.write("-")
  219.                 end
  220.             else
  221.                 if x == (w / 2) - 14 then
  222.                     term.write("|")
  223.                 elseif x == (w / 2) + 14 then
  224.                     term.write("|")
  225.                 else
  226.                     term.write(" ")
  227.                 end
  228.             end
  229.         end
  230.     end
  231.     term.setTextColor(colors.blue)
  232.     term.setCursorPos((w / 2) - (string.len(title) / 2), (h / 2) - 3)
  233.     term.write(" " .. title .. " ")
  234.     term.setTextColor(colors.black)
  235.     term.setCursorPos((w / 2) - 12, (h / 2) - 1)
  236.     term.write(text)
  237.     term.setTextColor(colors.white)
  238.     term.setBackgroundColor(colors.black)
  239.     term.setCursorPos((w / 2) - 12, (h / 2) + 1)
  240.     return read()
  241. end
  242.  
  243. local function getParentPath(path)
  244.     local slashPos = 0
  245.     local i = string.len(path) - 1
  246.     while slashPos == 0 do
  247.         if string.sub(path, i, i) == "/" then
  248.             slashPos = i
  249.         end
  250.         i = i - 1
  251.     end
  252.     return string.sub(path, 1, i)
  253. end
  254.  
  255. term.setCursorBlink(true)
  256.  
  257. while true do
  258.     drawMainScreen()
  259.     local event, button, x, y = os.pullEvent()
  260.     if currentScreen == "mainScreen" then
  261.         if event == "key" then
  262.             local char = keys.getName(button)
  263.             if char == "space" then
  264.                 char = " "
  265.             elseif char == "leftShift" or char == "rightShift" then
  266.                 nextCharIsCapital = true
  267.                 char = ""
  268.             elseif char == "up" then
  269.                 if activeColumn == "left" then
  270.                     if activeItemLeft > 1 then
  271.                         activeItemLeft = activeItemLeft - 1
  272.                     end
  273.                 elseif activeColumn == "right" then
  274.                     if activeItemRight > 1 then
  275.                         activeItemRight = activeItemRight - 1
  276.                     end
  277.                 end
  278.                 char = ""
  279.             elseif char == "down" then
  280.                 if activeColumn == "left" then
  281.                     if activeItemLeft < #fileListLeft then
  282.                         activeItemLeft = activeItemLeft + 1
  283.                     end
  284.                 elseif activeColumn == "right" then
  285.                     if activeItemRight < #fileListRight then
  286.                         activeItemRight = activeItemRight + 1
  287.                     end
  288.                 end
  289.                 char = ""
  290.             elseif char == "tab" then
  291.                 if activeColumn == "left" then
  292.                     activeColumn = "right"
  293.                 elseif activeColumn == "right" then
  294.                     activeColumn = "left"
  295.                 end
  296.                 char = ""
  297.             elseif char == "backspace" then
  298.                 currentCommand = string.sub(currentCommand, 1, string.len(currentCommand) - 1)
  299.                 char = ""
  300.             elseif char == "enter" then
  301.                 if currentCommand == "" then
  302.                     if activeColumn == "left" then
  303.                         if fileListLeft[activeItemLeft] == ".." then
  304.                             if currentPathLeft ~= "/" then
  305.                                 currentPathLeft = getParentPath(currentPathLeft)
  306.                                 if currentPathLeft == "" then
  307.                                     currentPathLeft = "/"
  308.                                 end
  309.                                 activeItemLeft = 1
  310.                                 pageLeft = 1
  311.                             end
  312.                         elseif fs.isDir(currentPathLeft .. "/" .. fileListLeft[activeItemLeft]) then
  313.                             if currentPathLeft == "/" then
  314.                                 currentPathLeft = currentPathLeft .. fileListLeft[activeItemLeft]
  315.                             else
  316.                                 currentPathLeft = currentPathLeft .. "/" .. fileListLeft[activeItemLeft]
  317.                             end
  318.                             activeItemLeft = 1
  319.                             pageLeft = 1
  320.                         end
  321.                     elseif activeColumn == "right" then
  322.                         if fileListRight[activeItemRight] == ".." then
  323.                             if currentPathRight ~= "/" then
  324.                                 currentPathRight = getParentPath(currentPathRight)
  325.                                 if currentPathRight == "" then
  326.                                     currentPathRight = "/"
  327.                                 end
  328.                                 activeItemRight = 1
  329.                                 pageRight = 1
  330.                             end
  331.                         elseif fs.isDir(currentPathRight .. "/" .. fileListRight[activeItemRight]) then
  332.                             if currentPathRight == "/" then
  333.                                 currentPathRight = currentPathRight .. fileListRight[activeItemRight]
  334.                             else
  335.                                 currentPathRight = currentPathRight .. "/" .. fileListRight[activeItemRight]
  336.                             end
  337.                             activeItemRight = 1
  338.                             pageRight = 1
  339.                         end
  340.                     end
  341.                 else
  342.                     if currentCommand == "exit" then
  343.                         currentCommand = ""
  344.                         error()
  345.                     else
  346.                         currentCommand = ""
  347.                     end
  348.                 end
  349.                 char = ""
  350.             elseif char == "f1" then
  351.                 --
  352.             elseif char == "f4" then
  353.                 --
  354.             elseif char == "f5" then
  355.                 if activeColumn == "left" then
  356.                     if fileListLeft[activeItemLeft] ~= ".." then
  357.                         if currentPathLeft == "/" then
  358.                             fs.copy("/" .. fileListLeft[activeItemLeft], currentPathRight)
  359.                         else
  360.                             fs.copy(currentPathLeft .. "/" .. fileListLeft[activeItemLeft], currentPathRight)
  361.                         end
  362.                     end
  363.                 elseif activeColumn == "right" then
  364.                     if fileListRight[activeItemRight] ~= ".." then
  365.                         if currentPathRight == "/" then
  366.                             fs.copy("/" .. fileListRight[activeItemRight], currentPathLeft)
  367.                         else
  368.                             fs.copy(currentPathRight .. "/" .. fileListRight[activeItemRight], currentPathLeft)
  369.                         end
  370.                     end
  371.                 end
  372.                 char = ""
  373.             elseif char == "f6" then
  374.                 local newFileName = drawInputBox("Move file", "New filename:")
  375.                 if activeColumn == "left" then
  376.                     if fileListLeft[activeItemLeft] ~= ".." then
  377.                         if currentPathLeft == "/" then
  378.                             fs.copy("/" .. fileListLeft[activeItemLeft], currentPathRight .. "/" .. newFileName)
  379.                         else
  380.                             fs.copy(currentPathLeft .. "/" .. fileListLeft[activeItemLeft], currentPathRight .. "/" .. newFileName)
  381.                         end
  382.                     end
  383.                 elseif activeColumn == "right" then
  384.                     if fileListRight[activeItemRight] ~= ".." then
  385.                         if currentPathRight == "/" then
  386.                             fs.copy("/" .. fileListRight[activeItemRight], currentPathLeft .. "/" .. newFileName)
  387.                         else
  388.                             fs.copy(currentPathRight .. "/" .. fileListRight[activeItemRight], currentPathLeft .. "/" .. newFileName)
  389.                         end
  390.                     end
  391.                 end
  392.                 char = ""
  393.             elseif char == "f7" then
  394.                 local directory = drawInputBox("Create a new directory", "Enter directory name:")
  395.                 if activeColumn == "left" then
  396.                     if currentPathLeft == "/" then
  397.                         fs.makeDir("/" .. directory)
  398.                     else
  399.                         fs.makeDir(currentPathLeft .. "/" .. directory)
  400.                     end
  401.                 elseif activeColumn == "right" then
  402.                     if currentPathRight == "/" then
  403.                         fs.makeDir("/" .. directory)
  404.                     else
  405.                         fs.makeDir(currentPathRight .. "/" .. directory)
  406.                     end
  407.                 end
  408.                 char = ""
  409.             elseif char == "f8" then
  410.                 local deleteApproval = drawInputBox("Delete", "Type YES to delete.")
  411.                 if deleteApproval == "YES" then
  412.                     if activeColumn == "left" then
  413.                         if fileListLeft[activeItemLeft] ~= ".." then
  414.                             if currentPathLeft == "/" then
  415.                                 fs.delete("/" .. fileListLeft[activeItemLeft])
  416.                             else
  417.                                 fs.delete(currentPathLeft .. "/" .. fileListLeft[activeItemLeft])
  418.                             end
  419.                         end
  420.                         activeItemLeft = 1
  421.                         pageLeft = 1
  422.                     elseif activeColumn == "right" then
  423.                         if fileListRight[activeItemRight] ~= ".." then
  424.                             if currentPathRight == "/" then
  425.                                 fs.delete("/" .. fileListRight[activeItemRight])
  426.                             else
  427.                                 fs.delete(currentPathRight .. "/" .. fileListRight[activeItemRight])
  428.                             end
  429.                         end
  430.                         activeItemRight = 1
  431.                         pageRight = 1
  432.                     end
  433.                 end
  434.                 char = ""
  435.             elseif char == "f10" then
  436.                 error()
  437.             end
  438.             if nextCharIsCapital then
  439.                 if char ~= "" then
  440.                     currentCommand = currentCommand .. string.upper(char)
  441.                     nextCharIsCapital = false
  442.                 end
  443.             else
  444.                 currentCommand = currentCommand .. char
  445.             end
  446.         end
  447.     end
  448. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement