Advertisement
Guest User

mc

a guest
Jul 1st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. -- Midnight Commander CC edition by RedTeapot
  2.  
  3. print("Starting Midnight Commander v0.1")
  4.  
  5. -- For tablet computers
  6. local screen_width, screen_height = term.getSize()
  7.  
  8. local state = {}
  9. state.path = {}
  10. state.path[1] = "/"
  11.  
  12. state.entry = {}
  13. state.entry.selected = 0
  14. state.entry.total = 1
  15. state.entry.list = {}
  16.  
  17. state.ui = {}
  18. state.ui.entries_per_screen = screen_height - 3
  19. state.ui.scroll = 0
  20.  
  21. local function get_path_string()
  22.     local result = ""
  23.     for i = 1, #state.path do
  24.         result = result .. state.path[i]
  25.         if i < #state.path and i > 1 then
  26.             result = result .. "/"
  27.         end
  28.     end
  29.     if #result > 1 then
  30.         result = result .. "/"
  31.     end
  32.     return result
  33. end
  34.  
  35. local function draw_header()
  36.     term.setCursorPos(1, 1)
  37.     term.setBackgroundColor(colors.blue)
  38.     term.setTextColor(colors.lightBlue)
  39.     term.clearLine()
  40.     term.setCursorPos(1, 1)
  41.     term.write("Midnight Commander v0.1")
  42. end
  43.  
  44. local function draw_subheader()
  45.     term.setCursorPos(1, 2)
  46.     term.setBackgroundColor(colors.blue)
  47.     term.setTextColor(colors.white)
  48.     term.clearLine()
  49.     term.write(get_path_string())
  50. end
  51.  
  52. local function get_current_entries()
  53.     local cd = get_path_string()
  54.     local entries = fs.list(cd)
  55.     entries[0] = ".."
  56.    
  57.     state.entry.list = entries
  58.     state.entry.total = #entries
  59.     return entries
  60. end
  61.  
  62. local function draw_files_list()
  63.     term.setCursorPos(1, 3)
  64.     term.setBackgroundColor(colors.black)
  65.     term.setTextColor(colors.white)
  66.    
  67.     local height = screen_height - 1 -- minus 1 for bottom panel
  68.    
  69.     local entries = get_current_entries()
  70.     local current_dir = get_path_string()
  71.    
  72.     for i = 3, height do
  73.         local file_num = i - 3 + state.ui.scroll
  74.         if file_num >= 0 and file_num <= #entries then
  75.             local path = current_dir .. entries[file_num]
  76.             term.setCursorPos(1, i)
  77.             if file_num == state.entry.selected then
  78.                 term.setBackgroundColor(colors.gray)
  79.             else
  80.                 term.setBackgroundColor(colors.black)
  81.             end
  82.             if entries[file_num] == ".." then
  83.                 term.setTextColor(colors.green)
  84.             elseif fs.isDir(path) then
  85.                 term.setTextColor(colors.yellow)
  86.             else
  87.                 term.setTextColor(colors.white)
  88.             end
  89.            
  90.             term.clearLine()
  91.             term.write(entries[file_num])
  92.         else
  93.             term.setCursorPos(1, i)
  94.             term.setBackgroundColor(colors.black)
  95.             term.clearLine()
  96.         end
  97.     end
  98. end
  99.  
  100. local function draw_bottom_panel()
  101.     term.setCursorPos(1, screen_height)
  102.     term.setBackgroundColor(colors.blue)
  103.     term.setTextColor(colors.white)
  104.     term.clearLine()
  105.     term.write("F10: Quit")
  106. end
  107.  
  108. local function draw()
  109.     term.setCursorPos(1, 1)
  110.     term.clear()
  111.     term.setCursorPos(1, 1)
  112.    
  113.     draw_header()
  114.     draw_subheader()
  115.     draw_files_list()
  116.     draw_bottom_panel()
  117. end
  118.  
  119. local function open()
  120.     local entry = state.entry.list[state.entry.selected]
  121.    
  122.     if entry == ".." and #state.path > 1 then
  123.         state_path_old = state.path
  124.         state.path = {}
  125.         for i = 1, #state_path_old - 1 do
  126.             state.path[i] = state_path_old[i]
  127.         end
  128.         state.entry.selected = 0
  129.     else
  130.         local path = get_path_string() .. state.entry.list[state.entry.selected]
  131.        
  132.         if fs.isDir(path) then
  133.             state.path[#state.path + 1] = state.entry.list[state.entry.selected]
  134.         end
  135.         state.entry.selected = 0
  136.     end
  137. end
  138.  
  139. draw()
  140.  
  141. while true do
  142.     event, arg1, arg2, arg3 = os.pullEvent()
  143.     if event == "key" then
  144.         key = keys.getName(arg1)
  145.         if key == "up" then
  146.             if state.entry.selected > 0 then
  147.                 state.entry.selected = state.entry.selected - 1
  148.             end
  149.         elseif key == "down" then
  150.             if state.entry.selected < state.entry.total then
  151.                 state.entry.selected = state.entry.selected + 1
  152.             end
  153.         elseif key == "f10" then
  154.             break
  155.         elseif key == "enter" then
  156.             open()
  157.         end
  158.         draw()
  159.     elseif event == "mouse_click" then
  160.         btn = arg1
  161.         x = arg2
  162.         y = arg3
  163.        
  164.         if btn == 1 then
  165.             if y > 2 and y < screen_height then
  166.                 local entry = y - 3
  167.                 if state.entry.selected == entry then
  168.                     open()
  169.                 else
  170.                     state.entry.selected = y - 3
  171.                 end
  172.             end
  173.         end
  174.        
  175.         draw()
  176.     end
  177. end
  178.  
  179. term.setBackgroundColor(colors.black)
  180. term.setTextColor(colors.white)
  181. term.clear()
  182. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement