-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for using color codes in prompt
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |