8000 Shell & multishell · neumond/python-computer-craft@1f94bca · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f94bca

Browse files
committed
Shell & multishell
1 parent cb60509 commit 1f94bca

File tree

4 files changed

+121
-16
lines changed

4 files changed

+121
-16
lines changed

computercraft/lua.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ def get_expr_code(self):
1010
raise NotImplementedError
1111

1212

13+
class ArbLuaExpr(LuaExpr):
14+
def __init__(self, code: str):
15+
self._code = code
16+
17+
def get_expr_code(self):
18+
return self._code
19+
20+
1321
def lua_string(v):
1422
return '"{}"'.format(
1523
v.replace('\\', '\\\\')

computercraft/subapis/multishell.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from typing import Optional
2+
13
from .base import BaseSubAPI
2-
from ..rproc import integer, string, nil, boolean
4+
from ..rproc import integer, nil, boolean, option_string
35

46

57
class MultishellAPI(BaseSubAPI):
@@ -9,18 +11,16 @@ async def getCurrent(self) -> int:
911
async def getCount(self) -> int:
1012
return integer(await self._send('getCount'))
1113

12-
async def launch(self, programPath: str, *args: str, environment: dict = None) -> int:
13-
if environment is None:
14-
environment = {}
14+
async def launch(self, environment: dict, programPath: str, *args: str) -> int:
1515
return integer(await self._send('launch', environment, programPath, *args))
1616

1717
async def setTitle(self, tabID: int, title: str):
1818
return nil(await self._send('setTitle', tabID, title))
1919

20-
async def getTitle(self, tabID: int) -> str:
21-
return string(await self._send('getTitle', tabID))
20+
async def getTitle(self, tabID: int) -> Optional[str]:
21+
return option_string(await self._send('getTitle', tabID))
2222

23-
async def setFocus(self, tabID: int):
23+
async def setFocus(self, tabID: int) -> bool:
2424
return boolean(await self._send('setFocus', tabID))
2525

2626
async def getFocus(self) -> int:

computercraft/subapis/shell.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import List, Dict
1+
from typing import List, Dict, Optional
22

33
from .base import BaseSubAPI
4-
from ..rproc import nil, string, boolean, integer, array_string, fact_mono_dict
4+
from ..rproc import nil, string, boolean, integer, array_string, fact_mono_dict, option_string
55

66

77
map_string_string = fact_mono_dict(string, string)
@@ -26,8 +26,8 @@ async def setPath(self, path: str):
2626
async def resolve(self, localPath: str) -> str:
2727
return string(await self._send('resolve', localPath))
2828

29-
async def resolveProgram(self, name: str) -> str:
30-
return string(await self._send('resolveProgram', name))
29+
async def resolveProgram(self, name: str) -> Optional[str]:
30+
return option_string(await self._send('resolveProgram', name))
3131

3232
async def aliases(self) -> Dict[str, str]:
3333
return map_string_string(await self._send('aliases'))
@@ -44,10 +44,13 @@ async def programs(self, showHidden: bool = None) -> List[str]:
4444
async def getRunningProgram(self) -> str:
4545
return string(await self._send('getRunningProgram'))
4646

47-
async def run(self, command: str, *args: List[str]):
47+
async def run(self, command: str, *args: str) -> bool:
4848
return boolean(await self._send('run', command, *args))
4949

50-
async def openTab(self, command: str, *args: List[str]) -> int:
50+
async def execute(self, command: str, *args: str) -> bool:
51+
return boolean(await self._send('execute', command, *args))
52+
53+
async def openTab(self, command: str, *args: str) -> int:
5154
return integer(await self._send('openTab', command, *args))
5255

5356
async def switchTab(self, tabID: int):
@@ -59,6 +62,11 @@ async def complete(self, prefix: str) -> List[str]:
5962
async def completeProgram(self, prefix: str) -> List[str]:
6063
return array_string(await self._send('completeProgram', prefix))
6164

62-
# TODO: autocomplete functions
63-
# async def setCompletionFunction(self, path: str)
64-
# async def getCompletionInfo(self) -> LuaTable
65+
# these functions won't be implemented
66+
# it's far better to keep this in lua code
67+
68+
# setCompletionFunction
69+
# getCompletionInfo
70+
71+
# we can create callbacks to python code, but this will require
72+
# connection to python, and will break the shell if python disconnects

testmod.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,4 +1543,93 @@ async def test_pocket(api):
15431543
await api.print('Test finished successfully')
15441544

15451545

1546+
async def test_multishell(api):
1547+
from computercraft.subapis.multishell import MultishellAPI
1548+
tbl = await get_object_table(api, 'multishell')
1549+
assert get_class_table(MultishellAPI) == tbl
1550+
1551+
await step(api, 'Close all additional shells')
1552+
1553+
assert await api.multishell.getCount() == 1
1554+
assert await api.multishell.getCurrent() == 1
1555+
assert await api.multishell.getFocus() == 1
1556+
assert isinstance(await api.multishell.getTitle(1), str)
1557+
1558+
title = f'new title {random.randint(1, 1000000)}'
1559+
assert await api.multishell.setTitle(1, title) is None
1560+
assert await api.multishell.getTitle(1) == title
1561+
1562+
assert await api.multishell.setFocus(1) is True
1563+
assert await api.multishell.setFocus(0) is False
1564+
assert await api.multishell.setFocus(2) is False
1565+
1566+
assert await api.multishell.getTitle(2) is None
1567+
1568+
assert await api.multishell.launch({}, 'rom/programs/fun/hello.lua') == 2
1569+
assert isinstance(await api.multishell.getTitle(2), str)
1570+
1571+
await api.print('Test finished successfully')
1572+
1573+
1574+
async def test_shell(api):
1575+
from computercraft.subapis.shell import ShellAPI
1576+
tbl = await get_object_table(api, 'shell')
1577+
1578+
del tbl['function']['setCompletionFunction']
1579+
del tbl['function']['getCompletionInfo']
1580+
assert get_class_table(ShellAPI) == tbl
1581+
1582+
assert await api.shell.complete('ls ro') == ['m/', 'm']
1583+
assert await api.shell.completeProgram('lu') == ['a']
1584+
1585+
ps = await api.shell.programs()
1586+
assert 'shutdown' in ps
1587+
1588+
als = await api.shell.aliases()
1589+
assert 'ls' in als
1590+
assert als['ls'] == 'list'
1591+
assert 'xls' not in als
1592+
assert await api.shell.setAlias('xls', 'list') is None
1593+
als = await api.shell.aliases()
1594+
assert 'xls' in als
1595+
assert als['xls'] == 'list'
1596+
assert await api.shell.clearAlias('xls') is None
1597+
als = await api.shell.aliases()
1598+
assert 'xls' not in als
1599+
1600+
assert await api.shell.getRunningProgram() == 'py'
1601+
1602+
assert await api.shell.resolveProgram('doesnotexist') is None
1603+
assert await api.shell.resolveProgram('hello') == 'rom/programs/fun/hello.lua'
1604+
1605+
assert await api.shell.dir() == ''
1606+
assert await api.shell.resolve('doesnotexist') == 'doesnotexist'
1607+
assert await api.shell.resolve('startup.lua') == 'startup.lua'
1608+
assert await api.shell.setDir('rom') is None
1609+
assert await api.shell.dir() == 'rom'
1610+
assert await api.shell.resolve('startup.lua') == 'rom/startup.lua'
1611+
assert await api.shell.setDir('') is None
1612+
1613+
assert isinstance(await api.shell.path(), str)
1614+
assert await api.shell.setPath(await api.shell.path()) is None
1615+
1616+
assert await api.shell.execute('hello') is True
1617+
assert await api.shell.run('hello') is True
1618+
assert await api.shell.execute('doesnotexist') is False
1619+
assert await api.shell.run('doesnotexist') is False
1620+
1621+
tab = await api.shell.openTab('hello')
1622+
assert isinstance(tab, int)
1623+
1624+
await step(api, f'Program has been launched in tab {tab}')
1625+
1626+
assert await api.shell.switchTab(tab) is None
1627+
1628+
await step(api, 'Computer will shutdown after test due to shell.exit')
1629+
1630+
assert await api.shell.exit() is None
1631+
1632+
await api.print('Test finished successfully')
1633+
1634+
15461635
# vector won't be implemented, use python equivalent

0 commit comments

Comments
 (0)
0