Configs2026年7月20日·1 分钟阅读

crossterm — Cross-Platform Terminal Manipulation Library for Rust

A pure Rust library for terminal manipulation that provides a unified API for cursor movement, styling, input events, and screen control across Windows, macOS, and Linux.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
crossterm Terminal Library
先审查命令
npx -y tokrepo@latest install 09956331-841a-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

crossterm is a pure Rust library that provides cross-platform terminal manipulation capabilities. It offers a single API for cursor control, text styling, input event handling, and raw mode across Windows (including legacy cmd.exe), macOS, and Linux terminals. It is the low-level foundation used by popular Rust TUI frameworks like Ratatui.

What crossterm Does

  • Controls cursor position, visibility, and shape across all major platforms
  • Applies text colors, background colors, bold, italic, underline, and other attributes
  • Reads keyboard, mouse, and resize events synchronously or asynchronously
  • Manages terminal modes including raw mode and alternate screen buffers
  • Provides both command-based (queued) and immediate execution models

Architecture Overview

crossterm abstracts platform-specific terminal APIs behind a unified command interface. On Unix systems it uses ANSI escape sequences written to stdout. On Windows it uses the Console API for legacy terminals and ANSI sequences for Windows Terminal. Commands can be executed immediately with execute!() or queued with queue!() for batch flushing. The event system uses platform-native polling (epoll on Linux, kqueue on macOS, WaitForMultipleObjects on Windows).

Self-Hosting & Configuration

  • Add crossterm as a dependency in Cargo.toml
  • No C dependencies or build scripts; compiles as pure Rust
  • Enable the event-stream feature for async event reading with tokio or async-std
  • Use the serde feature to serialize/deserialize events and styles
  • Compatible with Rust stable; no nightly features required

Key Features

  • Pure Rust with no C bindings or unsafe dependencies
  • Works on Windows (cmd.exe and Windows Terminal), macOS, and Linux
  • Supports both synchronous and async (tokio/async-std) event streams
  • Command pattern with queue!() and execute!() macros for efficient output
  • Foundation library for Ratatui, the most popular Rust TUI framework

Comparison with Similar Tools

  • termion — Unix-only Rust terminal library; crossterm adds Windows support
  • ncurses/pancurses — C-based curses bindings; crossterm is pure Rust without FFI
  • console (Rust) — focused on high-level styling; crossterm provides lower-level terminal control
  • tcell (Go) — similar cross-platform terminal library but for the Go ecosystem
  • prompt_toolkit (Python) — Python terminal toolkit; crossterm serves the Rust ecosystem

FAQ

Q: Does crossterm work on Windows cmd.exe? A: Yes. It uses the Windows Console API for legacy terminals and ANSI sequences for modern Windows Terminal.

Q: Is crossterm used by Ratatui? A: Yes. Ratatui uses crossterm as its default backend for terminal rendering.

Q: Can I read mouse events? A: Yes. Enable mouse capture with crossterm::event::EnableMouseCapture and read mouse events from the event stream.

Q: Does crossterm support async event handling? A: Yes. Enable the event-stream feature to use EventStream with tokio or async-std.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。