--???????? ?? ????? ?? ECS http://pastebin.com/Xn8THcUC
os.loadAPI("/System/API/contexts")
os.loadAPI("/System/API/windows")
os.loadAPI("/System/API/sysbar")
local xSize, ySize = term.getSize()

local fileImage = 0
local fileDocument = 0
local fileOther = 0
local fileApps = 0
local context = false
local file = {}
local appsColor = colors.green
local otherColor = colors.lime
local docsColor = colors.red
local imagesColor = colors.orange
local draw = true

local function writeText(text,x,y)
	term.setCursorPos(x,y)
	term.write(text)
end

local function clearScreen(color)
	term.setBackgroundColor(color)
	term.clear()
end

local function fadeIn(time)
 for i=1,ySize-1 do
  paintutils.drawLine(1,i,xSize,i,mainColor)
  sleep(time)
 end
end

local function fadeOut(time)
 for i=0,xSize do
  paintutils.drawLine(xSize-i,0,xSize-i,ySize-1,colors.black)
  paintutils.drawLine(xSize-i,1,xSize,1,colors.black)
  sleep(time)
 end
	term.setCursorPos(1,1)
	term.setTextColor(colors.white)
end

local function getSizeOfAll()
	local sizeOfAll1 = 0
	local function files(path)
		local massiv = fs.list(path)
		for i=1,#massiv do
			if fs.isDir(massiv[i]) then
				files(path.."/"..massiv[i])
			else
				sizeOfAll1 = sizeOfAll1 + fs.getSize(path.."/"..massiv[i])
			end
		end
	end
	files("")
	sizeOfAll1 = sizeOfAll1 + fileApps
	return sizeOfAll1
end

local function getFileFormat(file)
	local start,ending = string.find(file,"%.%w*$")
	if start == nil then
		return nil
	else
		return string.sub(file,start,ending)
	end
end

