More examples

Yay!
This commit is contained in:
SquidDev 2020-11-20 21:59:17 +00:00
parent b0651082f4
commit c35707725f
6 changed files with 97 additions and 30 deletions

View File

@ -35,14 +35,20 @@ export default {
await Promise.all(files await Promise.all(files
.filter(x => path.extname(x) !== ".ts") .filter(x => path.extname(x) !== ".ts")
.map(file => fs.writeFile(`${input}/mount/${file}.d.ts`, template)) .map(async file => {
const path = `${input}/mount/${file}.d.ts`;
const contents = await fs.readFile(path, { encoding: "utf-8" }).catch(() => "");
if (contents !== template) await fs.writeFile(path, template);
})
); );
return options; return options;
}, },
async transform(code, file) { async transform(code, file) {
// Allow loading files in /mount. // Allow loading files in /mount.
if (path.extname(file) != ".lua" && path.basename(file) != ".settings") return null; const ext = path.extname(file);
return `export default ${JSON.stringify(code)};\n`; return ext != '.tsx' && ext != '.ts' && path.dirname(file) === path.resolve(`${input}/mount`)
? `export default ${JSON.stringify(code)};\n`
: null;
}, },
} }
], ],

View File

@ -64,6 +64,10 @@ end
-- --
-- @treturn table|nil The parsed image data, suitable for use with -- @treturn table|nil The parsed image data, suitable for use with
-- @{paintutils.drawImage}, or `nil` if the file does not exist. -- @{paintutils.drawImage}, or `nil` if the file does not exist.
-- @usage Load an image and draw it.
--
-- local image = paintutils.loadImage("test-image.nfp")
-- paintutils.drawImage(image, term.getCursorPos())
function loadImage(path) function loadImage(path)
expect(1, path, "string") expect(1, path, "string")
@ -107,6 +111,7 @@ end
-- @tparam number endY The end y position of the line. -- @tparam number endY The end y position of the line.
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be -- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
-- the current background colour if not specified. -- the current background colour if not specified.
-- @usage paintutils.drawLine(2, 3, 30, 7, colors.red)
function drawLine(startX, startY, endX, endY, colour) function drawLine(startX, startY, endX, endY, colour)
expect(1, startX, "number") expect(1, startX, "number")
expect(2, startY, "number") expect(2, startY, "number")
@ -170,6 +175,7 @@ end
-- @tparam number endY The end y position of the line. -- @tparam number endY The end y position of the line.
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be -- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
-- the current background colour if not specified. -- the current background colour if not specified.
-- @usage paintutils.drawBox(2, 3, 30, 7, colors.red)
function drawBox(startX, startY, endX, endY, nColour) function drawBox(startX, startY, endX, endY, nColour)
expect(1, startX, "number") expect(1, startX, "number")
expect(2, startY, "number") expect(2, startY, "number")
@ -222,6 +228,7 @@ end
-- @tparam number endY The end y position of the line. -- @tparam number endY The end y position of the line.
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be -- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
-- the current background colour if not specified. -- the current background colour if not specified.
-- @usage paintutils.drawFilledBox(2, 3, 30, 7, colors.red)
function drawFilledBox(startX, startY, endX, endY, nColour) function drawFilledBox(startX, startY, endX, endY, nColour)
expect(1, startX, "number") expect(1, startX, "number")
expect(2, startY, "number") expect(2, startY, "number")

View File

