Textutils & pocket
This commit is contained in:
parent
48551dac9c
commit
92df8748d8
|
@ -120,6 +120,22 @@ array_integer = fact_array(integer)
|
|||
array_string = fact_array(string)
|
||||
option_integer = fact_option(integer)
|
||||
option_string = fact_option(string)
|
||||
_try_result = fact_tuple(boolean, option_string, tail_nils=1)
|
||||
|
||||
|
||||
def try_result(result):
|
||||
success, error_msg = _try_result(result)
|
||||
if success:
|
||||
assert error_msg is None
|
||||
return None
|
||||
else:
|
||||
raise LuaException(error_msg)
|
||||
|
||||
|
||||
def flat_try_result(result):
|
||||
if result is True:
|
||||
return None
|
||||
return try_result(result)
|
||||
|
||||
|
||||
option_string_bool = fact_option(fact_union(
|
||||
|
|
|
@ -20,6 +20,7 @@ from .subapis.keys import KeysAPI
|
|||
from .subapis.multishell import MultishellAPI
|
||||
from .subapis.os import OSAPI
|
||||
from .subapis.peripheral import PeripheralAPI
|
||||
from .subapis.pocket import PocketAPI
|
||||
from .subapis.rednet import RednetAPI
|
||||
from .subapis.redstone import RedstoneAPI
|
||||
from .subapis.settings import SettingsAPI
|
||||
|
@ -64,6 +65,7 @@ class CCAPI(RootAPIMixin):
|
|||
self.multishell = MultishellAPI(self)
|
||||
self.os = OSAPI(self)
|
||||
self.peripheral = PeripheralAPI(self)
|
||||
self.pocket = PocketAPI(self)
|
||||
self.rednet = RednetAPI(self)
|
||||
self.redstone = RedstoneAPI(self)
|
||||
self.settings = SettingsAPI(self)
|
||||
|
|
|
@ -4,10 +4,9 @@ from typing import Optional, List, Tuple, Any, Union
|
|||
|
||||
from .base import BaseSubAPI, LuaNum
|
||||
from .mixins import TermMixin
|
||||
from ..errors import LuaException
|
||||
from ..rproc import (
|
||||
boolean, nil, integer, string, option_integer, option_string,
|
||||
tuple2_integer, array_string, option_string_bool, fact_tuple,
|
||||
tuple2_integer, array_string, option_string_bool, try_result,
|
||||
)
|
||||
|
||||
|
||||
|
@ -217,9 +216,6 @@ class CCSpeaker(CCPeripheral):
|
|||
return boolean(await self._send('playSound', sound, volume, pitch))
|
||||
|
||||
|
||||
run_result = fact_tuple(boolean, option_string, tail_nils=1)
|
||||
|
||||
|
||||
class CCCommandBlock(CCPeripheral):
|
||||
async def getCommand(self) -> str:
|
||||
return string(await self._send('getCommand'))
|
||||
|
@ -228,11 +224,7 @@ class CCCommandBlock(CCPeripheral):
|
|||
return nil(await self._send('setCommand', command))
|
||||
|
||||
async def runCommand(self):
|
||||
success, error_msg = run_result(await self._send('runCommand'))
|
||||
if not success:
|
||||
raise LuaException(error_msg)
|
||||
else:
|
||||
assert error_msg is None
|
||||
return try_result(await self._send('runCommand'))
|
||||
|
||||
|
||||
TYPE_MAP = {
|
||||
|
|
12
computercraft/subapis/pocket.py
Normal file
12
computercraft/subapis/pocket.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from .base import BaseSubAPI
|
||||
from ..rproc import flat_try_result
|
||||
|
||||
|
||||
class PocketAPI(BaseSubAPI):
|
||||
_API = 'pocket'
|
||||
|
||||
async def equipBack(self):
|
||||
return flat_try_result(await self._send('equipBack'))
|
||||
|
||||
async def unequipBack(self) -> bool:
|
||||
return flat_try_result(await self._send('unequipBack'))
|
|
@ -1,19 +1,19 @@
|
|||
from typing import List, Union
|
||||
|
||||
from .base import BaseSubAPI, LuaNum
|
||||
from ..rproc import nil, string, integer, array_string
|
||||
from ..rproc import nil, string, integer
|
||||
|
||||
|
||||
class TextutilsAPI(BaseSubAPI):
|
||||
_API = 'textutils'
|
||||
|
||||
async def slowWrite(self, text: str, rate: LuaNum):
|
||||
async def slowWrite(self, text: str, rate: LuaNum = None):
|
||||
return nil(await self._send('slowWrite', text, rate))
|
||||
|
||||
async def slowPrint(self, text: str, rate: LuaNum):
|
||||
async def slowPrint(self, text: str, rate: LuaNum = None):
|
||||
return nil(await self._send('slowPrint', text, rate))
|
||||
|
||||
async def formatTime(self, time: LuaNum, twentyFourHour: bool) -> str:
|
||||
async def formatTime(self, time: LuaNum, twentyFourHour: bool = None) -> str:
|
||||
return string(await self._send('formatTime', time, twentyFourHour))
|
||||
|
||||
async def tabulate(self, *rows_and_colors: Union[list, int]):
|
||||
|
@ -25,8 +25,8 @@ class TextutilsAPI(BaseSubAPI):
|
|||
async def pagedPrint(self, text: str, freeLines: int = None) -> int:
|
||||
return integer(await self._send('pagedPrint', text, freeLines))
|
||||
|
||||
async def complete(self, partialName: str, environment: dict = None) -> List[str]:
|
||||
return array_string(await self._send('complete', partialName, environment))
|
||||
def complete(self, partial: str, possible: List[str]) -> List[str]:
|
||||
return [p[len(partial):] for p in possible if p.startswith(partial)]
|
||||
|
||||
# Questionable to implement
|
||||
# serialize
|
||||
|
@ -34,4 +34,7 @@ class TextutilsAPI(BaseSubAPI):
|
|||
|
||||
# Will not implement, use pythonic equivalents
|
||||
# serializeJSON
|
||||
# unserializeJSON
|
||||
# urlEncode
|
||||
# json_null
|
||||
# empty_json_array
|
||||
|
|
89
testmod.py
89
testmod.py
|
@ -1417,10 +1417,6 @@ async def test_commandblock_peripheral(api):
|
|||
await api.print('Test finished successfully')
|
||||
|
||||
|
||||
async def test_turtle_peripheral(api):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
async def test_modem_wrap(api):
|
||||
side = 'back'
|
||||
|
||||
|
@ -1460,3 +1456,88 @@ async def test_modem_wrap(api):
|
|||
|
||||
await api.print('You must have heard levelup sound')
|
||||
await api.print('Test finished successfully')
|
||||
|
||||
|
||||
async def test_turtle_peripheral(api):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
async def test_textutils(api):
|
||||
assert await api.textutils.slowWrite('write ') is None
|
||||
assert await api.textutils.slowWrite('write ', 5) is None
|
||||
assert await api.textutils.slowPrint('print') is None
|
||||
assert await api.textutils.slowPrint('print', 5) is None
|
||||
|
||||
assert await api.textutils.formatTime(0) == '0:00 AM'
|
||||
assert await api.textutils.formatTime(0, True) == '0:00'
|
||||
|
||||
table = [
|
||||
api.colors.red,
|
||||
['Planet', 'Distance', 'Mass'],
|
||||
api.colors.gray,
|
||||
['Mercury', '0.387', '0.055'],
|
||||
api.colors.lightGray,
|
||||
['Venus', '0.723', '0.815'],
|
||||
api.colors.green,
|
||||
['Earth', '1.000', '1.000'],
|
||||
api.colors.red,
|
||||
['Mars', '1.524', '0.107'],
|
||||
api.colors.orange,
|
||||
['Jupiter', '5.203', '318'],
|
||||
api.colors.yellow,
|
||||
['Saturn', '9.537', '95'],
|
||||
api.colors.cyan,
|
||||
['Uranus', '19.191', '14.5'],
|
||||
api.colors.blue,
|
||||
['Neptune', '30.069', '17'],
|
||||
api.colors.white,
|
||||
]
|
||||
|
||||
assert await api.textutils.tabulate(*table) is None
|
||||
|
||||
lines = await api.textutils.pagedPrint('''
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
Suspendisse feugiat diam et velit aliquam, nec porttitor eros facilisis.
|
||||
Nulla facilisi.
|
||||
Sed eget dui vel tellus aliquam fermentum.
|
||||
Aliquam sed lorem congue, dignissim nulla in, porta diam.
|
||||
Aliquam erat volutpat.
|
||||
'''.strip())
|
||||
assert isinstance(lines, int)
|
||||
assert lines > 0
|
||||
|
||||
assert await api.textutils.pagedTabulate(*table[:-1], *table[2:-1], *table[2:]) is None
|
||||
|
||||
assert api.textutils.complete('co', ['command', 'row', 'column']) == [
|
||||
'mmand', 'lumn']
|
||||
|
||||
await api.print('Test finished successfully')
|
||||
|
||||
|
||||
async def test_pocket(api):
|
||||
assert await api.peripheral.isPresent('back') is False
|
||||
|
||||
from computercraft.subapis.pocket import PocketAPI
|
||||
tbl = await get_object_table(api, 'pocket')
|
||||
assert get_class_table(PocketAPI) == tbl
|
||||
|
||||
await step(api, 'Clean inventory from any pocket upgrades')
|
||||
|
||||
with assert_raises(LuaException):
|
||||
await api.pocket.equipBack()
|
||||
with assert_raises(LuaException):
|
||||
await api.pocket.unequipBack()
|
||||
assert await api.peripheral.isPresent('back') is False
|
||||
|
||||
await step(api, 'Put any pocket upgrade to inventory')
|
||||
|
||||
assert await api.pocket.equipBack() is None
|
||||
assert await api.peripheral.isPresent('back') is True
|
||||
|
||||
assert await api.pocket.unequipBack() is None
|
||||
assert await api.peripheral.isPresent('back') is False
|
||||
|
||||
await api.print('Test finished successfully')
|
||||
|
||||
|
||||
# vector won't be implemented, use python equivalent
|
||||
|
|
Loading…
Reference in a new issue