From 1dcb6d67b7ef6bfd6075af7eb2fa34290d61d02f Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 2 Jul 2019 10:19:08 -0400 Subject: [PATCH] tweaks + Anavrins disk usage system module --- sys/apps/system/diskusage.lua | 146 ++++++++++++++++++ sys/autorun/clipboard.lua | 11 +- sys/init/1.device.lua | 18 ++- sys/modules/opus/input.lua | 43 +----- sys/modules/opus/ui.lua | 19 ++- sys/modules/opus/ui/components/NftImage.lua | 1 - .../opus/ui/components/ProgressBar.lua | 16 +- 7 files changed, 200 insertions(+), 54 deletions(-) create mode 100644 sys/apps/system/diskusage.lua diff --git a/sys/apps/system/diskusage.lua b/sys/apps/system/diskusage.lua new file mode 100644 index 0000000..0ce1e14 --- /dev/null +++ b/sys/apps/system/diskusage.lua @@ -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 diff --git a/sys/autorun/clipboard.lua b/sys/autorun/clipboard.lua index 72c9434..992af47 100644 --- a/sys/autorun/clipboard.lua +++ b/sys/autorun/clipboard.lua @@ -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 diff --git a/sys/init/1.device.lua b/sys/init/1.device.lua index 65b1e76..c4946d3 100644 --- a/sys/init/1.device.lua +++ b/sys/init/1.device.lua @@ -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 diff --git a/sys/modules/opus/input.lua b/sys/modules/opus/input.lua index b7894d1..62d51b4 100644 --- a/sys/modules/opus/input.lua +++ b/sys/modules/opus/input.lua @@ -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, diff --git a/sys/modules/opus/ui.lua b/sys/modules/opus/ui.lua index 87a6a67..50864c7 100644 --- a/sys/modules/opus/ui.lua +++ b/sys/modules/opus/ui.lua @@ -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 diff --git a/sys/modules/opus/ui/components/NftImage.lua b/sys/modules/opus/ui/components/NftImage.lua index b932dfb..3457711 100644 --- a/sys/modules/opus/ui/components/NftImage.lua +++ b/sys/modules/opus/ui/components/NftImage.lua @@ -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 diff --git a/sys/modules/opus/ui/components/ProgressBar.lua b/sys/modules/opus/ui/components/ProgressBar.lua index 56d6156..af12708 100644 --- a/sys/modules/opus/ui/components/ProgressBar.lua +++ b/sys/modules/opus/ui/components/ProgressBar.lua @@ -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