grumble icon indicating copy to clipboard operation
grumble copied to clipboard

Println arbitrarily prints line after the prompt

Open Go0p opened this issue 4 years ago • 4 comments

#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

Go0p avatar Dec 02 '21 09:12 Go0p

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 avatar Dec 02 '21 09:12 skaldesh

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

r0l1 avatar Dec 02 '21 10:12 r0l1

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

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

sairson avatar Dec 02 '21 11:12 sairson

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

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

Go0p avatar Dec 03 '21 01:12 Go0p