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

read_line displays the prompt message multiple times for strings ending with \n. #20563

Open
viniciusfdasilva opened this issue Jan 17, 2024 · 7 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@viniciusfdasilva
Copy link
Contributor

viniciusfdasilva commented Jan 17, 2024

Describe the bug

When I run the code with v from within a file, after executing the code, the message appears on the screen and waits for input. When I enter the input, the message reappears with each character I input.

I conducted several tests and concluded that this happens when a \n is inserted at the end of the message, as shown in the code:

With bug

import readline { read_line }

fn main()
{
        s := read_line("Prompt Message\n")!
        print("${s} \n")
}

No bug

import readline { read_line }

fn main()
{
        s := read_line("Prompt Message")!
        print("${s} \n")
}

Reproduction Steps

error

Expected Behavior

  • Prompt message showed once time

Current Behavior

error

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.4 ed754cf.9560d53

Environment details (OS name and version, etc.)

OS: linux, Ubuntu 23.04

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@viniciusfdasilva viniciusfdasilva added the Bug This tag is applied to issues which reports bugs. label Jan 17, 2024
@JalonSolov
Copy link
Contributor

It repeats the prompt for every character you type, as you type them.

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Jan 17, 2024
@viniciusfdasilva
Copy link
Contributor Author

@JalonSolov

Yes! And it's strange because without the \n it doesn't have this behavior!

@JalonSolov
Copy link
Contributor

Not so strange, depending on how it was written. A "common" way to do it would be to move the cursor to the start of the line, and re-print the prompt + the char you typed. But if the prompt has \n, it will force it to the next line every time. I don't know if that's how the V version was done, but I have seen it before.

@viniciusfdasilva
Copy link
Contributor Author

@JalonSolov

Sure! In my case, I hadn't seen it happen before! That's why I found it strange! So can we consider that there is no bug in the implementation? Who would be able to say? Because I really don't know if this is the intended behavior of the implementation, and since I've never seen this case happen, I opened this issue to report it!

@JalonSolov
Copy link
Contributor

It's definitely a bug. Just need someone to dig into the code and fix it.

@viniciusfdasilva
Copy link
Contributor Author

I found where the issue is occurring! However, I haven't found the solution yet. Once I find it, I'll submit a pull request

The error occurs precisely because in the file, for example, readline_nix.c.v, the function read_line_utf8(prompt string) ![]rune executes the following code:

for {
		flush_stdout()
		c := r.read_char() or { return err }
		a := r.analyse(c)
		if r.execute(a, c) {
			break
		}
	}

This for loop waits until the user enters \n to conclude the capture of the input. However, the r.execute function, while the value is different from \n and \r, also calls the r.refresh_line() function to add a new character.

And this r.refresh_line() function also prints to the prompt!

As a result, every time the user enters a character, the r.refresh_line() function is called and prints to the prompt!

To solve this, one could remove this print statement, but it's unclear if it might be necessary for other calls. I am still evaluating this before proposing a solution!

@JalonSolov
Copy link
Contributor

Right. Check both with & without \n in the prompt, and even with multiple \n's in the prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

No branches or pull requests

2 participants