tweaks + Anavrins disk usage system module

This commit is contained in:
kepler155c@gmail.com 2019-07-02 10:19:08 -04:00
parent aec5ac0121
commit 1dcb6d67b7
7 changed files with 200 additions and 54 deletions

View 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

View File

@ -5,19 +5,18 @@ local keyboard = _G.device.keyboard
local os = _G.os
local textutils = _G.textutils
local data
kernel.hook('clipboard_copy', function(_, args)
data = args[1]
keyboard.clipboard = args[1]
end)
keyboard.addHotkey('shift-paste', function()
local data = keyboard.clipboard
if type(data) == 'table' then
local s, m = pcall(textutils.serialize, data)
data = (s and m) or Util.tostring(data)
data = s and m or Util.tostring(data)
end
-- replace the event paste data with our internal data
-- args[1] = Util.tostring(data or '')
if data then
os.queueEvent('paste', data)
end

View File

@ -78,16 +78,12 @@ local modifiers = Util.transpose {
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]
-- maintain global keyboard modifier state
if modifiers[code] then
if event == 'key' then
keyboard.state[code] = true
elseif event == 'key_up' then
keyboard.state[code] = nil
end
if event == 'key' and modifiers[code] then
keyboard.state[code] = true
end
-- and fire hotkeys
@ -99,6 +95,14 @@ kernel.hook({ 'key', 'key_up', 'char', 'paste' }, function(event, eventData)
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)
local button = eventData[1]
if event == 'mouse_click' then

View File

@ -10,13 +10,7 @@ local modifiers = Util.transpose {
keys.leftAlt, keys.rightAlt,
}
local input = {
state = { },
}
if not keyboard then
keyboard = { state = input.state }
end
local input = { }
function input:modifierPressed()
return keyboard.state[keys.leftCtrl] or
@ -64,7 +58,6 @@ end
function input:reset()
self.state = { }
self.fired = nil
self.timer = nil
self.mch = nil
@ -81,7 +74,6 @@ function input:translate(event, code, p1, p2)
if event == 'key' then
if p1 then -- key is held down
if not modifiers[code] then
self.fired = true
local ch = input:toCode(keys.getName(code), code)
if #ch == 1 then
return {
@ -92,37 +84,19 @@ function input:translate(event, code, p1, p2)
return { code = ch }
end
elseif code then
--self.fired = true
local ch = input:toCode(keys.getName(code), code)
if #ch ~= 1 then
return { code = ch }
end
-- self.state[code] = true
local ch = input:toCode(keys.getName(code), code)
if #ch ~= 1 then
return { code = ch }
end
end
elseif event == 'char' then
local combo = isCombo()
--if not self.fired then
if combo or not (keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl]) then
self.fired = not combo
return { code = event, ch = code }
--end
-- return { code = event, ch = input:toCode(code) }
if combo or not (keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl]) then
return { code = event, ch = code }
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
self.fired = true
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] then
return { code = 'shift-paste', text = code }
else
@ -142,7 +116,6 @@ function input:translate(event, code, p1, p2)
elseif event == 'mouse_drag' then
self.mfired = true
self.fired = true
return {
code = input:toCode('mouse_drag', 255),
button = code,
@ -169,7 +142,6 @@ function input:translate(event, code, p1, p2)
self.mch = 'mouse_up'
self.mfired = input:toCode(self.mch, 255)
end
self.fired = true
return {
code = self.mfired,
button = code,
@ -182,7 +154,6 @@ function input:translate(event, code, p1, p2)
[ -1 ] = 'scroll_up',
[ 1 ] = 'scroll_down'
}
self.fired = true
return {
code = input:toCode(directions[code], 255),
x = p1,

View File

@ -457,7 +457,6 @@ function UI.Window:initChildren()
if not child.parent then
child.parent = self
child:setParent()
-- child:reposition() -- maybe
if self.enabled then
child:enable()
end
@ -468,6 +467,24 @@ function UI.Window:initChildren()
end
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
self.x = self.parent.width + self.x + 1
end

View File

@ -4,7 +4,6 @@ local UI = require('opus.ui')
UI.NftImage = class(UI.Window)
UI.NftImage.defaults = {
UIElement = 'NftImage',
event = 'button_press',
}
function UI.NftImage:setParent()
if self.image then

View File

@ -6,13 +6,23 @@ local colors = _G.colors
UI.ProgressBar = class(UI.Window)
UI.ProgressBar.defaults = {
UIElement = 'ProgressBar',
progressColor = colors.lime,
backgroundColor = colors.gray,
height = 1,
progressColor = colors.lime,
progressChar = UI.extChars and '\153' or ' ',
fillChar = ' ',
fillColor = colors.gray,
textColor = colors.green,
value = 0,
}
function UI.ProgressBar:draw()
self:clear()
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