Skip to content

Commit

Permalink
switch to character input on stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Mar 21, 2024
1 parent 273d9f2 commit ac1a483
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/rtems_proxy/telnet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import asyncio
import signal
import sys
import termios
import tty
from time import sleep

import telnetlib3
Expand All @@ -21,18 +24,29 @@ def terminate(self, *args):
self.terminated = True

async def user_input(self, writer):
loop = asyncio.events._get_running_loop()

while self.running:
# run the wait for input in a separate thread
cmd = await loop.run_in_executor(None, input)
# look for control + ] to terminate the session
if b"\x1d" in cmd.encode():
self.running = False
break
writer.write(cmd)
writer.write("\r")
writer.close()
def get_char():
ch = sys.stdin.read(1)
return ch

stdin_fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(stdin_fd)

try:
tty.setraw(sys.stdin.fileno())
loop = asyncio.events._get_running_loop()

while self.running:
# run the wait for input in a separate thread
next_ch = await loop.run_in_executor(None, get_char)
# look for control + ] to terminate the session
if b"\x1d" in next_ch.encode():
self.running = False
break
writer.write(next_ch)

finally:
termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_settings)
writer.close()

async def server_output(self, reader):
while self.running:
Expand Down

0 comments on commit ac1a483

Please sign in to comment.