This example demonstrates how to use Rust and crossterm to accept both piped stdin input and interactive terminal input in a single CLI app. Given the scarcity of examples on piping into crossterm, I created this to provide an example for anyone trying to do the same.
the key to using using piped inputs is to use the use-dev-tty
feature of crossterm. This allows you to use the input()
function to read from stdin. Without this feature, the input()
function will not work with piped input.
- Piped
stdin
Input: Accept input directly from another command using pipes. - Interactive Terminal Input: Type and interact directly within the terminal.
- Colored UI: Different colors for prompts, user input, and system responses for clarity.
- Command Handling: Recognizes
exit
andquit
commands for easy termination. - Keyboard Support: Use
CTRL+C
to exit and the backspace key for text corrections.
-
Clone the Repository:
git clone https://github.com/cosmikwolf/terminal-input-interface.git cd terminal-input-interface
-
Run the Application:
cargo run
-
Experience Piped Input:
echo "Sample piped input" | cargo run
- crossterm: Used for creating the terminal interface.
This project is under the MIT License. See the LICENSE file for more details.