Advertisement
DanielLaby99

LabyOS [XP] System

Feb 10th, 2014
49,518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.07 KB | None | 0 0
  1. -- http://www.computercraft.info/forums2/index.php?/topic/5509-advanced-computer-mouse-file-browser/
  2. local tArgs = {...}
  3. local ver = " Double rightclick to edit"
  4. local sTitle = "Explorer"
  5. local bugTest, norun, dir, showAll
  6. local _tArgs = {...}
  7. local config = "LabyOS/Config/System"
  8.  
  9. local temp
  10. if shell and shell.getRunningProgram then
  11. temp = shell.getRunningProgram()
  12. end
  13.  
  14. temp = temp
  15. local localPath = string.sub(temp,1,#temp-string.len(fs.getName(temp)))
  16. temp = nil -- just because not needed
  17.  
  18. -- load config file
  19.  
  20. local configSet = {}
  21. local cnf = {}
  22.  
  23. if fs.exists(config) then
  24. local file = fs.open(config,"r")
  25. if file then
  26. local item = file.readLine()
  27. while item do
  28. table.insert(cnf,item)
  29. item = file.readLine()
  30. end
  31. file.close()
  32. end
  33. end
  34.  
  35. for i = 1,10 do
  36. local test,data = pcall(textutils.unserialize,cnf[i])
  37. if test then
  38. configSet[i] = data
  39. else
  40. configSet[i] = nil
  41. end
  42. end
  43. cnf = nil
  44.  
  45. -- color configuration work in progress
  46. local titleBar = configSet[1] or {txt = colors.white,back = colors.blue}
  47. local addressBar = configSet[2] or {txt = colors.red,back = colors.lightGray}
  48. local itemWindo = configSet[3] or {txt = colors.white,back = colors.cyan}
  49. local rcmList = configSet[4] or {txt = colors.black,back = colors.lightGray} -- rcm = Right Click Menu List
  50. local rcmTitle = configSet[5] or {txt = colors.white,back = colors.blue}
  51. local dialogTitle = configSet[6] or {txt = colors.black,back = colors.blue}
  52. local dialogWindo = configSet[7] or {txt = colors.black,back = colors.white}
  53. local scrollCol = configSet[8] or {off = colors.gray, button = colors.gray,back = colors.lightGray}
  54.  
  55. local tIcons = configSet[9] or {
  56. back = {tCol = "lightGray",bCol = "blue",txt = " < "},
  57. disk = {tCol = "lime",bCol = "green",txt = "[*]"},
  58. audio = {tCol = "yellow",bCol = "red",txt = "(o)"},
  59. folder = {tCol = "lightGray",bCol = "blue",txt = "[=]"},
  60. file = {tCol = nil ,bCol = nil ,txt = nil}
  61. }
  62.  
  63. local customLaunch = configSet[10] or {
  64. ["Edit"] = "rom/programs/edit",
  65. ["Paint"] = "rom/programs/color/paint"
  66. }
  67.  
  68. local function saveCFG(overWrite)
  69. if not fs.exists(config) or overWrite then
  70. local cnf = {}
  71. local file = fs.open(config,"w")
  72. if file then
  73. file.write(textutils.serialize(titleBar).."\n")
  74. file.write(textutils.serialize(addressBar).."\n")
  75. file.write(textutils.serialize(itemWindo).."\n")
  76. file.write(textutils.serialize(rcmList).."\n")
  77. file.write(textutils.serialize(rcmTitle).."\n")
  78. file.write(textutils.serialize(dialogTitle).."\n")
  79. file.write(textutils.serialize(dialogWindo).."\n")
  80. file.write(textutils.serialize(scrollCol).."\n")
  81. file.write(textutils.serialize(tIcons).."\n")
  82. file.write(textutils.serialize(customLaunch).."\n")
  83. file.close()
  84. elseif overWrite then
  85. end
  86. end
  87. end
  88.  
  89. saveCFG()
  90.  
  91. -- end configuration
  92.  
  93. local function help()
  94. print([[Usage: browser [-d] [-h] [-a] [-u] [--debug] [--help] [--dir <dir>] [--all] [--update]
  95. --debug or -d: enable debug mode
  96. --help or -h: display this screen
  97. --dir: define initial directory
  98. --all or -a: show hidden files
  99. --update -u: update]])
  100. end
  101.  
  102. local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)
  103. return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )
  104. end
  105.  
  106.  
  107. for a = 1, #tArgs do
  108. if tArgs[a]:sub(1,2) == "--" then
  109. local cmd = tArgs[a]:sub(3):lower()
  110. if cmd == "debug" then
  111. bugTest = true
  112. elseif cmd == "help" then
  113. help()
  114. norun = true
  115. elseif cmd == "dir" then
  116. dir = tArgs[a+1]
  117. a = a + 1
  118. elseif cmd == "all" then
  119. showAll = true
  120. elseif cmd == "update" then
  121. update()
  122. end
  123. elseif tArgs[a]:sub(1,1) == "-" then
  124. for b = 2, #tArgs[a] do
  125. cmd = tArgs[a]:sub(b, b)
  126. if cmd == "d" then
  127. bugTest = true
  128. elseif cmd == "h" then
  129. help()
  130. norun = true
  131. elseif cmd == "p" then
  132. dir = tArgs[a+1]
  133. a = a + 1
  134. elseif cmd == "a" then
  135. showAll = true
  136. elseif cmd == "u" then
  137. update()
  138. end
  139. end
  140. else
  141. table.insert(_tArgs, tArgs[a])
  142. end
  143. end
  144.  
  145. if (not dir) and shell and shell.dir then
  146. dir = shell.dir()
  147. end
  148.  
  149. if dir and shell and shell.resolve then
  150. dir = shell.resolve(dir)
  151. end
  152.  
  153. dir = dir or "/"
  154.  
  155. if bugTest then -- this is that the var is for testing
  156. print("Dir: "..dir)
  157. os.startTimer(4)
  158. os.pullEvent()
  159. end
  160.  
  161. local function clear()
  162. term.setCursorBlink(false)
  163. term.setCursorPos(1,1)
  164. end
  165.  
  166. local function fixArgs(...)
  167. local tReturn={}
  168. local str=table.concat({...}," ")
  169. local sMatch
  170. while str and #str>0 do
  171. if string.sub(str,1,1)=="\"" then
  172. sMatch, str=string.match(str, "\"(.-)\"%s*(.*)")
  173. else
  174. sMatch, str=string.match(str, "(%S+)%s*(.*)")
  175. end
  176. table.insert(tReturn,sMatch)
  177. end
  178. return tReturn
  179. end
  180.  
  181. --[[ end Cruor function ]]--
  182.  
  183.  
  184. -- modified read made to play nice with coroutines
  185.  
  186. local function readMOD( _sReplaceChar, _tHistory,_wdth)
  187. local sLine = ""
  188. term.setCursorBlink( true )
  189.  
  190. local nHistoryPos = nil
  191. local nPos = 0
  192. if _sReplaceChar then
  193. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  194. end
  195.  
  196. local sx, sy = term.getCursorPos()
  197.  
  198. local w, h = term.getSize()
  199. if _wdth and type(_wdth) == "number" then
  200. w = sx + _wdth - 1
  201. end
  202.  
  203. local function redraw( _sCustomReplaceChar )
  204. local nScroll = 0
  205. if sx + nPos >= w then
  206. nScroll = (sx + nPos) - w
  207. end
  208.  
  209. term.setCursorPos( sx + _wdth - 1, sy )
  210. term.write(" ")
  211. term.setCursorPos( sx, sy )
  212. local sReplace = _sCustomReplaceChar or _sReplaceChar
  213. if sReplace then
  214. term.write( string.rep(sReplace,_wdth) )
  215. else
  216. term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )
  217. end
  218. term.setCursorPos( sx + nPos - nScroll, sy )
  219. end
  220.  
  221. while true do
  222. local sEvent, param = os.pullEvent()
  223. if sEvent == "char" then
  224. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  225. nPos = nPos + 1
  226. redraw()
  227.  
  228. elseif sEvent == "key" then
  229.  
  230. if param == keys.left then
  231. -- Left
  232. if nPos > 0 then
  233. nPos = nPos - 1
  234. redraw()
  235. end
  236.  
  237. elseif param == keys.right then
  238. -- Right
  239. if nPos < string.len(sLine) then
  240. nPos = nPos + 1
  241. redraw()
  242. end
  243.  
  244. elseif param == keys.up or param == keys.down then
  245. -- Up or down
  246. if _tHistory then
  247. redraw(" ");
  248. if param == keys.up then
  249. -- Up
  250. if nHistoryPos == nil then
  251. if #_tHistory > 0 then
  252. nHistoryPos = #_tHistory
  253. end
  254. elseif nHistoryPos > 1 then
  255. nHistoryPos = nHistoryPos - 1
  256. end
  257. else
  258. -- Down
  259. if nHistoryPos == #_tHistory then
  260. nHistoryPos = nil
  261. elseif nHistoryPos ~= nil then
  262. nHistoryPos = nHistoryPos + 1
  263. end
  264. end
  265.  
  266. if nHistoryPos then
  267. sLine = _tHistory[nHistoryPos]
  268. nPos = string.len( sLine )
  269. else
  270. sLine = ""
  271. nPos = 0
  272. end
  273. redraw()
  274. end
  275. elseif param == keys.backspace then
  276. -- Backspace
  277. if nPos > 0 then
  278. redraw(" ");
  279. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  280. nPos = nPos - 1
  281. redraw()
  282. end
  283. elseif param == keys.home then
  284. -- Home
  285. nPos = 0
  286. redraw()
  287. elseif param == keys.delete then
  288. if nPos < string.len(sLine) then
  289. redraw(" ");
  290. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  291. redraw()
  292. end
  293. elseif param == keys["end"] then
  294. -- End
  295. nPos = string.len(sLine)
  296. redraw()
  297. end
  298. elseif sEvent == "redraw" then
  299. redraw()
  300. elseif sEvent == "return" then
  301. term.setCursorBlink( false )
  302. return sLine
  303. end
  304. end
  305.  
  306. term.setCursorBlink( false )
  307.  
  308. return sLine
  309. end
  310.  
  311. -- end modified read
  312.  
  313. local function printC(posX,posY,textCol,backCol,text)
  314. term.setCursorPos(posX,posY)
  315. term.setTextColor(colors[textCol] or textCol)
  316. term.setBackgroundColor(colors[backCol] or backCol)
  317. term.write(text)
  318. end
  319.  
  320. local function InputBox(title)
  321. local boxW,boxH = 26,3
  322. local termX,termY = term.getSize()
  323. local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  324. local options = {"ok","cancel"}
  325.  
  326. local selected = 1
  327. local space = 0
  328. local range = {}
  329. for i = 1,#options do
  330. range[i] = {s = space,f = space + string.len(options[i])}
  331. space = space + string.len(options[i])+3
  332. end
  333. local ofC = (boxW/2) - (space/2)
  334.  
  335. local function drawBox()
  336. printC(ofsX,ofsY,colors.black,colors.blue,string.rep(" ",boxW))
  337. printC(ofsX+1,ofsY,colors.black,colors.blue,(title or "User Input"))
  338. printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(" ",boxW))
  339. printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(" ",boxW))
  340. printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(" ",boxW))
  341.  
  342. for i = 1,#options do
  343. if i == selected then
  344. term.setBackgroundColor(colors.lightGray)
  345. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  346. term.write("["..options[i].."]")
  347. term.setBackgroundColor(colors.white)
  348. term.write(" ")
  349. else
  350. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  351. term.write(" "..options[i].." ")
  352. end
  353. end
  354.  
  355. printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(" ",boxW-4))
  356. end
  357. drawBox()
  358. term.setCursorPos(ofsX+2,ofsY+2)
  359. local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)
  360. while true do
  361. local event = {os.pullEvent()}
  362. if event[1] == "key" or event[1] == "char" then
  363. if event[2] == 28 then
  364. local test,data = coroutine.resume(co,"return")
  365. return data
  366. else
  367. coroutine.resume(co,unpack(event))
  368. end
  369. elseif event[1] == "mouse_click" then
  370. if event[4] == ofsY + 3 then
  371. for i = 1,#options do
  372. if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  373. if options[i] == "ok" then
  374. local test,data = coroutine.resume(co,"return")
  375. return data
  376. elseif options[i] == "cancel" then
  377. return false
  378. end
  379. end
  380. end
  381. end
  382. end
  383. end
  384. end
  385.  
  386. local function dialogBox(title,message,options, h, w)
  387. term.setCursorBlink(false)
  388. local selected = 1
  389. title = title or ""
  390. message = message or ""
  391. options = options or {}
  392. local boxW,boxH = (w or 26), (h or 3)
  393. local termX,termY = term.getSize()
  394. local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  395.  
  396. local space = 0
  397. local range = {}
  398. for i = 1,#options do
  399. range[i] = {s = space,f = space + string.len(options[i])}
  400. space = space + string.len(options[i])+3
  401. end
  402. local ofC = math.ceil((boxW/2)) - math.ceil((space/2))
  403.  
  404. local function drawBox()
  405. printC(ofsX,ofsY,dialogTitle.txt,dialogTitle.back," "..title..string.rep(" ",boxW-#title-5).."_[]")
  406. term.setBackgroundColor(colors.red)
  407. term.setTextColor(colors.white)
  408. term.write("X")
  409. printC(ofsX,ofsY+1,dialogWindo.txt,dialogWindo.back,string.sub(" "..message..string.rep(" ",boxW),1,boxW))
  410. term.setCursorPos(ofsX,ofsY+2)
  411. term.write(string.rep(" ",boxW))
  412. term.setCursorPos(ofsX,ofsY+3)
  413. term.write(string.rep(" ",boxW))
  414. for i = 1,#options do
  415. if i == selected then
  416. printC(range[i].s + ofC + ofsX - 1,ofsY + 3,"black","lightGray","["..options[i].."]")
  417. term.setBackgroundColor(dialogWindo.back)
  418. term.setTextColor(dialogWindo.txt)
  419. term.write(" ")
  420. else
  421. term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  422. term.write(" "..options[i].." ")
  423. end
  424. end
  425. term.setCursorPos(ofsX + ofC + space,ofsY + 3)
  426. term.write(string.rep(" ",boxW - (ofC + space)))
  427. term.setBackgroundColor(colors.black)
  428. term.setTextColor(colors.white)
  429. end
  430. while true do
  431. drawBox()
  432. event = {os.pullEvent()}
  433. if event[1] == "key" then
  434. if event[2] == 203 then -- left
  435. selected = selected - 1
  436. if selected < 1 then
  437. selected = #options
  438. end
  439. elseif event[2] == 205 then -- right
  440. selected = selected + 1
  441. if selected > #options then
  442. selected = 1
  443. end
  444. elseif event[2] == 28 then -- enter
  445. return selected , options[selected]
  446. end
  447. elseif event[1] == "mouse_click" then
  448.  
  449. if bugTest then term.write("M "..event[2].." X "..event[3].." Y "..event[4].." ") end
  450.  
  451. if event[2] == 1 then
  452. if event[4] == ofsY + 3 then
  453. for i = 1,#options do
  454. if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  455. return i , options[i]
  456. end
  457. end
  458. end
  459. end
  460. end
  461. end
  462. end
  463.  
  464. local flag = true
  465. local fSlash = "/"
  466. local path = {dir:match("[^/]+")}
  467. local function stringPath() -- compacted this a lot
  468. return fSlash..table.concat(path,fSlash)
  469. end
  470.  
  471. local function osRunSpaces(sFileLocation,...) -- getRunningProgram() ["shell"] = shell
  472. clear()
  473. if os.newThread then
  474. os.newThread(false,...)
  475. else
  476. local fProg,probblem = loadfile(sFileLocation)
  477. if fProg then
  478. local tEnv = {["shell"] = {}}
  479. setmetatable(tEnv.shell,{ __index = shell})
  480. tEnv.shell.getRunningProgram = function()
  481. return sFileLocation
  482. end
  483. setmetatable(tEnv,{ __index = _G})
  484. setfenv(fProg,tEnv)
  485. local test,probblem = pcall(fProg,...)
  486. if not test then
  487. print(probblem)
  488. dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  489. else
  490. return true
  491. end
  492. else
  493. print(probblem)
  494. dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  495. end
  496. end
  497. end
  498.  
  499. local function rClickMenu(title,tList,tItem,posX,posY)
  500.  
  501. term.setCursorBlink(false)
  502. local BoxTitle = title
  503. local choices = {}
  504. local termX,termY = term.getSize()
  505. local offX,offY
  506.  
  507. local width = #BoxTitle + 2
  508. local hight
  509.  
  510. for k,v in pairs(tList) do
  511. if v ~= nil then
  512. table.insert(choices,k)
  513. end
  514. if width < #k + 2 then
  515. width = #k + 2
  516. end
  517. end
  518.  
  519. if #choices == 0 then
  520. return
  521. end
  522.  
  523. hight = #choices + 1
  524. table.sort(choices)
  525.  
  526. offX,offY = math.ceil((termX/2) - (width/2)),math.ceil((termY/2) - (hight/2))
  527.  
  528. if posX and posY then -- offX,offY = posX,posY
  529. if posX >= termX - width - 1 then
  530. offX = termX - width - 1
  531. else
  532. offX = posX
  533. end
  534. if posY >= termY - hight then
  535. offY = termY - hight
  536. else
  537. offY = posY
  538. end
  539. end
  540.  
  541. local function reDrawer()
  542. printC(offX,offY,rcmTitle.txt,rcmTitle.back," "..BoxTitle..string.rep(" ",width - #BoxTitle - 1))
  543. for i = 1,#choices do
  544. printC(offX,offY + i,rcmList.txt,rcmList.back," "..choices[i]..string.rep(" ",width - #choices[i] - 1))
  545. end
  546. end
  547.  
  548. while true do
  549. reDrawer()
  550. local event = {os.pullEvent()}
  551. if event[1] == "mouse_click" then
  552. if event[2] == 1 then -- event[3] = x event[4] = y
  553. if event[4] > offY and event[4] < hight + offY and event[3] >= offX and event[3] < width + offX then
  554. --dialogBox("ERROR:",type(tList[choices[event[4] - offY]]),{"ok"})
  555. if type(tList[choices[event[4] - offY]]) == "function" then
  556. return tList[choices[event[4] - offY]](tItem)
  557. elseif type(tList[choices[event[4] - offY]]) == "table" then
  558. return rClickMenu("Options",tList[choices[event[4] - offY]],tItem,event[3],event[4])
  559. elseif type(tList[choices[event[4] - offY]]) == "string" then
  560. return osRunSpaces(
  561. unpack(
  562. fixArgs(
  563. tList[choices[event[4] - offY]].." \""..stringPath()..fSlash..tItem.n.."\""
  564. )
  565. )
  566. )
  567. else
  568. dialogBox("ERROR:","somthing up with new rMenu",{"ok"})
  569. end
  570. else
  571. return
  572. end
  573. elseif event[2] == 2 then
  574. return
  575. end
  576. end
  577. end
  578.  
  579. end
  580.  
  581. local function preferences()
  582. local tItem = {
  583. {txt = "Title Bar",it = titleBar},
  584. {txt = "Address Bar",it = addressBar},
  585. {txt = "Item Windo", it = itemWindo},
  586. {txt = "Title Right Click Title",it = rcmTitle},
  587. {txt = "Right Click Menu",it = rcmList},
  588. {txt = "Title Dialog Box",it = dialogTitle},
  589. {txt = "Dialog Box",it = dialogWindo},
  590. {txt = "Scroll Bar",it = scrollCol}
  591. }
  592. local topL,topR = 13,5
  593. local width,hight = 23,6
  594. local bottomL,bottomR = topL + width,topR + hight
  595.  
  596. local listOffset = 0
  597. local sel = 1
  598. local otherSel = 1
  599. local otherItems = {}
  600.  
  601. if tItem[sel] then
  602. for k,v in pairs(tItem[sel].it) do
  603. table.insert(otherItems,{txt = k,it = v})
  604. end
  605. end
  606.  
  607. local function draw()
  608. printC(topL,topR,titleBar.txt,titleBar.back,string.sub(" Preferences "..string.rep(" ",width),1,width))
  609. for i = 0,12,4 do
  610. for a = 1,4 do
  611. --printC(topL + (a*12)-12 ,topR + ((i+4)/4),4,2^(a+i-1)," "..tostring(2^(a+i-1)))
  612. printC(topL + a-1 ,topR + ((i+4)/4),4,2^(a+i-1)," ")
  613. end
  614. end
  615. local sSel = " "
  616. for i = 1,hight - 2 do
  617. if i == sel - listOffset then
  618. sSel = ">"
  619. end
  620. if tItem[i+listOffset] then
  621. printC(topL + 4 ,topR + i,colors.black,colors.white,string.sub(sSel..tItem[i+listOffset].txt..string.rep(" ",width),1,width - 4))
  622. else
  623. printC(topL + 4 ,topR + i,colors.black,colors.white,sSel..string.rep(" ",width-5))
  624. end
  625. if i == sel - listOffset then
  626. sSel = " "
  627. end
  628. end
  629. term.setCursorPos(topL,topR + hight - 1)
  630. local loop = 1
  631. local length = 0
  632. for i = 1,#otherItems do
  633. if otherSel == i then
  634. sSel = ">"
  635. end
  636. if colors.black == otherItems[i].it or colors.gray == otherItems[i].it then
  637. term.setTextColor(colors.white)
  638. else
  639. term.setTextColor(colors.black)
  640. end
  641. term.setBackgroundColor(otherItems[i].it)
  642. term.write(sSel..tostring(otherItems[i].txt).." ")
  643. length = length + #otherItems[i].txt + 2
  644. if otherSel == i then
  645. sSel = " "
  646. end
  647. loop = loop+1
  648. end
  649. term.setBackgroundColor(colors.white)
  650. term.write(string.rep(" ",width - length))
  651. end
  652. while true do
  653. draw()
  654. local event = {os.pullEvent()}
  655. if event[1] == "mouse_click" and event[2] == 1 then
  656. if inBouwndry(event[3],event[4],topL,topR,width,hight) then
  657. local inSideX,inSideY = event[3] - topL,event[4] - topR
  658. if inBouwndry(inSideX+1,inSideY,1,1,4,4) and tItem[sel] then
  659. --[[
  660. term.setCursorPos(1,1)
  661. term.setBackgroundColor(2^(inSideX + ((inSideY*4)-4)))
  662. print(2^(inSideX + ((inSideY*4)-4))," ",inSideX + ((inSideY*4)-4)," ")
  663. ]]--
  664. tItem[sel]["it"][otherItems[otherSel].txt] = (2^(inSideX + ((inSideY*4)-4)))
  665. end
  666. end
  667. elseif event[1] == "key" then
  668. if event[2] == 200 then
  669. sel = sel - 1
  670. elseif event[2] == 208 then
  671. sel = sel + 1
  672. elseif event[2] == 203 then
  673. otherSel = otherSel - 1
  674. elseif event[2] == 205 then
  675. otherSel = otherSel + 1
  676. elseif event[2] == 28 then
  677. if dialogBox("Confirm","Save prefrences?",{"Yes","No"}) == 1 then
  678. saveCFG(true)
  679. end
  680. return
  681. end
  682. end
  683. if sel < 1 then
  684. sel = 1
  685. elseif sel > #tItem then
  686. sel = #tItem
  687. end
  688. if sel > listOffset + hight - 2 then
  689. listOffset = listOffset + 1
  690. elseif sel - listOffset < 1 then
  691. listOffset = listOffset - 1
  692. end
  693.  
  694. otherItems = {}
  695. if tItem[sel] then
  696. for k,v in pairs(tItem[sel].it) do
  697. table.insert(otherItems,{txt = k,it = v})
  698. end
  699. end
  700.  
  701. if otherSel < 1 then
  702. otherSel = 1
  703. elseif otherSel > #otherItems then
  704. otherSel = #otherItems
  705. end
  706.  
  707. if bugTest then
  708. term.setBackgroundColor(colors.black)
  709. term.setTextColor(colors.white)
  710. term.setCursorPos(1,1)
  711. term.clearLine()
  712. term.write("sel "..sel.." offset "..listOffset)
  713. end
  714. end
  715. end
  716.  
  717. local function fileSelect(mode) -- save_file open_file browse < not yet implemented
  718.  
  719. local title = sTitle.." "..ver
  720. local bRun = true
  721. local clipboard = nil
  722. local cut = false
  723.  
  724. local termX,termY = term.getSize()
  725. local offsetX,offsetY = 1,1
  726. local hight,width = math.ceil(termY-2),math.ceil(termX-2)
  727. local oldHight,oldWidth
  728.  
  729. -- offsets
  730. local boxOffX,boxOffY = offsetX,offsetY + 2
  731. local boxH,boxW = hight - 2 ,width - 2
  732.  
  733. local barX,barY = offsetX + 1,offsetY + 2
  734. local barH,barW = 1,width - 1
  735.  
  736. local tbarX,tbarY = offsetX + 1,offsetY + 1
  737. local tbarH,tbarW = 1,width - 1
  738.  
  739. local exitX,exitY = offsetX + width - 1 ,offsetY + 1
  740.  
  741. local pading = string.rep(" ",boxW)
  742. local list
  743.  
  744. local listOff = 0
  745.  
  746. local sPath
  747. local tItemList = {}
  748.  
  749. local function newList()
  750. listOff = 0
  751. flag = true
  752. tItemList = {{n = "..", id = "back"}} -- adds a back item at top of list
  753. sPath = stringPath()
  754. local folders = {}
  755. local files = {}
  756. local disks = {}
  757. if not fs.exists(sPath) then
  758. path = {}
  759. sPath = stringPath()
  760. dialogBox("ERROR:","Path no longer exists",{"ok"})
  761. end
  762. local test,list = pcall(fs.list,sPath) -- stopes fs.list crash
  763. if list == nil then
  764. list = {}
  765. dialogBox("ERROR : ","fs.list crashed",{"ok"})
  766. end
  767. if #path == 0 then
  768. for i,v in pairs(rs.getSides()) do
  769. if disk.isPresent(v) then
  770. if disk.hasData(v) then
  771. table.insert(tItemList,{n = disk.getMountPath(v), id = "disk",s = v})
  772. disks[disk.getMountPath(v)] = true
  773. elseif disk.hasAudio(v) then
  774. table.insert(tItemList,{n = disk.getAudioTitle(v), id = "audio",s = v})
  775. end
  776. end
  777. end
  778. end
  779. for i,v in pairs(list) do
  780. if fs.isDir(sPath..fSlash..v) then
  781. table.insert(folders,v)
  782. else
  783. table.insert(files,v)
  784. end
  785. end
  786. table.sort(folders)
  787. table.sort(files)
  788. for i,v in pairs(folders) do
  789. if disks[v] == nil then
  790. table.insert(tItemList,{n = v, id = "folder"})
  791. end
  792. end
  793. for i,v in pairs(files) do
  794. table.insert(tItemList,{n = v, id = "file"})
  795. end
  796. end
  797.  
  798. local function paste()
  799. if cut then
  800. local s, m = pcall(
  801. function()
  802. fs.move(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])
  803. cut = false
  804. clipboard = nil
  805. end)
  806. if not s then
  807. dialogBox("Error", (m or "Couldn't move"), {"ok"}, 4, 30)
  808. end
  809. if bugTest then
  810. local x, y = term.getCursorPos()
  811. term.setCursorPos(1, ({term.getSize()})[2])
  812. write("from "..clipboard[1]..fSlash..clipboard[2].." to "..stringPath()..fSlash..clipboard[2])
  813. end
  814. else
  815. local s, m = pcall(function()
  816. if fs.exists(stringPath()..fSlash..clipboard[2]) then
  817. fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash.."copy-"..clipboard[2])
  818. else
  819. fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])
  820. end
  821. end)
  822. if not s then
  823. dialogBox("Error", (m or "Couldn't copy"), {"ok"}, 4, 30)
  824. end
  825. if bugTest then
  826. local x, y = term.getCursorPos()
  827. term.setCursorPos(1, ({term.getSize()})[2])
  828. write("from "..clipboard[1]..fSlash..clipboard[2].." to "..stringPath()..fSlash..clipboard[2])
  829. end
  830. end
  831. newList()
  832. end
  833.  
  834. -- this section bellow handles the right click menu
  835.  
  836. local tmenu = {
  837. disk = {
  838. ["Open"] = function(tItem)
  839. table.insert(path,tItem.n)
  840. newList()
  841. end,
  842. ["Copy"] = function(tItem)
  843. clipboard = {stringPath(), tItem.n}
  844. cut = false
  845. end,
  846. ["Eject"] = function(tItem)
  847. if dialogBox("Confirm","Eject "..fSlash..tItem.n.." "..tItem.s,{"yes","no"}) == 1 then
  848. disk.eject(tItem.s)
  849. newList()
  850. end
  851. end,
  852. ["ID label"] = function(tItem)
  853. dialogBox("ID label",disk.getDiskID(tItem.s).." "..tostring(disk.getLabel(tItem.s)),{"ok"})
  854. end,
  855. ["Set label"] = function(tItem)
  856. local name = InputBox("Label?")
  857. if name then
  858. disk.setLabel(tItem.s,name)
  859. end
  860. end,
  861. ["Clear label"] = function(tItem)
  862. if dialogBox("Confirm","Cleal Label from "..tItem.s,{"yes","no"}) == 1 then
  863. disk.setLabel(tItem.s)
  864. end
  865. end
  866. },
  867. folder = {
  868. ["Open"] = function(temp)
  869. table.insert(path,temp.n)
  870. newList()
  871. end,
  872. ["Copy"] = function(tItem)
  873. clipboard = {stringPath(), tItem.n}
  874. cut = false
  875. end,
  876. ["Cut"] = function(tItem)
  877. clipboard = {stringPath(), tItem.n}
  878. cut = true
  879. end,
  880. ["Delete"] = function(tItem)
  881. if dialogBox("Confirm","Delete "..tItem.id.." "..tItem.n,{"yes","no"}) == 1 then
  882. if fs.isReadOnly(stringPath()..fSlash..tItem.n) then
  883. dialogBox("ERROR",tItem.id.." Is read Only",{"ok"})
  884. else
  885. fs.delete(stringPath()..fSlash..tItem.n)
  886. newList()
  887. end
  888. end
  889. end,
  890. ["Rename"] = function(tItem)
  891. local sName = InputBox("New Name")
  892. if type(sName) == "string" and sName ~= "" then
  893. local s, m = pcall(function()
  894. fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)
  895. end)
  896. if not s then
  897. dialogBox("Error", (m or "Rename failed"), {"ok"})
  898. end
  899. end
  900. newList()
  901. end
  902. },
  903. file = {
  904. ["Run"] = {
  905. ["Run"] = function(tItem)
  906. osRunSpaces(stringPath()..fSlash..tItem.n)
  907. end,
  908. ["Run CMD"] = function(tItem)
  909. local cmd = InputBox("Commands")
  910. if cmd then
  911. osRunSpaces(stringPath()..fSlash..tItem.n,unpack(fixArgs(cmd)))
  912. end
  913. end,
  914. },
  915. ["Open With"] = customLaunch,
  916. ["Rename"] = function(tItem)
  917. local sName = InputBox("New Name")
  918. if type(sName) == "string" and sName ~= "" then
  919. local s, m = pcall(function()
  920. fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)
  921. end)
  922. if not s then
  923. dialogBox("Error", (m or "Rename failed"), {"ok"})
  924. end
  925. end
  926. newList()
  927. end,
  928. ["Delete"] = function(tItem)
  929. if dialogBox("Confirm","Delete "..tItem.id.." "..tItem.n,{"yes","no"}) == 1 then
  930. if fs.isReadOnly(stringPath()..fSlash..tItem.n) then
  931. dialogBox("ERROR",tItem.id.." Is read Only",{"ok"})
  932. else
  933. fs.delete(stringPath()..fSlash..tItem.n)
  934. newList()
  935. end
  936. end
  937. end,
  938. ["Cut"] = function(tItem)
  939. clipboard = {stringPath(), tItem.n}
  940. cut = true
  941. end,
  942. ["Copy"] = function(tItem)
  943. clipboard = {stringPath(), tItem.n}
  944. cut = false
  945. end
  946. },
  947. audio = {
  948. ["Play"] = 1,
  949. ["Eject"] = 1
  950. },
  951. back = {
  952. },
  953. blank = { -- tmenu.blank.Paste =
  954. ["Paste"] = nil,
  955. ["New File"] = function()
  956. local name = InputBox()
  957. if name then
  958. if fs.exists(stringPath()..fSlash..name) then
  959. dialogBox("ERROR","Name exists",{"ok"})
  960. else
  961. local file = fs.open(stringPath()..fSlash..name,"w")
  962. if file then
  963. file.write("")
  964. file.close()
  965. newList()
  966. else
  967. dialogBox("ERROR","File not created",{"ok"})
  968. end
  969. end
  970. end
  971. end,
  972. ["New Folder"] = function()
  973. local name = InputBox()
  974. if name then
  975. if fs.exists(stringPath()..fSlash..name) then
  976. dialogBox("ERROR","Name exists",{"ok"})
  977. else
  978. if pcall(fs.makeDir,stringPath()..fSlash..name) then
  979. newList()
  980. else
  981. dialogBox("ERROR","Access Denied",{"ok"})
  982. end
  983. end
  984. end
  985. end,
  986. ["Preferences"] = preferences
  987. },
  988. }
  989.  
  990. -- end right click menu
  991.  
  992. local function scrollBar(posX,posY)
  993. if posX == boxOffX+boxW+1 and posY > boxOffY and posY <= boxOffY+boxH then
  994. if #tItemList > boxH then
  995. if posY == boxOffY + 1 then
  996. listOff = 0
  997. elseif posY == boxOffY+boxH then
  998. listOff = #tItemList + 1 - boxH
  999. else
  1000. listOff = math.ceil((posY - boxOffY - 1 )*(((#tItemList - boxH+2)/boxH)))
  1001. end
  1002. flag = true
  1003. end
  1004. end
  1005. end
  1006.  
  1007. newList()
  1008.  
  1009. while bRun do
  1010. if flag then
  1011. flag = false
  1012. -- clear
  1013. if oldHight ~= hight and oldWidth ~= width then
  1014. resetScreen()
  1015. oldHight,oldWidth = hight,width
  1016. end
  1017. -- draw top title bar
  1018. local b = tbarW - #title -2
  1019. if b < 0 then
  1020. b = 0
  1021. end
  1022. term.setTextColor(colors.white)
  1023. printC(tbarX,tbarY,titleBar.txt,titleBar.back,string.sub(" "..title,1,tbarW)..string.rep(" ",b))
  1024. term.setTextColor(colors.white)
  1025. term.setBackgroundColor(colors.red)
  1026. term.write("X")
  1027.  
  1028. -- draw location bar
  1029. local a = barW - #sPath - 1
  1030. if a < 0 then
  1031. a = 0
  1032. end
  1033. local tmppath = sPath
  1034. if shell and shell.getDisplayName then
  1035. tmppath = shell.getDisplayName(sPath)
  1036. --dialogBox("yay")
  1037. else
  1038. --dialogBox("moop")
  1039. end
  1040. tmppath = tmppath or sPath
  1041. local a = barW - #tmppath - 1
  1042. if a < 0 then
  1043. a = 0
  1044. end
  1045. printC(barX,barY,addressBar.txt,addressBar.back,string.sub(" "..tmppath,1,barW)..string.rep(" ",a))
  1046.  
  1047. -- draw scroll bar
  1048. if #tItemList > boxH then
  1049. term.setBackgroundColor(scrollCol.back)
  1050. for i = 1,boxH do
  1051. term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  1052. local scroll = math.floor( boxH* (listOff/(#tItemList-boxH+2)) )+1
  1053. if i == scroll then
  1054. term.setBackgroundColor(scrollCol.button)
  1055. term.write(" ")
  1056. term.setBackgroundColor(scrollCol.back)
  1057. else
  1058. term.write(" ")
  1059. end
  1060. end
  1061. else
  1062. term.setBackgroundColor(scrollCol.off)
  1063. for i = 1,boxH do
  1064. term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  1065. term.write(" ")
  1066. end
  1067. end
  1068.  
  1069. -- draw main section
  1070.  
  1071. for i = 1,boxH do -- listOff
  1072. local sel = i+listOff
  1073. if tItemList[sel] then
  1074. printC(1+boxOffX,i+boxOffY,(tIcons[tItemList[sel].id].tCol or itemWindo.txt),(tIcons[tItemList[sel].id].bCol or itemWindo.back),( tIcons[tItemList[sel].id].txt or " "))
  1075. printC(4+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,string.sub(" "..tItemList[sel].n..pading,1,boxW-3))
  1076. else
  1077. printC(1+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,pading)
  1078. end
  1079. end
  1080.  
  1081. if bugTest then
  1082. printC(1,1,"black","white",listOff.." "..boxOffY.." "..boxH)
  1083. end
  1084.  
  1085. end
  1086.  
  1087. -- react to events
  1088.  
  1089. event, button, X, Y = os.pullEvent("mouse_click")
  1090.  
  1091. if X >= 1 and X <= 6 and Y == 19 then
  1092. startmenu()
  1093. end
  1094. if X >= 8 and X <= 20 and Y == 19 and closetask1 == 0 then
  1095. openrun()
  1096. openrun2()
  1097. end
  1098. if X >= 49 and X <= 49 and Y == 2 then
  1099. resetDesktop()
  1100. end
  1101. local event = {os.pullEvent()}
  1102.  
  1103. if event[1] == "mouse_click" then
  1104. if inBouwndry(event[3],event[4],boxOffX+1,boxOffY+1,boxW,boxH) then
  1105. local selected = tItemList[event[4]+listOff-boxOffY]
  1106. if selected and inBouwndry(event[3],event[4],boxOffX+1,event[4],#selected.n + 4,1) then
  1107. if event[2] == 1 then -- left mouse
  1108. if selected.id == "back" then
  1109. table.remove(path,#path)
  1110. newList()
  1111. elseif selected.id == "folder" or selected.id == "disk" then
  1112. table.insert(path,selected.n)
  1113. newList()
  1114. elseif selected.id == "file" then
  1115. if dialogBox("Run file ?",selected.n,{"yes","no"}) == 1 then
  1116. osRunSpaces(stringPath()..fSlash..selected.n)
  1117. end
  1118. flag = true
  1119. end
  1120. elseif event[2] == 2 then -- right mouse
  1121. rClickMenu("Options",tmenu[selected.id],selected,event[3],event[4])
  1122. flag = true
  1123. end
  1124. elseif event[2] == 2 then -- right clicking not on object
  1125. if clipboard then
  1126. tmenu.blank.Paste = paste
  1127. else
  1128. tmenu.blank.Paste = nil
  1129. end
  1130. rClickMenu("Options",tmenu["blank"],selected,event[3],event[4])
  1131. flag = true
  1132. end
  1133. elseif event[2] == 1 and event[3] == exitX and event[4] == exitY then
  1134. bRun = false
  1135. flag = true
  1136. elseif event[2] == 1 then
  1137. scrollBar(event[3],event[4])
  1138. end
  1139. elseif event[1] == "mouse_scroll" then -- flag this needs new math
  1140. local old = listOff
  1141. listOff = listOff + event[2]
  1142. if listOff < 0 then
  1143. listOff = 0
  1144. end
  1145. if #tItemList + 1 - boxH > 0 and listOff > #tItemList + 1 - boxH then
  1146. listOff = #tItemList + 1 - boxH
  1147. elseif listOff > 0 and #tItemList + 1 - boxH < 0 then
  1148. listOff = 0
  1149. end
  1150. if listOff ~= old then
  1151. flag = true
  1152. end
  1153.  
  1154. elseif event[1] == "mouse_drag" then -- scroll bar
  1155. scrollBar(event[3],event[4])
  1156. elseif event[1] == "disk" or event[1] == "disk_eject" then
  1157. newList()
  1158. elseif event[1] == "window_resize" then
  1159. termX,termY = term.getSize()
  1160. offsetX,offsetY = 1,1
  1161. hight,width = math.ceil(termY-2),math.ceil(termX-2)
  1162.  
  1163. boxOffX,boxOffY = offsetX,offsetY + 2
  1164. boxH,boxW = hight - 2 ,width - 2
  1165.  
  1166. barX,barY = offsetX + 1,offsetY + 2
  1167. barH,barW = 1,width - 1
  1168.  
  1169. tbarX,tbarY = offsetX + 1,offsetY + 1
  1170. tbarH,tbarW = 1,width - 1
  1171.  
  1172. exitX,exitY = offsetX + width - 1 ,offsetY + 1
  1173. pading = string.rep(" ",boxW)
  1174.  
  1175. flag = true
  1176. elseif event[1] == "redraw" then
  1177. flag = true
  1178. end
  1179. end
  1180. end
  1181.  
  1182. local function main()
  1183. if term.isColor() then
  1184. clear()
  1185. fileSelect()
  1186. clear()
  1187. else
  1188. error("Not an Advanced Computer (gold) ")
  1189. end
  1190. end
  1191.  
  1192. local trash = (norun or main())
  1193. error("Terminated")
  1194. shell.run("LabyOS/Programme/Desktop")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement