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

[Question] Prevent readline to print. #26

Closed
RickvanLoo opened this issue Feb 14, 2016 · 2 comments · Fixed by #27
Closed

[Question] Prevent readline to print. #26

RickvanLoo opened this issue Feb 14, 2016 · 2 comments · Fixed by #27
Assignees

Comments

@RickvanLoo
Copy link

I'm currently working on a CLI client for a chat application and I really like how this library manipulates the log output. There is currently besides this library no Go library available that refreshes the stdout without including all kinds of lay-outing I do not need.

But I have a question: Is it possible to prevent Readline() from printing to the terminal output?
Currently my output looks like this

(INSERT) > test
2016/02/14 15:59:07 > Rivalo > test

Where (INSERT) > is the prompt for reading the line, and the next line simply returns the sent message inside the chat channel.
I want to prevent the User's message from displaying twice but keep the consistency with the messages from others, which means that after a \n readline() shouldn't print its just received line to the stdout.

Is there currently an option or workaround for this I'm overlooking in the documentation?
(Please, in future, add comments above exported functions, it makes the godoc documentation easier to read)

@chzyer
Copy link
Owner

chzyer commented Feb 14, 2016

@rivalo
readline() wouldn't print anything it received to the stdout in a new line. Maybe you can just not print messages which belong to user so it will not displaying twice.
Maybe I'm misunderstanding what you mean. Would you mind to show you code?

However, sorry for the missing comments. ಥ_ಥ

@RickvanLoo
Copy link
Author

    rl, err := readline.New("> ")
    if err != nil {
        panic(err)
    }

    defer rl.Close()
    log.SetOutput(rl.Stderr()) // let "log" write to l.Stderr instead of os.Stderr

    for {
        line, err := rl.Readline()
        if err != nil { // io.EOF
            break
        }

        if line == ":quit" {
            break
        }

        dg.ChannelMessageSend(State.Channel.ID, line)
    }

This is the only code from readline I'm currently using.
dg.ChannelMessageSend sends the message to a channel, where a goroutine prints channel messages (when a new one is received) using log.printf()
Using this code, the prompt "> " + the user input can still be read after pressing enter and new logs flow in.

Just like in the demo's, you do a 'say hello' command then the program responds by logging 'hello' over and over, but the initial 'say hello' command including it's newline prompt >> is still kept in the terminal above the 'hello' lines.

I was wondering if there is an option to prevent that from happening using the current library functions. Else I have to try to hack something together that works 😃

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

Successfully merging a pull request may close this issue.

2 participants