Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.76 KB | None | 0 0
  1. local cmp = require('component')
  2. local event = require('event')
  3. local serialization = require('serialization')
  4. local gpu=cmp.gpu
  5. local term =require('term')
  6. local computer = require('computer')
  7. local filesystem=require('filesystem')
  8. local unicode = require('unicode')
  9. local keyboard = require("keyboard")
  10. local shell = require("shell")
  11. local object = require('object')
  12. ----
  13. local __name = 'server'
  14. local __timezone = 0
  15. local __correct = 0
  16.  
  17. local tz = __timezone + __correct
  18. local t_correction = tz * 3600
  19. ----
  20. local oldBack=gpu.getBackground()
  21. local oldFore=gpu.getForeground()
  22. local rX , rY = gpu.getResolution()
  23. ----
  24. function setOld()
  25. gpu.setBackground(oldBack)
  26. gpu.setForeground(oldFore)
  27. end
  28.  
  29. function getTime()
  30. local file = io.open('/tmp/'..__name..'.dt','w')
  31. file:write('')
  32. file:close()
  33. local lastmod = tonumber(string.sub(filesystem.lastModified('/tmp/'..__name..'.dt'),1,-4)) + t_correction
  34. return lastmod
  35. end
  36.  
  37. local function getEvent(...)
  38. args = {...}
  39. evnt = nil
  40. if type(args[1]) == 'number' then
  41. time = args[1]
  42. end
  43. evtrue = true
  44. local function getRawEvent()
  45. return {event.pull()}
  46. end
  47. while evtrue do
  48. -- s , evnt = pcall(getRawEvent)
  49. evnt = {event.pull(time)}
  50. -- if s ~= false then
  51. for i=1,#args do
  52. if evnt[1] == args[i] and time == nil then
  53. evtrue = false
  54. break
  55. elseif time ~= nil then
  56. evtrue = false
  57. break
  58. end
  59. end
  60. --end
  61. end
  62. return evnt
  63. end
  64. ----
  65. function getPath(arr)
  66. local path = ''
  67. for i=1,#arr do
  68. path = path .. arr[i]
  69. end
  70. return path
  71. end
  72.  
  73. local function saveTbl(tbl, fl)
  74. file = io.open(fl, 'w')
  75. file:write(serialization.serialize(tbl))
  76. file:close()
  77. end
  78.  
  79. local function loadTbl(fl)
  80. file = io.open(fl, 'r')
  81. if file == nil then
  82. file = io.open(fl, 'w')
  83. file:write('{}')
  84. return {} else
  85. return serialization.unserialize(file:read('*a'))
  86. end
  87. file:close()
  88. end
  89.  
  90. ex = loadTbl('.expansion')
  91.  
  92. function getFiles(path)
  93. arr={{},{}} arr2 = {}
  94. for val,val2 in filesystem.list(path) do if val~= nil then if filesystem.isDirectory(val) then arr[1][#arr[1]+1] = val else arr[2][#arr[2]+1] = val end end end
  95. for i=1,#arr do
  96. for j=1,#arr[i] do
  97. arr2[#arr2+1]=arr[i][j]
  98. end
  99. end
  100. return arr2
  101. end
  102.  
  103. function rec(path)
  104. local colors = {'0x0AA00','0xF06306','0xAA0000'}
  105. if filesystem.isDirectory(path) then
  106. str = {'',0xFFFFFF}
  107. else
  108. N = filesystem.size(path)
  109. if unicode.len(tostring(N)) <= 3 then
  110. str = {tostring(N)..' B',colors[1]}
  111. elseif unicode.len(tostring(N)) <= 5 then
  112. str = {tostring(round(N / 1024 , 2)) .. ' KB',colors[2]}
  113. else
  114. str = {tostring(round(N / 1024 / 1024,2)) .. ' MB',colors[3]}
  115. end
  116. end
  117. return str
  118. end
  119.  
  120. function round(num, decimalPlaces)
  121. local mult = 10 ^ (decimalPlaces or 0)
  122. return math.floor(num * mult + 0.5) / mult
  123. end
  124.  
  125. function getColorFile(str,n,o)
  126. local out = o
  127. for i=1,#ex do
  128. if unicode.sub(str,unicode.len(str)-unicode.len(ex[i][1])+1,unicode.len(str)) == ex[i][1] then
  129. out = ex[i][n]
  130. end
  131. end
  132. return out
  133. end
  134.  
  135. function finder(arr,str)
  136. local out = {false}
  137. for i = 1,#arr do
  138. if arr[i] == str then
  139. out = {true,i}
  140. break
  141. end
  142. end
  143. return out
  144. end
  145.  
  146. function drawOneLine(arr,i,n)
  147. fi = finder(selected,arr[i])
  148. if fi[1] then
  149. drawOneSegClick(arr,active_position,0x007DFF,1)
  150. else
  151. drawOneSegClick(arr,i,0xFFFFFF,n)
  152. end
  153. end
  154.  
  155. function drawOneSegClick(arr,i,color,n)
  156. cf = rec(getPath(paths)..arr[i])
  157. l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
  158. gpu.setBackground(color)
  159. gpu.fill(k+1,1+n,l,1,' ')
  160. gpu.setForeground(getColorFile(arr[i],2,0))
  161. object.text(math.ceil(rX/4)+1,1+n,l,1,tostring(arr[i]),'first','')
  162. gpu.setForeground(tonumber(cf[2]))
  163. gpu.set(rX-2- unicode.len(tostring(cf[1])),1+n,tostring(cf[1]))
  164. end
  165.  
  166. function drawClick(arr)
  167. if oldSelected == nil then oldSelected = selected end
  168. for i=active_position,rY-2+active_position-1 do
  169. for j=1,#oldSelected do
  170. if oldSelected[j] == arr[i] and oldSelected[j] ~= selected[j] then
  171. n = i - active_position + 1
  172. drawOneSegClick(arr,i,0xFFFFFF,n)
  173. break
  174. end
  175. end
  176. end
  177. for i=active_position,rY-2+active_position-1 do
  178. for j=1,#selected do
  179. if selected[j] == arr[i] then
  180. n = i - active_position + 1
  181. drawOneSegClick(arr,i,0x007DFF,n)
  182. break
  183. end
  184. end
  185. end
  186. oldSelected = selected
  187. end
  188.  
  189. function reDrawFiles(arr,p)
  190. if lastP == nil then lastP = 1 end
  191. if p == -1 and rY < #arr and lastP ~= rY-2+active_position-1 then
  192. gpu.copy(k+1,3,rX-k-2-1,rY-1-1,0,-1)
  193. drawOneLine(arr,rY-2+active_position-1,rY-1-1)
  194. lastP = rY-2+active_position-1
  195. elseif p == 1 and rY < #arr and lastP ~= active_position then
  196. gpu.copy(k+1,2,rX-k-2-1,rY-2-1,0,1)
  197. drawOneLine(arr,active_position,1)
  198. lastP = active_position
  199. end
  200. end
  201.  
  202. function drawFiles(arr)
  203. object.square(k+1,2,rX-k-2,rY-2,' ',0xffffff,0xFFFFFF)
  204. n = 0
  205. for i=active_position,rY-2+active_position-1 do
  206. if arr[i] ~= nil then
  207. n = n+1
  208. drawOneLine(arr,i,n)
  209. drawOneSegClick(arr,i,0xFFFFFF,n)
  210. end
  211. end
  212. end
  213.  
  214. function drawDiskScroll()
  215. object.drawBoard('disk',disks,disk_position)
  216. object.drawScroll('disk_scroll',disk_position,#disks,math.ceil(rY / 4)-2)
  217. end
  218.  
  219. function reDrawTextBox()
  220. gpu.setForeground(0xffffff)
  221. gpu.setBackground(0)
  222. object.delObject('textBox','FirstBox')
  223. object.add('textBox',1,2,k-2,3,getPath(paths),1,0x999999,0xffffff,0xFFDD00,0,0xffffff,0,'FirstBox')
  224. object.drawObject('textBox','FirstBox')
  225. end
  226.  
  227. function drawClickMenu(x,y,w,h,arr)
  228. gpu.setForeground(0)
  229. -- if x+w > rX and y + h > rY then
  230. for i=1,#arr do
  231. if math.fmod(i,2) == 0 then
  232. gpu.setBackground(0xdcdcdc)
  233. else
  234. gpu.setBackground(0x898989)
  235. end
  236. gpu.fill(x,y+i,w,1,' ')
  237. gpu.set(x+1,y+i,arr[i])
  238. end
  239. -- end
  240. setOld()
  241. end
  242.  
  243. function clickMenu(arr)
  244. local menuPositions = {}
  245. for i = 1,#arr do
  246. menuPositions[#menuPositions+1] = arr[i][1]
  247. end
  248. drawClickMenu()
  249. end
  250.  
  251. function mainDraw(path)
  252. object.init()
  253. rX , rY = gpu.getResolution()
  254. selected = {}
  255. k = math.ceil(rX/4)
  256. active_position = 1
  257. object.square(1,1,rX,1,' ',0xffffff,0x222222)
  258. object.add('button',rX-3+1,1,3,1,'[X]',1,0xffffff,0xFF0000,'Выход')
  259. object.add('button',2,1,3,1,' < ',1,0xffffff,0x0000FF,'Назад')
  260. object.add('scroll',k+1,2,rX-k,rY-1,' ',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',rX-1,2,2,rY-2,'vertical')
  261. object.drawAllObject()
  262. object.square(1,2,k,rY-1,' ',0xffffff,0xAAAAAA)
  263. object.square(k+1,2,rX-k,rY-1,' ',0xffffff,0xFFFFFF)
  264. f = getFiles(path)
  265. drawFiles(f)
  266. object.square(1,rY,rX,1,' ',0xffffff,0x222222)
  267. setOld()
  268. reDrawTextBox()
  269. object.add('board',1,5,k-2,math.ceil(rY / 4),0xffffff,0x505050,0xffffff,123123,true,1,'disk')
  270. object.add('scroll',1,5,k,math.ceil(rY / 4)-2,' ',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'disk_scroll',k-1,2,2,math.ceil(rY / 4),'vertical')
  271. drawDiskScroll()
  272. end
  273.  
  274. function getMyDisks(mode)
  275. local disks={}
  276. local fromAddress = filesystem.get(os.getenv("_")).address
  277. local candidates = {}
  278. for address in cmp.list("filesystem", true) do
  279. local dev = cmp.proxy(address)
  280. if --[[not dev.isReadOnly() and]] dev.address ~= computer.tmpAddress() then
  281. table.insert(candidates, dev)
  282. end
  283. end
  284. if mode==2 then
  285. for i = 1, #candidates do
  286. local label = candidates[i].getLabel()
  287. if label and label ~= 'raid' then
  288. label = label
  289. else
  290. label = candidates[i].address
  291. end
  292. disks[#disks+1]={label,candidates[i].address}
  293. end
  294. return disks
  295. elseif mode == 1 then
  296. for i = 1, #candidates do
  297. local label = candidates[i].getLabel()
  298. if label and label ~= 'raid' then
  299. label = label
  300. else
  301. label = candidates[i].address
  302. end
  303. disks[#disks+1]=label
  304. end
  305. return disks
  306. end
  307. end
  308.  
  309. function startDisk()
  310. local disks=getMyDisks(2)
  311. arrMyDiskAddress=filesystem.get(os.getenv("_"))
  312. for i=1,#disks do
  313. if arrMyDiskAddress.address:sub(1,8) == disks[i][1]:sub(1,8) or arrMyDiskAddress.getLabel():sub(1,8) == disks[i][1]:sub(1,8) then
  314. posDisk=i
  315. break
  316. end
  317. end
  318. return 'mnt/'..tostring(arrMyDiskAddress.address:sub(1,3))..'/' , posDisk
  319. end
  320.  
  321. object.init()
  322. --
  323. pa , disk_position = startDisk()
  324. disks = getMyDisks(1)
  325. dsk = getMyDisks(2)
  326. paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
  327. path = getPath(paths)
  328. --
  329. mainDraw('..')
  330. --
  331. fileBuffer = {}
  332. function reDrawAllFiles()
  333. active_position = 1
  334. dsk = getMyDisks(2)
  335. paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
  336. reDrawTextBox()
  337. f = getFiles(getPath(paths))
  338. selected = {}
  339. drawFiles(f)
  340. end
  341.  
  342. function inBuffer()
  343. local arr = {}
  344. for i=1,#selected do
  345. arr[#arr+1]=tostring(getPath(paths)..tostring(selected[i]))
  346. end
  347. return arr
  348. end
  349.  
  350. function paste()
  351. if fileBuffer[1] ~= nil and fileBuffer[1] == 'cut' then
  352. for i=1,#fileBuffer[2] do
  353. local p = filesystem.segments(fileBuffer[2][i])
  354. shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
  355. shell.execute('del '..fileBuffer[2][i])
  356. end
  357. fileBuffer = {}
  358. elseif fileBuffer[1] ~= nil then
  359. for i=1,#fileBuffer[2] do
  360. local p = filesystem.segments(fileBuffer[2][i])
  361. shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
  362. end
  363. end
  364. end
  365.  
  366. function op()
  367. paths[#paths+1] = selected[1]
  368. f = getFiles(getPath(paths))
  369. drawFiles(f)
  370. reDrawTextBox()
  371. active_position = 1
  372. shiftSelected = nil
  373. end
  374.  
  375. function clock()
  376. gpu.setBackground(0x222222)
  377. gpu.setForeground(0xffffff)
  378. local time = os.date('%H:%M:%S',getTime())
  379. gpu.set(rX-unicode.len(time)-1,rY,time)
  380. end
  381.  
  382. while true do
  383. e = getEvent(0.0001,'touch','scroll','component_added','component_removed','key_up')
  384. if e[1] ~= nil then
  385. if e[1] ~= 'component_added' and e[1] ~= 'component_removed' and e[1] ~= 'key_up' then
  386. arr=object.getPressedObject(e[3],e[4])
  387. end
  388. if arr ~= false and arr[1] == 'button' then
  389. if arr[2] == 'Выход' then
  390. setOld()
  391. term.clear()
  392. os.exit()
  393. elseif arr ~= false and arr[2] == 'Назад' then
  394. if #paths > 1 then
  395. table.remove(paths)
  396. f = getFiles(getPath(paths))
  397. active_position = 1
  398. selected = {}
  399. drawFiles(f)
  400. reDrawTextBox()
  401. end
  402. end
  403. elseif arr ~= false and arr[1] == 'textBox' and e[1] == 'touch' and arr[2] == 'FirstBox' then
  404. npa = object.textBox('FirstBox',false,'*')
  405. if filesystem.exists(npa) then
  406. paths = {}
  407. for val,val2 in pairs(filesystem.segments(npa)) do
  408. paths[#paths+1] = val2..'/'
  409. end
  410. reDrawTextBox()
  411. f = getFiles(getPath(paths))
  412. drawFiles(f)
  413. end
  414. elseif arr ~= false and arr[1] == 'scroll' and e[1] == 'scroll' then
  415. if arr~= false and arr[2]=='FirstScroll' then
  416. active_position = active_position -e[5]
  417. if active_position > #f-rY+3 then active_position = #f-rY+3 end
  418. if active_position < 1 then active_position = 1 end
  419. object.drawScroll('FirstScroll',active_position,#f,rY-2)
  420. reDrawFiles(f,e[5])
  421. elseif arr ~=false and arr[2]=='disk_scroll' then
  422. disk_position = disk_position -e[5]
  423. if disk_position > #disks then disk_position = #disks end
  424. if disk_position < 1 then disk_position = 1 end
  425. drawDiskScroll()
  426. shiftSelected = nil
  427. reDrawAllFiles()
  428. end
  429. elseif e[1] == 'key_up' then
  430. if e[4] == 46 and keyboard.isControlDown() then
  431. fileBuffer = {'copy',inBuffer()}
  432. elseif e[4] == 47 and keyboard.isControlDown() then
  433. paste()
  434. f = getFiles(getPath(paths))
  435. selected = {}
  436. drawFiles(f)
  437. elseif e[4] == 45 and keyboard.isControlDown() then
  438. fileBuffer = {'cut',inBuffer()}
  439. elseif e[4] == 30 and keyboard.isControlDown() then
  440. selected=f
  441. drawClick(f)
  442. elseif e[4] == 211 then
  443. for i=1,#selected do
  444. shell.execute('rm '..getPath(paths)..selected[i])
  445. end
  446. selected = {}
  447. active_position = 1
  448. f = getFiles(getPath(paths))
  449. drawFiles(f)
  450. end
  451. elseif e[1] == 'component_added' or e[1] == 'component_removed' and e[3] == 'filesystem' then
  452. dsks = getMyDisks(1)
  453. if tostring(unicode.sub(e[2],1,3)..'/') == paths[2] and e[1] == 'component_removed' then
  454. disk_position = 1
  455. drawDiskScroll()
  456. reDrawAllFiles()
  457. elseif #disks > #dsks then
  458. if disk_position > #dsks and disks[disk_position] == '' then
  459.  
  460. end
  461. end
  462. disks = getMyDisks(1)
  463. drawDiskScroll()
  464. shiftSelected = nil
  465. elseif arr ~= false and e[1] == 'touch' and arr[2] == 'FirstScroll' then
  466. if keyboard.isControlDown() and e[5] == 0 then
  467. fi = finder(selected,f[active_position+(e[4]-2)])
  468. if fi[1] == false then
  469. selected[#selected+1] = f[active_position+(e[4]-2)]
  470. drawClick(f)
  471. end
  472. elseif arr ~= false and keyboard.isControlDown() == false and keyboard.isShiftDown() == false then
  473. selected = {f[active_position+(e[4]-2)]}
  474. shiftSelected = active_position+(e[4]-2)
  475. drawClick(f)
  476. elseif arr~= false and keyboard.isShiftDown() then
  477. if shiftSelected ~= nil then
  478. local function addSelected(j,k)
  479. selected = {}
  480. for i=j,k do
  481. selected[#selected+1] = f[i]
  482. end
  483. drawClick(f)
  484. return selected
  485. end
  486. if shiftSelected > active_position+(e[4]-2) then
  487. selected = addSelected(active_position+(e[4]-2),shiftSelected)
  488. elseif shiftSelected < active_position+(e[4]-2) then
  489. selected = addSelected(shiftSelected,active_position+(e[4]-2))
  490. end
  491. end
  492. end
  493. fi = finder(selected,f[active_position+(e[4]-2)])
  494. if fi[1] and #selected == 1 and selected[#selected] == lastClick then
  495. if unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) == '/' then
  496. op()
  497. elseif e[5] == 0 and unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) ~= '/' then
  498. setOld()
  499. term.clear()
  500. shell.execute(getColorFile(selected[1],3,'')..' '..getPath(paths)..selected[1])
  501. event.pull()
  502. mainDraw(getPath(paths))
  503. elseif e[5] == 1 and #selected == 1 and unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) ~= '/' and lastClick == selected[#selected] then
  504. setOld()
  505. --shell.execute('edit '..getPath(paths)..selected[1])
  506. drawClickMenu(e[3],e[4],20,12,{'Открыть','Открыть с помощью..','Редактировать','------------------','Переименовать','Копировать','Вырезать','Удалить','Создать...','------------------','Свойства'})
  507. mainDraw(getPath(paths))
  508. end
  509. end
  510. lastClick = f[active_position+(e[4]-2)]
  511. end
  512. else
  513. clock()
  514. end
  515. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement