From 89e47f8b93a17b3607ad1f962020baf13c1a5276 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sat, 29 Dec 2018 22:33:58 -0500 Subject: [PATCH] file associations --- sys/apps/Files.lua | 126 ++++++++++++++++++++++++++++++++++++++++++--- sys/apps/pain.lua | 1 + sys/etc/app.db | 2 +- 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 sys/apps/pain.lua diff --git a/sys/apps/Files.lua b/sys/apps/Files.lua index 464ab75..c2a108a 100644 --- a/sys/apps/Files.lua +++ b/sys/apps/Files.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Config = require('config') local Event = require('event') local UI = require('ui') @@ -13,13 +11,14 @@ local shell = _ENV.shell UI:configure('Files', ...) -local config = { +local config = Config.load('Files', { showHidden = false, showDirSizes = false, +}) +config.associations = config.associations or { + nft = 'pain', } -Config.load('Files', config) - local copied = { } local marked = { } local directories = { } @@ -61,6 +60,11 @@ local Browser = UI.Page { { text = 'Hidden ^h', event = 'toggle_hidden' }, { text = 'Dir Size ^s', event = 'toggle_dirSize' }, } }, + { text = '\206', + x = -3, + dropdown = { + { text = 'Associations', event = 'associate' }, + } }, }, }, grid = UI.ScrollingGrid { @@ -78,6 +82,56 @@ local Browser = UI.Page { { key = 'totalSize', width = 6 }, }, }, + associations = UI.SlideOut { + backgroundColor = colors.cyan, + menuBar = UI.MenuBar { + buttons = { + { text = 'Save', event = 'save' }, + { text = 'Cancel', event = 'cancel' }, + }, + }, + grid = UI.ScrollingGrid { + x = 2, ex = -6, y = 3, ey = -5, + columns = { + { heading = 'Name', key = 'name' }, + { heading = 'Value', key = 'value' }, + }, + sortColumn = 'name', + accelerators = { + delete = 'remove_entry', + }, + }, + remove = UI.Button { + x = -4, y = 6, + text = '-', event = 'remove_entry', help = 'Remove', + }, + form = UI.Form { + x = 3, y = -3, ey = -2, + margin = 1, + manualControls = true, + [1] = UI.TextEntry { + width = 20, + formLabel = 'Name', formKey = 'name', + shadowText = 'extension', + required = true, + limit = 64, + }, + [2] = UI.TextEntry { + width = 20, + formLabel = 'Name', formKey = 'value', + shadowText = 'program', + required = true, + limit = 64, + }, + add = UI.Button { + x = -11, y = 1, + text = 'Add', event = 'add_association', + }, + }, + statusBar = UI.StatusBar { + backgroundColor = colors.cyan, + }, + }, accelerators = { q = 'quit', e = 'edit', @@ -294,6 +348,9 @@ function Browser:eventHandler(event) self.grid:draw() self:setStatus('Refreshed') + elseif event.type == 'associate' then + self.associations:show() + elseif event.type == 'toggle_hidden' then config.showHidden = not config.showHidden Config.update('Files', config) @@ -336,7 +393,12 @@ function Browser:eventHandler(event) if file.isDir then self:setDir(file.fullName) else - self:run(file.name) + local ext = file.name:match('%.(%w+)$') + if ext and config.associations[ext] then + self:run(config.associations[ext], '/' .. file.fullName) + else + self:run(file.name) + end end end @@ -405,6 +467,58 @@ function Browser:eventHandler(event) return true end +--[[ Associations slide out ]] -- +function Browser.associations:show() + self.grid.values = { } + for k, v in pairs(config.associations) do + table.insert(self.grid.values, { + name = k, + value = v, + }) + end + self.grid:update() + UI.SlideOut.show(self) + self:setFocus(self.form[1]) +end + +function Browser.associations:eventHandler(event) + if event.type == 'remove_entry' then + local row = self.grid:getSelected() + if row then + Util.removeByValue(self.grid.values, row) + self.grid:update() + self.grid:draw() + end + + elseif event.type == 'add_association' then + if self.form:save() then + local entry = Util.find(self.grid.values, 'name', self.form[1].value) or { } + entry.name = self.form[1].value + entry.value = self.form[2].value + table.insert(self.grid.values, entry) + self.form[1]:reset() + self.form[2]:reset() + self.grid:update() + self.grid:draw() + end + + elseif event.type == 'cancel' then + self:hide() + + elseif event.type == 'save' then + config.associations = { } + for _, v in pairs(self.grid.values) do + config.associations[v.name] = v.value + end + Config.update('Files', config) + self:hide() + + else + return UI.SlideOut.eventHandler(self, event) + end + return true +end + --[[-- Startup logic --]]-- local args = { ... } diff --git a/sys/apps/pain.lua b/sys/apps/pain.lua new file mode 100644 index 0000000..44f9ead --- /dev/null +++ b/sys/apps/pain.lua @@ -0,0 +1 @@ +require('util').runUrl(_ENV, 'http://pastebin.com/raw/wJQ7jav0', ...) \ No newline at end of file diff --git a/sys/etc/app.db b/sys/etc/app.db index 353b20b..643db6a 100644 --- a/sys/etc/app.db +++ b/sys/etc/app.db @@ -90,7 +90,7 @@ icon = "\030 \031f\0307\031f\159\030 \159\030 \ \030 \031f\0308\031f\135\0307\0318\144\140\030f\0317\159\143\031c\139\0302\135\030f\0312\157\ \030 \031f\030f\0318\143\133\0312\136\0302\031f\159\159\143\131\030f\0312\132", - run = "http://pastebin.com/raw/wJQ7jav0", + run = "pain", }, [ "48d6857f6b2869d031f463b13aa34df47e18c548" ] = { title = "Breakout",