### Install Tauri Plugin Pseudo Terminal (Shell) Source: https://github.com/tnze/tauri-plugin-pty/blob/main/README.md Instructions for installing the tauri-plugin-pty using Cargo and npm. This involves adding the plugin to your project's dependencies. ```shell # Install this plugin in your Cargo.toml carp add tauri-plugin-pty # Install the api package npm install tauri-pty ``` -------------------------------- ### Initialize Tauri Plugin Pseudo Terminal (Rust) Source: https://github.com/tnze/tauri-plugin-pty/blob/main/README.md Example of how to initialize the tauri-plugin-pty within a Tauri application's Rust backend. This involves adding the plugin to the Tauri builder. ```rust tauri::Builder::default() .plugin(tauri_plugin_pty::init()) // add this .run(tauri::generate_context!()) .expect("error while running tauri application"); ... ``` -------------------------------- ### Install Tauri Plugin PTY Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Installs the Tauri Plugin PTY Rust crate and its JavaScript API package. This is the first step to enable PTY functionality in your Tauri application. ```bash # Install the Rust plugin in your Tauri project cargo add tauri-plugin-pty # Install the JavaScript API package npm install tauri-pty ``` -------------------------------- ### Complete xterm.js Integration Example Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt This example demonstrates a full integration of the Tauri PTY plugin with xterm.js to create a terminal emulator within a Tauri application. It handles PTY output, terminal input, resizing, and process exit events, providing a robust terminal experience. Dependencies include `tauri-pty`, `@xterm/xterm`, `@xterm/addon-fit`, and `@tauri-apps/plugin-os`. ```typescript import { Terminal } from "@xterm/xterm"; import { FitAddon } from "@xterm/addon-fit"; import "@xterm/xterm/css/xterm.css"; import { spawn } from "tauri-pty"; import { platform } from "@tauri-apps/plugin-os"; // Initialize xterm.js terminal const term = new Terminal({ convertEol: true, cursorBlink: true, fontSize: 14, fontFamily: "Menlo, Monaco, 'Courier New', monospace", theme: { background: "#1e1e1e", foreground: "#d4d4d4" } }); // Setup fit addon for automatic resizing const fitAddon = new FitAddon(); term.loadAddon(fitAddon); term.open(document.getElementById("terminal")); fitAddon.fit(); // Spawn appropriate shell based on platform const shell = platform() === "windows" ? "powershell.exe" : "bash"; const pty = spawn(shell, [], { cols: term.cols, rows: term.rows, env: { TERM: "xterm-256color" } }); // Connect PTY output to terminal display pty.onData((data) => { term.write(new Uint8Array(data)); }); // Connect terminal input to PTY term.onData((data) => { pty.write(data); }); // Handle terminal resize term.onResize((e) => { pty.resize(e.cols, e.rows); }); // Handle window resize window.addEventListener("resize", () => { fitAddon.fit(); }); // Handle process exit pty.onExit(({ exitCode }) => { term.write(`\r\n\r\n[Process exited with code ${exitCode}]\r\n`); }); ``` -------------------------------- ### IPtyForkOptions Interface for PTY Process Configuration Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt The `IPtyForkOptions` and `IWindowsPtyForkOptions` interfaces define configuration settings for spawning PTY processes. These options cover terminal name, dimensions, working directory, environment variables, and platform-specific settings like ConPTY on Windows. Proper configuration ensures the PTY behaves as expected. Dependencies include `tauri-pty`. ```typescript import { spawn, IPtyForkOptions, IWindowsPtyForkOptions } from "tauri-pty"; // Unix-specific options const unixOptions: IPtyForkOptions = { name: "xterm-256color", // Terminal name ($TERM) cols: 80, // Initial columns rows: 24, // Initial rows cwd: "/home/user/project", // Working directory env: { // Environment variables PATH: "/usr/local/bin:/usr/bin:/bin", HOME: "/home/user", SHELL: "/bin/bash" }, encoding: "utf8", // Character encoding handleFlowControl: false, // XON/XOFF flow control uid: 1000, // Unix user ID (use with caution) gid: 1000 // Unix group ID (use with caution) }; // Windows-specific options const windowsOptions: IWindowsPtyForkOptions = { cols: 120, rows: 30, cwd: "C:\\Users\\user\\project", useConpty: true, // Use ConPTY (Windows 10 1809+) conptyInheritCursor: true // Inherit cursor position }; const pty = spawn("bash", ["-l"], unixOptions); ``` -------------------------------- ### Spawn Pseudo Terminal Process (TypeScript) Source: https://github.com/tnze/tauri-plugin-pty/blob/main/README.md Demonstrates how to spawn a pseudo-terminal process using the tauri-pty library in TypeScript. It shows initialization of xterm.js and data transport between the terminal and the spawned process. ```typescript import { Terminal } from "xterm" import { spawn } from "tauri-pty"; // init xterm.js const term = new Terminal(); term.open(/* DOM Elem */); // spawn shell const pty = spawn("powershell.exe", [/* args */], { cols: term.cols, rows: term.rows, }) // transport data pty.onData(data => term.write(data)) term.onData(data => pty.write(data)) ``` -------------------------------- ### Spawn Pseudo Terminal Process (TypeScript) Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Spawns a new pseudo terminal process with configurable options like dimensions, working directory, and environment variables. Supports different shells and platforms, including specific options for Windows using ConPTY. ```typescript import { spawn } from "tauri-pty"; // Spawn a shell with custom configuration const pty = spawn("bash", ["-l"], { cols: 80, // Terminal width in columns rows: 24, // Terminal height in rows cwd: "/home/user", // Working directory env: { // Environment variables "TERM": "xterm-256color", "LANG": "en_US.UTF-8" }, name: "xterm-256color" // Terminal name for $TERM }); // On Windows, spawn PowerShell const ptyWindows = spawn("powershell.exe", [], { cols: 120, rows: 30, useConpty: true, // Use ConPTY on Windows 10+ conptyInheritCursor: false // ConPTY cursor inheritance }); console.log(`Spawned process with PID: ${pty.pid}`); console.log(`Terminal size: ${pty.cols}x${pty.rows}`); ``` -------------------------------- ### Initialize Tauri Plugin PTY in Rust Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Registers the PTY plugin within your Tauri application's main.rs file. This makes the PTY functionality available to your application. ```rust # src-tauri/src/main.rs #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] fn main() { tauri::Builder::default() .plugin(tauri_plugin_pty::init()) // Register the PTY plugin .run(tauri::generate_context!()) .expect("error while running tauri application"); } ``` -------------------------------- ### Send Data to PTY using write Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt The `write` method sends input data to the pseudo-terminal. It can be used to send commands, keystrokes, and control sequences. This method is crucial for interacting with spawned processes, allowing for dynamic command execution and user input simulation. Dependencies include the `tauri-pty` library. ```typescript import { spawn } from "tauri-pty"; const pty = spawn("bash", [], { cols: 80, rows: 24 }); // Send a command to the shell pty.write("echo 'Hello from Tauri PTY'\n"); // Send special key sequences pty.write("\x03"); // Ctrl+C (SIGINT) pty.write("\x04"); // Ctrl+D (EOF) pty.write("\x1a"); // Ctrl+Z (SIGTSTP) // Send arrow keys (ANSI escape sequences) pty.write("\x1b[A"); // Up arrow pty.write("\x1b[B"); // Down arrow pty.write("\x1b[C"); // Right arrow pty.write("\x1b[D"); // Left arrow ``` -------------------------------- ### Resize Terminal Dimensions using resize Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt The `resize` method updates the pseudo-terminal dimensions, essential for maintaining correct text wrapping and cursor positioning when the UI terminal size changes. It takes the new column and row counts as arguments. This is often integrated with window resize events for a dynamic user experience. Dependencies include the `tauri-pty` library. ```typescript import { spawn } from "tauri-pty"; const pty = spawn("bash", [], { cols: 80, rows: 24 }); // Resize to new dimensions pty.resize(120, 40); console.log(`New size: ${pty.cols}x${pty.rows}`); // Integrate with window resize events window.addEventListener("resize", () => { const terminalElement = document.getElementById("terminal"); const cols = Math.floor(terminalElement.clientWidth / 9); // Approximate char width const rows = Math.floor(terminalElement.clientHeight / 17); // Approximate char height pty.resize(cols, rows); }); ``` -------------------------------- ### Configure Tauri Capability for PTY Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Configures the necessary capability permissions in your Tauri application's schema file to allow PTY operations. This ensures the plugin has the required access. ```json { "$schema": "../gen/schemas/desktop-schema.json", "identifier": "default", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "core:default", "pty:default" ] } ``` -------------------------------- ### Receive Data from PTY (TypeScript) Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Sets up a listener to receive data output from the pseudo terminal. Data is received as a Uint8Array and can be decoded into a string or directly used with terminal UI libraries like xterm.js. The listener can be disposed to stop receiving data. ```typescript import { spawn } from "tauri-pty"; const pty = spawn("bash", [], { cols: 80, rows: 24 }); // Listen for data from the PTY const dataListener = pty.onData((data: Uint8Array) => { // Convert to string for display const text = new TextDecoder().decode(data); console.log("Received from PTY:", text); // Or write directly to xterm.js // terminal.write(data); }); // Later, stop listening dataListener.dispose(); ``` -------------------------------- ### Terminate PTY Process using kill Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt The `kill` method terminates the spawned pseudo-terminal process. On Unix-like systems, it supports optionally specifying a signal name for more controlled termination. This method is vital for resource management and cleanly shutting down background processes. Dependencies include the `tauri-pty` library. ```typescript import { spawn } from "tauri-pty"; const pty = spawn("bash", [], { cols: 80, rows: 24 }); // Gracefully terminate the process pty.kill(); // On Unix, you can specify a signal (not supported on Windows) // pty.kill("SIGTERM"); // pty.kill("SIGKILL"); ``` -------------------------------- ### Handle PTY Process Exit (TypeScript) Source: https://context7.com/tnze/tauri-plugin-pty/llms.txt Attaches an event handler to be notified when the spawned pseudo terminal process terminates. It provides the exit code and, on Unix systems, the signal number that caused the termination. This is useful for cleanup or restarting logic. ```typescript import { spawn } from "tauri-pty"; const pty = spawn("bash", ["-c", "echo 'Hello World'; exit 0"], { cols: 80, rows: 24 }); // Handle process exit const exitListener = pty.onExit(({ exitCode, signal }) => { console.log(`Process exited with code: ${exitCode}`); if (signal !== undefined) { console.log(`Terminated by signal: ${signal}`); } // Cleanup or restart logic if (exitCode !== 0) { console.error("Process failed!"); } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.