From 55ef54c94f2e0a9a0f4a367d600d89f68bef5dbb Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 9 Sep 2023 17:29:28 -0600 Subject: [PATCH 1/2] Use std::io::IsTerminal instead of is-terminal which raises MSRV to 1.70.0 --- CHANGELOG.md | 7 +++ Cargo.toml | 2 +- fuzz/Cargo.lock | 130 ------------------------------------------------ src/lib.rs | 2 +- 4 files changed, 9 insertions(+), 132 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3dd01851..18e4f1dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# Unreleased (ReleaseDate) + +### Changed + +- Switch from `is-terminal` to `std::io::IsTerminal`. +- Minimal Supported Rust Version bumped to 1.70 + # 0.10.0 (2022-12-27) ## Fixed - Fix panic due to incorrect ANSI escape handling ([#137]) diff --git a/Cargo.toml b/Cargo.toml index 65fd38eab..df0469637 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ keywords = ["tab", "table", "format", "pretty", "print"] categories = ["command-line-interface"] license = "BSD-3-Clause" edition = "2018" +rust-version = "1.70" exclude = [ "prettytable-evcxr.png" ] @@ -39,6 +40,5 @@ name = "prettytable" unicode-width = "0.1" term = "0.7" lazy_static = "1.4" -is-terminal = "0.4" encode_unicode = "1.0" csv = { version = "1.1", optional = true } diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 43cf56484..e662f080c 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -90,27 +90,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "getrandom" version = "0.2.8" @@ -122,37 +101,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "hermit-abi" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] - -[[package]] -name = "io-lifetimes" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "is-terminal" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" -dependencies = [ - "hermit-abi", - "io-lifetimes", - "rustix", - "windows-sys", -] - [[package]] name = "itoa" version = "0.4.8" @@ -191,12 +139,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "linux-raw-sys" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" - [[package]] name = "memchr" version = "2.5.0" @@ -215,7 +157,6 @@ version = "0.10.0" dependencies = [ "csv", "encode_unicode", - "is-terminal", "lazy_static", "term", "unicode-width", @@ -273,20 +214,6 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -[[package]] -name = "rustix" -version = "0.36.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" -dependencies = [ - "bitflags", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - [[package]] name = "rustversion" version = "1.0.11" @@ -386,60 +313,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" - -[[package]] -name = "windows_i686_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" - -[[package]] -name = "windows_i686_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/src/lib.rs b/src/lib.rs index e4fdb78d4..a1a315bac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,7 +194,7 @@ impl<'a> TableSlice<'a> { /// # Returns /// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens pub fn print_tty(&self, force_colorize: bool) -> Result { - use is_terminal::IsTerminal; + use std::io::IsTerminal; match (stdout(), io::stdout().is_terminal() || force_colorize) { (Some(mut o), true) => self.print_term(&mut *o), _ => self.print(&mut io::stdout()), From 1e2c2d5c3d888037465e029b5df7155e797b2189 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 9 Sep 2023 17:52:41 -0600 Subject: [PATCH 2/2] Table::print_term will now only print colors to terminals The practical benefit is that it is now possible to print a colorized table to stderr. --- CHANGELOG.md | 1 + src/lib.rs | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e4f1dd7..4ca3d4e63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Switch from `is-terminal` to `std::io::IsTerminal`. - Minimal Supported Rust Version bumped to 1.70 +- `Table::print_term` will now only print colors if the output is a terminal. # 0.10.0 (2022-12-27) ## Fixed diff --git a/src/lib.rs b/src/lib.rs index a1a315bac..d334cc69d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,13 +10,13 @@ extern crate lazy_static; use std::fmt; -use std::io::{self, Error, Write}; +use std::io::{self, Error, Write, IsTerminal}; use std::iter::{FromIterator, IntoIterator}; use std::ops::{Index, IndexMut}; use std::slice::{Iter, IterMut}; pub use term::{color, Attr}; -pub(crate) use term::{stdout, Terminal}; +pub(crate) use term::Terminal; mod cell; pub mod format; @@ -180,9 +180,16 @@ impl<'a> TableSlice<'a> { } /// Print the table to terminal `out`, applying styles when needed and returns the number of - /// line printed, or an error - pub fn print_term(&self, out: &mut T) -> Result { - self.__print(out, Row::print_term) + /// line printed, or an error. Colors won't be displayed unless `out` is a terminal. + pub fn print_term(&self, out: &mut T) -> Result + where T: Write + IsTerminal + ?Sized + { + if out.is_terminal() { + let mut term = term::TerminfoTerminal::new(out).unwrap(); + self.__print(&mut term, Row::print_term) + } else { + self.__print(out, Row::print) + } } /// Print the table to standard output. Colors won't be displayed unless @@ -194,10 +201,11 @@ impl<'a> TableSlice<'a> { /// # Returns /// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens pub fn print_tty(&self, force_colorize: bool) -> Result { - use std::io::IsTerminal; - match (stdout(), io::stdout().is_terminal() || force_colorize) { - (Some(mut o), true) => self.print_term(&mut *o), - _ => self.print(&mut io::stdout()), + let mut out = io::stdout(); + if out.is_terminal() || force_colorize { + self.print_term(&mut out) + } else { + self.print(&mut out) } } @@ -370,7 +378,9 @@ impl Table { /// Print the table to terminal `out`, applying styles when needed and returns the number /// of lines printed, or an error - pub fn print_term(&self, out: &mut T) -> Result { + pub fn print_term(&self, out: &mut T) -> Result + where T: IsTerminal + Write + ?Sized + { self.as_slice().print_term(out) }