import os
import re
import subprocess
import sys
SOCKET = os.environ.get("TMUX_SOCKET", "")
PROMPT = "❯" SGR = re.compile(r"\x1b\[[0-9;]*m")
def classify(capture):
lines = [l for l in capture.splitlines() if PROMPT in l]
if not lines:
return "no-prompt", ""
raw = lines[-1].split(PROMPT, 1)[1]
text = SGR.sub("", raw).strip()
if not text:
return "empty", text
if "\x1b[2m" in raw:
return "suggestion", text
return "typed", text
def main(panes):
for pane in panes:
try:
out = subprocess.run(
["tmux", *(["-S", SOCKET] if SOCKET else []),
"capture-pane", "-t", pane, "-p", "-e"],
capture_output=True, text=True, check=True,
).stdout
except subprocess.CalledProcessError:
print(f"{pane} missing |")
continue
state, text = classify(out)
if state == "no-prompt":
print(f"{pane} no-prompt |")
continue
print(f"{pane} {state} | {text[:100]}")
if __name__ == "__main__":
main(sys.argv[1:])