Files
wren/example/pexpect_demo.wren
T
retoor 79ff93c9a2 feat: add pexpect and yaml modules with jinja from-import support
Add new pexpect module for process spawning and interaction, including Spawn class with expect, sendline, and timeout handling. Introduce yaml module for YAML parsing with support for nested structures, lists, and data types. Extend jinja template engine with brace tokenization and FromImportNode for macro imports across templates. Register all new foreign functions in modules.c and include generated demo scripts.
2026-01-25 21:09:29 +00:00

72 lines
2.0 KiB
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "pexpect" for Spawn, Pexpect
System.print("=== Pexpect Module Demo ===\n")
System.print("--- Basic Spawn and Expect ---")
var child = Spawn.new("echo 'Hello from pexpect!'")
var idx = child.expect(["Hello"])
System.print("Matched pattern index: %(idx)")
System.print("After match: %(child.after)")
child.close()
System.print("\n--- Interactive Process ---")
child = Spawn.new("cat")
child.sendline("First line")
child.expect(["First line"])
System.print("Echo received: %(child.after)")
child.sendline("Second line")
child.expect(["Second line"])
System.print("Echo received: %(child.after)")
child.sendeof()
child.close()
System.print("\n--- Multiple Patterns ---")
child = Spawn.new("printf 'username: '")
idx = child.expect(["password:", "username:", "login:"])
System.print("Matched: %(idx == 1 ? "username" : "other")")
child.close()
System.print("\n--- Exact String Matching ---")
child = Spawn.new("echo 'The answer is 42'")
idx = child.expectExact(["42"])
System.print("Found exact match: %(child.after)")
System.print("Text before: %(child.before)")
child.close()
System.print("\n--- Timeout Handling ---")
child = Spawn.new("sleep 10")
child.timeout = 0.5
idx = child.expect(["never_match"])
if (idx == Pexpect.TIMEOUT) {
System.print("Got timeout as expected")
}
child.terminate(true)
child.close()
System.print("\n--- Process Properties ---")
child = Spawn.new("echo test")
System.print("PID: %(child.pid)")
System.print("Is alive: %(child.isalive)")
child.expect(["test"])
child.wait()
System.print("Exit status: %(child.exitstatus)")
child.close()
System.print("\n--- Read Non-blocking ---")
child = Spawn.new("printf 'quick output'")
var data = child.readNonblocking(100, 1)
System.print("Read: %(data.trim())")
child.close()
System.print("\n--- Constants ---")
System.print("EOF constant: %(Pexpect.EOF)")
System.print("TIMEOUT constant: %(Pexpect.TIMEOUT)")
System.print("\n--- Pexpect.run() ---")
var output = Pexpect.run("echo 'Simple run'")
System.print("Output: %(output.trim())")
System.print("\n=== Demo Complete ===")