From fee7ca24deb48fe7ffb0603e5c96d6ee2680ae62 Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Sat, 26 Jan 2019 13:08:02 +0300 Subject: [PATCH] Added Lua interpreter application --- .gitignore | 2 +- Applications/Finder.app/Main.lua | 2 +- Applications/Lua.app/Icon.pic | Bin 272 -> 272 bytes Applications/Lua.app/Main.lua | 196 +++++++++++++++++++++++++++++-- Libraries/Component.lua | 12 ++ Libraries/GUI.lua | 28 +++-- Libraries/Paths.lua | 1 - Libraries/Text.lua | 7 +- 8 files changed, 220 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 382f1c84..58cf7f4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .DS_Store Mounts/ Temporary/ -Users/ \ No newline at end of file +Users/ diff --git a/Applications/Finder.app/Main.lua b/Applications/Finder.app/Main.lua index 9089399d..5a82f0a7 100644 --- a/Applications/Finder.app/Main.lua +++ b/Applications/Finder.app/Main.lua @@ -283,7 +283,7 @@ updateSidebar = function() for proxy, path in filesystem.mounts() do if not proxy.networkModem and not proxy.networkFTP then if proxy ~= filesystem.getProxy() then - addSidebarItem(" " .. (proxy.getLabel() or filesystem.name(path)), path).onTouch = function() + addSidebarItem(" " .. (proxy.getLabel() or proxy.address), path).onTouch = function() onFavouriteTouch(path) end end diff --git a/Applications/Lua.app/Icon.pic b/Applications/Lua.app/Icon.pic index 5b15d8193dbd1b77207e29d6abddd955244ba252..eada71e80d9033f352b3977c64288d33a9c6653b 100644 GIT binary patch literal 272 zcmXw!Q3?Vv3`BR*Zrd-<;9Ug4FR!E^=neMMZhHkmLD0L*Mb@+%KLU?QW|ACthkZz1 zmSMwu!-514us%8h!g(?b%x4=&oKL1EmOrZP*;PSezT0eM`8E^>jdm=>oRM{HK>X;n znBV5nt$$DQ2&|u;05}1LLDMHabb-@ez8E^@YUr7-E6Ny|Una}8gNTdmftDa`uYRzy Fgdc~{M*aW* literal 272 zcmXw!O$q`r42ApB%(N@d;9Ug4l~)=;&>PIs{#-#&5S+W@B4av@i@?{fkJt2kI335x zw7Q*?1uG(XfZ?U~6Nm@ak@{-4AVf)@4H+b5+7N)GhuI+Y+mHaSBlT$+l03m_3ACgX z!yx(paAb^ HISTORY_LIMIT then + table.remove(lines, 1) + end +end + +local function addMultiple(color, ...) + local args = {...} + for i = 1, #args do + if type(args[i]) == "table" then + args[i] = text.serialize(args[i], true, " ", 3) + else + args[i] = tostring(args[i]) + end + end + + for part in table.concat(args, ", "):gmatch("[^\r\n]+") do + add(color, part) + end + + textBox:scrollToEnd() +end + +local function addPrint(...) + addMultiple(nil, ...) +end + +local function addError(...) + addMultiple(0x880000, ...) +end + +local sandbox = {} + +for key, value in pairs(_G) do + sandbox[key] = value +end + +sandbox.print = function(...) + addPrint(...) + workspace:draw() +end + +local function updateTree() + local function updateRecursively(t, definitionName, offset) + local list = {} + for key in pairs(t) do + table.insert(list, key) + end + + local i, expandables = 1, {} + while i <= #list do + if type(t[list[i]]) == "table" then + table.insert(expandables, list[i]) + table.remove(list, i) + else + i = i + 1 + end + end + + table.sort(expandables, function(a, b) return unicode.lower(tostring(a)) < unicode.lower(tostring(b)) end) + table.sort(list, function(a, b) return unicode.lower(tostring(a)) < unicode.lower(tostring(b)) end) + + for i = 1, #expandables do + local definition = definitionName .. expandables[i] .. "." + + tree:addItem( + tostring(expandables[i]), + definition, + offset, + true + ) + + if tree.expandedItems[definition] then + updateRecursively(t[expandables[i]], definition, offset + 2) + end + end + + for i = 1, #list do + tree:addItem( + tostring(list[i]), + definitionName .. list[i] .. "()", + offset, + false + ) + end + end + + tree.items = {} + updateRecursively(sandbox, "", 1) +end + +tree.onItemExpanded = function() + updateTree() +end + +tree.onItemSelected = function() + input.text = lastInput .. tree.selectedItem + workspace:draw() +end + +input.onKeyDown = function(workspace, input, e1, e2, e3, e4) + if e4 == 28 then + local text = input.text:match("^[%s%t]+(.)") or input.text + input.text = "" + lastInput = "" + + add(0x969696, "> " .. text) + + local pizda = text:match("^=+(.+)") + if pizda then + text = "return " .. pizda + end + + local result, reason = load(text, "=lua", "t", sandbox) + if result then + result = {xpcall(result, debug.traceback)} + + if result[1] then + if #result > 1 then + addPrint(table.unpack(result, 2)) + end + else + addError(table.unpack(result, 2)) + end + else + addError(reason) + end + + workspace:draw() + else + lastInput = input.text + end +end + +input.onInputFinished = function() + end window.onResize = function(width, height) - window.backgroundPanel.width, window.backgroundPanel.height = width, height - textBox.width, textBox.height = width - 2, height - 4 - input.localY, input.width = height - input.height + 1, width + window.backgroundPanel.localX = tree.width + 1 + window.backgroundPanel.width = width - tree.width + window.backgroundPanel.height = height - 3 + + textBox.localX = window.backgroundPanel.localX + 1 + textBox.width = window.backgroundPanel.width - 2 + textBox.height = window.backgroundPanel.height - 2 + + tree.height = height - 3 + + input.localX = window.backgroundPanel.localX + input.localY = height - input.height + 1 + input.width = window.backgroundPanel.width end --------------------------------------------------------------------------------- +updateTree() window.actionButtons:moveToFront() window:resize(window.width, window.height) workspace:draw() diff --git a/Libraries/Component.lua b/Libraries/Component.lua index fff20fea..446aca9c 100644 --- a/Libraries/Component.lua +++ b/Libraries/Component.lua @@ -13,3 +13,15 @@ end function component.isAvailable(type) return component.list(type)() and true or false end + +-- Allows writing component.gpu.set(...) instead of component.get("gpu").set(...) +setmetatable(component, { + __index = function(_, key) + local proxy, reason = component.get(key) + if proxy then + return proxy + else + error(reason) + end + end, +}) \ No newline at end of file diff --git a/Libraries/GUI.lua b/Libraries/GUI.lua index d32b5bb5..537e422e 100755 --- a/Libraries/GUI.lua +++ b/Libraries/GUI.lua @@ -58,8 +58,6 @@ local GUI = { WINDOW_TAB_BAR_SELECTED_BACKGROUND_COLOR = 0xF0F0F0, WINDOW_TAB_BAR_SELECTED_TEXT_COLOR = 0x2D2D2D, - PALETTE_CONFIG_PATH = paths.system.libraries .. ".palette.cfg", - LUA_SYNTAX_COLOR_SCHEME = { background = 0x1E1E1E, text = 0xE1E1E1, @@ -2798,7 +2796,7 @@ local function inputStartInput(input) input.text = "" end - input:setCursorPosition(unicode.len(input.text) + 1) + input:setCursorPosition(input.cursorPosition) input.stopInputObject.width, input.stopInputObject.height = input.firstParent.width, input.firstParent.height input.firstParent:addChild(input.stopInputObject) @@ -2806,10 +2804,11 @@ local function inputStartInput(input) inputCursorBlink(input.firstParent, input, true) end -local function inputEventHandler(workspace, input, e1, e2, e3, e4, e5, e6) +local function inputEventHandler(workspace, input, e1, e2, e3, e4, e5, e6, ...) if e1 == "touch" or e1 == "drag" then + input:setCursorPosition(input.textCutFrom + e3 - input.x - input.textOffset) + if input.focused then - input:setCursorPosition(input.textCutFrom + e3 - input.x - input.textOffset) inputCursorBlink(workspace, input, true) else input:startInput() @@ -2833,6 +2832,11 @@ local function inputEventHandler(workspace, input, e1, e2, e3, e4, e5, e6) end inputStopInput(workspace, input) + + if input.onKeyDown then + input.onKeyDown(workspace, input, e1, e2, e3, e4, e5, e6, ...) + end + return -- Arrows up/down/left/right elseif e4 == 200 then @@ -2883,6 +2887,10 @@ local function inputEventHandler(workspace, input, e1, e2, e3, e4, e5, e6) end end + if input.onKeyDown then + input.onKeyDown(workspace, input, e1, e2, e3, e4, e5, e6, ...) + end + inputCursorBlink(workspace, input, true) elseif e1 == "clipboard" and input.focused then input.text = unicode.sub(input.text, 1, input.cursorPosition - 1) .. e3 .. unicode.sub(input.text, input.cursorPosition, -1) @@ -3373,13 +3381,15 @@ function GUI.palette(x, y, startColor) y = y + 2 end + local paletteConfigPath = paths.user.applicationData .. "GUI/Palette.cfg" + local favourites - if filesystem.exists(GUI.PALETTE_CONFIG_PATH) then - favourites = filesystem.readTable(GUI.PALETTE_CONFIG_PATH) + if filesystem.exists(paletteConfigPath) then + favourites = filesystem.readTable(paletteConfigPath) else favourites = {} for i = 1, 6 do favourites[i] = color.HSBToInteger(math.random(0, 360), 1, 1) end - filesystem.writeTable(GUI.PALETTE_CONFIG_PATH, favourites) + filesystem.writeTable(paletteConfigPath, favourites) end local favouritesContainer = palette:addChild(GUI.container(58, 24, 12, 1)) @@ -3409,7 +3419,7 @@ function GUI.palette(x, y, startColor) favouritesContainer.children[i].colors.pressed.background = 0x0 end - filesystem.writeTable(GUI.PALETTE_CONFIG_PATH, favourites) + filesystem.writeTable(paletteConfigPath, favourites) workspace:draw() end diff --git a/Libraries/Paths.lua b/Libraries/Paths.lua index 296e6831..3433fd20 100755 --- a/Libraries/Paths.lua +++ b/Libraries/Paths.lua @@ -20,7 +20,6 @@ paths.system.applicationMineCodeIDE = paths.system.applications .. "MineCode IDE paths.system.applicationFinder = paths.system.applications .. "Finder.app/Main.lua" paths.system.applicationPictureEdit = paths.system.applications .. "Picture Edit.app/Main.lua" paths.system.applicationSettings = paths.system.applications .. "Settings.app/Main.lua" -paths.system.applicationTerminal = paths.system.applications .. "Terminal.app/Main.lua" -------------------------------------------------------------------------------- diff --git a/Libraries/Text.lua b/Libraries/Text.lua index ccfcd88f..71178bf3 100755 --- a/Libraries/Text.lua +++ b/Libraries/Text.lua @@ -3,15 +3,16 @@ local text = {} -------------------------------------------------------------------------------- -function text.serialize(t, prettyLook, indentationWidth, indentUsingTabs, recursionStackLimit) +function text.serialize(t, prettyLook, indentator, recursionStackLimit) checkArg(1, t, "table") recursionStackLimit = recursionStackLimit or math.huge - local indentationSymbolAdder = string.rep(indentUsingTabs and " " or " ", indentationWidth or 2) + indentator = indentator or " " + local equalsSymbol = prettyLook and " = " or "=" local function serialize(t, currentIndentationSymbol, currentRecusrionStack) - local result, nextIndentationSymbol, keyType, valueType, stringValue = {"{"}, currentIndentationSymbol .. indentationSymbolAdder + local result, nextIndentationSymbol, keyType, valueType, stringValue = {"{"}, currentIndentationSymbol .. indentator if prettyLook then table.insert(result, "\n")