55 lines
1.2 KiB
Python
55 lines
1.2 KiB
Python
import pty
|
|
import os
|
|
import sys
|
|
import time
|
|
import select
|
|
import termios
|
|
import struct
|
|
import fcntl
|
|
|
|
def set_winsize(fd, row, col):
|
|
winsize = struct.pack("HHHH", row, col, 0, 0)
|
|
fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)
|
|
|
|
def send_text(fd, text):
|
|
for char in text:
|
|
os.write(fd, char.encode('utf-8'))
|
|
time.sleep(0.1)
|
|
|
|
pid, fd = pty.fork()
|
|
if pid == 0:
|
|
os.environ['TERM'] = 'xterm-256color'
|
|
os.execvp('codex', ['codex', '--no-alt-screen'])
|
|
else:
|
|
set_winsize(fd, 40, 120)
|
|
time.sleep(6)
|
|
|
|
# First /status
|
|
send_text(fd, "/status\r")
|
|
time.sleep(8)
|
|
|
|
# Clear line by pressing ESC, or Backspace
|
|
send_text(fd, "\x1b") # ESC
|
|
time.sleep(1)
|
|
|
|
# Second /status
|
|
send_text(fd, "/status\r")
|
|
time.sleep(5)
|
|
|
|
output = b""
|
|
while True:
|
|
r, _, _ = select.select([fd], [], [], 2)
|
|
if r:
|
|
try:
|
|
data = os.read(fd, 4096)
|
|
if not data: break
|
|
output += data
|
|
except OSError:
|
|
break
|
|
else:
|
|
break
|
|
|
|
print(output.decode('utf-8', errors='ignore'))
|
|
|
|
send_text(fd, "/exit\r")
|
|
time.sleep(1)
|