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 position calculation on Windows #702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::sync::Arc;

use log::{debug, warn};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE, WORD};
use winapi::shared::winerror;
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
Expand Down Expand Up @@ -468,12 +467,13 @@ impl Renderer for ConsoleRenderer {
/// Characters with 2 column width are correctly handled (not split).
fn calculate_position(&self, s: &str, orig: Position) -> Position {
let mut pos = orig;
let mut esc_sec = 0;
for c in s.graphemes(true) {
if c == "\n" {
pos.col = 0;
pos.row += 1;
} else {
let cw = c.width();
let cw = width(c, &mut esc_sec);
pos.col += cw;
if pos.col > self.cols {
pos.row += 1;
Expand Down Expand Up @@ -883,7 +883,19 @@ impl Drop for Handle {

#[cfg(test)]
mod test {
use super::Console;
use super::*;

#[test]
fn prompt_with_ansi_escape_codes() {
let out = ConsoleRenderer::new(
unsafe { processenv::GetStdHandle(winbase::STD_OUTPUT_HANDLE) },
true,
BellStyle::default(),
);
let pos = out.calculate_position("\x1b[1;32m>>\x1b[0m ", Position::default());
assert_eq!(3, pos.col);
assert_eq!(0, pos.row);
}

#[test]
fn test_send() {
Expand Down