Newby translatted some c++ into first rustc
⚓ Rust 📅 2026-03-21 👤 surdeus 👁️ 1//author: Richard Wade Brooks @richard.wade.brooks@gmail.com
//
// This work is dedicated to the public domain under CC0 1.0.
// You may use, modify, and distribute it without restriction.
// Deed - CC0 1.0 Universal - Creative Commons
fn main() {
//if connected to terminal use color in stderr outputs
let mut on_console: bool;
on_console = stderr_is_terminal();
if on_console {
//eprintln!("\x1b[91mcolor in stderr\x1b[0m"); //red
//eprintln!("\x1b[92mcolor in stderr\x1b[0m"); //green
//eprintln!("\x1b[93mcolor in stderr\x1b[0m"); //yellow
//eprintln!("\x1b[94mcolor in stderr\x1b[0m"); //blue
eprintln!("\x1b[95mcolor in stderr\x1b[0m"); //purple
//eprintln!("\x1b[96mcolor in stderr\x1b[0m"); //cyan
//eprintln!("\x1b[97mcolor in stderr\x1b[0m"); //white
} else {
eprintln!("not connected to terminal");
}
println!("\n\n\n");
on_console = stdout_is_terminal();
if on_console {
println!("stdout is connected to a terminal")
} else {
println!("stdout is connected to a file or pipe")
}
on_console = stderr_is_terminal();
if on_console {
println!("stderr is connected to a terminal")
} else {
println!("stderr is connected to a file or pipe")
}
on_console = stdin_is_terminal();
if on_console {
println!("stdin is connected to a terminal")
} else {
println!("stdin is connected to a file or pipe")
}
}
//----------------------------------------------------------
fn stderr_is_terminal() -> bool {
use std::io::{self, IsTerminal};
let on_console = io::stderr().is_terminal();
return on_console;
}
fn stdout_is_terminal() -> bool {
use std::io::{self, IsTerminal};
let on_console = io::stdout().is_terminal();
return on_console;
}
fn stdin_is_terminal() -> bool {
use std::io::{self, IsTerminal};
let on_console = io::stdin().is_terminal();
return on_console;
}
/*
How to test from a bash shell
Save this helper script as test_terminal_modes.sh
#!/usr/bin/env bash
set -u
echo
echo "1) Normal terminal"
echo "Command: ./stream_env"
./stream_env
echo
echo "2) stdin redirected from file"
echo "Command: ./stream_env < test.log"
./stream_env < test.log
echo
echo "3) stdout and stderr redirected to file"
echo "Command: ./stream_env > out.log 2>&1"
./stream_env > out.log 2>&1
echo "Contents of out.log:"
cat out.log
echo
echo "4) stdin piped from another program"
echo "Command: echo hello | ./stream_env"
echo hello | ./stream_env
echo
echo "5) stdout piped to another program"
echo "Command: ./stream_env | cat"
./stream_env | cat
echo
echo "6) stdout and stderr both piped"
echo "Command: ./stream_env 2>&1 | cat"
./stream_env 2>&1 | cat
echo
echo "7) full pipeline"
echo "Command: echo hello | ./stream_env | cat"
echo hello | ./stream_env | cat
Make it executable:
chmod +x test_terminal_modes.sh
Run it:
./test_terminal_modes.sh
Expected results
-
./stream_env
stdin is_terminal: true
stdout is_terminal: true
stderr is_terminal: true -
./stream_env < test.log
stdin is_terminal: false
stdout is_terminal: true
stderr is_terminal: true -
./stream_env > out.log 2>&1
while running:
stdin is_terminal: true
stdout is_terminal: false
stderr is_terminal: falsethose lines go into out.log, not to the screen
-
echo hello | ./stream_env
stdin is_terminal: false
stdout is_terminal: true
stderr is_terminal: true -
./stream_env | cat
stdin is_terminal: true
stdout is_terminal: false
stderr is_terminal: true -
./stream_env 2>&1 | cat
stdin is_terminal: true
stdout is_terminal: false
stderr is_terminal: false -
echo hello | ./stream_env | cat
stdin is_terminal: false
stdout is_terminal: false
stderr is_terminal: true
Notes
-
Order matters in shell redirection.
This:
./stream_env > out.log 2>&1sends stdout to out.log first, then sends stderr to the same place.
But this:
./stream_env 2>&1 > out.logsends stderr to the current stdout first, usually the screen,
and only then sends stdout to out.log. -
A bare file name at the end of the command is just an argument.
This:
./stream_env 2>&1 test.logdoes not redirect to test.log.
It passes "test.log" as an argument to the program. -
stderr is printed with eprintln!, so when stderr is still attached
to the terminal, it may appear on screen even if stdout is piped.
*/
1 post - 1 participant
🏷️ Rust_feed