PageRenderTime 85ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/rust/termcolor/README.md

https://bitbucket.org/vionika/spin.android
Markdown | 86 lines | 63 code | 23 blank | 0 comment | 0 complexity | 30747a27682855847ff2addb640eaa4c MD5 | raw file
Possible License(s): JSON, 0BSD, AGPL-1.0, BSD-2-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0, CC0-1.0, AGPL-3.0, MPL-2.0, Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, Unlicense
  1. termcolor
  2. =========
  3. A simple cross platform library for writing colored text to a terminal. This
  4. library writes colored text either using standard ANSI escape sequences or
  5. by interacting with the Windows console. Several convenient abstractions
  6. are provided for use in single-threaded or multi-threaded command line
  7. applications.
  8. [![Linux build status](https://api.travis-ci.org/BurntSushi/ripgrep.png)](https://travis-ci.org/BurntSushi/ripgrep)
  9. [![Windows build status](https://ci.appveyor.com/api/projects/status/github/BurntSushi/ripgrep?svg=true)](https://ci.appveyor.com/project/BurntSushi/ripgrep)
  10. [![](https://img.shields.io/crates/v/termcolor.svg)](https://crates.io/crates/termcolor)
  11. Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
  12. ### Documentation
  13. [https://docs.rs/termcolor](https://docs.rs/termcolor)
  14. ### Usage
  15. Add this to your `Cargo.toml`:
  16. ```toml
  17. [dependencies]
  18. termcolor = "0.3"
  19. ```
  20. and this to your crate root:
  21. ```rust
  22. extern crate termcolor;
  23. ```
  24. ### Organization
  25. The `WriteColor` trait extends the `io::Write` trait with methods for setting
  26. colors or resetting them.
  27. `StandardStream` and `StandardStreamLock` both satisfy `WriteColor` and are
  28. analogous to `std::io::Stdout` and `std::io::StdoutLock`, or `std::io::Stderr`
  29. and `std::io::StderrLock`.
  30. `Buffer` is an in memory buffer that supports colored text. In a parallel
  31. program, each thread might write to its own buffer. A buffer can be printed to
  32. stdout or stderr using a `BufferWriter`. The advantage of this design is that
  33. each thread can work in parallel on a buffer without having to synchronize
  34. access to global resources such as the Windows console. Moreover, this design
  35. also prevents interleaving of buffer output.
  36. `Ansi` and `NoColor` both satisfy `WriteColor` for arbitrary implementors of
  37. `io::Write`. These types are useful when you know exactly what you need. An
  38. analogous type for the Windows console is not provided since it cannot exist.
  39. ### Example: using `StandardStream`
  40. The `StandardStream` type in this crate works similarly to `std::io::Stdout`,
  41. except it is augmented with methods for coloring by the `WriteColor` trait.
  42. For example, to write some green text:
  43. ```rust
  44. use std::io::Write;
  45. use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
  46. let mut stdout = StandardStream::stdout(ColorChoice::Always);
  47. stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
  48. writeln!(&mut stdout, "green text!")?;
  49. ```
  50. ### Example: using `BufferWriter`
  51. A `BufferWriter` can create buffers and write buffers to stdout or stderr. It
  52. does *not* implement `io::Write` or `WriteColor` itself. Instead, `Buffer`
  53. implements `io::Write` and `termcolor::WriteColor`.
  54. This example shows how to print some green text to stderr.
  55. ```rust
  56. use std::io::Write;
  57. use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
  58. let mut bufwtr = BufferWriter::stderr(ColorChoice::Always);
  59. let mut buffer = bufwtr.buffer();
  60. buffer.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
  61. writeln!(&mut buffer, "green text!")?;
  62. bufwtr.print(&buffer)?;
  63. ```