-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from bash/better-detection
Better detection, document caveats
- Loading branch information
Showing
15 changed files
with
304 additions
and
167 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Caveats | ||
|
||
Extra care needs to be taken on Unix if your program might share | ||
the terminal with another program. This might be the case | ||
if you expect your output to be used with a pager e.g. `your_program` | `less`. | ||
In that case, a race condition exists because the pager will also set the terminal to raw mode. | ||
The `pager` example shows a heuristic to deal with this issue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
metadata=$(cargo metadata --format-version 1 --no-deps | jq '.packages | map(select(.name == "terminal-colorsaurus")) | first | .metadata.docs.rs') | ||
features=$(echo "$metadata" | jq -r '.features | join(",")') | ||
|
||
export RUSTDOCARGS=$(echo "$metadata" | jq -r '.["rustdoc-args"] | join(" ")') | ||
cargo +nightly doc -Zunstable-options -Zrustdoc-scrape-examples --features "$features" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::error::Error; | ||
use terminal_colorsaurus::{color_scheme, QueryOptions}; | ||
|
||
/// This example is best in a couple of different ways: | ||
/// 1. `cargo run --example pager`—should print the color scheme. | ||
/// 2. `cargo run --example pager | less`—should not print the color scheme. | ||
/// 3. `cargo run --example pager > file.txt`—should print the color scheme. | ||
/// 4. `cargo run --example pager > /dev/null`—should print the color scheme. | ||
fn main() -> Result<(), Box<dyn Error>> { | ||
if should_auto_detect() { | ||
eprintln!( | ||
"Here's the color scheme: {:#?}", | ||
color_scheme(QueryOptions::default())? | ||
); | ||
} else { | ||
eprintln!("You're likely using a pager, doing nothing"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
// Our stdout might be piped to a pager (e.g. `less`), | ||
// in which case we have a race condition for enabling/disabling the raw mode | ||
// and for reading/writing to the terminal. | ||
// | ||
// Note that this is just heuristic with both | ||
// false negatives (output not piped to a pager) and | ||
// false positives (stderr piped to a pager). | ||
#[cfg(unix)] | ||
fn should_auto_detect() -> bool { | ||
use std::os::fd::AsFd; | ||
!is_pipe(std::io::stdout().as_fd()).unwrap_or_default() | ||
} | ||
|
||
#[cfg(not(unix))] | ||
fn should_auto_detect() -> bool { | ||
true | ||
} | ||
|
||
// The mode can be bitwise AND-ed with S_IFMT to extract the file type code, and compared to the appropriate constant | ||
// Source: https://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html | ||
#[cfg(unix)] | ||
fn is_pipe(fd: std::os::fd::BorrowedFd) -> std::io::Result<bool> { | ||
use libc::{S_IFIFO, S_IFMT}; | ||
Ok(fstat(fd)?.st_mode & S_IFMT == S_IFIFO) | ||
} | ||
|
||
#[cfg(unix)] | ||
fn fstat(fd: std::os::fd::BorrowedFd) -> std::io::Result<libc::stat> { | ||
use std::os::fd::AsRawFd as _; | ||
// SAFETY: | ||
// 1. File descriptor is valid (we have a borrowed fd for the lifetime of this function) | ||
// 2. fstat64 fills the stat structure for us (if successful). | ||
unsafe { | ||
let mut stat = std::mem::zeroed(); | ||
let ret = libc::fstat(fd.as_raw_fd(), &mut stat); | ||
if ret == 0 { | ||
Ok(stat) | ||
} else { | ||
Err(std::io::Error::last_os_error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
#[cfg(not(target_os = "macos"))] | ||
mod poll; | ||
#[cfg(not(target_os = "macos"))] | ||
pub(crate) use poll::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.