Advertisement
BigSHinyToys

Mouse File Browser 0.6_a

Nov 18th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.09 KB | None | 0 0
  1. --[[
  2. file selection and dilog box API / lib / other
  3. by BigSHinyToys
  4. note: send link to nightin9ale on CC forums
  5. --]]
  6.  
  7. local tArgs = {...}
  8. local ver = "0.6_a"
  9. local bugTest, norun, dir, showAll
  10. local _tArgs = {}
  11.  
  12. local function help()
  13. print("Usage: browser [-d] [-h] [-a] [--debug] [--help] [--dir <dir>] [--all]")
  14. print("--debug or -d: enable debug mode")
  15. print("--help or -h: display this screen")
  16. print("--dir: define initial directory")
  17. print("--all or -a: show hidden files")
  18. end
  19.  
  20. for a = 1, #tArgs do
  21. if tArgs[a]:sub(1,2) == "--" then
  22. local cmd = tArgs[a]:sub(3):lower()
  23. if cmd == "debug" then
  24. bugTest = true
  25. elseif cmd == "help" then
  26. help()
  27. norun = true
  28. elseif cmd == "dir" then
  29. dir = tArgs[a+1]
  30. a = a + 1
  31. elseif cmd == "all" then
  32. showAll = true
  33. end
  34. elseif tArgs[a]:sub(1,1) == "-" then
  35. for b = 2, #tArgs[a] do
  36. cmd = tArgs[a]:sub(b, b)
  37. if cmd == "d" then
  38. bugTest = true
  39. elseif cmd == "h" then
  40. help()
  41. norun = true
  42. elseif cmd == "p" then
  43. dir = tArgs[a+1]
  44. a = a + 1
  45. elseif cmd == "a" then
  46. showAll = true
  47. end
  48. end
  49. else
  50. table.insert(_tArgs, tArgs[a])
  51. end
  52. end
  53.  
  54. if (not dir) and shell and shell.dir then
  55. dir = shell.dir()
  56. end
  57.  
  58. if dir and shell and shell.resolve then
  59. dir = shell.resolve(dir)
  60. end
  61.  
  62. dir = dir or "/"
  63.  
  64. if bugTest then -- this is that the var is for testing
  65. print("Dir: "..dir)
  66. sleep(4)
  67. end
  68.  
  69. local function clear()
  70. term.setBackgroundColor(colors.black)
  71. term.setTextColor(colors.white)
  72. term.clear()
  73. term.setCursorBlink(false)
  74. term.setCursorPos(1,1)
  75. end
  76.  
  77. --[[
  78. Code thanks to Cruor
  79. http://www.computercraft.info/forums2/index.php?/topic/5802-support-for-shell/
  80. ]]--
  81. local function fixArgs(...)
  82. local tReturn={}
  83. local str=table.concat({...}," ")
  84. local sMatch
  85. while str and #str>0 do
  86. if string.sub(str,1,1)=="\"" then
  87. sMatch, str=string.match(str, "\"(.-)\"%s*(.*)")
  88. else
  89. sMatch, str=string.match(str, "(%S+)%s*(.*)")
  90. end
  91. table.insert(tReturn,sMatch)
  92. end
  93. return tReturn
  94. end
  95. --[[ end Cruor function ]]--
  96.  
  97.  
  98. -- modified read made to play nice with coroutines
  99.  
  100. local function readMOD( _sReplaceChar, _tHistory,_wdth)
  101. local sLine = ""
  102. term.setCursorBlink( true )
  103.  
  104. local nHistoryPos = nil
  105. local nPos = 0
  106. if _sReplaceChar then
  107. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  108. end
  109.  
  110. local sx, sy = term.getCursorPos()
  111.  
  112. local w, h = term.getSize()
  113. if _wdth and type(_wdth) == "number" then
  114. w = sx + _wdth - 1
  115. end
  116.  
  117. local function redraw( _sCustomReplaceChar )
  118. local nScroll = 0
  119. if sx + nPos >= w then
  120. nScroll = (sx + nPos) - w
  121. end
  122.  
  123. term.setCursorPos( sx + _wdth - 1, sy )
  124. term.write(" ")
  125. term.setCursorPos( sx, sy )
  126. local sReplace = _sCustomReplaceChar or _sReplaceChar
  127. if sReplace then
  128. term.write( string.rep(sReplace,_wdth) )
  129. else
  130. term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )
  131. end
  132. term.setCursorPos( sx + nPos - nScroll, sy )
  133. end
  134.  
  135. while true do
  136. local sEvent, param = os.pullEvent()
  137. if sEvent == "char" then
  138. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  139. nPos = nPos + 1
  140. redraw()
  141.  
  142. elseif sEvent == "key" then
  143.  
  144. if param == keys.left then
  145. -- Left
  146. if nPos > 0 then
  147. nPos = nPos - 1
  148. redraw()
  149. end
  150.  
  151. elseif param == keys.right then
  152. -- Right
  153. if nPos < string.len(sLine) then
  154. nPos = nPos + 1
  155. redraw()
  156. end
  157.  
  158. elseif param == keys.up or param == keys.down then
  159. -- Up or down
  160. if _tHistory then
  161. redraw(" ");
  162. if param == keys.up then
  163. -- Up
  164. if nHistoryPos == nil then
  165. if #_tHistory > 0 then
  166. nHistoryPos = #_tHistory
  167. end
  168. elseif nHistoryPos > 1 then
  169. nHistoryPos = nHistoryPos - 1
  170. end
  171. else
  172. -- Down
  173. if nHistoryPos == #_tHistory then
  174. nHistoryPos = nil
  175. elseif nHistoryPos ~= nil then
  176. nHistoryPos = nHistoryPos + 1
  177. end
  178. end
  179.  
  180. if nHistoryPos then
  181. sLine = _tHistory[nHistoryPos]
  182. nPos = string.len( sLine )
  183. else
  184. sLine = ""
  185. nPos = 0
  186. end
  187. redraw()
  188. end
  189. elseif param == keys.backspace then
  190. -- Backspace
  191. if nPos > 0 then
  192. redraw(" ");
  193. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  194. nPos = nPos - 1
  195. redraw()
  196. end
  197. elseif param == keys.home then
  198. -- Home
  199. nPos = 0
  200. redraw()
  201. elseif param == keys.delete then
  202. if nPos < string.len(sLine) then
  203. redraw(" ");
  204. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  205. redraw()
  206. end
  207. elseif param == keys["end"] then
  208. -- End
  209. nPos = string.len(sLine)
  210. redraw()
  211. end
  212. elseif sEvent == "redraw" then
  213. redraw()
  214. elseif sEvent == "return" then
  215. term.setCursorBlink( false )
  216. return sLine
  217. end
  218. end
  219.  
  220. term.setCursorBlink( false )
  221.  
  222. return sLine
  223. end
  224.  
  225. -- end modified read
  226.  
  227. local function printC(posX,posY,textCol,backCol,text)
  228. term.setCursorPos(posX,posY)
  229. term.setTextColor(textCol)
  230. term.setBackgroundColor(backCol)
  231. term.write(text)
  232. end
  233.  
  234. local function InputBox(title)
  235. local boxW,boxH = 26,3
  236. local termX,termY = term.getSize()
  237. local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  238. local options = {"ok","cancel"}
  239.  
  240. local selected = 1
  241. local space = 0
  242. local range = {}
  243. for i = 1,#options do
  244. range[i] = {s = space,f = space + string.len(options[i])}
  245. space = space + string.len(options[i])+3
  246. end
  247. local ofC = (boxW/2) - (space/2)
  248.  
  249. local function drawBox()
  250. printC(ofsX,ofsY,colors.black,colors.blue,string.rep(" ",boxW))
  251. printC(ofsX+1,ofsY,colors.black,colors.blue,(title or "User Input"))
  252. printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(" ",boxW))
  253. printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(" ",boxW))
  254. printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(" ",boxW))
  255.  
  256. for i = 1,#options do
  257. if i == selected then
  258. term.setBackgroundColor(colors.lightGray)
  259. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  260. term.write("["..options[i].."]")
  261. term.setBackgroundColor(colors.white)
  262. term.write(" ")
  263. else
  264. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  265. term.write(" "..options[i].." ")
  266. end
  267. end
  268.  
  269. printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(" ",boxW-4))
  270. end
  271. drawBox()
  272. term.setCursorPos(ofsX+2,ofsY+2)
  273. local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)
  274. while true do
  275. local event = {os.pullEvent()}
  276. if event[1] == "key" or event[1] == "char" then
  277. if event[2] == 28 then
  278. local test,data = coroutine.resume(co,"return")
  279. return data
  280. else
  281. coroutine.resume(co,unpack(event))
  282. end
  283. elseif event[1] == "mouse_click" then
  284. if event[4] == ofsY + 3 then
  285. for i = 1,#options do
  286. if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  287. if options[i] == "ok" then
  288. local test,data = coroutine.resume(co,"return")
  289. return data
  290. elseif options[i] == "cancel" then
  291. return
  292. end
  293. end
  294. end
  295. end
  296. end
  297. end
  298. end
  299.  
  300. local function dialogBox(title,message,options, h, w)
  301. term.setCursorBlink(false)
  302. local selected = 1
  303. title = title or ""
  304. message = message or ""
  305. options = options or {}
  306. local boxW,boxH = (w or 26), (h or 3)
  307. local termX,termY = term.getSize()
  308. local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  309.  
  310. local space = 0
  311. local range = {}
  312. for i = 1,#options do
  313. range[i] = {s = space,f = space + string.len(options[i])}
  314. space = space + string.len(options[i])+3
  315. end
  316. local ofC = math.ceil((boxW/2)) - math.ceil((space/2))
  317.  
  318. local function drawBox()
  319. if term.isColor() then
  320. term.setTextColor(colors.black)
  321. term.setCursorPos(ofsX,ofsY) -- F*** YOU 3 hours to find out i forgot to round numbers before sending them to this Fing function
  322. term.setBackgroundColor(colors.blue)
  323. term.write(" "..title..string.rep(" ",boxW-#title-5).."_[]")
  324. term.setBackgroundColor(colors.red)
  325. term.setTextColor(colors.white)
  326. term.write("X")
  327. term.setCursorPos(ofsX,ofsY+1)
  328. term.setBackgroundColor(colors.white)
  329. term.setTextColor(colors.black)
  330. term.write(string.sub(" "..message..string.rep(" ",boxW),1,boxW)) -- edited
  331. term.setCursorPos(ofsX,ofsY+2)
  332. term.write(string.rep(" ",boxW))
  333. term.setCursorPos(ofsX,ofsY+3)
  334. term.write(string.rep(" ",boxW))
  335.  
  336. for i = 1,#options do
  337. if i == selected then
  338. term.setBackgroundColor(colors.lightGray)
  339. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  340. term.write("["..options[i].."]")
  341. term.setBackgroundColor(colors.white)
  342. term.write(" ")
  343. else
  344. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  345. term.write(" "..options[i].." ")
  346. end
  347. end
  348. term.setCursorPos(ofsX + ofC + space,ofsY + 3)
  349. term.write(string.rep(" ",boxW - (ofC + space)))
  350. term.setBackgroundColor(colors.black)
  351. term.setTextColor(colors.white)
  352. end
  353. end
  354. while true do
  355. drawBox()
  356. event = {os.pullEvent()}
  357. if event[1] == "key" then
  358. if event[2] == 203 then -- left
  359. selected = selected - 1
  360. if selected < 1 then
  361. selected = #options
  362. end
  363. elseif event[2] == 205 then -- right
  364. selected = selected + 1
  365. if selected > #options then
  366. selected = 1
  367. end
  368. elseif event[2] == 28 then -- enter
  369. return selected , options[selected]
  370. end
  371. elseif event[1] == "mouse_click" then
  372. term.setCursorPos(1,1)
  373. term.clearLine()
  374.  
  375. if bugTest then term.write("M "..event[2].." X "..event[3].." Y "..event[4]) end
  376.  
  377. if event[2] == 1 then
  378. if event[4] == ofsY + 3 then
  379. for i = 1,#options do
  380. if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  381. return i , options[i]
  382. end
  383. end
  384. end
  385. end
  386. end
  387. end
  388. end
  389.  
  390. local function rClickMenu(title,tList,posX,posY)
  391.  
  392. term.setCursorBlink(false)
  393. local BoxTitle = title
  394. local choices = tList
  395. local termX,termY = term.getSize()
  396. local offX,offY
  397.  
  398. local width = #BoxTitle + 2
  399. local hight = #choices + 1
  400.  
  401. for i = 1,#choices do
  402. if width < #choices[i] + 2 then
  403. width = #choices[i] + 2
  404. end
  405. end
  406.  
  407. offX,offY = math.ceil((termX/2) - (width/2)),math.ceil((termY/2) - (hight/2))
  408.  
  409. if posX and posY then -- offX,offY = posX,posY
  410. if posX >= termX - width - 1 then
  411. offX = termX - width - 1
  412. else
  413. offX = posX
  414. end
  415. if posY >= termY - hight then
  416. offY = termY - hight
  417. else
  418. offY = posY
  419. end
  420. end
  421.  
  422. local function reDrawer()
  423. printC(offX,offY,colors.black,colors.blue," "..BoxTitle..string.rep(" ",width - #BoxTitle - 1))
  424. for i = 1,#choices do
  425. printC(offX,offY + i,colors.black,colors.white," "..choices[i]..string.rep(" ",width - #choices[i] - 1))
  426. end
  427. end
  428.  
  429. while true do
  430. reDrawer()
  431. local event = {os.pullEvent()}
  432. if event[1] == "mouse_click" then
  433. if event[2] == 1 then -- event[3] = x event[4] = y
  434. if event[4] > offY and event[4] < hight + offY and event[3] >= offX and event[3] < width + offX then
  435. return choices[event[4] - offY]
  436. else
  437. return
  438. end
  439. elseif event[2] == 2 then
  440. return
  441. end
  442. end
  443. end
  444.  
  445. end
  446.  
  447. local function osRunSpaces(...)
  448. clear()
  449. return os.run(getfenv(),...)
  450. end
  451.  
  452. local function fileSelect(mode) -- save_file open_file browse < not yet implemented
  453.  
  454. local title = "File Browser "..ver
  455. local bRun = true
  456. local flag = true
  457. local clipboard = nil
  458. local cut = false
  459.  
  460. local termX,termY = term.getSize()
  461. local offsetX,offsetY = 1,1
  462. local hight,width = math.ceil(termY-2),math.ceil(termX-2)
  463. local oldHight,oldWidth
  464.  
  465. local boxOffX,boxOffY = offsetX,offsetY + 2
  466. local boxH,boxW = hight - 2 ,width - 2
  467.  
  468. local barX,barY = offsetX + 1,offsetY + 2
  469. local barH,barW = 1,width - 1
  470.  
  471. local tbarX,tbarY = offsetX + 1,offsetY + 1
  472. local tbarH,tbarW = 1,width - 1
  473.  
  474. local exitX,exitY = offsetX + width - 1 ,offsetY + 1
  475.  
  476. local parth = {dir:match("[^/]+")}
  477. local list
  478.  
  479. local fSlash = "/"
  480. local listOff = 0
  481.  
  482. local function stringParth() -- compacted this alot
  483. return fSlash..table.concat(parth,fSlash)
  484. end
  485.  
  486. local sParth = stringParth()
  487. local files = {}
  488. local folders = {}
  489. local disks = {}
  490.  
  491. local function NewList()
  492. flag = true
  493. listOff = 0
  494. sParth = stringParth()
  495. files = {}
  496. folders = {}
  497. disks = {}
  498. test,list = pcall(fs.list,sParth)
  499. if list == nil then
  500. list = {}
  501. dialogBox("ERROR : ","fs.list crashed",{"ok"})
  502. end
  503. for i = 1,#list do
  504. if showAll or list[i]:sub(1,1) ~= "." then
  505. if fs.isDir(sParth..fSlash..list[i]) then
  506. table.insert(folders,list[i])
  507. else
  508. table.insert(files,list[i])
  509. end
  510. end
  511. end
  512. if #parth == 0 then
  513. for i,v in pairs(rs.getSides()) do
  514. if disk.isPresent(v) and disk.hasData(v) then
  515. disks[disk.getMountPath(v)] = v
  516. end
  517. end
  518. end
  519. table.sort(disks)
  520. table.sort(folders)
  521. table.sort(files)
  522. end
  523.  
  524. NewList()
  525.  
  526. local function size(test)
  527. if #test > boxW - 3 then
  528. return string.sub(test,1,boxW-3)
  529. end
  530. return test
  531. end
  532.  
  533.  
  534. while bRun do
  535. if flag then
  536. flag = false
  537. -- clear
  538. if oldHight ~= hight and oldWidth ~= width then
  539. term.setBackgroundColor(colors.black)
  540. term.clear()
  541. oldHight,oldWidth = hight,width
  542. end
  543. -- draw top title bar
  544. term.setCursorPos(tbarX,tbarY)
  545. term.setTextColor(colors.black)
  546. term.setBackgroundColor(colors.blue)
  547. local b = tbarW - #title -2
  548. if b < 0 then
  549. b = 0
  550. end
  551. term.write(string.sub(" "..title,1,tbarW)..string.rep(" ",b))
  552. term.setTextColor(colors.white)
  553. term.setBackgroundColor(colors.red)
  554. term.write("X")
  555.  
  556. -- draw location bar
  557.  
  558. term.setCursorPos(barX,barY)
  559. term.setTextColor(colors.black)
  560. term.setBackgroundColor(colors.lightGray)
  561. local a = barW - #sParth - 1
  562. if a < 0 then
  563. a = 0
  564. end
  565. local tmppath = sParth
  566. if shell and shell.getDisplayName then
  567. tmppath = shell.getDisplayName(sParth)
  568. --dialogBox("yay")
  569. else
  570. --dialogBox("moop")
  571. end
  572. tmppath = tmppath or sParth
  573. local a = barW - #tmppath - 1
  574. if a < 0 then
  575. a = 0
  576. end
  577. term.write(string.sub(" "..tmppath,1,barW)..string.rep(" ",a))
  578.  
  579. -- draw scroll bar
  580. if folders[1] ~= ".." then
  581. table.insert(folders, 1, "..")
  582. end
  583.  
  584. if #folders + #files > boxH then
  585. term.setBackgroundColor(colors.lightGray)
  586. for i = 1,boxH do
  587. term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  588. local scroll = math.floor( boxH* (listOff/(#folders + #files-boxH+2)) )+1
  589. if i == scroll then
  590. term.setBackgroundColor(colors.gray)
  591. term.write(" ")
  592. term.setBackgroundColor(colors.lightGray)
  593. else
  594. term.write(" ")
  595. end
  596. end
  597. else
  598. term.setBackgroundColor(colors.gray)
  599. for i = 1,boxH do
  600. term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  601. term.write(" ")
  602. end
  603. end
  604.  
  605. -- draw main section
  606.  
  607. term.setTextColor(colors.black)
  608. term.setBackgroundColor(colors.cyan)
  609. for i = 1,boxH do
  610. term.setCursorPos(1+boxOffX,i+boxOffY)
  611. if folders[i+listOff] then
  612. if disks[folders[i+listOff]] then
  613. term.setTextColor(colors.orange)
  614. term.setBackgroundColor(colors.blue)
  615. term.write("D!")
  616. else
  617. term.setTextColor(colors.blue)
  618. term.setBackgroundColor(colors.lightBlue)
  619. term.write("[]")
  620. end
  621. if folders[i+listOff]:sub(1,1) == "." and folders[i+listOff] ~= ".." then
  622. term.setTextColor(colors.gray)
  623. else
  624. term.setTextColor(colors.black)
  625. end
  626. term.setBackgroundColor(colors.cyan)
  627. local repTimes = boxW - #folders[i+listOff] - 3
  628. if repTimes < 0 then
  629. repTimes = 0
  630. end
  631. term.write(" "..size(folders[i+listOff])..string.rep(" ",repTimes))
  632. elseif files[i-#folders+listOff] then
  633. local repTimes = boxW - #files[i-#folders+listOff] - 3
  634. if repTimes < 0 then
  635. repTimes = 0
  636. end
  637. term.write(" "..size(files[i-#folders+listOff])..string.rep(" ",repTimes))
  638. else
  639. term.write(string.rep(" ",boxW))
  640. end
  641. end
  642. term.setTextColor(colors.white)
  643. term.setBackgroundColor(colors.black)
  644.  
  645. if bugTest then
  646. term.setCursorPos(1,1)
  647. term.write(listOff.." "..boxOffY.." "..boxH)
  648. end
  649.  
  650. -- resume("redraw")
  651. end
  652. -- react to events
  653.  
  654. local event = {os.pullEvent()}
  655.  
  656. if event[1] == "mouse_click" then
  657. if event[2] == 1 then -- left mouse
  658. local temp = nil
  659. if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  660. temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  661. end
  662. if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  663. if temp == ".." then
  664. table.remove(parth,#parth)
  665. NewList()
  666. elseif fs.isDir(stringParth()..fSlash..temp) then
  667. table.insert(parth,temp)
  668. NewList()
  669. else
  670. if fs.exists(stringParth()..fSlash..temp) then
  671. if dialogBox("Run file ?",temp,{"yes","no"}) == 1 then
  672. local test,info = osRunSpaces(stringParth()..fSlash..temp)
  673. term.setCursorBlink(false)
  674. if not test then
  675. dialogBox("ERROR",tostring(info),{"ok"})
  676. end
  677. end
  678. flag = true
  679. end
  680. end
  681. elseif event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  682. if #folders + #files > boxH then
  683. if event[4] == boxOffY + 1 then
  684. listOff = 0
  685. elseif event[4] == boxOffY+boxH then
  686. listOff = #folders + #files + 1 - boxH
  687. else
  688. listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  689. end
  690. flag = true
  691. end
  692. elseif event[3] == exitX and event[4] == exitY then
  693. if dialogBox("Confirm","Exit application",{"yes","no"}) == 1 then
  694. bRun = false
  695. end
  696. flag = true
  697. end
  698. elseif event[2] == 2 then -- right mouse
  699. local temp = nil
  700. local sChoice = nil
  701. local sType = nil
  702.  
  703. if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  704. temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  705. end
  706.  
  707. -- moved
  708. local paste
  709. if clipboard then
  710. paste = "Paste"
  711. end
  712.  
  713. if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  714. sType = "File"
  715.  
  716. if fs.isDir(stringParth()..fSlash..temp) then
  717. sType = "Folder"
  718. end
  719.  
  720. if disks[temp] then
  721. sChoice = rClickMenu("Options",{"Open","Eject"},event[3]+1,event[4]+1)
  722. else
  723. if sType == "Folder" then
  724. sChoice = rClickMenu("Options",{"Open","Delete"},event[3]+1,event[4]+1)
  725. else
  726. if windowDimensions then
  727. sChoice = rClickMenu("Options",{"Run","Run CMD","deBug","Run win","Rename","Edit","Paint","Delete", "Cut", "Copy", paste},event[3]+1,event[4]+1)
  728. else
  729. sChoice = rClickMenu("Options",{"Run","Run CMD","deBug","CHIP 8","Rename","Edit","Paint","Delete", "Cut", "Copy", paste},event[3]+1,event[4]+1)
  730. end
  731. end
  732. end
  733.  
  734. else
  735. if event[3] > boxOffX and event[3] <= 5 + boxOffX and event[3] < boxOffX + boxW then
  736. if event[4] > offsetY and event[4] < hight + offsetY then
  737. sChoice = rClickMenu("Options",{"New File","New Folder", paste},event[3]+1,event[4]+1)
  738. end
  739. else
  740. table.remove(parth,#parth)
  741. NewList()
  742. end
  743. end
  744.  
  745. if sChoice == "Run win" and windowDimensions then -- choice execution
  746. osRunSpaces("start",stringParth()..fSlash..temp) -- might be a probblem -- shell
  747. flag = true
  748. elseif sChoice == "deBug" then
  749. osRunSpaces(stringParth()..fSlash..temp)
  750. term.setCursorBlink(false)
  751. os.pullEvent("key")
  752. elseif sChoice == "CHIP 8" then -- this was a temo hackin will be going
  753. local function openDevice(sType)
  754. for i,v in pairs(rs.getSides()) do
  755. if peripheral.isPresent(v) and peripheral.getType(v) == sType then
  756. return peripheral.wrap(v),v
  757. end
  758. end
  759. end
  760. local func,side = openDevice("monitor")
  761. osRunSpaces("chip8",stringParth()..fSlash..temp,side)
  762. elseif sChoice == "New File" then -- experemntal
  763. local name = InputBox()
  764. if name then
  765. if fs.exists(stringParth()..fSlash..name) then
  766. dialogBox("ERROR","Name exists",{"ok"})
  767. else
  768. local file = fs.open(stringParth()..fSlash..name,"w")
  769. if file then
  770. file.write("")
  771. file.close()
  772. NewList()
  773. else
  774. dialogBox("ERROR","File not created",{"ok"})
  775. end
  776. end
  777. end
  778. elseif sChoice == "New Folder" then -- experemental
  779. local name = InputBox()
  780. if name then
  781. if fs.exists(stringParth()..fSlash..name) then
  782. dialogBox("ERROR","Name exists",{"ok"})
  783. else
  784. if pcall(fs.makeDir,stringParth()..fSlash..name) then
  785. NewList()
  786. else
  787. dialogBox("ERROR","Access Denied",{"ok"})
  788. end
  789. end
  790. end
  791. elseif sChoice == "Open" then
  792. table.insert(parth,temp)
  793. NewList()
  794. elseif sChoice == "Run" then
  795. local test,info = osRunSpaces(stringParth()..fSlash..temp)
  796. term.setCursorBlink(false)
  797. if not test then
  798. dialogBox("ERROR",tostring(info),{"ok"})
  799. end
  800. elseif sChoice == "Run CMD" then
  801. local cmd = fixArgs(InputBox("Commands"))
  802. if cmd ~= nil then
  803. local test,info = osRunSpaces(stringParth()..fSlash..temp,unpack(cmd))
  804. term.setCursorBlink(false)
  805. if not test then
  806. dialogBox("ERROR",tostring(info),{"ok"})
  807. end
  808. end
  809. elseif sChoice == "Edit" then
  810. if sType == "File" then
  811. osRunSpaces("/rom/programs/edit",stringParth()..fSlash..temp)
  812. else
  813. dialogBox("ERROR","Can't edit a Folder",{"ok"})
  814. end
  815. elseif sChoice == "Delete" then
  816. if dialogBox("Confirm","Delete "..sType.." "..temp,{"yes","no"}) == 1 then
  817. if fs.isReadOnly(stringParth()..fSlash..temp) then
  818. dialogBox("ERROR",sType.." Is read Only",{"ok"})
  819. else
  820. fs.delete(stringParth()..fSlash..temp)
  821. NewList()
  822. end
  823. end
  824. elseif sChoice == "Paint" then
  825. if sType == "File" then
  826. osRunSpaces("/rom/programs/color/paint",stringParth()..fSlash..temp)
  827. else
  828. dialogBox("ERROR","Can't edit a Folder",{"ok"})
  829. end
  830. elseif sChoice == "Eject" then
  831. if dialogBox("Confirm","Eject disk "..disks[temp].." "..fSlash..temp,{"yes","no"}) == 1 then
  832. disk.eject(disks[temp])
  833. NewList()
  834. end
  835. elseif sChoice == "Copy" then
  836. clipboard = {stringParth(), temp}
  837. cut = false
  838. elseif sChoice == "Cut" then
  839. clipboard = {stringParth(), temp}
  840. cut = true
  841. elseif sChoice == "Paste" then
  842. if cut then
  843. local s, m = pcall(function()
  844. fs.move(clipboard, stringParth().."/"..temp)
  845. cut = false
  846. clipboard = nil
  847. end)
  848. if not s then
  849. dialogBox("Error", (m or "Couldn't move"), {"ok"}, 4, 30)
  850. end
  851. if bugTest then
  852. local x, y = term.getCursorPos()
  853. term.setCursorPos(1, ({term.getSize()})[2])
  854. write("from "..clipboard[1].."/"..clipboard[2].." to "..stringParth().."/"..clipboard[2])
  855. end
  856. else
  857. local s, m = pcall(function()
  858. fs.copy(clipboard[1].."/"..clipboard[2], stringParth().."/"..clipboard[2])
  859. end)
  860. if not s then
  861. dialogBox("Error", (m or "Couldn't copy"), {"ok"}, 4, 30)
  862. end
  863. if bugTest then
  864. local x, y = term.getCursorPos()
  865. term.setCursorPos(1, ({term.getSize()})[2])
  866. write("from "..clipboard[1].."/"..clipboard[2].." to "..stringParth().."/"..clipboard[2])
  867. end
  868. end
  869. NewList()
  870. elseif sChoice == "Rename" then -- new parts of the above thanks kilo
  871. local sName = InputBox("New Name")
  872. if type(sName) == "string" and sName ~= "" then
  873. local s, m = pcall(function()
  874. fs.move(stringParth()..fSlash..temp,stringParth()..fSlash..sName)
  875. end)
  876. if not s then
  877. dialogBox("Error", (m or "Rename failed"), {"ok"})
  878. end
  879. end
  880. NewList()
  881. end
  882. flag = true
  883. end
  884. elseif event[1] == "mouse_scroll" then -- flag this needs new math
  885. local old = listOff
  886. listOff = listOff + event[2]
  887. if listOff < 0 then
  888. listOff = 0
  889. end
  890. if #folders + #files + 1 - boxH > 0 and listOff > #folders + #files + 1 - boxH then
  891. listOff = #folders + #files + 1 - boxH
  892. elseif listOff > 0 and #folders + #files + 1 - boxH < 0 then
  893. listOff = 0
  894. end
  895. if listOff ~= old then
  896. flag = true
  897. end
  898.  
  899. elseif event[1] == "mouse_drag" then -- test
  900. if event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  901. if #folders + #files > boxH then
  902. if event[4] == boxOffY + 1 then
  903. listOff = 0
  904. elseif event[4] == boxOffY+boxH then
  905. listOff = #folders + #files + 1 - boxH
  906. else
  907. listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  908. end
  909. flag = true
  910. end
  911. end
  912.  
  913. elseif event[1] == "disk" or event[1] == "disk_eject" then
  914. NewList()
  915. elseif event[1] == "window_resize" then
  916. termX,termY = term.getSize()
  917. offsetX,offsetY = 1,1
  918. hight,width = math.ceil(termY-2),math.ceil(termX-2)
  919.  
  920. boxOffX,boxOffY = offsetX,offsetY + 2
  921. boxH,boxW = hight - 2 ,width - 2
  922.  
  923. barX,barY = offsetX + 1,offsetY + 2
  924. barH,barW = 1,width - 1
  925.  
  926. tbarX,tbarY = offsetX + 1,offsetY + 1
  927. tbarH,tbarW = 1,width - 1
  928.  
  929. exitX,exitY = offsetX + width - 1 ,offsetY + 1
  930.  
  931. flag = true
  932. end
  933. end
  934. end
  935. local function main()
  936. if term.isColor() then
  937. clear()
  938. fileSelect()
  939. clear()
  940. else
  941. error("Not an Advanced Computer (gold) ")
  942. end
  943. end
  944. local trash = (norun or main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement