Code Monkey home page Code Monkey logo

termcolor's Introduction

termcolor

A simple cross platform library for writing colored text to a terminal. This library writes colored text either using standard ANSI escape sequences or by interacting with the Windows console. Several convenient abstractions are provided for use in single-threaded or multi-threaded command line applications.

Build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/termcolor

Usage

Add this to your Cargo.toml:

[dependencies]
termcolor = "1.1"

Organization

The WriteColor trait extends the io::Write trait with methods for setting colors or resetting them.

StandardStream and StandardStreamLock both satisfy WriteColor and are analogous to std::io::Stdout and std::io::StdoutLock, or std::io::Stderr and std::io::StderrLock.

Buffer is an in memory buffer that supports colored text. In a parallel program, each thread might write to its own buffer. A buffer can be printed to stdout or stderr using a BufferWriter. The advantage of this design is that each thread can work in parallel on a buffer without having to synchronize access to global resources such as the Windows console. Moreover, this design also prevents interleaving of buffer output.

Ansi and NoColor both satisfy WriteColor for arbitrary implementors of io::Write. These types are useful when you know exactly what you need. An analogous type for the Windows console is not provided since it cannot exist.

Example: using StandardStream

The StandardStream type in this crate works similarly to std::io::Stdout, except it is augmented with methods for coloring by the WriteColor trait. For example, to write some green text:

use std::io::{self, Write};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

fn write_green() -> io::Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
    writeln!(&mut stdout, "green text!")
}

Example: using BufferWriter

A BufferWriter can create buffers and write buffers to stdout or stderr. It does not implement io::Write or WriteColor itself. Instead, Buffer implements io::Write and termcolor::WriteColor.

This example shows how to print some green text to stderr.

use std::io::{self, Write};
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

fn write_green() -> io::Result<()> {
    let mut bufwtr = BufferWriter::stderr(ColorChoice::Always);
    let mut buffer = bufwtr.buffer();
    buffer.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
    writeln!(&mut buffer, "green text!")?;
    bufwtr.print(&buffer)
}

Automatic color selection

When building a writer with termcolor, the caller must provide a ColorChoice selection. When the color choice is Auto, termcolor will attempt to determine whether colors should be enabled by inspecting the environment. Currently, termcolor will inspect the TERM and NO_COLOR environment variables:

  • If NO_COLOR is set to any value, then colors will be suppressed.
  • If TERM is set to dumb, then colors will be suppressed.
  • In non-Windows environments, if TERM is not set, then colors will be suppressed.

This decision procedure may change over time.

Currently, termcolor does not attempt to detect whether a tty is present or not. To achieve that, please use the atty crate.

Minimum Rust version policy

This crate's minimum supported rustc version is 1.34.0.

The current policy is that the minimum Rust version required to use this crate can be increased in minor version updates. For example, if crate 1.0 requires Rust 1.20.0, then crate 1.0.z for all values of z will also require Rust 1.20.0 or newer. However, crate 1.y for y > 0 may require a newer minimum version of Rust.

In general, this crate will be conservative with respect to the minimum supported version of Rust.

termcolor's People

Contributors

burntsushi avatar okdana avatar balajisivaraman avatar tiehuis avatar ericbn avatar lyuha avatar theamazingfedex avatar ignatenkobrain avatar kpp avatar matthiaskrgr avatar sebnow avatar chocolateboy avatar simenb avatar durka avatar behnam avatar martinlindhe avatar lilianmoraru avatar mernen avatar moshen avatar mpacer avatar mvitz avatar amhk avatar mookid avatar thedrow avatar pkgw avatar samuelcolvin avatar kennytm avatar tjdgus3537 avatar lilydjwg avatar ahmedelgabri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.