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')
elseif event.type == 'craft' then
UI:setPage('craft')
UI:setPage('craft', self.grid:getSelected())
elseif event.type == 'forget' then
local item = self.grid:getSelected()
@@ -850,7 +850,7 @@ end
local craftPage = UI.Dialog {
height = 6, width = UI.term.width - 10,
title = 'Enter amount to craft',
idField = UI.TextEntry {
count = UI.TextEntry {
x = 15,
y = 3,
width = 10,
@@ -874,7 +874,8 @@ function craftPage:draw()
self:write(6, 3, 'Quantity')
end
function craftPage:enable()
function craftPage:enable(item)
self.item = item
craftingPaused = true
self:focusFirst()
UI.Dialog.enable(self)
@@ -888,7 +889,15 @@ end
function craftPage:eventHandler(event)
if event.type == 'cancel' then
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
return UI.Dialog.eventHandler(self, event)
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
return fs.complete(text, shell.dir(), true, false)
end
@@ -32,15 +40,19 @@ local isError
local fileInfo
local dirty = { y = 1, ey = h }
local mark = { anchor, active, continue }
local mark = { }
local keyboard
local searchPattern
local undo = { chain = { }, pointer = 0 }
local complete = { }
local clipboard
if not clipboard then
_G.clipboard = { internal, data }
clipboard.shim = true
-- do we need a clipboard shim
if not multishell or not multishell.hook then -- is this OpusOS ?
if _G.clipboard then -- has it been installed already
clipboard = _G.clipboard
else
clipboard = { }
function clipboard.setData(data)
clipboard.data = data
@@ -64,6 +76,9 @@ if not clipboard then
clipboard.internal = mode
end
end
_G.clipboard = clipboard
end
end
local color = {
@@ -110,7 +125,7 @@ local keyMapping = {
[ 'control-up' ] = 'scroll_up',
[ 'scrollDown' ] = 'scroll_down',
[ 'control-down' ] = 'scroll_down',
[ 'mouse_click' ] = 'goto',
[ 'mouse_click' ] = 'go_to',
[ 'control-l' ] = 'goto_line',
-- marking
@@ -140,7 +155,7 @@ local keyMapping = {
[ 'control-x' ] = 'cut',
[ 'control-c' ] = 'copy',
[ 'control-v' ] = 'paste',
[ 'control-t' ] = 'toggle_clipboard',
[ 'control-m' ] = 'toggle_clipboard',
-- file
[ 'control-s' ] = 'save',
@@ -246,7 +261,7 @@ local function save( _sPath )
local function innerSave()
file = fs.open( _sPath, "w" )
if file then
for n, sLine in ipairs( tLines ) do
for _,sLine in ipairs( tLines ) do
file.write(sLine .. "\n")
end
else
@@ -388,10 +403,13 @@ local function redraw()
end
if not (w < 32 and #sStatus > 0) then
local clipboardIndicator = 'S'
local clipboardIndicator = ''
if clipboard then
clipboardIndicator = 'S'
if clipboard.isInternal() then
clipboardIndicator = 'I'
end
end
local modifiedIndicator = ' '
if undo.chain[1] then
@@ -485,13 +503,6 @@ local __actions = {
addUndo = function(entry)
local last = undo.chain[#undo.chain]
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.args[3] == entry.args[1] and
last.args[4] == entry.args[2] then
@@ -575,7 +586,7 @@ local __actions = {
goto_line = function()
local lineNo = tonumber(actions.input('Line: '))
if lineNo then
actions.goto(1, lineNo)
actions.go_to(1, lineNo)
else
setStatus('Invalid line number')
end
@@ -593,9 +604,9 @@ local __actions = {
if ny < y or ny == y and nx <= x then
setStatus(messages.wrapped)
end
actions.goto(nx, ny)
actions.go_to(nx, ny)
actions.mark_to(nx + #pattern, ny)
actions.goto(nx, ny)
actions.go_to(nx, ny)
return
end
sx = 1
@@ -625,7 +636,7 @@ local __actions = {
if bReadOnly then
setError("Access denied")
else
local ok, err = save(sPath)
local ok = save(sPath)
if ok then
setStatus('"%s" %dL, %dC written',
fileInfo.path, #tLines, fs.getSize(fileInfo.abspath))
@@ -641,7 +652,7 @@ local __actions = {
run = function()
local sTempPath = "/.temp"
local ok, err = save(sTempPath)
local ok = save(sTempPath)
if ok then
local nTask = shell.openTab(sTempPath)
if nTask then
@@ -732,7 +743,7 @@ local __actions = {
mark_to = function(nx, ny)
actions.mark_begin()
actions.goto(nx, ny)
actions.go_to(nx, ny)
actions.mark_finish()
end,
@@ -795,9 +806,7 @@ local __actions = {
actions.dirty_all()
end,
setCursor = function(newX, newY)
local oldX, oldY = lastPos.x, lastPos.y
setCursor = function()
lastPos.x = x
lastPos.y = y
@@ -806,27 +815,23 @@ local __actions = {
if screenX < 1 then
scrollX = x - 1
screenX = 1
actions.dirty_all()
elseif screenX > w then
scrollX = x - w
screenX = w
actions.dirty_all()
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
actions.dirty_all()
elseif screenY > h - 1 then
scrollY = y - (h - 1)
screenY = h - 1
actions.dirty_all()
end
end,
top = function()
actions.goto(1, 1)
actions.go_to(1, 1)
end,
bottom = function()
@@ -856,11 +861,11 @@ local __actions = {
end,
pageUp = function()
actions.goto(x, y - (h - 1))
actions.go_to(x, y - (h - 1))
end,
pageDown = function()
actions.goto(x, y + (h - 1))
actions.go_to(x, y + (h - 1))
end,
home = function()
@@ -966,7 +971,7 @@ local __actions = {
local front = tLines[sy]:sub(1, sx - 1)
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)
end
tLines[y] = front .. back
@@ -981,7 +986,7 @@ local __actions = {
local count = 0
local lines = { }
for y = csy, cey do
for _ = csy, cey do
local line = tLines[y]
if line then
local x = 1
@@ -1042,22 +1047,25 @@ local __actions = {
end,
toggle_clipboard = function()
if clipboard.shim then
if clipboard then
clipboard.setInternal(not clipboard.internal)
end
if clipboard.isInternal() then
setStatus('Using internal clipboard')
else
setStatus('Using system clipboard')
end
end
end,
copy_marked = function()
local text, size =
actions.copyText(mark.x, mark.y, mark.ex, mark.ey)
local text, size = actions.copyText(mark.x, mark.y, mark.ex, mark.ey)
if clipboard then
clipboard.setData(text)
setStatus('%d chars copied', size)
clipboard.useInternal(true)
else
os.queueEvent('clipboard_copy', text)
end
setStatus('%d chars copied', size)
end,
cut = function()
@@ -1078,7 +1086,7 @@ local __actions = {
if mark.active then
actions.delete()
end
if clipboard.isInternal() then
if clipboard and clipboard.isInternal() then
text = clipboard.getText()
end
if text then
@@ -1089,7 +1097,7 @@ local __actions = {
end
end,
goto = function(cx, cy)
go_to = function(cx, cy)
y = math.min(math.max(cy, 1), #tLines)
x = math.min(math.max(cx, 1), #tLines[y] + 1)
end,
@@ -1120,8 +1128,7 @@ load(sPath)
term.setCursorBlink(true)
redraw()
if not keyboard then
keyboard = { control, shift, combo }
keyboard = { }
function keyboard:translate(event, code)
if event == 'key' then
@@ -1129,14 +1136,18 @@ if not keyboard then
if ch then
if code == keys.leftCtrl or code == keys.rightCtrl then
if not self.control then
self.control = true
self.combo = false
end
return
end
if code == keys.leftShift or code == keys.rightShift then
if not self.shift then
self.shift = true
self.combo = false
end
return
end
@@ -1213,7 +1224,6 @@ if not keyboard then
return event
end
end
end
while bRunning do
local sEvent, param, param2, param3 = os.pullEventRaw()

View File

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