Introduction to run-pty
The run-pty utility is a powerful tool designed for developers to execute and control command-line applications with pseudo-terminal handling. It provides a wide range of APIs to enhance productivity and control over terminal applications.
API Examples
run_pty(command: str, **kwargs)
Executes a command in a new pseudo-terminal.
import run_pty result = run_pty.run_pty("ls -la") print(result.stdout)
watch_pty(command: str, callback)
Executes a command and monitors its output, using a callback to process each line.
def my_callback(line): print(f"Output: {line}") run_pty.watch_pty("ping google.com", my_callback)
send_signal(pty_proc, signal)
Sends a signal to a running pty process.
import signal proc = run_pty.run_pty("top") run_pty.send_signal(proc, signal.SIGINT)
terminate(pty_proc)
Terminates a running pty process.
proc = run_pty.run_pty("top") run_pty.terminate(proc)
App Example Using run-pty
Let’s create a simple Python application that uses various run-pty APIs to manage a long-running process.
import run_pty import signal import time def long_running_task(): print("Starting a long-running task...") proc = run_pty.run_pty("ping google.com") # Watch and process the output def process_output(line): print(f"Ping Output: {line}") run_pty.watch_pty("ping google.com", process_output) # Run the task for 10 seconds and then terminate time.sleep(10) run_pty.send_signal(proc, signal.SIGINT) print("Task completed.") if __name__ == "__main__": long_running_task()
With these examples, you can see how versatile and powerful run-pty is for managing and interacting with command-line applications programmatically.
Hash: 37b641f1affc8a9ed251f9e8cb2958611b77002c40c577c2f9143913f86201ba