Skip to content

Instantly share code, notes, and snippets.

@llandsmeer
Created May 31, 2022 16:21
Show Gist options
  • Save llandsmeer/804dd7d9c176690f889159272e51c99c to your computer and use it in GitHub Desktop.
Save llandsmeer/804dd7d9c176690f889159272e51c99c to your computer and use it in GitHub Desktop.
import sys
import pyte
import struct
import select
import time
import fcntl
import os
import pty
import subprocess
import termios
from subprocess import PIPE
def get_tmux(t):
server, client = pty.openpty()
cols, rows = map(int, subprocess.getoutput("tmux display -p -t 0 '#{window_width}x#{window_height}'").split('x'))
s = struct.pack('HHHH', rows, cols, 0, 0)
fcntl.ioctl(client, termios.TIOCSWINSZ, s)
subprocess.Popen(
['tmux', '-u', '-N', 'attach', '-t', str(t)],
preexec_fn=os.setsid,
stdin=client,
stdout=client,
stderr=client,
universal_newlines=True
)
time.sleep(0.1) # wait a while else there is no output yet
output = b''
start = time.monotonic()
while True:
r, _, _ = select.select([ server ], [], [], 0)
if server not in r or time.monotonic() - start > 1:
# if there is no output anymore, we exit
# if we're reading for too, it's probably scrolling continuously so skip that
break
output += os.read(server, 1024)
os.close(client)
screen = pyte.Screen(cols, rows)
stream = pyte.Stream(screen)
stream.feed(output.decode('utf8'))
return '\n'.join(screen.display)
print('''
<style>
pre {
background-color: black;
color: white;
}
</style>
''')
for pane in subprocess.getoutput('tmux -N ls').splitlines():
paneid = int(pane.split(':')[0])
pane_title = pane
pane_content = get_tmux(paneid)
print(f'<h1>{pane_title}</h1>')
print(f'<pre>{pane_content}</pre>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment