Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Midnight Commander CC edition by RedTeapot
- print("Starting Midnight Commander v0.1")
- -- For tablet computers
- local screen_width, screen_height = term.getSize()
- local state = {}
- state.path = {}
- state.path[1] = "/"
- state.entry = {}
- state.entry.selected = 0
- state.entry.total = 1
- state.entry.list = {}
- state.ui = {}
- state.ui.entries_per_screen = screen_height - 3
- state.ui.scroll = 0
- local function get_path_string()
- local result = ""
- for i = 1, #state.path do
- result = result .. state.path[i]
- if i < #state.path and i > 1 then
- result = result .. "/"
- end
- end
- if #result > 1 then
- result = result .. "/"
- end
- return result
- end
- local function draw_header()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.lightBlue)
- term.clearLine()
- term.setCursorPos(1, 1)
- term.write("Midnight Commander v0.1")
- end
- local function draw_subheader()
- term.setCursorPos(1, 2)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.clearLine()
- term.write(get_path_string())
- end
- local function get_current_entries()
- local cd = get_path_string()
- local entries = fs.list(cd)
- entries[0] = ".."
- state.entry.list = entries
- state.entry.total = #entries
- return entries
- end
- local function draw_files_list()
- term.setCursorPos(1, 3)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local height = screen_height - 1 -- minus 1 for bottom panel
- local entries = get_current_entries()
- local current_dir = get_path_string()
- for i = 3, height do
- local file_num = i - 3 + state.ui.scroll
- if file_num >= 0 and file_num <= #entries then
- local path = current_dir .. entries[file_num]
- term.setCursorPos(1, i)
- if file_num == state.entry.selected then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.black)
- end
- if entries[file_num] == ".." then
- term.setTextColor(colors.green)
- elseif fs.isDir(path) then
- term.setTextColor(colors.yellow)
- else
- term.setTextColor(colors.white)
- end
- term.clearLine()
- term.write(entries[file_num])
- else
- term.setCursorPos(1, i)
- term.setBackgroundColor(colors.black)
- term.clearLine()
- end
- end
- end
- local function draw_bottom_panel()
- term.setCursorPos(1, screen_height)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.clearLine()
- term.write("F10: Quit")
- end
- local function draw()
- term.setCursorPos(1, 1)
- term.clear()
- term.setCursorPos(1, 1)
- draw_header()
- draw_subheader()
- draw_files_list()
- draw_bottom_panel()
- end
- local function open()
- local entry = state.entry.list[state.entry.selected]
- if entry == ".." and #state.path > 1 then
- state_path_old = state.path
- state.path = {}
- for i = 1, #state_path_old - 1 do
- state.path[i] = state_path_old[i]
- end
- state.entry.selected = 0
- else
- local path = get_path_string() .. state.entry.list[state.entry.selected]
- if fs.isDir(path) then
- state.path[#state.path + 1] = state.entry.list[state.entry.selected]
- end
- state.entry.selected = 0
- end
- end
- draw()
- while true do
- event, arg1, arg2, arg3 = os.pullEvent()
- if event == "key" then
- key = keys.getName(arg1)
- if key == "up" then
- if state.entry.selected > 0 then
- state.entry.selected = state.entry.selected - 1
- end
- elseif key == "down" then
- if state.entry.selected < state.entry.total then
- state.entry.selected = state.entry.selected + 1
- end
- elseif key == "f10" then
- break
- elseif key == "enter" then
- open()
- end
- draw()
- elseif event == "mouse_click" then
- btn = arg1
- x = arg2
- y = arg3
- if btn == 1 then
- if y > 2 and y < screen_height then
- local entry = y - 3
- if state.entry.selected == entry then
- open()
- else
- state.entry.selected = y - 3
- end
- end
- end
- draw()
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement