Skip to content

Commit

Permalink
[Update] typer as cli & formatter & ^J fixed
Browse files Browse the repository at this point in the history
use `typer` as cli structure
use `black` as formatter
fix ^J in readline tab list
  • Loading branch information
jingfelix committed Jan 23, 2023
1 parent 3c66e0a commit d95f446
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
21 changes: 15 additions & 6 deletions rcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import readline
import asyncio
import logging
import typer

app = typer.Typer()


# TODO: load config from file or input
HOST = "192.168.0.30"
PORT = 25575
PASSWORD = "logitech"
# HOST = "192.168.0.30"
# PORT = 25575
# PASSWORD = "logitech"

LOG_FILENAME = "/tmp/completer.log"
logging.basicConfig(
Expand All @@ -23,7 +27,7 @@

# 注册补全功能
with open(history_path, "r") as f:
OPTIONS = f.readlines()
OPTIONS = [line.replace("\n", "") for line in f.readlines()]

readline.set_completer(SimpleCompleter(OPTIONS).complete)

Expand All @@ -32,8 +36,13 @@
# 使用 vi 模式
readline.parse_and_bind("set editing-mode vi")

if __name__ == "__main__":

connection = Rcon(host=HOST, port=PORT, password=PASSWORD)
def main(host: str, port: int, password: str):

connection = Rcon(host=host, port=port, password=password)

asyncio.run(connection.mainloop())


if __name__ == "__main__":
typer.run(main)
3 changes: 2 additions & 1 deletion rcon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import os.path as path
history_path = path.join(path.dirname(__file__), "history.log")

history_path = path.join(path.dirname(__file__), "history.log")
1 change: 0 additions & 1 deletion rcon/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ def complete(self, text, state):
response = None
logging.debug("complete(%s, %s) => %s", repr(text), state, repr(response))
return response

4 changes: 2 additions & 2 deletions rcon/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ async def mainloop(self):
try:
command = "list"
response = await self.client.send_cmd(command)
print(f'[{datetime.now().strftime("%H:%M:%S")}] {response[0]}')
print(f'[{datetime.now().strftime("%H:%M:%S")}] {response[0]}\n')

while True:
command = input("> ").strip()
response = await self.client.send_cmd(command)
print(f'[{datetime.now().strftime("%H:%M:%S")}] {response[0]}')
print(f'[{datetime.now().strftime("%H:%M:%S")}] {response[0]}\n')

except KeyboardInterrupt:
await self.client.close()
Expand Down

0 comments on commit d95f446

Please sign in to comment.