I would like to simulate a coding session (for video recording session : I am not a touch typist :-)
For example, I have a shell script like this (test.sh)
hello="Hello"
world="world"
echo $hello", "$world
And I have a python script like this (Simulate_KeyPresses.py) :
import sys
import time
import subprocess
def send_letter(letter):
# V1 : simple print
sys.stdout.write(letter)
sys.stdout.flush()
# V2: Test with expect (apt-get install expect)
# cmd = """echo 'send "{}"' | expect""".format(c)
# subprocess.run(cmd, shell=True)
def simulate_keypresses(content):
lines = content.split("\n")
for line in lines:
for c in line:
send_letter(c)
time.sleep(0.03)
send_letter("\n")
time.sleep(0.5)
if __name__ == "__main__":
filename = sys.argv[1]
with open(filename, "r") as f:
content = f.read()
simulate_keypresses(content)
Which I can invoke like this :
python Simulate_KeyPresses.py test.sh
And it works beautifully. However when I pipe it to bash, like this:
python Simulate_KeyPresses.py test.sh | /bin/bash
I get
Hello, world
i.e I only get stdout and the key presses are not shown.
What I would like to see:
hello="Hello"
world="world"
echo $hello", "$world
Hello, world
I found a related answer (Simulate interactive python session), but it only handle python coding sessions.
I tried to use Expect, but it does not work as intended (does not show stdin also).
Help would be appreciated!