Guest User

Lattice_pre1.0

a guest
Sep 21st, 2015
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.21 KB | None | 0 0
  1. lat_theme = nil
  2. --image files:
  3. local img_folder = {{16,16,},
  4. {16,16,16,16,16,},
  5. {16,16,16,16,16,},
  6. {16,16,16,16,16,}}
  7. local img_file = {{8,8,8,8,8,},
  8. {8,1,1,1,8,},
  9. {8,1,1,1,8,},
  10. {8,8,8,8,8,},}
  11. --code:
  12. local tArgs = { ... }
  13. local home
  14. local root
  15. local clipboard
  16. if type(lat_theme) ~= "table" then
  17. lat_theme = {fore=colors.cyan;
  18. back=colors.white;
  19. text=colors.black;
  20. selected=colors.cyan;
  21. dir=colors.lime;
  22. multiselect=colors.lightGray;
  23. }
  24. end
  25. if #tArgs > 0 then
  26. home = tArgs[1]
  27. else
  28. home = ""
  29. end
  30. if #tArgs > 1 then
  31. root = tArgs[2]
  32. else
  33. root = "/"
  34. end
  35. local history = {}
  36. local w,h = term.getSize()
  37. local iconw = 8
  38. local iconh = 7
  39. local nIco = math.floor((w-1)/iconw)
  40. --mode 1 is the list view, mode 2 is the detail view, mode 3 is the icon view
  41. local mode = 2
  42. local function mathdown(num)
  43. if num % 1 ~= 0 then
  44. return math.floor(num)
  45. end
  46. return num - 1
  47. end
  48. local function formatNumber(num)
  49. local extra = {"B","KB","MB"}
  50. local eindex = 1
  51. while num > 1000 do
  52. num = num / 1000
  53. num = math.floor(num)
  54. eindex = eindex + 1
  55. end
  56. return tostring(num..extra[eindex])
  57. end
  58. local function drawLine(y,txt,col,align)
  59. term.setTextColor(colors.white)
  60. if type(align) ~= "number" then align = 2 end
  61. if type(col) ~= "number" then col = colors.white end
  62. term.setBackgroundColor(col)
  63. term.setCursorPos(1,y)
  64. term.clearLine()
  65. local xpos
  66. xpos = align == 1 and 1 or (align == 2 and math.floor(w/2-#txt/2) or w-#txt)
  67. term.setCursorPos(xpos,y)
  68. term.write(txt)
  69. end
  70. local function split(inputstr, sep)
  71. if sep == nil then
  72. sep = "%s"
  73. end
  74. local t={} ; i=1
  75. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  76. t[i] = str
  77. i = i + 1
  78. end
  79. return t
  80. end
  81. local function diabox(txt)
  82. term.setBackgroundColor(lat_theme.fore)
  83. term.setTextColor(lat_theme.text)
  84. paintutils.drawFilledBox(math.floor(w/2-(#txt+2)/2),math.floor(h/2)-2,math.floor(w/2+(#txt+2)/2),math.floor(h/2)+2,lat_theme.fore)
  85. term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)-1)
  86. term.write(txt)
  87. term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)+1)
  88. end
  89. function readtextbox(txt)
  90. diabox(txt)
  91. return read()
  92. end
  93. function buttonbox(txt,buttons)
  94. if type(buttons) ~= "table" then
  95. buttons = {{" Yes ",colors.red,colors.white},{" No ",colors.gray,colors.white}}
  96. end
  97. if txt == "" or txt == nil then
  98. txt = " Are you sure? "
  99. end
  100. local x,y
  101. diabox(txt)
  102. for i=1,#buttons do
  103. x,y = term.getCursorPos()
  104. x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  105. term.setCursorPos(x,y)
  106. term.setBackgroundColor(buttons[i][2])
  107. term.setTextColor(buttons[i][3])
  108. term.write(buttons[i][1])
  109. end
  110. while true do
  111. local _,b,x2,y2 = os.pullEvent("mouse_click")
  112. if b == 1 and y == y2 then
  113. for i=1,#buttons do
  114. local x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  115. if x2 > x - 1 and x2 < x + #buttons[i][1] then
  116. return i
  117. end
  118. end
  119. end
  120. end
  121. end
  122. local function clear()
  123. term.setBackgroundColor(lat_theme.back)
  124. term.setTextColor(colors.black)
  125. for i=2,h-1 do
  126. term.setCursorPos(1,i)
  127. term.clearLine()
  128. end
  129. term.setCursorPos(1,1)
  130. end
  131. local function posToIndex(maxn,x,y,scroll)
  132. if mode < 3 then
  133. return y+scroll-2 < maxn and y+scroll-1 or 0
  134. else
  135. local row = mathdown((y+scroll)/iconh)
  136. local column = math.floor(1+x/iconw)
  137. local index = row*nIco+column
  138. if column <= nIco and index <= maxn then
  139. return index
  140. else
  141. return 0
  142. end
  143. end
  144. end
  145. local function IndexToPos(index,scroll,rev)
  146. if mode < 3 then
  147. return 2,index-scroll
  148. else
  149. local row = mathdown(index/nIco)
  150. local col = (index-1) % nIco
  151. local x = 2+(col)*iconw
  152. local y = 2+(row)*iconh - scroll
  153. if rev then
  154. return y,x
  155. else
  156. return x,y
  157. end
  158. end
  159. end
  160. local function sort(path,items)
  161. local tbl = {} --i'll soon make a better sorting but this is enough for now
  162. table.sort(items)
  163. --insert dirs
  164. for i=1,#items do
  165. if fs.isDir(path..items[i]) then
  166. tbl[#tbl+1] = items[i]
  167. end
  168. end
  169. for i=1,#items do
  170. if not fs.isDir(path..items[i]) then
  171. tbl[#tbl+1] = items[i]
  172. end
  173. end
  174. return tbl
  175. end
  176. local function list(path,scroll,selected,selection)
  177. if selection == nil then selection = {} end
  178. clear()
  179. term.setCursorPos(1,2)
  180. local items = sort(path,fs.list(path))
  181. local cy = 1
  182. local i
  183. if mode < 3 then
  184. i = scroll+1
  185. elseif mode == 3 then
  186. i = scroll-nIco
  187. i = i <= 0 and 1 or i
  188. end
  189. while IndexToPos(i,scroll,true) < h do
  190. if type(items[i]) ~= "string" then break end
  191. if mode < 3 then
  192. cy = cy + 1
  193. term.setCursorPos(2,cy)
  194. else
  195. local x,y = IndexToPos(i,scroll)
  196. term.setCursorPos(x,y)
  197. paintutils.drawImage(fs.isDir(path..items[i]) and img_folder or img_file,x,y)
  198. term.setBackgroundColor(lat_theme.back)
  199. term.setCursorPos(x,y+4)
  200. end
  201.  
  202. if i ~= selected then
  203. if not fs.isDir(path..items[i]) then
  204. term.setTextColor(lat_theme.text)
  205. else
  206. term.setTextColor(lat_theme.dir)
  207. end
  208. else
  209. term.setTextColor(lat_theme.selected)
  210. end
  211. if selection[i] then
  212. term.setBackgroundColor(lat_theme.multiselect)
  213. else
  214. term.setBackgroundColor(lat_theme.back)
  215. end
  216. if mode < 3 then
  217. term.write(items[i])
  218. else
  219. local str = items[i]
  220. if #str > iconw-1 then
  221. str = str:sub(1,iconw-1)
  222. end
  223. term.write(str)
  224. end
  225. if mode == 2 then
  226. term.setCursorPos(math.floor(w/2),cy)
  227. if fs.isDir(path..items[i]) then
  228. term.write("-")
  229. else
  230. term.write(formatNumber(fs.getSize(path..items[i])))
  231. end
  232. end
  233. i = i + 1
  234. end
  235. return items
  236. end
  237. local function drawMenu(path)
  238. drawLine(1,"< ^ > / +",lat_theme.fore,1)
  239. local str = "Lattice - " .. path
  240. str = #str < w/2 and str or str:sub(1,math.floor(w/2)) .. "..."
  241. term.setCursorPos(10+math.floor((w-12)/2-#str/2),1)
  242. term.write(str)
  243. term.setCursorPos(w,1)
  244. term.setTextColor(colors.white)
  245. term.setBackgroundColor(colors.red)
  246. term.write("x")
  247. drawLine(h,path,lat_theme.fore,1)
  248. end
  249. local function popupMenu(x,y,isSelected,isDir,isMultiSelect)
  250. local ydir = y < h/2 and 1 or -1
  251. local content
  252. if isMultiSelect then
  253. content = {"Rename","Move","Copy","Delete"}
  254. elseif isSelected and isDir then
  255. content = {"Open","Unpack","Rename","Move","Copy","C.t.Clipboard","Delete"}
  256. elseif isSelected then
  257. content = {"Run","Run w/ Args","Edit","Open with...","Rename","Move","Copy","C.t.Clipboard","Delete"}
  258. else
  259. content = {"New file","New dir","New ...","Find file","P.f.Clipboard","P.f.Pastebin","Download file","Toggle view"}
  260. end
  261. for k,v in pairs(content) do
  262. term.setBackgroundColor(lat_theme.fore)
  263. term.setTextColor(colors.white)
  264. term.setCursorPos(x,y+k*ydir)
  265. for i=1,15 do term.write(" ") end
  266. term.setCursorPos(x+1,y+k*ydir)
  267. term.write(v)
  268. end
  269. term.setCursorPos(x,y)
  270. for i=1,15 do term.write(" ") end
  271. term.setCursorPos(x,y+(#content+1)*ydir)
  272. for i=1,15 do term.write(" ") end
  273. return true
  274. end
  275. local function popupEvent(popup,isSelected,isDir,isMultiSelect,...)
  276. local event = {...}
  277. local ydir = popup[2] < h/2 and 1 or -1
  278. if event[1] == "mouse_click" and event[2] == 1 then
  279. local content
  280. if isMultiSelect then
  281. content = {"r","m","c","delete"}
  282. elseif isSelected and isDir then
  283. content = {"enter","u","r","m","c","l","delete"}
  284. elseif isSelected then
  285. content = {"enter","q","e","w","r","m","c","l","delete"}
  286. else
  287. content = {"insert","n","new","find","pfcb","pfpb","df","o"}
  288. end
  289. --check if the popup has been clicked
  290. if event[3] >= popup[1] and event[3] <= popup[1] + 15 and event[4]*ydir >= popup[2]*ydir and event[4]*ydir <= popup[2]*ydir + #content then
  291. return true,content[ydir*event[4]-ydir*popup[2]]
  292. end
  293. return false
  294. end
  295. end
  296. local function createNewName(name,typ,path)
  297. if typ == nil then typ = 1 end
  298. if typ == 1 then return "Unpacked_" .. name end
  299. if typ == 2 then
  300. local num = ""
  301. local rawname = name
  302. while fs.exists(table.concat({path,name})) do
  303. num = tonumber(name:sub(#name,#name))
  304. if type(num) == "number" then
  305. num = num + 1
  306. name = rawname .. "_" .. tostring(num)
  307. else
  308. num = 2
  309. end
  310. end
  311. return table.concat({rawname,"_",tostring(num)})
  312. end
  313. return name .. "_pasted"
  314. end
  315. local function findFile(path)
  316. local name = readtextbox("Please enter a wildcard "..path)
  317. local results = fs.find(fs.combine(path,name))
  318. return results
  319. end
  320. local function downloadfile(path)
  321. clear()
  322. local url = readtextbox(" Please enter url ")
  323. clear()
  324. print("Progress:")
  325. print("Checking url")
  326. if url:sub(1,4) ~= "http" then
  327. url = "http://"..url
  328. print("Added http:// to your url")
  329. else
  330. print("Entered url is fine")
  331. end
  332. print("Requesting "..url)
  333. http.request(url)
  334. print("Press enter to cancel")
  335. local event,key
  336. while event ~= "http_success" and event ~= "http_failure" and not (event == "key" and key == keys.enter) do
  337. event,key = os.pullEvent()
  338. end
  339. if event == "http_success" then
  340. print("Downloading "..url)
  341. local httpgot = http.get(url)
  342. print("Done")
  343. local name = readtextbox("Please name the file")
  344. if name == nil or name == "" then return end
  345. while fs.exists(path..name) do name = createNewName(name,3) end
  346. local file = fs.open(path..name,"w")
  347. file.write(httpgot.readAll())
  348. file.close()
  349. httpgot.close()
  350. clear()
  351. buttonbox("File downloaded",{{"OK",colors.gray,colors.white}})
  352. elseif event == "http_failure" then
  353. buttonbox("http failure",{{"OK",colors.gray,colors.white}})
  354. end
  355. end
  356. local function toggleView()
  357. mode = mode + 1
  358. mode = mode > 3 and 1 or mode
  359. mode = mode < 1 and 3 or mode
  360. mode = math.floor(mode)
  361. end
  362. ------------------------------------------------------
  363. --actions:
  364. --folder:
  365. local function openFolder(path,history,items,selected)
  366. if fs.isDir(path) then
  367. path = path .. items[selected] .. "/"
  368. history[#history+1] = path
  369. end
  370. return path,history
  371. end
  372. local function unpackFolder(path,items,selected)
  373. if fs.isDir(path..items[selected]) then
  374. local con = fs.list(path..items[selected].."/")
  375. for i=1,#con do
  376. local altname = con[i]
  377. while fs.exists(path..altname) do altname = createNewName(altname,2,path) end
  378. fs.move(path..items[selected].."/"..con[i],path..altname)
  379. end
  380. fs.delete(path..items[selected])
  381. end
  382. end
  383. --file:
  384. local function runFile(path)
  385. if fs.exists(path) and not fs.isDir(path) then
  386. term.setBackgroundColor(colors.black)
  387. term.setTextColor(colors.white)
  388. term.clear()
  389. term.setCursorPos(1,1)
  390. os.pullEvent()
  391. shell.run(path)
  392. print("Press any key to continue")
  393. os.pullEvent()
  394. end
  395. end
  396. local function runFileWArgs(path)
  397. if fs.exists(path) and not fs.isDir(path) then
  398. local args = readtextbox("Please enter all arguments to run with!")
  399. term.setBackgroundColor(colors.black)
  400. term.setTextColor(colors.white)
  401. term.clear()
  402. term.setCursorPos(1,1)
  403. os.pullEvent()
  404. shell.run(path .. " " .. args)
  405. print("Press any key to continue")
  406. os.pullEvent()
  407. end
  408. end
  409. local function renamefile(path,items,selected)
  410. if fs.exists(path) then
  411. local nname = readtextbox("Please enter the new name (empty = cancel)")
  412. if nname ~= nil or nname ~= "" then
  413. if not fs.exists(path..nname) then
  414. fs.move(path..items[selected],path..nname)
  415. selected = 0
  416. else
  417. clear()
  418. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  419. end
  420. end
  421. end
  422. end
  423. local function moveFile(path,items,selected)
  424. if fs.exists(path..items[selected]) then
  425. local dest = readtextbox("Move to (empty=cancel) "..root)
  426. if dest ~= nil or dest ~= "" then
  427. if #dest < #items[selected] or dest:sub(#dest-#items[selected],#dest) ~= items[selected] then
  428. if dest:sub(#dest,#dest) == "/" then dest = dest .. items[selected] else
  429. dest = dest .. "/" .. items[selected]
  430. end
  431. end
  432. if not fs.exists(root..dest) then
  433. fs.move(path..items[selected],root..dest)
  434. else
  435. clear()
  436. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  437. end
  438. end
  439. end
  440. end
  441. local function copyFile(path,items,selected)
  442. if fs.exists(path..items[selected]) then
  443. local dest = readtextbox("Copy to (empty=cancel) "..root)
  444. if dest ~= nil or dest ~= "" then
  445. if #dest < #items[selected] or dest:sub(#dest-#items[selected],#dest) ~= items[selected] then
  446. if dest:sub(#dest,#dest) == "/" then dest = dest .. items[selected] else
  447. dest = dest .. "/" .. items[selected]
  448. end
  449. end
  450. if not fs.exists(root..dest) then
  451. fs.copy(path..items[selected],root..dest)
  452. else
  453. clear()
  454. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  455. end
  456. end
  457. end
  458. end
  459. local function deleteFile(path)
  460. if fs.exists(path) then
  461. local bclicked = buttonbox()
  462. if bclicked == 1 then
  463. fs.delete(path)
  464. end
  465. end
  466. end
  467. local function newFile(path)
  468. local name = readtextbox("Filename: (empty=cancel) ")
  469. if name ~= nil or name ~= "" and not fs.exists(path..name) then
  470. shell.run("edit",path..name)
  471. end
  472. end
  473. local function newDir(path)
  474. local name = readtextbox("Directory's name: (empty=cancel)")
  475. if name ~= nil or name ~= "" and not fs.exists(path..name) then
  476. fs.makeDir(path..name)
  477. end
  478. end
  479. local function checkPath(path)
  480. if path:sub(#path,#path) ~= "/" then path = path .. "/" end
  481. if path:sub(1,1) ~= "/" then path = "/" .. path end
  482. if not fs.isDir(path) then
  483. if fs.isDir(root..home) then
  484. path = root..home
  485. elseif fs.isDir(root) then
  486. path = root
  487. else
  488. error("Not valid root folder",0)
  489. end
  490. end
  491. return path
  492. end
  493. local function shiftselect(ysc,selected,selection)
  494. local direction = ysc-selected
  495. if direction > 0 then
  496. direction = 1
  497. else
  498. direction = - 1
  499. end
  500. for i=selected,ysc,direction do
  501. selection[i] = true
  502. end
  503. return selection
  504. end
  505. local function moveFiles(path,items,selection)
  506. local foldername = readtextbox("Enter destination folder name "..root)
  507. if foldername == nil then return end
  508. if not fs.exists(root..foldername) then
  509. fs.makeDir(root..foldername)
  510. elseif not fs.isDir(root..foldername) then
  511. foldername = createNewName(foldername,2,path)
  512. end
  513. local dest = root..foldername
  514. dest = checkPath(dest)
  515. local goalk
  516. for k,v in pairs(selection) do
  517. if v then
  518. if fs.exists(dest..v) then
  519. goalk = createNewName(k,2,path)
  520. end
  521. fs.move(path..items[k],dest..items[goalk])
  522. end
  523. end
  524. end
  525. local function copyFiles(path,items,selection)
  526. local foldername = readtextbox("Enter destination folder name "..root)
  527. if foldername == nil then return end
  528. if not fs.exists(root..foldername) then
  529. fs.makeDir(root..foldername)
  530. elseif not fs.isDir(root..foldername) then
  531. foldername = createNewName(foldername,2,path)
  532. end
  533. local dest = root..foldername
  534. dest = checkPath(dest)
  535. local goalk
  536. for k,v in pairs(selection) do
  537. if v then
  538. if fs.exists(dest..v) then
  539. goalk = createNewName(k,2,path)
  540. end
  541. fs.copy(path..items[k],dest..items[goalk])
  542. end
  543. end
  544. end
  545. local function deleteFiles(path,items,selection)
  546. local bclicked = buttonbox()
  547. if bclicked == 1 then
  548. for k,v in pairs(selection) do
  549. if v then
  550. fs.delete(path..items[k])
  551. end
  552. end
  553. clear()
  554. buttonbox("Files deleted",{{"OK",colors.gray,colors.white}})
  555. end
  556. end
  557. local function renameFiles(path,items,selection)
  558. local strtoreplace = readtextbox("Enter pattern to replace")
  559. local newstr = readtextbox("Enter string to replace with")
  560. for k,v in pairs(selection) do
  561. if v then
  562. local nname = items[k]:gsub(strtoreplace,newstr)
  563. if fs.exists(path..nname) then
  564. createNewName(nname,2,path)
  565. fs.move(path..items[k],path..nname)
  566. end
  567. end
  568. end
  569. end
  570. local path = checkPath(root) .. home
  571. history[1] = path
  572. local scroll = 0
  573. local selected = 0
  574. local items
  575. local endprogram = false
  576. local isCtrlDown = false
  577. local isShiftDown = false
  578. local selection = {}
  579. local isMultiSelect = false
  580. local redraw = 1 -- 0 - no redraw, 1 - redraw all, 2 - redraw only files
  581. local popup = {x,y,isOpen}
  582. local index = 0
  583. ------------------------------------------------------
  584. local function fillTable(count,tbl,value)
  585. if not tbl then tbl = {} end
  586. if not value then value = false end
  587. if not count then error("Not valid count",2) end
  588. for i=1,count do
  589. tbl[i] = value
  590. end
  591. return tbl
  592. end
  593. local function goUp()
  594.  
  595. end
  596. ------------------------------------------------------
  597. while not endprogram do
  598. repeat
  599. path = checkPath(path)
  600. if redraw == 1 or redraw == 2 then
  601. items = list(path,scroll,selected,selection)
  602. drawMenu(path)
  603. if popup[3] then
  604. popupMenu(popup[1],popup[2],selected ~= 0,fs.isDir(path..tostring(items[selected])),isMultiSelect)
  605. end
  606. redraw = 0
  607. end
  608. local e,b,x,y = os.pullEvent()
  609. if popup[3] then
  610. local response,key = popupEvent(popup,selected ~= 0,fs.isDir(path..tostring(items[selected])),isMultiSelect,e,b,x,y)
  611. if response then
  612. if keys[key] then
  613. os.queueEvent("key",keys[key])
  614. else
  615. os.queueEvent("action",key)
  616. end
  617. redraw = 1
  618. popup[3] = false
  619. break
  620. elseif response == false then
  621. redraw = 1
  622. popup[3] = false
  623. end
  624. end
  625. if e == "mouse_click" then
  626. index = posToIndex(#items,x,y,scroll)
  627. if b == 1 then
  628. if y == h then
  629. local npath = readtextbox("Enter new path: (empty=cancel) "..root)
  630. if npath ~= nil and npath ~= "" and fs.isDir(root..npath) then
  631. path = root .. npath
  632. selected = 0
  633. scroll = 0
  634. history[#history+1] = path
  635. end
  636. redraw = 1
  637. elseif y == 1 then
  638. if x == 9 then
  639. selected = 0
  640. isMultiSelect = false
  641. selection = fillTable(#items)
  642. popup = {9,2,true}
  643. redraw = 1
  644. end
  645. if x == 7 then
  646. path = root
  647. history[#history+1] = path
  648. selected = 0
  649. scroll = 0
  650. redraw = 1
  651. end
  652. if x == 3 then
  653. local t = split(path,"/")
  654. if #t > 0 then
  655. t[#t] = nil
  656. path = table.concat(t,"/")
  657. path = checkPath(path)
  658. selected = 0
  659. scroll = 0
  660. history[#history+1] = path
  661. end
  662. if path:sub(1,#root) ~= root then
  663. path = root
  664. end
  665. redraw = 1
  666. selected = 0
  667. isMultiSelect = false
  668. selection = fillTable(#items)
  669. end
  670. if x == 1 then
  671. while true do
  672. history[#history] = nil
  673. if #history == 0 then
  674. if fs.exists(root..home) then
  675. path = root..home
  676. history[1] = path
  677. elseif fs.exists(root) then
  678. path = root
  679. history[1] = path
  680. else
  681. error("Root folder deleted",0)
  682. end
  683. end
  684. if fs.exists(history[#history]) then
  685. path = history[#history]
  686. break
  687. end
  688. end
  689. selected = 0
  690. isMultiSelect = false
  691. selection = fillTable(#items)
  692. redraw = 1
  693. end
  694. if x == w then
  695. term.setBackgroundColor(colors.black)
  696. term.setTextColor(colors.white)
  697. term.setCursorPos(1,1)
  698. term.clear()
  699. endprogram = true
  700. redraw = 1
  701. end
  702. else
  703. if index == selected and not isCtrlDown and not isShiftDown then
  704. if items[selected] ~= nil then
  705. if fs.isDir(path..items[selected]) then
  706. isMultiSelect = false
  707. selection = fillTable(#fs.list(path..items[selected]))
  708. path,history = openFolder(path,history,items,selected)
  709. selected = 0
  710. scroll = 0
  711. redraw = 1
  712. else
  713. runFile(path..items[selected])
  714. redraw = 1
  715. end
  716. end
  717. else
  718. if not isCtrlDown and not isShiftDown then
  719. selected = index
  720. if isMultiSelect then
  721. isMultiSelect = false
  722. selection = fillTable(#items)
  723. end
  724. redraw = 1
  725. elseif isShiftDown then
  726. shiftselect(index,selected,selection)
  727. redraw = 1
  728. isMultiSelect = true
  729. elseif isCtrlDown then
  730. if not isMultiSelect then
  731. isMultiSelect = true
  732. if selected ~= 0 then
  733. selection[selected] = true
  734. end
  735. end
  736. selection[index] = not selection[index]
  737. local hasTrue = false
  738. for k,v in pairs(selection) do
  739. hasTrue = hasTrue and true or v
  740. end
  741. if not hasTrue then
  742. isMultiSelect = false
  743. isCtrlDown = false
  744. end
  745. redraw = 1
  746. end
  747. end
  748. end
  749. elseif b == 2 and y > 1 and y < h then
  750. if index ~= selected then
  751. selected = index
  752. end
  753. popup[1], popup[2],popup[3] = x,y,true
  754. redraw = 1
  755. end
  756. elseif e == "mouse_up" and not isCtrlDown and not isShiftDown then
  757. if selected == 0 then break end
  758. local index
  759. if mode > 2 then
  760. index = posToIndex(#items,x,y,scroll)
  761. else
  762. index = y+scroll-1
  763. end
  764. if y > 1 and y < h and index ~= selected and index < #items then
  765. if fs.isDir(path..items[index]) and not fs.exists(path..items[index].."/"..items[selected]) then
  766. if buttonbox("Move "..items[selected].." to "..items[index].."?",{{" Yes ",colors.green,colors.white},{" Cancel ",colors.gray,colors.white}}) == 1 then
  767. fs.move(path..items[selected],path..items[index].."/"..items[selected])
  768. end
  769. redraw = 1
  770. selected = 0
  771. isMultiSelect = false
  772. selection = fillTable(#items)
  773. end
  774. end
  775. if y == 1 then
  776. local t = split(path,"/")
  777. local roottable = split(root,"/")
  778. if #t > #roottable then
  779. t[#t] = nil
  780. local upperpath = table.concat(t,"/")
  781. upperpath = checkPath(upperpath)
  782. if not fs.exists(upperpath..items[selected]) then
  783. if buttonbox("Move "..items[selected].." to parent dir?",{{" Yes ",colors.green,colors.white},{" Cancel ",colors.gray,colors.white}}) == 1 then
  784. fs.move(path..items[selected],upperpath..items[selected])
  785. redraw = 1
  786. selected = 0
  787. isMultiSelect = false
  788. selection = fillTable(#items)
  789. end
  790. end
  791. end
  792. end
  793. elseif e == "mouse_scroll" then
  794. if b == 1 then
  795. if mode < 3 and #items - scroll > h-2 then
  796. scroll = scroll + 1
  797. elseif mode == 3 and scroll < math.ceil(#items/nIco)*iconh-(h-2) then
  798. scroll = scroll + 1
  799. end
  800. end
  801. if b == -1 and scroll > 0 then
  802. scroll = scroll - 1
  803. end
  804. redraw = 2
  805.  
  806. ---------------------------------------------------
  807. --hotkeys:
  808. elseif e == "key" then
  809. if b == keys.f1 then
  810. clear()
  811. print("Hotkey help:")
  812. print([[Up/Down arrow keys + Page Up/Down = navigation
  813. G = Go to root folder H = Go to home folder
  814. Insert = new file N = New directory
  815. Backspace = exit 0 (number) = deselect
  816. Enter = Open/Run E = Edit P = Paint
  817. U = Unpack folder L = Copy to clipboard
  818. Q = Run w/ Args W = Open with...
  819. M = move C = copy R = rename
  820. Delete = delete file
  821. hold Ctrl/Shift + clicking = multiselect
  822. O (letter) = toggle view
  823.  
  824. Right click to open popup menus
  825. Click '<' and '>' to go back/forward in history
  826. Click '^' to go up (..)
  827. Click '/' to go in root folder
  828. Press any key to go back]])
  829. os.pullEvent("key")
  830. redraw = 1
  831. end
  832. if b == keys.insert then
  833. newFile(path)
  834. redraw = 1
  835. elseif b == keys.delete then
  836. if selected ~= 0 then
  837. deleteFile(path..items[selected])
  838. else
  839. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  840. end
  841. redraw = 1
  842. end
  843. if b == keys.backspace then
  844. term.setBackgroundColor(colors.black)
  845. term.setTextColor(colors.white)
  846. term.setCursorPos(1,1)
  847. term.clear()
  848. return
  849. end
  850. if b == keys.enter then
  851. if selected ~= 0 then
  852. if fs.isDir(path..items[selected]) then
  853. path,history = openFolder(path,history,items,selected)
  854. selected = 0
  855. scroll = 0
  856. else
  857. runFile(path..items[selected])
  858. end
  859. else
  860. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  861. end
  862. redraw = 1
  863. end
  864. if b == keys.m then
  865. if selected ~= 0 then
  866. moveFile(path,items,selected)
  867. else
  868. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  869. end
  870. redraw = 1
  871. end
  872. if b == keys.c then
  873. if selected ~= 0 then
  874. copyFile(path,items,selected)
  875. else
  876. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  877. end
  878. redraw = 1
  879. end
  880. if b == keys.r then
  881. if selected ~= 0 then
  882. renamefile(path,items,selected)
  883. else
  884. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  885. end
  886. redraw = 1
  887. end
  888. if b == keys.zero then
  889. selected = 0
  890. selection = fillTable(#items)
  891. isMultiSelect = false
  892. isCtrlDown = false
  893. isShiftDown = false
  894. redraw = 1
  895. end
  896. if b == keys.up and selected > 1 then
  897. selected = selected - 1
  898. if mode < 3 and selected < scroll+1 then
  899. scroll = scroll - 1
  900. end
  901. redraw = 1
  902. end
  903. if b == keys.down and selected < #items then
  904. selected = selected + 1
  905. if selected > (scroll + h-3) then
  906. scroll = scroll + 1
  907. end
  908. redraw = 1
  909. end
  910. if b == keys.pageUp and selected then
  911. selected = selected - (h-3)
  912. scroll = selected-1
  913. if selected < 0 or scroll < 0 then scroll,selected = 0,1 end
  914. redraw = 1
  915. end
  916. if b == keys.pageDown and selected < #items-(h-2) then
  917. selected = selected + (h-3)
  918. scroll = selected-1
  919. if scroll > #items-(h-2) or selected > #items then scroll,selected = #items-(h-2),#items end
  920. if selected < 0 then scroll,selected = 0,0 end
  921. redraw = 1
  922. end
  923. if b == keys.g then
  924. path = root
  925. history[#history+1] = path
  926. selected = 0
  927. scroll = 0
  928. redraw = 1
  929. end
  930. if b == keys.h then
  931. if fs.exists(root .. home) then
  932. path = root .. home
  933. history[#history+1] = path
  934. selected = 0
  935. scroll = 0
  936. redraw = 1
  937. end
  938. end
  939. if b == keys.w then
  940. if selected ~= 0 then
  941. local program = readtextbox("Please choose a program: "..root)
  942. if program ~= nil or program ~= "" then
  943. shell.run(root..program,path..items[selected])
  944. end
  945. else
  946. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  947. end
  948. redraw = 1
  949. end
  950. if b == keys.e then
  951. if selected ~= 0 then
  952. shell.run("edit",path..items[selected])
  953. else
  954. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  955. end
  956. redraw = 1
  957. end
  958. if b == keys.u then
  959. if selected ~= 0 then
  960. unpackFolder(path,items,selected,history,scroll)
  961. selected = 0
  962. else
  963. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  964. end
  965. redraw = 1
  966. end
  967. if b == keys.l then
  968. if selected ~= 0 then
  969. clipboard = path .. items[selected]
  970. else
  971. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  972. end
  973. redraw = 1
  974. end
  975. if b == keys.p then
  976. if selected ~= 0 then
  977. shell.run("paint",path..items[selected])
  978. else
  979. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  980. end
  981. redraw = 1
  982. end
  983. if b == keys.n then
  984. local dirname = readtextbox("Please enter directory name (empty=cancel)")
  985. if dirname ~= nil and dirname ~= "" then
  986. if not fs.exists(path..dirname) then
  987. fs.makeDir(path..dirname)
  988. else
  989. buttonbox("PFile exists",{{"OK",colors.gray,colors.white}})
  990. end
  991. end
  992. redraw = 1
  993. end
  994. if b == keys.q then
  995. if selected ~= 0 then
  996. runFileWArgs(path..items[selected])
  997. else
  998. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  999. end
  1000. redraw = 1
  1001. end
  1002. if b == keys.o then
  1003. toggleView()
  1004. redraw = 1
  1005. end
  1006. if b == keys.leftCtrl or b == keys.rightCtrl then
  1007. isCtrlDown = true
  1008. end
  1009. if b == keys.leftShift or b == keys.rightShift then
  1010. isShiftDown = true
  1011. end
  1012. elseif e == "key_up" then
  1013. if b == keys.leftCtrl or b == keys.rightCtrl then
  1014. isCtrlDown = false
  1015. end
  1016. if b == keys.leftShift or b == keys.rightShift then
  1017. isShiftDown = false
  1018. end
  1019. elseif e == "action" then
  1020. if b == "new" then
  1021. local exc = buttonbox("Please choose a program",{{"Paint",colors.yellow,colors.black},{"Custom",colors.yellow,colors.black}})
  1022. clear()
  1023. local filename = readtextbox("Please enter a filename")
  1024. if not fs.exists(path..filename) then
  1025. if exc == 1 then
  1026. shell.run("paint",path..filename)
  1027. else
  1028. clear()
  1029. local prog = readtextbox("Please choose a program "..root)
  1030. shell.run(prog,path..filename)
  1031. end
  1032. end
  1033. elseif b == "find" then
  1034. local results = findFile(path)
  1035. buttonbox(#results.." results saved in current path",{{"OK",colors.gray,colors.white}})
  1036. local file = fs.open(path..createNewName("search_results",2,path),"w")
  1037. for k,v in pairs(results) do
  1038. file.writeLine(v)
  1039. end
  1040. file.close()
  1041. elseif b == "pfcb" then
  1042. if type(clipboard) == "string" and fs.exists(clipboard) then
  1043. if clipboard:sub(#clipboard,#clipboard) == "/" then clipboard = clipboard:sub(1,#clipboard-1) end
  1044. local splitted = split(clipboard,"/")
  1045. local name = splitted[#splitted]
  1046. while fs.exists(path..name) do name = createNewName(name,3) end
  1047. fs.copy(clipboard,path..name)
  1048. buttonbox("Pasted as "..name.." in directory "..path,{{"OK",colors.gray,colors.white}})
  1049. else
  1050. buttonbox("File not found: "..tostring(clipboard),{{"OK",colors.gray,colors.white}})
  1051. end
  1052. elseif b == "pfpb" then
  1053. local name
  1054. repeat
  1055. name = readtextbox("Please enter a filename")
  1056. until not fs.exists(path..name)
  1057. local url = readtextbox("Please enter the pastebin URL")
  1058. term.setCursorPos(1,1)
  1059. term.setBackgroundColor(colors.white)
  1060. term.setTextColor(colors.gray)
  1061. term.clear()
  1062. print("Downloading file: ")
  1063. shell.run("pastebin get "..url.." "..path..name)
  1064. print("Ended. Press any key to continue")
  1065. os.pullEvent()
  1066. elseif b == "df" then
  1067. downloadfile(path)
  1068. end
  1069. redraw = 1
  1070. end
  1071. until true
  1072. end
Add Comment
Please, Sign In to add comment