Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cmp = require('component')
- local event = require('event')
- local serialization = require('serialization')
- local gpu=cmp.gpu
- local term =require('term')
- local computer = require('computer')
- local filesystem=require('filesystem')
- local unicode = require('unicode')
- local keyboard = require("keyboard")
- local shell = require("shell")
- local object = require('object')
- ----
- local __name = 'server'
- local __timezone = 0
- local __correct = 0
- local tz = __timezone + __correct
- local t_correction = tz * 3600
- ----
- local oldBack=gpu.getBackground()
- local oldFore=gpu.getForeground()
- local rX , rY = gpu.getResolution()
- ----
- function setOld()
- gpu.setBackground(oldBack)
- gpu.setForeground(oldFore)
- end
- function getTime()
- local file = io.open('/tmp/'..__name..'.dt','w')
- file:write('')
- file:close()
- local lastmod = tonumber(string.sub(filesystem.lastModified('/tmp/'..__name..'.dt'),1,-4)) + t_correction
- return lastmod
- end
- local function getEvent(...)
- args = {...}
- evnt = nil
- if type(args[1]) == 'number' then
- time = args[1]
- end
- evtrue = true
- local function getRawEvent()
- return {event.pull()}
- end
- while evtrue do
- -- s , evnt = pcall(getRawEvent)
- evnt = {event.pull(time)}
- -- if s ~= false then
- for i=1,#args do
- if evnt[1] == args[i] and time == nil then
- evtrue = false
- break
- elseif time ~= nil then
- evtrue = false
- break
- end
- end
- --end
- end
- return evnt
- end
- ----
- function getPath(arr)
- local path = ''
- for i=1,#arr do
- path = path .. arr[i]
- end
- return path
- end
- local function saveTbl(tbl, fl)
- file = io.open(fl, 'w')
- file:write(serialization.serialize(tbl))
- file:close()
- end
- local function loadTbl(fl)
- file = io.open(fl, 'r')
- if file == nil then
- file = io.open(fl, 'w')
- file:write('{}')
- return {} else
- return serialization.unserialize(file:read('*a'))
- end
- file:close()
- end
- ex = loadTbl('.expansion')
- function getFiles(path)
- arr={{},{}} arr2 = {}
- 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
- for i=1,#arr do
- for j=1,#arr[i] do
- arr2[#arr2+1]=arr[i][j]
- end
- end
- return arr2
- end
- function rec(path)
- local colors = {'0x0AA00','0xF06306','0xAA0000'}
- if filesystem.isDirectory(path) then
- str = {'',0xFFFFFF}
- else
- N = filesystem.size(path)
- if unicode.len(tostring(N)) <= 3 then
- str = {tostring(N)..' B',colors[1]}
- elseif unicode.len(tostring(N)) <= 5 then
- str = {tostring(round(N / 1024 , 2)) .. ' KB',colors[2]}
- else
- str = {tostring(round(N / 1024 / 1024,2)) .. ' MB',colors[3]}
- end
- end
- return str
- end
- function round(num, decimalPlaces)
- local mult = 10 ^ (decimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function getColorFile(str,n,o)
- local out = o
- for i=1,#ex do
- if unicode.sub(str,unicode.len(str)-unicode.len(ex[i][1])+1,unicode.len(str)) == ex[i][1] then
- out = ex[i][n]
- end
- end
- return out
- end
- function finder(arr,str)
- local out = {false}
- for i = 1,#arr do
- if arr[i] == str then
- out = {true,i}
- break
- end
- end
- return out
- end
- function drawOneLine(arr,i,n)
- fi = finder(selected,arr[i])
- if fi[1] then
- drawOneSegClick(arr,active_position,0x007DFF,1)
- else
- drawOneSegClick(arr,i,0xFFFFFF,n)
- end
- end
- function drawOneSegClick(arr,i,color,n)
- cf = rec(getPath(paths)..arr[i])
- l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
- gpu.setBackground(color)
- gpu.fill(k+1,1+n,l,1,' ')
- gpu.setForeground(getColorFile(arr[i],2,0))
- object.text(math.ceil(rX/4)+1,1+n,l,1,tostring(arr[i]),'first','')
- gpu.setForeground(tonumber(cf[2]))
- gpu.set(rX-2- unicode.len(tostring(cf[1])),1+n,tostring(cf[1]))
- end
- function drawClick(arr)
- if oldSelected == nil then oldSelected = selected end
- for i=active_position,rY-2+active_position-1 do
- for j=1,#oldSelected do
- if oldSelected[j] == arr[i] and oldSelected[j] ~= selected[j] then
- n = i - active_position + 1
- drawOneSegClick(arr,i,0xFFFFFF,n)
- break
- end
- end
- end
- for i=active_position,rY-2+active_position-1 do
- for j=1,#selected do
- if selected[j] == arr[i] then
- n = i - active_position + 1
- drawOneSegClick(arr,i,0x007DFF,n)
- break
- end
- end
- end
- oldSelected = selected
- end
- function reDrawFiles(arr,p)
- if lastP == nil then lastP = 1 end
- if p == -1 and rY < #arr and lastP ~= rY-2+active_position-1 then
- gpu.copy(k+1,3,rX-k-2-1,rY-1-1,0,-1)
- drawOneLine(arr,rY-2+active_position-1,rY-1-1)
- lastP = rY-2+active_position-1
- elseif p == 1 and rY < #arr and lastP ~= active_position then
- gpu.copy(k+1,2,rX-k-2-1,rY-2-1,0,1)
- drawOneLine(arr,active_position,1)
- lastP = active_position
- end
- end
- function drawFiles(arr)
- object.square(k+1,2,rX-k-2,rY-2,' ',0xffffff,0xFFFFFF)
- n = 0
- for i=active_position,rY-2+active_position-1 do
- if arr[i] ~= nil then
- n = n+1
- drawOneLine(arr,i,n)
- drawOneSegClick(arr,i,0xFFFFFF,n)
- end
- end
- end
- function drawDiskScroll()
- object.drawBoard('disk',disks,disk_position)
- object.drawScroll('disk_scroll',disk_position,#disks,math.ceil(rY / 4)-2)
- end
- function reDrawTextBox()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0)
- object.delObject('textBox','FirstBox')
- object.add('textBox',1,2,k-2,3,getPath(paths),1,0x999999,0xffffff,0xFFDD00,0,0xffffff,0,'FirstBox')
- object.drawObject('textBox','FirstBox')
- end
- function drawClickMenu(x,y,w,h,arr)
- gpu.setForeground(0)
- -- if x+w > rX and y + h > rY then
- for i=1,#arr do
- if math.fmod(i,2) == 0 then
- gpu.setBackground(0xdcdcdc)
- else
- gpu.setBackground(0x898989)
- end
- gpu.fill(x,y+i,w,1,' ')
- gpu.set(x+1,y+i,arr[i])
- end
- -- end
- setOld()
- end
- function clickMenu(arr)
- local menuPositions = {}
- for i = 1,#arr do
- menuPositions[#menuPositions+1] = arr[i][1]
- end
- drawClickMenu()
- end
- function mainDraw(path)
- object.init()
- rX , rY = gpu.getResolution()
- selected = {}
- k = math.ceil(rX/4)
- active_position = 1
- object.square(1,1,rX,1,' ',0xffffff,0x222222)
- object.add('button',rX-3+1,1,3,1,'[X]',1,0xffffff,0xFF0000,'Выход')
- object.add('button',2,1,3,1,' < ',1,0xffffff,0x0000FF,'Назад')
- object.add('scroll',k+1,2,rX-k,rY-1,' ',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',rX-1,2,2,rY-2,'vertical')
- object.drawAllObject()
- object.square(1,2,k,rY-1,' ',0xffffff,0xAAAAAA)
- object.square(k+1,2,rX-k,rY-1,' ',0xffffff,0xFFFFFF)
- f = getFiles(path)
- drawFiles(f)
- object.square(1,rY,rX,1,' ',0xffffff,0x222222)
- setOld()
- reDrawTextBox()
- object.add('board',1,5,k-2,math.ceil(rY / 4),0xffffff,0x505050,0xffffff,123123,true,1,'disk')
- 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')
- drawDiskScroll()
- end
- function getMyDisks(mode)
- local disks={}
- local fromAddress = filesystem.get(os.getenv("_")).address
- local candidates = {}
- for address in cmp.list("filesystem", true) do
- local dev = cmp.proxy(address)
- if --[[not dev.isReadOnly() and]] dev.address ~= computer.tmpAddress() then
- table.insert(candidates, dev)
- end
- end
- if mode==2 then
- for i = 1, #candidates do
- local label = candidates[i].getLabel()
- if label and label ~= 'raid' then
- label = label
- else
- label = candidates[i].address
- end
- disks[#disks+1]={label,candidates[i].address}
- end
- return disks
- elseif mode == 1 then
- for i = 1, #candidates do
- local label = candidates[i].getLabel()
- if label and label ~= 'raid' then
- label = label
- else
- label = candidates[i].address
- end
- disks[#disks+1]=label
- end
- return disks
- end
- end
- function startDisk()
- local disks=getMyDisks(2)
- arrMyDiskAddress=filesystem.get(os.getenv("_"))
- for i=1,#disks do
- 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
- posDisk=i
- break
- end
- end
- return 'mnt/'..tostring(arrMyDiskAddress.address:sub(1,3))..'/' , posDisk
- end
- object.init()
- --
- pa , disk_position = startDisk()
- disks = getMyDisks(1)
- dsk = getMyDisks(2)
- paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
- path = getPath(paths)
- --
- mainDraw('..')
- --
- fileBuffer = {}
- function reDrawAllFiles()
- active_position = 1
- dsk = getMyDisks(2)
- paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
- reDrawTextBox()
- f = getFiles(getPath(paths))
- selected = {}
- drawFiles(f)
- end
- function inBuffer()
- local arr = {}
- for i=1,#selected do
- arr[#arr+1]=tostring(getPath(paths)..tostring(selected[i]))
- end
- return arr
- end
- function paste()
- if fileBuffer[1] ~= nil and fileBuffer[1] == 'cut' then
- for i=1,#fileBuffer[2] do
- local p = filesystem.segments(fileBuffer[2][i])
- shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
- shell.execute('del '..fileBuffer[2][i])
- end
- fileBuffer = {}
- elseif fileBuffer[1] ~= nil then
- for i=1,#fileBuffer[2] do
- local p = filesystem.segments(fileBuffer[2][i])
- shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
- end
- end
- end
- function op()
- paths[#paths+1] = selected[1]
- f = getFiles(getPath(paths))
- drawFiles(f)
- reDrawTextBox()
- active_position = 1
- shiftSelected = nil
- end
- function clock()
- gpu.setBackground(0x222222)
- gpu.setForeground(0xffffff)
- local time = os.date('%H:%M:%S',getTime())
- gpu.set(rX-unicode.len(time)-1,rY,time)
- end
- while true do
- e = getEvent(0.0001,'touch','scroll','component_added','component_removed','key_up')
- if e[1] ~= nil then
- if e[1] ~= 'component_added' and e[1] ~= 'component_removed' and e[1] ~= 'key_up' then
- arr=object.getPressedObject(e[3],e[4])
- end
- if arr ~= false and arr[1] == 'button' then
- if arr[2] == 'Выход' then
- setOld()
- term.clear()
- os.exit()
- elseif arr ~= false and arr[2] == 'Назад' then
- if #paths > 1 then
- table.remove(paths)
- f = getFiles(getPath(paths))
- active_position = 1
- selected = {}
- drawFiles(f)
- reDrawTextBox()
- end
- end
- elseif arr ~= false and arr[1] == 'textBox' and e[1] == 'touch' and arr[2] == 'FirstBox' then
- npa = object.textBox('FirstBox',false,'*')
- if filesystem.exists(npa) then
- paths = {}
- for val,val2 in pairs(filesystem.segments(npa)) do
- paths[#paths+1] = val2..'/'
- end
- reDrawTextBox()
- f = getFiles(getPath(paths))
- drawFiles(f)
- end
- elseif arr ~= false and arr[1] == 'scroll' and e[1] == 'scroll' then
- if arr~= false and arr[2]=='FirstScroll' then
- active_position = active_position -e[5]
- if active_position > #f-rY+3 then active_position = #f-rY+3 end
- if active_position < 1 then active_position = 1 end
- object.drawScroll('FirstScroll',active_position,#f,rY-2)
- reDrawFiles(f,e[5])
- elseif arr ~=false and arr[2]=='disk_scroll' then
- disk_position = disk_position -e[5]
- if disk_position > #disks then disk_position = #disks end
- if disk_position < 1 then disk_position = 1 end
- drawDiskScroll()
- shiftSelected = nil
- reDrawAllFiles()
- end
- elseif e[1] == 'key_up' then
- if e[4] == 46 and keyboard.isControlDown() then
- fileBuffer = {'copy',inBuffer()}
- elseif e[4] == 47 and keyboard.isControlDown() then
- paste()
- f = getFiles(getPath(paths))
- selected = {}
- drawFiles(f)
- elseif e[4] == 45 and keyboard.isControlDown() then
- fileBuffer = {'cut',inBuffer()}
- elseif e[4] == 30 and keyboard.isControlDown() then
- selected=f
- drawClick(f)
- elseif e[4] == 211 then
- for i=1,#selected do
- shell.execute('rm '..getPath(paths)..selected[i])
- end
- selected = {}
- active_position = 1
- f = getFiles(getPath(paths))
- drawFiles(f)
- end
- elseif e[1] == 'component_added' or e[1] == 'component_removed' and e[3] == 'filesystem' then
- dsks = getMyDisks(1)
- if tostring(unicode.sub(e[2],1,3)..'/') == paths[2] and e[1] == 'component_removed' then
- disk_position = 1
- drawDiskScroll()
- reDrawAllFiles()
- elseif #disks > #dsks then
- if disk_position > #dsks and disks[disk_position] == '' then
- end
- end
- disks = getMyDisks(1)
- drawDiskScroll()
- shiftSelected = nil
- elseif arr ~= false and e[1] == 'touch' and arr[2] == 'FirstScroll' then
- if keyboard.isControlDown() and e[5] == 0 then
- fi = finder(selected,f[active_position+(e[4]-2)])
- if fi[1] == false then
- selected[#selected+1] = f[active_position+(e[4]-2)]
- drawClick(f)
- end
- elseif arr ~= false and keyboard.isControlDown() == false and keyboard.isShiftDown() == false then
- selected = {f[active_position+(e[4]-2)]}
- shiftSelected = active_position+(e[4]-2)
- drawClick(f)
- elseif arr~= false and keyboard.isShiftDown() then
- if shiftSelected ~= nil then
- local function addSelected(j,k)
- selected = {}
- for i=j,k do
- selected[#selected+1] = f[i]
- end
- drawClick(f)
- return selected
- end
- if shiftSelected > active_position+(e[4]-2) then
- selected = addSelected(active_position+(e[4]-2),shiftSelected)
- elseif shiftSelected < active_position+(e[4]-2) then
- selected = addSelected(shiftSelected,active_position+(e[4]-2))
- end
- end
- end
- fi = finder(selected,f[active_position+(e[4]-2)])
- if fi[1] and #selected == 1 and selected[#selected] == lastClick then
- if unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) == '/' then
- op()
- elseif e[5] == 0 and unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) ~= '/' then
- setOld()
- term.clear()
- shell.execute(getColorFile(selected[1],3,'')..' '..getPath(paths)..selected[1])
- event.pull()
- mainDraw(getPath(paths))
- 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
- setOld()
- --shell.execute('edit '..getPath(paths)..selected[1])
- drawClickMenu(e[3],e[4],20,12,{'Открыть','Открыть с помощью..','Редактировать','------------------','Переименовать','Копировать','Вырезать','Удалить','Создать...','------------------','Свойства'})
- mainDraw(getPath(paths))
- end
- end
- lastClick = f[active_position+(e[4]-2)]
- end
- else
- clock()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement