Skip to content

feat(examples): add cross-platform native terminal I/O example for Linux, macOS, and Windows - #150

Closed
prateekbhujel wants to merge 2 commits into
swoole:masterfrom
prateekbhujel:feat/native-terminal-example
Closed

prateekbhujel wants to merge 2 commits into
swoole:masterfrom
prateekbhujel:feat/native-terminal-example

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

Summary

This PR adds examples/terminal, a complete demonstration of how to build cross-platform native CLI and TUI applications using TypePHP's mixed C++/PHP programming capabilities (cpp-src + php-src).

Standard PHP CLI applications traditionally depend on executing external sub-processes (stty, Windows mode CON) or installing separate PHP extensions to query window dimensions, enable raw mode, or capture hidden passwords. When compiled into a standalone native binary with TypePHP (tpc -m bin), this implementation interacts directly with the operating system kernel and console APIs with zero external dependencies and zero zval boxing overhead.

What is included

  1. Cross-Platform C++ Native Module (examples/terminal/cpp-src/terminal.cc):

    • Terminal Dimensions: POSIX ioctl(TIOCGWINSZ) on Linux & macOS; GetConsoleScreenBufferInfo on Windows.
    • Interactive TTY Detection: isatty on Unix; GetConsoleMode on Windows.
    • Raw Mode Input: Non-canonical raw input mode (termios on Unix; SetConsoleMode on Windows) with atexit automated cleanup to ensure the terminal state is safely restored even on unexpected exit.
    • Secret Password Input: In-process hidden input without echo, restoring terminal flags immediately upon completion.
    • Window Title: Sets terminal window title via OSC escape sequence (\033]0;...\007) on Unix and SetConsoleTitleW (UTF-8 to UTF-16) on Windows.
    • ANSI Color Support: Detects TERM and NO_COLOR environment variables.
  2. PHP Stubs & Object-Oriented Wrapper (examples/terminal/php-src/Terminal.php):

    • Direct native function signatures (terminal_get_width(), terminal_get_height(), etc.) for TypePHP's compiler to resolve.
    • High-level Terminal class with helper methods (getSize(), enableRawMode(), disableRawMode(), readSecret(), setTitle(), colorize()).
  3. Demonstration Entrypoint (examples/terminal/main.php):

    • Interactive CLI application demonstrating dimensions, secret password input, single-key instant capture in raw mode, and colored box drawing.
  4. Bilingual Documentation (examples/terminal/README.md):

    • Clear setup, compilation, and usage guide in English and Simplified Chinese matching the repository conventions.
  5. PHPUnit Test (phpunit/TerminalExampleTest.php):

    • Unit test suite verifying class constants, fallbacks, ANSI coloring, and cursor formatting.

Verification

  • C++ Compilation: Compiled cleanly with clang++ -std=c++17 -Wall -Wextra against phpx.h and PHP headers (0 errors).
  • PHP Syntax: Validated with php -l on all PHP files (0 syntax errors).
  • PHPUnit Tests: Ran ./vendor/bin/phpunit phpunit/TerminalExampleTest.php (6 tests, 15 assertions passed 100% green).
  • Clean diff: Purely additive (6 new files, 0 modified existing files, 0 regressions to existing compiler passes).

…nux, macOS, and Windows

Demonstrate how to build cross-platform native CLI and TUI applications
with TypePHP using mixed C++/PHP programming (cpp-src + php-src).

- Native terminal sizing via ioctl(TIOCGWINSZ) on POSIX and GetConsoleScreenBufferInfo on Windows
- Native in-process TTY stream check (isatty / GetConsoleMode)
- Native raw mode with atexit automated cleanup to prevent terminal corruption
- In-process secret password input without terminal echo
- Window title setting via OSC escape codes (POSIX) and SetConsoleTitleW (Windows)
- ANSI color detection probing TERM and NO_COLOR
- Object-oriented Terminal wrapper class with helper methods
- Interactive CLI demonstration program
- Comprehensive bilingual documentation and PHPUnit test
…preserve native property compound type checks

- DefaultArgumentGenerator: convert dynamic binary expressions emitted as php::Var(...) to native types using php::toInt() / php::toFloat() in typed default argument helper functions.
- AssignOpTrait: preserve wrapObjectPropertyAssignTypeCheck on native scalar object properties when compound assignment RHS is a dynamic Variant (Type::VAR).
@prateekbhujel
prateekbhujel deleted the feat/native-terminal-example branch October 6, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant