Skip to content

Commit

Permalink
Add an example for using color codes in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
murarth committed Jun 1, 2018
1 parent 442ce8d commit 8b125e2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ winapi = { version = "0.3", features = [
"winerror", "winuser" ] }

[dev-dependencies]
ansi_term = "0.11"
assert_matches = "1.2"
rand = "0.5"
35 changes: 35 additions & 0 deletions examples/color_prompt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! Demonstrates how to use color escape sequences in the prompt string,
//! using the `ansi_term` crate.
extern crate ansi_term;
extern crate linefeed;

use std::io;

use ansi_term::Color;

use linefeed::{Interface, ReadResult};

fn main() -> io::Result<()> {
let interface = Interface::new("color-demo")?;

let style = Color::Red.bold();
let text = "color-demo> ";

// The character values '\x01' and '\x02' are used to indicate the beginning
// and end of an escape sequence. This informs linefeed, which cannot itself
// interpret the meaning of escape sequences, that these characters are not
// visible when the prompt is drawn and should not factor into calculating
// the visible length of the prompt string.
interface.set_prompt(&format!("\x01{prefix}\x02{text}\x01{suffix}\x02",
prefix=style.prefix(),
text=text,
suffix=style.suffix()))?;

while let ReadResult::Input(line) = interface.read_line()? {
println!("read input: {:?}", line);
interface.add_history_unique(line);
}

Ok(())
}

0 comments on commit 8b125e2

Please sign in to comment.