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

Println arbitrarily prints line after the prompt #41

Open
Go0p opened this issue Dec 2, 2021 · 4 comments
Open

Println arbitrarily prints line after the prompt #41

Go0p opened this issue Dec 2, 2021 · 4 comments
Labels

Comments

@Go0p
Copy link

Go0p commented Dec 2, 2021

#39

test code

var app = grumble.New(&grumble.Config{
	Name:              "foo",
	Description:       "An awesome foo bar",
})
func init() {
	app.AddCommand(&grumble.Command{
		Name:    "listen",
		Aliases: []string{"l"},
		Help:    "listen",
		Args: func(a *grumble.Args) {
			a.String("host", `a host to listen to (for ipv6 put in square brackets. eg: "[::1]")`, grumble.Default("0.0.0.0"))
		},
		Run: func(c *grumble.Context) error {
			go Pr()
			return nil
		},
	})
}

func Pr()  {
	app.Println("[-] A listener is already running.")
}

in windwos10 cmd

image

in windwos10 powershell

image

@skaldesh
Copy link
Member

skaldesh commented Dec 2, 2021

Thank you for this sample. I can now reproduce it as well.
The issue is probably the concurrent write to Stdout(). Even though we are using readline's Stdout() (when possible), I am not sure if they implemented it properly.

This is their code that handles the writes around stdout:

func (w *wrapWriter) Write(b []byte) (int, error) {
	if !w.t.IsReading() {
		return w.target.Write(b)
	}

	var (
		n   int
		err error
	)
	w.r.buf.Refresh(func() {
		n, err = w.target.Write(b)
	})

	if w.r.IsSearchMode() {
		w.r.SearchRefresh(-1)
	}
	if w.r.IsInCompleteMode() {
		w.r.CompleteRefresh()
	}
	return n, err
}

The IsReading() method uses an atomic for the check, but no kind of locking is taking place...

@skaldesh skaldesh added the bug label Dec 2, 2021
@r0l1
Copy link
Member

r0l1 commented Dec 2, 2021

@skaldesh let's try a quick fix with a mutex. In the long term we should search for a readline alternative (#15)

@sairson
Copy link

sairson commented Dec 2, 2021

There is a solution in project Sliver that I think you can try

Clearln = "\r\x1b[2K"
fmt.Println(Clearn + " demo string")

@Go0p
Copy link
Author

Go0p commented Dec 3, 2021

@sairson Thank you for your help, it looks like this now, but it looks more comfortable than before

{79c8a22a-db50-43f0-ae73-0347cb9190c2}

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

No branches or pull requests

4 participants