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

The last line of text printed by the program disappears if it doesn't end with a newline #216

Closed
tudurom opened this issue May 12, 2019 · 17 comments · Fixed by #224
Closed

Comments

@tudurom
Copy link

tudurom commented May 12, 2019

Sample program:

use rustyline::{Editor,error::ReadlineError};

fn main() {
    let mut e: Editor<()> = Editor::new();

    loop {
        let rl = e.readline("$ ");
        match rl {
            Ok(_) => {
                print!("text\nwithout\nnewline");
            }
            Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
            Err(err) => panic!(err),
        }
    }
}

Expected output:

$ some text here
text
without
newline$ some other

Actual output:

$ some text here
text
without
$ some other
@gwenn gwenn added the bug label May 13, 2019
@gwenn
Copy link
Collaborator

gwenn commented May 13, 2019

I will try to see how other libraries behave in this case.

@gwenn
Copy link
Collaborator

gwenn commented May 13, 2019

On unix, linefeed, linenoise, behave the same. liner appends a '⏎'.

@gwenn gwenn added enhancement and removed bug labels May 13, 2019
@gwenn
Copy link
Collaborator

gwenn commented May 14, 2019

Ok,
We need to retrieve the current cursor position (like this) ! I guess it should be easier on Windows.

@gwenn
Copy link
Collaborator

gwenn commented May 18, 2019

Could you please give PR #224 a try ?

@tudurom
Copy link
Author

tudurom commented May 22, 2019

It is the exact same output, the last word (newline) doesn't appear.

@gwenn
Copy link
Collaborator

gwenn commented May 22, 2019

On Windows ? I need time or your help to find why the PR does not work.
On unix, do you mind checking the logs RUST_LOG=rustyline:debug cargo run --example example 2> debug.log ("initial cursor location: ???"). I made this local change just to test the PR:

-                println!("Line: {}", line);
+                print!("Line: {}\nbim", line);

@tudurom
Copy link
Author

tudurom commented May 26, 2019

Tested on linux, the bim is not shown.

I am not well versed in cargo commands, running that above yields only this in debug.log:

    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/examples/example`

@gwenn
Copy link
Collaborator

gwenn commented May 26, 2019

Sorry, the syntax is 'pkg=level' (not 'pkg:level')...
You should run:

RUST_LOG=rustyline=debug cargo run --example example 2> debug.log

For example:

$ cat debug.log
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/examples/example`
[2019-05-26T10:14:48Z DEBUG rustyline::tty::unix] initial cursor location: '1'
[2019-05-26T10:14:50Z DEBUG rustyline::tty::unix] key: Char('t')
[2019-05-26T10:14:50Z DEBUG rustyline::keymap] Emacs command: SelfInsert(1, 't')
[2019-05-26T10:14:50Z DEBUG rustyline::undo] Changeset::insert(0, 't')
...

Thanks for your help.

@tudurom
Copy link
Author

tudurom commented May 26, 2019

Here it is:

    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/examples/example`
[2019-05-26T10:24:20Z DEBUG rustyline::tty::unix] initial cursor location: '1'
[2019-05-26T10:24:21Z DEBUG rustyline::tty::unix] key: Char('a')
[2019-05-26T10:24:21Z DEBUG rustyline::keymap] Emacs command: SelfInsert(1, 'a')
[2019-05-26T10:24:21Z DEBUG rustyline::undo] Changeset::insert(0, 'a')
[2019-05-26T10:24:22Z DEBUG rustyline::tty::unix] key: Enter
[2019-05-26T10:24:22Z DEBUG rustyline::keymap] Emacs command: AcceptLine
[2019-05-26T10:24:22Z DEBUG rustyline::tty::unix] initial cursor location: '4'
[2019-05-26T10:24:23Z DEBUG rustyline::tty::unix] key: Char('b')
[2019-05-26T10:24:23Z DEBUG rustyline::keymap] Emacs command: SelfInsert(1, 'b')
[2019-05-26T10:24:23Z DEBUG rustyline::undo] Changeset::insert(0, 'b')
[2019-05-26T10:24:24Z DEBUG rustyline::tty::unix] key: Enter
[2019-05-26T10:24:24Z DEBUG rustyline::keymap] Emacs command: AcceptLine
[2019-05-26T10:24:24Z DEBUG rustyline::tty::unix] initial cursor location: '4'
[2019-05-26T10:24:26Z DEBUG rustyline::tty::unix] key: Ctrl('D')
[2019-05-26T10:24:26Z DEBUG rustyline::keymap] Emacs command: EndOfFile

One observation is that "bim" appears and then quickly disappears, replaced by the prompt.

@gwenn
Copy link
Collaborator

gwenn commented May 26, 2019

You are right!
I just made a fix: 444d53f
Could you please give another try (just update/pull your local branch) ?
Sorry.

@tudurom
Copy link
Author

tudurom commented May 26, 2019

Excellent, it works! Thank you!

@tudurom
Copy link
Author

tudurom commented May 26, 2019

Last minute update: changed the example to this:

print!("Line: {}", line);

Basically, eliminated the "bim" text.

Now, the text gets shown on the screen only if it has at most 2 characters.

@tudurom
Copy link
Author

tudurom commented May 26, 2019

It also shows if the input has at least 13 characters.
So, if the input is between 3 and 12 characters long, it doesn't appear.

Also, if the line doesn't end with a newline (like in the example), it appears on its own line, unlike the expected example I gave in the issue where the text just appears on the left of the prompt.

So, instead of

> line 1
line2>

The result is this (considering that the fix works for inputs between 3 and 12 characters):

> line1
line2
>

@gwenn
Copy link
Collaborator

gwenn commented May 26, 2019

Logs suggest that there is something wrong with initial cursor location.

@gwenn
Copy link
Collaborator

gwenn commented May 26, 2019

Silly me...
I read only the first digit...

@gwenn
Copy link
Collaborator

gwenn commented May 26, 2019

Could you give another try to this ?

@tudurom
Copy link
Author

tudurom commented May 27, 2019

Works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants