Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext Tauri Terminal Commands

From Leeroopedia
Knowledge Sources
Domains Terminal, Tauri_Desktop, PTY
Last Updated 2026-10-24 00:00 GMT

Overview

Tauri command handlers for spawning and managing PTY-based terminal sessions, providing spawn, write, resize, and kill operations with real-time output streaming via Tauri events.

Description

The terminal.rs module in src-tauri/src/commands/ implements four Tauri commands backed by the portable_pty crate. terminal_spawn creates a pseudo-terminal pair, spawns a child process with configurable command, args, cwd, env, and dimensions, then streams output via terminal-output events and exit codes via terminal-exit events. terminal_write sends input to the PTY writer. terminal_resize updates the PTY dimensions. terminal_kill terminates the child process and cleans up the session. All sessions are stored in a shared HashMap<String, TerminalSession> in Tauri's AppState.

Usage

Invoke these commands from the React frontend to embed interactive terminal sessions in the desktop app's TerminalSidebar component.

Code Reference

Source Location

Signature

#[tauri::command]
pub(crate) fn terminal_spawn(
    app: tauri::AppHandle,
    state: State<AppState>,
    options: TerminalSpawnOptions,
) -> CmdResult<serde_json::Value>

#[tauri::command]
pub(crate) fn terminal_write(
    state: State<AppState>,
    options: TerminalWriteOptions,
) -> CmdResult<()>

#[tauri::command]
pub(crate) fn terminal_resize(
    state: State<AppState>,
    options: TerminalResizeOptions,
) -> CmdResult<()>

#[tauri::command]
pub(crate) fn terminal_kill(
    state: State<AppState>,
    options: TerminalKillOptions,
) -> CmdResult<()>

pub(crate) struct TerminalSpawnOptions {
    id: Option<String>,
    command: String,
    args: Option<Vec<String>>,
    cwd: Option<String>,
    env: Option<HashMap<String, String>>,
    cols: Option<u16>,
    rows: Option<u16>,
}

Import

// Registered in src-tauri/src/main.rs invoke_handler:
// tauri::generate_handler![
//     commands::terminal::terminal_spawn,
//     commands::terminal::terminal_write,
//     commands::terminal::terminal_resize,
//     commands::terminal::terminal_kill,
// ]

I/O Contract

Inputs

Name Type Required Description
id Option<String> No Session ID (auto-generated as "term-N" if omitted)
command String Yes Executable path to spawn
args Option<Vec<String>> No Command arguments
cwd Option<String> No Working directory
env Option<HashMap<String,String>> No Environment variables
cols Option<u16> No Terminal columns (default 80)
rows Option<u16> No Terminal rows (default 24)
data String Yes (write) Input data to write to PTY

Outputs

Name Type Description
spawn returns JSON { id: String } — session identifier
terminal-output Event { id, data } — streamed PTY output
terminal-exit Event { id, code } — process exit code

Usage Examples

Spawning a Terminal Session

import { invoke } from '@tauri-apps/api/core';

const result = await invoke('terminal_spawn', {
  options: {
    command: 'bash',
    cols: 120,
    rows: 40,
    cwd: '/home/user/project'
  }
});
const termId = result.id; // "term-1"

Listening to Output and Sending Input

import { invoke } from '@tauri-apps/api/core';
import { listen } from '@tauri-apps/api/event';

// Listen for output
const unlisten = await listen('terminal-output', (event) => {
  const { id, data } = event.payload;
  if (id === termId) {
    term.write(data); // write to xterm.js instance
  }
});

// Send user input
await invoke('terminal_write', { options: { id: termId, data: 'ls -la\n' } });

// Resize terminal
await invoke('terminal_resize', { options: { id: termId, cols: 120, rows: 40 } });

// Kill session on cleanup
await invoke('terminal_kill', { options: { id: termId } });
await unlisten();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment