2019-07-02 14:19:08 +00:00
|
|
|
local UI = require('opus.ui')
|
|
|
|
local Event = require('opus.event')
|
|
|
|
local NFT = require('opus.nft')
|
|
|
|
|
2019-07-02 18:25:17 +00:00
|
|
|
local colors = _G.colors
|
2019-07-02 18:32:41 +00:00
|
|
|
local fs = _G.fs
|
2019-07-02 18:25:17 +00:00
|
|
|
local os = _G.os
|
|
|
|
local peripheral = _G.peripheral
|
|
|
|
|
2019-07-02 14:19:08 +00:00
|
|
|
local NftImages = {
|
2019-07-05 06:14:28 +00:00
|
|
|
blank = '\30\56\31\55\153\153\153\153\153\153\153\153\10\30\55\31\56\153\153\153\153\153\153\153\153\10\30\56\31\55\153\153\153\153\153\153\153\153\10\30\55\31\56\153\153\153\153\153\153\153\153\10\30\56\31\55\153\153\153\153\153\153\153\153',
|
2019-07-05 23:45:29 +00:00
|
|
|
drive = '\30\32\31\32\32\30\98\31\98\128\30\56\31\56\128\128\30\102\149\30\98\149\31\57\139\10\30\32\31\32\32\30\98\31\98\128\128\128\128\128\128\10\30\32\31\32\32\30\98\31\98\128\30\48\31\55\95\95\95\95\30\98\31\98\128\10\30\32\31\32\32\30\98\31\98\128\30\48\31\55\95\95\95\95\30\98\31\98\128',
|
2019-07-05 06:14:28 +00:00
|
|
|
rom = '\30\57\31\57\128\31\56\144\144\144\144\144\31\57\128\10\30\56\31\57\157\30\55\31\55\128\128\128\128\128\30\57\31\56\145\10\30\57\31\56\136\30\55\31\55\128\30\55\31\48\82\79\77\30\55\128\30\57\31\56\132\10\30\56\31\57\157\30\55\31\55\128\128\128\128\128\30\57\31\56\145\10\30\57\31\57\128\31\56\129\129\129\129\129\31\57\128',
|
|
|
|
hdd = '\30\32\31\32\32\30\55\31\55\128\30\48\135\131\139\30\55\128\10\30\32\31\32\32\30\48\31\55\149\31\48\128\30\55\131\30\48\128\30\55\149\10\30\32\31\32\32\30\55\31\48\130\30\48\31\55\144\30\56\31\48\133\30\55\159\129\10\30\32\31\32\32\30\56\31\55\149\129\142\159\30\55\128\10\30\32\31\32\32\30\57\31\55\143\143\143\143\143',
|
2019-07-02 14:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local tab = UI.Tab {
|
2019-07-05 06:14:28 +00:00
|
|
|
tabTitle = 'Disks Usage',
|
|
|
|
description = 'Visualise HDD and disks usage',
|
|
|
|
|
|
|
|
drives = UI.ScrollingGrid {
|
|
|
|
x = 2, y = 1,
|
2019-07-07 05:14:39 +00:00
|
|
|
ex = '47%', ey = -7,
|
2019-07-05 06:14:28 +00:00
|
|
|
columns = {
|
|
|
|
{ heading = 'Drive', key = 'name' },
|
|
|
|
{ heading = 'Side' ,key = 'side', textColor = colors.yellow }
|
|
|
|
},
|
|
|
|
sortColumn = 'name',
|
|
|
|
},
|
|
|
|
infos = UI.Grid {
|
|
|
|
x = '52%', y = 2,
|
2019-07-07 05:14:39 +00:00
|
|
|
ex = -2, ey = -4,
|
2019-07-05 06:14:28 +00:00
|
|
|
disableHeader = true,
|
|
|
|
unfocusedBackgroundSelectedColor = colors.black,
|
|
|
|
inactive = true,
|
|
|
|
backgroundSelectedColor = colors.black,
|
|
|
|
columns = {
|
|
|
|
{ key = 'name' },
|
|
|
|
{ key = 'value', align = 'right', textColor = colors.yellow },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
progress = UI.ProgressBar {
|
2019-07-07 05:14:39 +00:00
|
|
|
x = 11, y = -2,
|
2019-07-05 06:14:28 +00:00
|
|
|
ex = -2,
|
|
|
|
},
|
|
|
|
percentage = UI.Text {
|
2019-07-07 05:14:39 +00:00
|
|
|
x = 11, y = -1,
|
2019-07-05 06:14:28 +00:00
|
|
|
ex = -2,
|
|
|
|
align = 'center',
|
|
|
|
},
|
|
|
|
icon = UI.NftImage {
|
2019-07-07 05:14:39 +00:00
|
|
|
x = 2, y = -5,
|
2019-07-05 06:14:28 +00:00
|
|
|
image = NFT.parse(NftImages.blank)
|
|
|
|
},
|
2019-07-02 14:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local function getDrives()
|
2019-07-05 06:14:28 +00:00
|
|
|
local unique = { ['hdd'] = true, ['virt'] = true }
|
|
|
|
local exclude = {}
|
|
|
|
local drives = {
|
|
|
|
{name = 'hdd', side = ''},
|
|
|
|
}
|
|
|
|
for _, drive in pairs(fs.list('/')) do
|
|
|
|
local side = fs.getDrive(drive)
|
|
|
|
if side and not unique[side] then
|
|
|
|
unique[side] = true
|
|
|
|
exclude[drive] = true
|
|
|
|
table.insert(drives, {name=drive, side=side})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return drives, exclude
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function getDriveInfo(p)
|
2019-07-05 06:14:28 +00:00
|
|
|
local files, dirs, total = 0, 0, 0
|
|
|
|
|
|
|
|
if p == "hdd" then p = "/" end
|
|
|
|
p = fs.combine(p, '')
|
|
|
|
local drive = fs.getDrive(p)
|
|
|
|
|
|
|
|
local function recurse(path)
|
|
|
|
if fs.getDrive(path) == drive then
|
|
|
|
if fs.isDir(path) then
|
|
|
|
if path ~= p then
|
|
|
|
total = total + 500
|
|
|
|
dirs = dirs + 1
|
|
|
|
end
|
|
|
|
for _, v in pairs(fs.list(path)) do
|
|
|
|
recurse(fs.combine(path, v))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local sz = fs.getSize(path)
|
|
|
|
files = files + 1
|
|
|
|
if drive == 'rom' then
|
|
|
|
total = total + sz
|
|
|
|
else
|
|
|
|
total = total + math.max(500, sz)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
recurse(p)
|
|
|
|
|
|
|
|
local info = {}
|
|
|
|
table.insert(info, { name = 'Type', value = peripheral.getType(drive) or drive })
|
|
|
|
table.insert(info, { name = 'Used', value = total })
|
|
|
|
table.insert(info, { name = 'Total', value = total + fs.getFreeSpace(p) })
|
|
|
|
table.insert(info, { name = 'Free', value = fs.getFreeSpace(p) })
|
|
|
|
table.insert(info, { })
|
|
|
|
table.insert(info, { name = 'Files', value = files })
|
|
|
|
table.insert(info, { name = 'Dirs', value = dirs })
|
|
|
|
return info, math.floor((total / (total + fs.getFreeSpace(p))) * 100)
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function tab:updateInfo()
|
2019-07-05 06:14:28 +00:00
|
|
|
local selected = self.drives:getSelected()
|
|
|
|
local info, percent = getDriveInfo(selected and selected.name or self.drives.values[1].name)
|
|
|
|
self.infos:setValues(info)
|
|
|
|
self.progress.value = percent
|
|
|
|
self.percentage.value = ('%#3d%%'):format(percent)
|
2019-07-05 23:45:29 +00:00
|
|
|
self.icon.image = NFT.parse(NftImages[info[1].value] or NftImages.blank)
|
2019-07-05 06:14:28 +00:00
|
|
|
self:draw()
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function tab:updateDrives()
|
2019-07-05 06:14:28 +00:00
|
|
|
local drives, exclude = getDrives()
|
|
|
|
self.exclude = exclude
|
|
|
|
self.drives:setValues(drives)
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function tab:enable()
|
2019-07-05 06:14:28 +00:00
|
|
|
self:updateDrives()
|
|
|
|
self:updateInfo()
|
|
|
|
UI.Tab.enable(self)
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function tab:eventHandler(event)
|
2019-07-05 06:14:28 +00:00
|
|
|
if event.type == 'grid_focus_row' then
|
|
|
|
self:updateInfo()
|
|
|
|
else return UI.Tab.eventHandler(self, event)
|
|
|
|
end
|
|
|
|
return true
|
2019-07-02 14:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Event.on({ 'disk', 'disk_eject' }, function()
|
2019-07-05 06:14:28 +00:00
|
|
|
os.sleep(1)
|
|
|
|
tab:updateDrives()
|
|
|
|
tab:updateInfo()
|
|
|
|
tab:sync()
|
2019-07-02 14:19:08 +00:00
|
|
|
end)
|
|
|
|
|
2019-07-07 05:14:39 +00:00
|
|
|
return tab
|