From 581c5c7eebbe0f477c4a46108d59c79fb3a72362 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 25 Jul 2024 15:44:59 +0800 Subject: [PATCH] readline: fix s := read_line('Message\nName:') on unix/linux (#21930) --- vlib/readline/readline_nix.c.v | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/vlib/readline/readline_nix.c.v b/vlib/readline/readline_nix.c.v index 8bc6eb575067c2..dcfe35d6095e34 100644 --- a/vlib/readline/readline_nix.c.v +++ b/vlib/readline/readline_nix.c.v @@ -374,15 +374,20 @@ fn get_prompt_offset(prompt string) int { // refresh_line redraws the current line, including the prompt. fn (mut r Readline) refresh_line() { mut end_of_input := [0, 0] - end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len, - end_of_input) + last_prompt_line := if r.prompt.contains('\n') { + r.prompt.all_after_last('\n') + } else { + r.prompt + } + end_of_input = calculate_screen_position(last_prompt_line.len, 0, get_screen_columns(), + r.current.len, end_of_input) end_of_input[1] += r.current.filter(it == `\n`).len mut cursor_pos := [0, 0] - cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor, - cursor_pos) + cursor_pos = calculate_screen_position(last_prompt_line.len, 0, get_screen_columns(), + r.cursor, cursor_pos) shift_cursor(0, -r.cursor_row_offset) term.erase_toend() - print(r.prompt) + print(last_prompt_line) print(r.current.string()) if end_of_input[0] == 0 && end_of_input[1] > 0 { print('\n')