Added icons to diskusage

This commit is contained in:
xAnavrins 2019-07-05 02:14:28 -04:00
parent 08eac79109
commit 4a089ecd85
1 changed files with 112 additions and 110 deletions

View File

@ -8,146 +8,148 @@ local os = _G.os
local peripheral = _G.peripheral local peripheral = _G.peripheral
local NftImages = { local NftImages = {
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', 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',
drive = '', disk = '\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',
rom = '', 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 = '', 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',
} }
local tab = UI.Tab { local tab = UI.Tab {
tabTitle = 'Disks Usage', tabTitle = 'Disks Usage',
description = 'Visualise HDD and disks usage', description = 'Visualise HDD and disks usage',
drives = UI.ScrollingGrid { drives = UI.ScrollingGrid {
x = 2, y = 1, x = 2, y = 1,
ex = '47%', ey = 8, ex = '47%', ey = 8,
columns = { columns = {
{ heading = 'Drive', key = 'name' }, { heading = 'Drive', key = 'name' },
{ heading = 'Side' ,key = 'side', textColor = colors.yellow } { heading = 'Side' ,key = 'side', textColor = colors.yellow }
}, },
sortColumn = 'name', sortColumn = 'name',
}, },
infos = UI.Grid { infos = UI.Grid {
x = '52%', y = 2, x = '52%', y = 2,
ex = -2, ey = 8, ex = -2, ey = 8,
disableHeader = true, disableHeader = true,
unfocusedBackgroundSelectedColor = colors.black, unfocusedBackgroundSelectedColor = colors.black,
inactive = true, inactive = true,
backgroundSelectedColor = colors.black, backgroundSelectedColor = colors.black,
columns = { columns = {
{ key = 'name' }, { key = 'name' },
{ key = 'value', align = 'right', textColor = colors.yellow }, { key = 'value', align = 'right', textColor = colors.yellow },
} }
}, },
progress = UI.ProgressBar { progress = UI.ProgressBar {
x = 11, y = 10, x = 11, y = 10,
ex = -2, ex = -2,
}, },
percentage = UI.Text { percentage = UI.Text {
x = 11, y = 11, x = 11, y = 11,
ex = -2, ex = -2,
align = 'center', align = 'center',
}, },
icon = UI.NftImage { icon = UI.NftImage {
x = 2, y = 10, x = 2, y = 10,
image = NFT.parse(NftImages.blank) image = NFT.parse(NftImages.blank)
}, },
} }
local function getDrives() local function getDrives()
local unique = { ['hdd'] = true, ['virt'] = true } local unique = { ['hdd'] = true, ['virt'] = true }
local exclude = {} local exclude = {}
local drives = { local drives = {
{name = 'hdd', side = ''}, {name = 'hdd', side = ''},
} }
for _, drive in pairs(fs.list('/')) do for _, drive in pairs(fs.list('/')) do
local side = fs.getDrive(drive) local side = fs.getDrive(drive)
if side and not unique[side] then if side and not unique[side] then
unique[side] = true unique[side] = true
exclude[drive] = true exclude[drive] = true
table.insert(drives, {name=drive, side=side}) table.insert(drives, {name=drive, side=side})
end end
end end
return drives, exclude return drives, exclude
end end
local function getDriveInfo(p) local function getDriveInfo(p)
local files, dirs, total = 0, 0, 0 local files, dirs, total = 0, 0, 0
if p == "hdd" then p = "/" end if p == "hdd" then p = "/" end
p = fs.combine(p, '') p = fs.combine(p, '')
local drive = fs.getDrive(p) local drive = fs.getDrive(p)
local function recurse(path) local function recurse(path)
if fs.getDrive(path) == drive then if fs.getDrive(path) == drive then
if fs.isDir(path) then if fs.isDir(path) then
if path ~= p then if path ~= p then
total = total + 500 total = total + 500
dirs = dirs + 1 dirs = dirs + 1
end end
for _, v in pairs(fs.list(path)) do for _, v in pairs(fs.list(path)) do
recurse(fs.combine(path, v)) recurse(fs.combine(path, v))
end end
else else
local sz = fs.getSize(path) 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
files = files + 1 recurse(p)
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 })
local info = {} table.insert(info, { name = 'Used', value = total })
table.insert(info, { name = 'Type', value = peripheral.getType(drive) or drive }) table.insert(info, { name = 'Total', value = total + fs.getFreeSpace(p) })
table.insert(info, { name = 'Used', value = total }) table.insert(info, { name = 'Free', value = fs.getFreeSpace(p) })
table.insert(info, { name = 'Total', value = total + fs.getFreeSpace(p) }) table.insert(info, { })
table.insert(info, { name = 'Free', value = fs.getFreeSpace(p) }) table.insert(info, { name = 'Files', value = files })
table.insert(info, { }) table.insert(info, { name = 'Dirs', value = dirs })
table.insert(info, { name = 'Files', value = files }) return info, math.floor((total / (total + fs.getFreeSpace(p))) * 100)
table.insert(info, { name = 'Dirs', value = dirs })
return info, math.floor((total / (total + fs.getFreeSpace(p))) * 100)
end end
function tab:updateInfo() function tab:updateInfo()
local selected = self.drives:getSelected() local selected = self.drives:getSelected()
local info, percent = getDriveInfo(selected and selected.name or self.drives.values[1].name) _syslog(selected)
self.infos:setValues(info) local info, percent = getDriveInfo(selected and selected.name or self.drives.values[1].name)
self.progress.value = percent self.infos:setValues(info)
self.percentage.value = ('%#3d%%'):format(percent) self.progress.value = percent
self:draw() self.percentage.value = ('%#3d%%'):format(percent)
self.icon.image = NFT.parse(NftImages[selected.name] or NftImages.blank)
self:draw()
end end
function tab:updateDrives() function tab:updateDrives()
local drives, exclude = getDrives() local drives, exclude = getDrives()
self.exclude = exclude self.exclude = exclude
self.drives:setValues(drives) self.drives:setValues(drives)
end end
function tab:enable() function tab:enable()
self:updateDrives() self:updateDrives()
self:updateInfo() self:updateInfo()
UI.Tab.enable(self) UI.Tab.enable(self)
end end
function tab:eventHandler(event) function tab:eventHandler(event)
if event.type == 'grid_focus_row' then if event.type == 'grid_focus_row' then
self:updateInfo() self:updateInfo()
end else return UI.Tab.eventHandler(self, event)
return UI.Tab.eventHandler(self, event) end
return true
end end
Event.on({ 'disk', 'disk_eject' }, function() Event.on({ 'disk', 'disk_eject' }, function()
os.sleep(1) os.sleep(1)
tab:updateDrives() tab:updateDrives()
tab:updateInfo() tab:updateInfo()
tab:sync() tab:sync()
end) end)
return tab return tab