readline: fix cursor position with prompt including ansi escape sequences

pull/2723/head
Henrixounez 2019-11-10 17:33:21 +01:00 committed by Alexander Medvednikov
parent 73bd82e706
commit f8ab629986
2 changed files with 16 additions and 1 deletions

View File

@ -36,6 +36,7 @@ mut:
overwrite bool overwrite bool
cursor_row_offset int cursor_row_offset int
prompt string prompt string
prompt_offset int
previous_lines []ustring previous_lines []ustring
search_index int search_index int
is_tty bool is_tty bool

View File

@ -93,6 +93,7 @@ pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring {
r.cursor = 0 r.cursor = 0
r.prompt = prompt r.prompt = prompt
r.search_index = 0 r.search_index = 0
r.prompt_offset = get_prompt_offset(prompt)
if r.previous_lines.len <= 1 { if r.previous_lines.len <= 1 {
r.previous_lines << ''.ustring() r.previous_lines << ''.ustring()
r.previous_lines << ''.ustring() r.previous_lines << ''.ustring()
@ -150,6 +151,19 @@ pub fn read_line(prompt string) ?string {
return s return s
} }
fn get_prompt_offset(prompt string) int {
mut len := 0
for i := 0; i < prompt.len; i++ {
if prompt[i] == `\e` {
for ;i < prompt.len && prompt[i] != `m`; i++ {}
} else {
len = len + 1
}
}
return prompt.len - len
}
fn (r Readline) analyse(c int) Action { fn (r Readline) analyse(c int) Action {
match c { match c {
`\0` { return .eof } `\0` { return .eof }
@ -291,7 +305,7 @@ fn (r mut Readline) refresh_line() {
if end_of_input[0] == 0 && end_of_input[1] > 0 { if end_of_input[0] == 0 && end_of_input[1] > 0 {
print('\n') print('\n')
} }
shift_cursor(cursor_pos[0], - (end_of_input[1] - cursor_pos[1])) shift_cursor(cursor_pos[0] - r.prompt_offset, - (end_of_input[1] - cursor_pos[1]))
r.cursor_row_offset = cursor_pos[1] r.cursor_row_offset = cursor_pos[1]
} }