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

Fix calculated width of prompt with ansi formatting #23

Merged
merged 2 commits into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
println!("No previous history.");
}
loop {
let readline = rl.readline(">> ");
let readline = rl.readline("\x1b[1;32m>>\x1b[0m ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to have this in the example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not necessary.
It just shows that rustyline properly handles prompt with ansi escape codes.

match readline {
Ok(line) => {
rl.add_history_entry(&line);
Expand Down
6 changes: 3 additions & 3 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ fn filename_complete(path: &str) -> Result<Vec<String>> {
let dir = if dir_path.starts_with("~") {
// ~[/...]
if let Some(home) = home_dir() {
match dir_path.relative_from("~") {
Some(rel_path) => home.join(rel_path),
None => home,
match dir_path.strip_prefix("~") {
Ok(rel_path) => home.join(rel_path),
_ => home,
}
} else {
dir_path.to_path_buf()
Expand Down
40 changes: 36 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//! }
//! ```
#![feature(io)]
#![feature(path_relative_from)]
#![feature(str_char)]
#![feature(unicode)]
extern crate libc;
Expand Down Expand Up @@ -65,7 +64,7 @@ impl<'out, 'prompt> State<'out, 'prompt> {
State {
out: out,
prompt: prompt,
prompt_width: unicode_width::UnicodeWidthStr::width(prompt),
prompt_width: width(prompt),
buf: String::with_capacity(capacity),
pos: 0,
cols: cols,
Expand All @@ -91,7 +90,7 @@ impl<'out, 'prompt> State<'out, 'prompt> {
}

fn refresh_prompt_and_line(&mut self, prompt: &str) -> Result<()> {
let prompt_width = unicode_width::UnicodeWidthStr::width(prompt);
let prompt_width = width(prompt);
self.refresh(prompt, prompt_width)
}

Expand Down Expand Up @@ -256,7 +255,34 @@ fn beep() -> Result<()> {

// Control characters are treated as having zero width.
fn width(s: &str) -> usize {
unicode_width::UnicodeWidthStr::width(s)
if s.contains('\x1b') {
let mut w = 0;
let mut esc_seq = 0;
for c in s.chars() {
if esc_seq == 1 {
if c == '[' { // CSI
esc_seq = 2;
} else { // two-character sequence
esc_seq = 0;
}
} else if esc_seq == 2 {
if c == ';' || (c >= '0' && c <= '9') {
} else if c == 'm' { // last
esc_seq = 0
} else { // not supported
w += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
esc_seq = 0
}
} else if c == '\x1b' {
esc_seq = 1;
} else {
w += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
}
}
w
} else {
unicode_width::UnicodeWidthStr::width(s)
}
}

/// Insert the character `ch` at cursor current position.
Expand Down Expand Up @@ -997,4 +1023,10 @@ mod test {
assert_eq!("rust", s.buf);
assert_eq!(4, s.pos);
}

#[test]
fn prompt_with_ansi_escape_codes() {
let w = super::width("\x1b[1;32m>>\x1b[0m ");
assert_eq!(3, w);
}
}