mirror of
https://github.com/kepler155c/opus
synced 2025-01-01 03:10:28 +00:00
tweaks + Anavrins disk usage system module
This commit is contained in:
parent
aec5ac0121
commit
1dcb6d67b7
146
sys/apps/system/diskusage.lua
Normal file
146
sys/apps/system/diskusage.lua
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
local UI = require('opus.ui')
|
||||||
|
local Event = require('opus.event')
|
||||||
|
local Util = require('opus.util')
|
||||||
|
local NFT = require('opus.nft')
|
||||||
|
|
||||||
|
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',
|
||||||
|
drive = '',
|
||||||
|
rom = '',
|
||||||
|
hdd = '',
|
||||||
|
}
|
||||||
|
|
||||||
|
local tab = UI.Tab {
|
||||||
|
tabTitle = '1.Disks Usage',
|
||||||
|
description = 'Visualise HDD and disks usage',
|
||||||
|
drives = UI.ScrollingGrid {
|
||||||
|
y = 2, ey = 9, x = 2, ex = '47%',
|
||||||
|
columns = {
|
||||||
|
{ heading = 'Drive', key = 'name' },
|
||||||
|
{ heading = 'Side' ,key = 'side' }
|
||||||
|
},
|
||||||
|
sortColumn = 'name',
|
||||||
|
},
|
||||||
|
infos = UI.Grid {
|
||||||
|
x = '52%', y = 3, ex = -2, ey = 9,
|
||||||
|
disableHeader = true,
|
||||||
|
unfocusedBackgroundSelectedColor = colors.black,
|
||||||
|
inactive = true,
|
||||||
|
backgroundSelectedColor = colors.black, --??
|
||||||
|
columns = {
|
||||||
|
{key = 'name' },
|
||||||
|
{key = 'value', align = 'right' },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
progress = UI.ProgressBar {
|
||||||
|
x = 11, y = 11, ex = -2,
|
||||||
|
barChar = '\127',
|
||||||
|
textColor = colors.blue,
|
||||||
|
backgroundColor = colors.black,
|
||||||
|
},
|
||||||
|
percentage = UI.Text {
|
||||||
|
x = 11, y = 12, ex = -2,
|
||||||
|
align = 'center',
|
||||||
|
},
|
||||||
|
icon = UI.NftImage {
|
||||||
|
x = 2, y = 11,
|
||||||
|
image = NFT.parse(NftImages.blank)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
local function getDrives()
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getDriveInfo(p)
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
function tab:updateInfo()
|
||||||
|
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)
|
||||||
|
self:draw()
|
||||||
|
end
|
||||||
|
|
||||||
|
function tab:updateDrives()
|
||||||
|
local drives, exclude = getDrives()
|
||||||
|
self.exclude = exclude
|
||||||
|
self.drives:setValues(drives)
|
||||||
|
end
|
||||||
|
|
||||||
|
function tab:enable()
|
||||||
|
self:updateDrives()
|
||||||
|
self:updateInfo()
|
||||||
|
UI.Tab.enable(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function tab:eventHandler(event)
|
||||||
|
if event.type == 'grid_focus_row' then
|
||||||
|
self:updateInfo()
|
||||||
|
end
|
||||||
|
return UI.Tab.eventHandler(self, event)
|
||||||
|
end
|
||||||
|
|
||||||
|
Event.on({ 'disk', 'disk_eject' }, function()
|
||||||
|
sleep(1)
|
||||||
|
tab:updateDrives()
|
||||||
|
tab:updateInfo()
|
||||||
|
tab:sync()
|
||||||
|
end)
|
||||||
|
|
||||||
|
return tab
|
@ -5,19 +5,18 @@ local keyboard = _G.device.keyboard
|
|||||||
local os = _G.os
|
local os = _G.os
|
||||||
local textutils = _G.textutils
|
local textutils = _G.textutils
|
||||||
|
|
||||||
local data
|
|
||||||
|
|
||||||
kernel.hook('clipboard_copy', function(_, args)
|
kernel.hook('clipboard_copy', function(_, args)
|
||||||
data = args[1]
|
keyboard.clipboard = args[1]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
keyboard.addHotkey('shift-paste', function()
|
keyboard.addHotkey('shift-paste', function()
|
||||||
|
local data = keyboard.clipboard
|
||||||
|
|
||||||
if type(data) == 'table' then
|
if type(data) == 'table' then
|
||||||
local s, m = pcall(textutils.serialize, data)
|
local s, m = pcall(textutils.serialize, data)
|
||||||
data = (s and m) or Util.tostring(data)
|
data = s and m or Util.tostring(data)
|
||||||
end
|
end
|
||||||
-- replace the event paste data with our internal data
|
|
||||||
-- args[1] = Util.tostring(data or '')
|
|
||||||
if data then
|
if data then
|
||||||
os.queueEvent('paste', data)
|
os.queueEvent('paste', data)
|
||||||
end
|
end
|
||||||
|
@ -78,16 +78,12 @@ local modifiers = Util.transpose {
|
|||||||
keys.leftAlt, keys.rightAlt,
|
keys.leftAlt, keys.rightAlt,
|
||||||
}
|
}
|
||||||
|
|
||||||
kernel.hook({ 'key', 'key_up', 'char', 'paste' }, function(event, eventData)
|
kernel.hook({ 'key', 'char', 'paste' }, function(event, eventData)
|
||||||
local code = eventData[1]
|
local code = eventData[1]
|
||||||
|
|
||||||
-- maintain global keyboard modifier state
|
-- maintain global keyboard modifier state
|
||||||
if modifiers[code] then
|
if event == 'key' and modifiers[code] then
|
||||||
if event == 'key' then
|
keyboard.state[code] = true
|
||||||
keyboard.state[code] = true
|
|
||||||
elseif event == 'key_up' then
|
|
||||||
keyboard.state[code] = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- and fire hotkeys
|
-- and fire hotkeys
|
||||||
@ -99,6 +95,14 @@ kernel.hook({ 'key', 'key_up', 'char', 'paste' }, function(event, eventData)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
kernel.hook('key_up', function(_, eventData)
|
||||||
|
local code = eventData[1]
|
||||||
|
|
||||||
|
if modifiers[code] then
|
||||||
|
keyboard.state[code] = nil
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
kernel.hook({ 'mouse_click', 'mouse_up', 'mouse_drag' }, function(event, eventData)
|
kernel.hook({ 'mouse_click', 'mouse_up', 'mouse_drag' }, function(event, eventData)
|
||||||
local button = eventData[1]
|
local button = eventData[1]
|
||||||
if event == 'mouse_click' then
|
if event == 'mouse_click' then
|
||||||
|
@ -10,13 +10,7 @@ local modifiers = Util.transpose {
|
|||||||
keys.leftAlt, keys.rightAlt,
|
keys.leftAlt, keys.rightAlt,
|
||||||
}
|
}
|
||||||
|
|
||||||
local input = {
|
local input = { }
|
||||||
state = { },
|
|
||||||
}
|
|
||||||
|
|
||||||
if not keyboard then
|
|
||||||
keyboard = { state = input.state }
|
|
||||||
end
|
|
||||||
|
|
||||||
function input:modifierPressed()
|
function input:modifierPressed()
|
||||||
return keyboard.state[keys.leftCtrl] or
|
return keyboard.state[keys.leftCtrl] or
|
||||||
@ -64,7 +58,6 @@ end
|
|||||||
|
|
||||||
function input:reset()
|
function input:reset()
|
||||||
self.state = { }
|
self.state = { }
|
||||||
self.fired = nil
|
|
||||||
|
|
||||||
self.timer = nil
|
self.timer = nil
|
||||||
self.mch = nil
|
self.mch = nil
|
||||||
@ -81,7 +74,6 @@ function input:translate(event, code, p1, p2)
|
|||||||
if event == 'key' then
|
if event == 'key' then
|
||||||
if p1 then -- key is held down
|
if p1 then -- key is held down
|
||||||
if not modifiers[code] then
|
if not modifiers[code] then
|
||||||
self.fired = true
|
|
||||||
local ch = input:toCode(keys.getName(code), code)
|
local ch = input:toCode(keys.getName(code), code)
|
||||||
if #ch == 1 then
|
if #ch == 1 then
|
||||||
return {
|
return {
|
||||||
@ -92,37 +84,19 @@ function input:translate(event, code, p1, p2)
|
|||||||
return { code = ch }
|
return { code = ch }
|
||||||
end
|
end
|
||||||
elseif code then
|
elseif code then
|
||||||
--self.fired = true
|
local ch = input:toCode(keys.getName(code), code)
|
||||||
local ch = input:toCode(keys.getName(code), code)
|
if #ch ~= 1 then
|
||||||
if #ch ~= 1 then
|
return { code = ch }
|
||||||
return { code = ch }
|
end
|
||||||
end
|
|
||||||
-- self.state[code] = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif event == 'char' then
|
elseif event == 'char' then
|
||||||
local combo = isCombo()
|
local combo = isCombo()
|
||||||
--if not self.fired then
|
if combo or not (keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl]) then
|
||||||
if combo or not (keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl]) then
|
return { code = event, ch = code }
|
||||||
self.fired = not combo
|
|
||||||
return { code = event, ch = code }
|
|
||||||
--end
|
|
||||||
-- return { code = event, ch = input:toCode(code) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif event == 'key_upx' then
|
|
||||||
if not self.fired then
|
|
||||||
--if self.state[code] then
|
|
||||||
self.fired = true
|
|
||||||
local ch = input:toCode(keys.getName(code), code)
|
|
||||||
self.state[code] = nil
|
|
||||||
return { code = ch }
|
|
||||||
--end
|
|
||||||
end
|
|
||||||
self.state[code] = nil
|
|
||||||
|
|
||||||
elseif event == 'paste' then
|
elseif event == 'paste' then
|
||||||
self.fired = true
|
|
||||||
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] then
|
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] then
|
||||||
return { code = 'shift-paste', text = code }
|
return { code = 'shift-paste', text = code }
|
||||||
else
|
else
|
||||||
@ -142,7 +116,6 @@ function input:translate(event, code, p1, p2)
|
|||||||
|
|
||||||
elseif event == 'mouse_drag' then
|
elseif event == 'mouse_drag' then
|
||||||
self.mfired = true
|
self.mfired = true
|
||||||
self.fired = true
|
|
||||||
return {
|
return {
|
||||||
code = input:toCode('mouse_drag', 255),
|
code = input:toCode('mouse_drag', 255),
|
||||||
button = code,
|
button = code,
|
||||||
@ -169,7 +142,6 @@ function input:translate(event, code, p1, p2)
|
|||||||
self.mch = 'mouse_up'
|
self.mch = 'mouse_up'
|
||||||
self.mfired = input:toCode(self.mch, 255)
|
self.mfired = input:toCode(self.mch, 255)
|
||||||
end
|
end
|
||||||
self.fired = true
|
|
||||||
return {
|
return {
|
||||||
code = self.mfired,
|
code = self.mfired,
|
||||||
button = code,
|
button = code,
|
||||||
@ -182,7 +154,6 @@ function input:translate(event, code, p1, p2)
|
|||||||
[ -1 ] = 'scroll_up',
|
[ -1 ] = 'scroll_up',
|
||||||
[ 1 ] = 'scroll_down'
|
[ 1 ] = 'scroll_down'
|
||||||
}
|
}
|
||||||
self.fired = true
|
|
||||||
return {
|
return {
|
||||||
code = input:toCode(directions[code], 255),
|
code = input:toCode(directions[code], 255),
|
||||||
x = p1,
|
x = p1,
|
||||||
|
@ -457,7 +457,6 @@ function UI.Window:initChildren()
|
|||||||
if not child.parent then
|
if not child.parent then
|
||||||
child.parent = self
|
child.parent = self
|
||||||
child:setParent()
|
child:setParent()
|
||||||
-- child:reposition() -- maybe
|
|
||||||
if self.enabled then
|
if self.enabled then
|
||||||
child:enable()
|
child:enable()
|
||||||
end
|
end
|
||||||
@ -468,6 +467,24 @@ function UI.Window:initChildren()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function UI.Window:layout()
|
function UI.Window:layout()
|
||||||
|
local function calc(p, max)
|
||||||
|
p = tonumber(p:match('(%d+)%%'))
|
||||||
|
return p and math.floor(max * p / 100) or 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(self.x) == 'string' then
|
||||||
|
self.x = calc(self.x, self.parent.width)
|
||||||
|
end
|
||||||
|
if type(self.ex) == 'string' then
|
||||||
|
self.ex = calc(self.ex, self.parent.width)
|
||||||
|
end
|
||||||
|
if type(self.y) == 'string' then
|
||||||
|
self.y = calc(self.y, self.parent.height)
|
||||||
|
end
|
||||||
|
if type(self.ey) == 'string' then
|
||||||
|
self.ey = calc(self.ey, self.parent.height)
|
||||||
|
end
|
||||||
|
|
||||||
if self.x < 0 then
|
if self.x < 0 then
|
||||||
self.x = self.parent.width + self.x + 1
|
self.x = self.parent.width + self.x + 1
|
||||||
end
|
end
|
||||||
|
@ -4,7 +4,6 @@ local UI = require('opus.ui')
|
|||||||
UI.NftImage = class(UI.Window)
|
UI.NftImage = class(UI.Window)
|
||||||
UI.NftImage.defaults = {
|
UI.NftImage.defaults = {
|
||||||
UIElement = 'NftImage',
|
UIElement = 'NftImage',
|
||||||
event = 'button_press',
|
|
||||||
}
|
}
|
||||||
function UI.NftImage:setParent()
|
function UI.NftImage:setParent()
|
||||||
if self.image then
|
if self.image then
|
||||||
|
@ -6,13 +6,23 @@ local colors = _G.colors
|
|||||||
UI.ProgressBar = class(UI.Window)
|
UI.ProgressBar = class(UI.Window)
|
||||||
UI.ProgressBar.defaults = {
|
UI.ProgressBar.defaults = {
|
||||||
UIElement = 'ProgressBar',
|
UIElement = 'ProgressBar',
|
||||||
progressColor = colors.lime,
|
|
||||||
backgroundColor = colors.gray,
|
backgroundColor = colors.gray,
|
||||||
height = 1,
|
height = 1,
|
||||||
|
progressColor = colors.lime,
|
||||||
|
progressChar = UI.extChars and '\153' or ' ',
|
||||||
|
fillChar = ' ',
|
||||||
|
fillColor = colors.gray,
|
||||||
|
textColor = colors.green,
|
||||||
value = 0,
|
value = 0,
|
||||||
}
|
}
|
||||||
function UI.ProgressBar:draw()
|
function UI.ProgressBar:draw()
|
||||||
self:clear()
|
|
||||||
local width = math.ceil(self.value / 100 * self.width)
|
local width = math.ceil(self.value / 100 * self.width)
|
||||||
self:clearArea(1, 1, width, self.height, self.progressColor)
|
|
||||||
|
local filler = string.rep(self.fillChar, self.width)
|
||||||
|
local progress = string.rep(self.progressChar, width)
|
||||||
|
|
||||||
|
for i = 1, self.height do
|
||||||
|
self:write(1, i, filler, nil, self.fillColor)
|
||||||
|
self:write(1, i, progress, self.progressColor)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user