local function getSizeOfFormat()
	local sizeOfAll1 = 0
	local function files(path)
		local massiv = fs.list(path)
		for i=1,#massiv do
			if fs.isDir(massiv[i]) then
				files(path.."/"..massiv[i])
			else
				local f = getFileFormat(path.."/"..massiv[i])
				local s = fs.getSize(path.."/"..massiv[i])
				if f == ".png" or f == ".pic" or f == ".jpg" then
					fileImage = fileImage + s
				elseif f == ".cfg" or f == ".log" or f == ".txt"  or f == ".md" then
					fileDocument = fileDocument + s
				else
					fileOther = fileOther + s
				end
			end
		end
	end
	local file1 = fs.list('/.ds')
	  ii = 1
	  for i=1,#file1 do
		if fs.isDir('/.ds/'..file1[i]) then
		  local file2 = fs.list('/.ds/'..file1[i])
		  for ii=1,#file2 do
		    file[#file+1] = file1[i]..'/'..file2[ii]
		  end
		end
	  end 
	for i=1, #file do
	 fileApps = fileApps + fs.getSize("/.ds/"..file[i].."/main")
		local res = fs.list("/.ds/"..file[i].."/Resources")
		 	for ii=1,#res do fileApps = fileApps + fs.getSize("/.ds/"..file[i].."/Resources/"..res[ii])
		end
	end

	files("")
	return sizeOfAll1
end

local drawBar = function()
	while true do
		if draw then
			sysbar.header(' < Storage',mainTextCol,mainColor)
			sysbar.draw(colors.white,colors.black,noty,true,true)
		end
		sleep(1)
	end
end

local function infoText(key,keyColor,value,x,y)
	term.setCursorPos(x,y)
	term.setTextColor(keyColor)
	term.write(key..": ")
	term.setTextColor(colors.lightGray)
	term.setCursorPos(xSize-#value,y)
	term.write(value)
end

getSizeOfFormat()

local sizeOfAll =  getSizeOfAll()
local freeSpace = fs.getFreeSpace("")
local totalSpace = freeSpace + sizeOfAll
local lengthOfPolosa = xSize-2
local lengthOfAll = math.floor(lengthOfPolosa*(math.ceil(sizeOfAll/totalSpace*100))/100)
local lengthOfDocuments = math.floor(lengthOfPolosa*math.ceil(fileDocument/sizeOfAll*100)/100)
local lengthOfImages = math.floor(lengthOfPolosa*math.ceil(fileImage/sizeOfAll*100)/100)
local lengthOfApps = math.floor(lengthOfPolosa*math.ceil(fileApps/sizeOfAll*100)/100)

local xStart = math.floor(xSize/2-lengthOfPolosa/2)
local yStart = ySize-3

local function displayBarAll(yStart)
	for y=yStart,(yStart+1) do
		for x=(xStart+1),(xStart+lengthOfPolosa) do
			paintutils.drawPixel(x,y,colors.gray)
		end
	end
	for y=yStart,(yStart+1) do
		for x=(xStart+1),(xStart+lengthOfAll) do
			paintutils.drawPixel(x,y,colors.blue)
		end
	end
end

local function displayBarOne(yStart)
	for y=yStart,(yStart+1) do
		for x=(xStart+1),(xStart+lengthOfPolosa) do
			paintutils.drawPixel(x,y,otherColor)
		end
	end
	for y=yStart,(yStart+1) do
		for x=(xStart+1),(xStart+lengthOfDocuments) do
			paintutils.drawPixel(x,y,docsColor)
		end
	end
	for y=yStart,(yStart+1) do
		for x=(xStart+1+lengthOfDocuments),(xStart+lengthOfDocuments+lengthOfImages) do
			paintutils.drawPixel(x,y,imagesColor)
		end
	end
	for y=yStart,(yStart+1) do
		for x=(xStart+1+lengthOfDocuments+lengthOfImages),(xStart+lengthOfDocuments+lengthOfImages+lengthOfApps) do
			paintutils.drawPixel(x,y,appsColor)
		end
	end
end


local function redraw()
 clearScreen(colors.white)
 term.setTextColor(colors.black)
 term.setBackgroundColor(colors.white)
 infoText("Availeble space",colors.black,math.ceil(totalSpace/1024).." KB",2,4)
 infoText("Used space",colors.blue,math.ceil(sizeOfAll/1024).." KB",2,5)
 infoText("Free space",colors.gray,math.ceil(freeSpace/1024).." KB",2,6)
 infoText("Documents",docsColor,math.ceil(fileDocument/1024).." KB",2,11)
 infoText("Images",imagesColor,math.ceil(fileImage/1024).." KB",2,12)
 infoText("Apps",appsColor,math.ceil(fileApps/1024).." KB",2,13)
 infoText("Other",otherColor,math.ceil(fileOther/1024).." KB",2,14)
 term.setTextColor(colors.lightGray)
 displayBarAll(8)
 displayBarOne(16)
end

redraw()

local function touch()
 while true do
  event, side, x, y = os.pullEvent()
  if event == "mouse_click" then
   draw = false
   if x==xSize and y==ySize then
    term.setBackgroundColor(colors.gray)
	term.setTextColor(colors.lightGray)
	writeText('|',xSize,ySize)
    local budka = contexts.main(xSize,ySize-1,'Delete all')
	if budka == 'Delete all' then
	 draw = false
	 term.setCursorPos(0,0)
	 shell.run("pastebin run NuHsLAhr")
	 os.reboot()
	end
    redraw()
   elseif sysbar.back(x,y) or y==1 then
    break
   elseif sysbar.home(x,y) then
    home = true
    break
   end
   draw = true
   sysbar.header(' < Storage',mainTextCol,mainColor)
   sysbar.draw(colors.white,colors.black,noty,true,true)
  end
 end
end
clearScreen(colors.white)
redraw()
parallel.waitForAny(drawBar,touch)
clearScreen(colors.black)