Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table::print_term will now only print colors to terminals #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ 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
- `Table::print_term` will now only print colors if the output is a terminal.

# 0.10.0 (2022-12-27)
## Fixed
- Fix panic due to incorrect ANSI escape handling ([#137])
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down Expand Up @@ -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 }
130 changes: 0 additions & 130 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T: Terminal + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
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<T>(&self, out: &mut T) -> Result<usize, Error>
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
Expand All @@ -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<usize, Error> {
use is_terminal::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)
}
}

Expand Down Expand Up @@ -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<T: Terminal + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
pub fn print_term<T>(&self, out: &mut T) -> Result<usize, Error>
where T: IsTerminal + Write + ?Sized
{
self.as_slice().print_term(out)
}

Expand Down