@ -1,18 +1,19 @@
--- Provides a simple implementation of multitasking. --[[- Provides a simple implementation of multitasking.
--
-- Functions are not actually executed simultaniously, but rather this API will Functions are not actually executed simultaniously, but rather this API will
-- automatically switch between them whenever they yield (eg whenever they call automatically switch between them whenever they yield (eg whenever they call
-- @{coroutine.yield}, or functions that call that - eg `os.pullEvent` - or @{coroutine.yield}, or functions that call that - eg `os.pullEvent` - or
-- functions that call that, etc - basically, anything that causes the function functions that call that, etc - basically, anything that causes the function
-- to "pause"). to "pause").
--
-- Each function executed in "parallel" gets its own copy of the event queue, Each function executed in "parallel" gets its own copy of the event queue,
-- and so "event consuming" functions (again, mostly anything that causes the and so "event consuming" functions (again, mostly anything that causes the
-- script to pause - eg `sleep`, `rednet.receive`, most of the `turtle` API, script to pause - eg `sleep`, `rednet.receive`, most of the `turtle` API,
-- etc) can safely be used in one without affecting the event queue accessed by etc) can safely be used in one without affecting the event queue accessed by
-- the other. the other.
--
-- @module parallel @module parallel
]]
local function create(...) local function create(...)
local tFns = table.pack(...) local tFns = table.pack(...)
@ -70,21 +71,53 @@ local function runUntilLimit(_routines, _limit)
end end
end end
--- Switches between execution of the functions, until any of them --[[- Switches between execution of the functions, until any of them
-- finishes. If any of the functions errors, the message is propagated upwards finishes. If any of the functions errors, the message is propagated upwards
-- from the @{parallel.waitForAny} call. from the @{parallel.waitForAny} call.
--
-- @tparam function ... The functions this task will run @tparam function ... The functions this task will run
@usage Print a message every second until the `q` key is pressed.
local function tick()
while true do
os.sleep(1)
print("Tick")
end
end
local function wait_for_q()
repeat
local _, key = os.pullEvent("key")
until key == keys.q
print("Q was pressed!")
end
parallel.waitForAny(tick, wait_for_q)
print("Everything done!")
]]
function waitForAny(...) function waitForAny(...)
local routines = create(...) local routines = create(...)
return runUntilLimit(routines, #routines - 1) return runUntilLimit(routines, #routines - 1)
end end
--- Switches between execution of the functions, until all of them are --[[- Switches between execution of the functions, until all of them are
-- finished. If any of the functions errors, the message is propagated upwards finished. If any of the functions errors, the message is propagated upwards
-- from the @{parallel.waitForAll} call. from the @{parallel.waitForAll} call.
--
-- @tparam function ... The functions this task will run @tparam function ... The functions this task will run
@usage Start off two timers and wait for them both to run.
local function a()
os.sleep(1)
print("A is done")
end
local function b()
os.sleep(3)
print("B is done")
end
parallel.waitForAll(a, b)
print("Everything done!")
]]
function waitForAll(...) function waitForAll(...)
local routines = create(...) local routines = create(...)
return runUntilLimit(routines, 0) return runUntilLimit(routines, 0)

View File

@ -252,8 +252,8 @@ local function drawCanvasLine(y)
bg = bg .. color_hex_lookup[pixel or canvasColour] bg = bg .. color_hex_lookup[pixel or canvasColour]
else else
text = text .. "\127" text = text .. "\127"
fg = fg .. color_hex_lookup[canvasColour] fg = fg .. color_hex_lookup[colours.grey]
bg = bg .. color_hex_lookup[colours.grey] bg = bg .. color_hex_lookup[canvasColour]
end end
end end

View File

@ -4,10 +4,15 @@ import type { ComponentChild } from "preact";
import settingsFile from "./mount/.settings"; import settingsFile from "./mount/.settings";
import startupFile from "./mount/startup.lua"; import startupFile from "./mount/startup.lua";
import exprTemplate from "./mount/expr_template.lua"; import exprTemplate from "./mount/expr_template.lua";
import exampleImage from "./mount/example.nfp";
const defaultFiles: { [filename: string]: string } = { const defaultFiles: { [filename: string]: string } = {
".settings": settingsFile, ".settings": settingsFile,
"startup.lua": startupFile, "startup.lua": startupFile,
// TODO: Ideally this'd be in data/image.nfp or something, but copy-cat's
// dir bootstrapping doesn't cope with that right now.
"test-image.nfp": exampleImage
}; };
const clamp = (value: number, min: number, max: number): number => { const clamp = (value: number, min: number, max: number): number => {

16
src/web/mount/example.nfp Normal file
View File

@ -0,0 +1,16 @@
fffffffffffffffffffffff
f444444444444444444444f
f444444444444444444444f
f44fffffffffffffffff44f
f44ff0ffffffffffffff44f
f44fff0fffffffffffff44f
f44ff0ffffffffffffff44f
f44fffffffffffffffff44f
f44fffffffffffffffff44f
f44fffffffffffffffff44f
f44fffffffffffffffff44f
f44fffffffffffffffff44f
f444444444444444444444f
f4444444444444444fff44f
f444444444444444444444f
fffffffffffffffffffffff