non-global clipboard, ad hoc crafting

This commit is contained in:
kepler155c
2017-10-14 03:39:47 -04:00
parent 3734afed92
commit 1f31f7ef40
3 changed files with 208 additions and 183 deletions

View File

@@ -675,7 +675,7 @@ function listingPage:eventHandler(event)
UI:setPage('learn') UI:setPage('learn')
elseif event.type == 'craft' then elseif event.type == 'craft' then
UI:setPage('craft') UI:setPage('craft', self.grid:getSelected())
elseif event.type == 'forget' then elseif event.type == 'forget' then
local item = self.grid:getSelected() local item = self.grid:getSelected()
@@ -850,7 +850,7 @@ end
local craftPage = UI.Dialog { local craftPage = UI.Dialog {
height = 6, width = UI.term.width - 10, height = 6, width = UI.term.width - 10,
title = 'Enter amount to craft', title = 'Enter amount to craft',
idField = UI.TextEntry { count = UI.TextEntry {
x = 15, x = 15,
y = 3, y = 3,
width = 10, width = 10,
@@ -874,7 +874,8 @@ function craftPage:draw()
self:write(6, 3, 'Quantity') self:write(6, 3, 'Quantity')
end end
function craftPage:enable() function craftPage:enable(item)
self.item = item
craftingPaused = true craftingPaused = true
self:focusFirst() self:focusFirst()
UI.Dialog.enable(self) UI.Dialog.enable(self)
@@ -888,7 +889,15 @@ end
function craftPage:eventHandler(event) function craftPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
UI:setPreviousPage() UI:setPreviousPage()
--elseif event.type == 'accept' then elseif event.type == 'accept' then
local key = uniqueKey(self.item)
local craftList = { }
craftList[key] = Util.shallowCopy(self.item)
craftList[key].count = tonumber(self.count.value)
craftingPaused = false
craftItems(craftList, inventoryAdapter:listItems())
UI:setPreviousPage()
else else
return UI.Dialog.eventHandler(self, event) return UI.Dialog.eventHandler(self, event)
end end

View File

@@ -1,4 +1,12 @@
shell.setCompletionFunction(shell.getRunningProgram(), function(shell, index, text) local colors = _G.colors
local fs = _G.fs
local keys = _G.keys
local multishell = _ENV.multishell
local os = _G.os
local shell = _ENV.shell
local term = _G.term
shell.setCompletionFunction(shell.getRunningProgram(), function(_, index, text)
if index == 1 then if index == 1 then
return fs.complete(text, shell.dir(), true, false) return fs.complete(text, shell.dir(), true, false)
end end
@@ -32,37 +40,44 @@ local isError
local fileInfo local fileInfo
local dirty = { y = 1, ey = h } local dirty = { y = 1, ey = h }
local mark = { anchor, active, continue } local mark = { }
local keyboard local keyboard
local searchPattern local searchPattern
local undo = { chain = { }, pointer = 0 } local undo = { chain = { }, pointer = 0 }
local complete = { } local complete = { }
local clipboard
if not clipboard then -- do we need a clipboard shim
_G.clipboard = { internal, data } if not multishell or not multishell.hook then -- is this OpusOS ?
clipboard.shim = true if _G.clipboard then -- has it been installed already
clipboard = _G.clipboard
else
clipboard = { }
function clipboard.setData(data) function clipboard.setData(data)
clipboard.data = data clipboard.data = data
if data then if data then
clipboard.useInternal(true) clipboard.useInternal(true)
end
end end
end
function clipboard.getText() function clipboard.getText()
if clipboard.data then if clipboard.data then
return tostring(clipboard.data) return tostring(clipboard.data)
end
end end
end
function clipboard.isInternal() function clipboard.isInternal()
return clipboard.internal return clipboard.internal
end
function clipboard.useInternal(mode)
if mode ~= clipboard.mode then
clipboard.internal = mode
end end
function clipboard.useInternal(mode)
if mode ~= clipboard.mode then
clipboard.internal = mode
end
end
_G.clipboard = clipboard
end end
end end
@@ -110,7 +125,7 @@ local keyMapping = {
[ 'control-up' ] = 'scroll_up', [ 'control-up' ] = 'scroll_up',
[ 'scrollDown' ] = 'scroll_down', [ 'scrollDown' ] = 'scroll_down',
[ 'control-down' ] = 'scroll_down', [ 'control-down' ] = 'scroll_down',
[ 'mouse_click' ] = 'goto', [ 'mouse_click' ] = 'go_to',
[ 'control-l' ] = 'goto_line', [ 'control-l' ] = 'goto_line',
-- marking -- marking
@@ -140,7 +155,7 @@ local keyMapping = {
[ 'control-x' ] = 'cut', [ 'control-x' ] = 'cut',
[ 'control-c' ] = 'copy', [ 'control-c' ] = 'copy',
[ 'control-v' ] = 'paste', [ 'control-v' ] = 'paste',
[ 'control-t' ] = 'toggle_clipboard', [ 'control-m' ] = 'toggle_clipboard',
-- file -- file
[ 'control-s' ] = 'save', [ 'control-s' ] = 'save',
@@ -171,7 +186,7 @@ end
local function getFileInfo(path) local function getFileInfo(path)
local abspath = shell.resolve(path) local abspath = shell.resolve(path)
local fi = { local fi = {
abspath = abspath, abspath = abspath,
path = path, path = path,
@@ -184,7 +199,7 @@ local function getFileInfo(path)
else else
fi.isReadOnly = fs.isReadOnly(fi.abspath) fi.isReadOnly = fs.isReadOnly(fi.abspath)
end end
return fi return fi
end end
@@ -246,16 +261,16 @@ local function save( _sPath )
local function innerSave() local function innerSave()
file = fs.open( _sPath, "w" ) file = fs.open( _sPath, "w" )
if file then if file then
for n, sLine in ipairs( tLines ) do for _,sLine in ipairs( tLines ) do
file.write(sLine .. "\n") file.write(sLine .. "\n")
end end
else else
error( "Failed to open ".._sPath ) error( "Failed to open ".._sPath )
end end
end end
local ok, err = pcall( innerSave ) local ok, err = pcall( innerSave )
if file then if file then
file.close() file.close()
end end
return ok, err return ok, err
@@ -265,7 +280,7 @@ local function split(str, pattern)
pattern = pattern or "(.-)\n" pattern = pattern or "(.-)\n"
local t = {} local t = {}
local function helper(line) table.insert(t, line) return "" end local function helper(line) table.insert(t, line) return "" end
helper((str:gsub(pattern, helper))) helper((str:gsub(pattern, helper)))
return t return t
end end
@@ -315,8 +330,8 @@ local function writeHighlighted(sLine, ny)
return nil return nil
end end
while #sLine > 0 do while #sLine > 0 do
sLine = sLine =
tryWrite(sLine, "^%-%-%[%[.-%]%]", color.commentColor ) or tryWrite(sLine, "^%-%-%[%[.-%]%]", color.commentColor ) or
tryWrite(sLine, "^%-%-.*", color.commentColor ) or tryWrite(sLine, "^%-%-.*", color.commentColor ) or
tryWrite(sLine, "^\".-[^\\]\"", color.stringColor ) or tryWrite(sLine, "^\".-[^\\]\"", color.stringColor ) or
@@ -343,8 +358,8 @@ local function writeHighlighted(sLine, ny)
if ny == mark.ey then if ny == mark.ey then
ex = mark.ex ex = mark.ex
end end
buffer.bg = string.rep('f', sx - 1) .. buffer.bg = string.rep('f', sx - 1) ..
string.rep('7', ex - sx) .. string.rep('7', ex - sx) ..
string.rep('f', #buffer.text - ex + 1) string.rep('f', #buffer.text - ex + 1)
else else
@@ -388,9 +403,12 @@ local function redraw()
end end
if not (w < 32 and #sStatus > 0) then if not (w < 32 and #sStatus > 0) then
local clipboardIndicator = 'S' local clipboardIndicator = ''
if clipboard.isInternal() then if clipboard then
clipboardIndicator = 'I' clipboardIndicator = 'S'
if clipboard.isInternal() then
clipboardIndicator = 'I'
end
end end
local modifiedIndicator = ' ' local modifiedIndicator = ' '
@@ -485,13 +503,6 @@ local __actions = {
addUndo = function(entry) addUndo = function(entry)
local last = undo.chain[#undo.chain] local last = undo.chain[#undo.chain]
if last and last.action == entry.action then if last and last.action == entry.action then
--[[
debug('---')
debug(last)
debug(last.args)
debug(entry)
debug(entry.args)
]]--
if last.action == 'deleteText' then if last.action == 'deleteText' then
if last.args[3] == entry.args[1] and if last.args[3] == entry.args[1] and
last.args[4] == entry.args[2] then last.args[4] == entry.args[2] then
@@ -575,7 +586,7 @@ local __actions = {
goto_line = function() goto_line = function()
local lineNo = tonumber(actions.input('Line: ')) local lineNo = tonumber(actions.input('Line: '))
if lineNo then if lineNo then
actions.goto(1, lineNo) actions.go_to(1, lineNo)
else else
setStatus('Invalid line number') setStatus('Invalid line number')
end end
@@ -593,9 +604,9 @@ local __actions = {
if ny < y or ny == y and nx <= x then if ny < y or ny == y and nx <= x then
setStatus(messages.wrapped) setStatus(messages.wrapped)
end end
actions.goto(nx, ny) actions.go_to(nx, ny)
actions.mark_to(nx + #pattern, ny) actions.mark_to(nx + #pattern, ny)
actions.goto(nx, ny) actions.go_to(nx, ny)
return return
end end
sx = 1 sx = 1
@@ -625,7 +636,7 @@ local __actions = {
if bReadOnly then if bReadOnly then
setError("Access denied") setError("Access denied")
else else
local ok, err = save(sPath) local ok = save(sPath)
if ok then if ok then
setStatus('"%s" %dL, %dC written', setStatus('"%s" %dL, %dC written',
fileInfo.path, #tLines, fs.getSize(fileInfo.abspath)) fileInfo.path, #tLines, fs.getSize(fileInfo.abspath))
@@ -641,7 +652,7 @@ local __actions = {
run = function() run = function()
local sTempPath = "/.temp" local sTempPath = "/.temp"
local ok, err = save(sTempPath) local ok = save(sTempPath)
if ok then if ok then
local nTask = shell.openTab(sTempPath) local nTask = shell.openTab(sTempPath)
if nTask then if nTask then
@@ -732,7 +743,7 @@ local __actions = {
mark_to = function(nx, ny) mark_to = function(nx, ny)
actions.mark_begin() actions.mark_begin()
actions.goto(nx, ny) actions.go_to(nx, ny)
actions.mark_finish() actions.mark_finish()
end, end,
@@ -795,9 +806,7 @@ local __actions = {
actions.dirty_all() actions.dirty_all()
end, end,
setCursor = function(newX, newY) setCursor = function()
local oldX, oldY = lastPos.x, lastPos.y
lastPos.x = x lastPos.x = x
lastPos.y = y lastPos.y = y
@@ -806,27 +815,23 @@ local __actions = {
if screenX < 1 then if screenX < 1 then
scrollX = x - 1 scrollX = x - 1
screenX = 1
actions.dirty_all() actions.dirty_all()
elseif screenX > w then elseif screenX > w then
scrollX = x - w scrollX = x - w
screenX = w
actions.dirty_all() actions.dirty_all()
end end
if screenY < 1 then if screenY < 1 then
scrollY = y - 1 scrollY = y - 1
screenY = 1
actions.dirty_all() actions.dirty_all()
elseif screenY > h - 1 then elseif screenY > h - 1 then
scrollY = y - (h - 1) scrollY = y - (h - 1)
screenY = h - 1
actions.dirty_all() actions.dirty_all()
end end
end, end,
top = function() top = function()
actions.goto(1, 1) actions.go_to(1, 1)
end, end,
bottom = function() bottom = function()
@@ -856,11 +861,11 @@ local __actions = {
end, end,
pageUp = function() pageUp = function()
actions.goto(x, y - (h - 1)) actions.go_to(x, y - (h - 1))
end, end,
pageDown = function() pageDown = function()
actions.goto(x, y + (h - 1)) actions.go_to(x, y + (h - 1))
end, end,
home = function() home = function()
@@ -966,7 +971,7 @@ local __actions = {
local front = tLines[sy]:sub(1, sx - 1) local front = tLines[sy]:sub(1, sx - 1)
local back = tLines[ey]:sub(ex, #tLines[ey]) local back = tLines[ey]:sub(ex, #tLines[ey])
for k = 2, ey - sy + 1 do for _ = 2, ey - sy + 1 do
table.remove(tLines, y + 1) table.remove(tLines, y + 1)
end end
tLines[y] = front .. back tLines[y] = front .. back
@@ -981,7 +986,7 @@ local __actions = {
local count = 0 local count = 0
local lines = { } local lines = { }
for y = csy, cey do for _ = csy, cey do
local line = tLines[y] local line = tLines[y]
if line then if line then
local x = 1 local x = 1
@@ -1042,22 +1047,25 @@ local __actions = {
end, end,
toggle_clipboard = function() toggle_clipboard = function()
if clipboard.shim then if clipboard then
clipboard.setInternal(not clipboard.internal) clipboard.setInternal(not clipboard.internal)
end if clipboard.isInternal() then
if clipboard.isInternal() then setStatus('Using internal clipboard')
setStatus('Using internal clipboard') else
else setStatus('Using system clipboard')
setStatus('Using system clipboard') end
end end
end, end,
copy_marked = function() copy_marked = function()
local text, size = local text, size = actions.copyText(mark.x, mark.y, mark.ex, mark.ey)
actions.copyText(mark.x, mark.y, mark.ex, mark.ey) if clipboard then
clipboard.setData(text) clipboard.setData(text)
clipboard.useInternal(true)
else
os.queueEvent('clipboard_copy', text)
end
setStatus('%d chars copied', size) setStatus('%d chars copied', size)
clipboard.useInternal(true)
end, end,
cut = function() cut = function()
@@ -1078,7 +1086,7 @@ local __actions = {
if mark.active then if mark.active then
actions.delete() actions.delete()
end end
if clipboard.isInternal() then if clipboard and clipboard.isInternal() then
text = clipboard.getText() text = clipboard.getText()
end end
if text then if text then
@@ -1089,7 +1097,7 @@ local __actions = {
end end
end, end,
goto = function(cx, cy) go_to = function(cx, cy)
y = math.min(math.max(cy, 1), #tLines) y = math.min(math.max(cy, 1), #tLines)
x = math.min(math.max(cx, 1), #tLines[y] + 1) x = math.min(math.max(cx, 1), #tLines[y] + 1)
end, end,
@@ -1120,98 +1128,100 @@ load(sPath)
term.setCursorBlink(true) term.setCursorBlink(true)
redraw() redraw()
if not keyboard then keyboard = { }
keyboard = { control, shift, combo }
function keyboard:translate(event, code) function keyboard:translate(event, code)
if event == 'key' then if event == 'key' then
local ch = keys.getName(code) local ch = keys.getName(code)
if ch then if ch then
if code == keys.leftCtrl or code == keys.rightCtrl then
self.control = true
self.combo = false
return
end
if code == keys.leftShift or code == keys.rightShift then
self.shift = true
self.combo = false
return
end
if self.shift then
if #ch > 1 then
ch = 'shift-' .. ch
elseif self.control then
-- will create control-X
-- better than shift-control-x
ch = ch:upper()
end
self.combo = true
end
if self.control then
ch = 'control-' .. ch
self.combo = true
-- even return numbers such as
-- control-seven
return ch
end
-- filter out characters that will be processed in
-- the subsequent char event
if ch and #ch > 1 and (code < 2 or code > 11) then
return ch
end
end
elseif event == 'key_up' then
if code == keys.leftCtrl or code == keys.rightCtrl then if code == keys.leftCtrl or code == keys.rightCtrl then
self.control = false if not self.control then
elseif code == keys.leftShift or code == keys.rightShift then self.control = true
self.shift = false self.combo = false
else end
return return
end end
-- only send through the shift / control event if it wasn't if code == keys.leftShift or code == keys.rightShift then
-- used in combination with another event if not self.shift then
if not self.combo then self.shift = true
return keys.getName(code) self.combo = false
end
return
end end
elseif event == 'char' then
if not self.control then
self.combo = true
return event
end
elseif event == 'mouse_click' then
local buttons = { 'mouse_click', 'mouse_rightclick', 'mouse_doubleclick' }
self.combo = true
if self.shift then if self.shift then
return 'shift-' .. buttons[code] if #ch > 1 then
ch = 'shift-' .. ch
elseif self.control then
-- will create control-X
-- better than shift-control-x
ch = ch:upper()
end
self.combo = true
end end
return buttons[code]
elseif event == "mouse_scroll" then if self.control then
local directions = { ch = 'control-' .. ch
[ -1 ] = 'scrollUp', self.combo = true
[ 1 ] = 'scrollDown' -- even return numbers such as
} -- control-seven
return directions[code] return ch
end
elseif event == 'paste' then -- filter out characters that will be processed in
-- the subsequent char event
if ch and #ch > 1 and (code < 2 or code > 11) then
return ch
end
end
elseif event == 'key_up' then
if code == keys.leftCtrl or code == keys.rightCtrl then
self.control = false
elseif code == keys.leftShift or code == keys.rightShift then
self.shift = false
else
return
end
-- only send through the shift / control event if it wasn't
-- used in combination with another event
if not self.combo then
return keys.getName(code)
end
elseif event == 'char' then
if not self.control then
self.combo = true self.combo = true
return event return event
elseif event == 'mouse_drag' then
return event
end end
elseif event == 'mouse_click' then
local buttons = { 'mouse_click', 'mouse_rightclick', 'mouse_doubleclick' }
self.combo = true
if self.shift then
return 'shift-' .. buttons[code]
end
return buttons[code]
elseif event == "mouse_scroll" then
local directions = {
[ -1 ] = 'scrollUp',
[ 1 ] = 'scrollDown'
}
return directions[code]
elseif event == 'paste' then
self.combo = true
return event
elseif event == 'mouse_drag' then
return event
end end
end end

View File

@@ -1,8 +1,11 @@
requireInjector(getfenv(1)) _G.requireInjector()
local Point = require('point') local Point = require('point')
local Util = require('util') local Util = require('util')
local os = _G.os
local turtle = _G.turtle
local checkedNodes, nodes local checkedNodes, nodes
local function addNode(node) local function addNode(node)
@@ -47,7 +50,7 @@ local function findObsidian()
if turtle.getItemCount(16) > 0 then if turtle.getItemCount(16) > 0 then
print('Inventory full') print('Inventory full')
print('Enter to continue...') print('Enter to continue...')
read() _G.read()
end end
if b and b.name == 'minecraft:obsidian' then if b and b.name == 'minecraft:obsidian' then
@@ -63,40 +66,43 @@ local function findObsidian()
break break
end end
local node = Point.closest(turtle.point, nodes) node = Point.closest(turtle.point, nodes)
if not turtle.gotoPoint(node) then if not turtle.gotoPoint(node) then
break break
end end
until turtle.abort until turtle.abort
end end
turtle.reset() turtle.run(function()
turtle.setPolicy(turtle.policies.digOnly) turtle.reset()
local s, m = turtle.run(function() turtle.setPolicy(turtle.policies.digOnly)
repeat
checkedNodes = { } local s, m = pcall(function()
nodes = { } repeat
checkedNodes = { }
nodes = { }
local _,b = turtle.inspectDown() local _,b = turtle.inspectDown()
if not b or b.name ~= 'minecraft:obsidian' then if not b or b.name ~= 'minecraft:obsidian' then
break break
end end
findObsidian() findObsidian()
if not turtle.select('minecraft:water_bucket') then if not turtle.select('minecraft:water_bucket') then
break break
end end
turtle.goto(0, 0) turtle._goto(0, 0)
turtle.placeDown() turtle.placeDown()
os.sleep(2) os.sleep(2)
turtle.placeDown() turtle.placeDown()
turtle.down() turtle.down()
turtle.select(1) turtle.select(1)
until turtle.abort until turtle.abort
end)
if not s and m then
error(m)
end
turtle._goto(0, 0, 0, 0)
end) end)
turtle.goto(0, 0, 0, 0)
turtle.reset()
if not s and m then
error(m)
end