dixonwille / wmenu

An easy to use menu structure for cli applications that prompts users to make choices.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I would expected for menu option to display first the question then the options

tronch0 opened this issue · comments

I would expect to have a menu option to display first the question then the options, but unfortunately, I don't see it.
Am I missing something?

I see the question display and the response parsing are tightly coupled

@RonQedit Not sure I get what you are asking? Do you have an example?

I think I follow. Do you want an option to switch which displays first? Currently, it is options than the question. But you want question then option. Am I following?

@dixonwille right, an menu option that i can choose what to display first:

  1. Qestion and then the options.
  2. Options and then the question.

You understand it correctly.

Thanks
Ron

As you mentioned, the question and the ask feature are pretty tightly coupled. This package was one of my first GoLang packages and I wish I could rewrite it without breaking things. I may attempt that one day but today is not that day.

As for you feature, I think it is a good one to have but can be difficult to implement. I will see if there is a way to split ask out (probably requiring a change to WLog...).

Sounds good :) thanks!

how about add a func in wlog to support this feature
like this one

func (ui *BasicUI) AskWithCb(message, trim string, cb func(ui *BasicUI)) (string, error) {
	if message != "" {
		ui.Output(message)
		cb(ui)
	}
	reader := bufio.NewReader(ui.Reader)
	res, err := reader.ReadString('\n')
	if err != nil {
		return "", err
	}
	res = strings.Replace(res, "\r", "", -1) //this will only be useful under windows
	res = strings.Replace(res, "\n", "", -1)
	res = strings.Trim(res, trim)
	return res, err
}

and the test case

func TestAskWithCb(t *testing.T) {
	assert := assert.New(t)
	for _, c := range trimCases {
		writer, errWriter, in := initTest(c.input)
		basic := New(in, writer, errWriter)
		res, err := basic.AskWithCb("Awesome string", c.trim,func(ui *BasicUI) {ui.Output("option")})
		if err != nil {
			assert.Fail(err.Error())
		}
		out:= writer.String()
		// if err != nil {
		// 	assert.Fail(err.Error())
		// }
		_, err = errWriter.ReadString((byte)('\n'))
		expectedString := "Awesome string\noption\n"
		assert.Equal("EOF", err.Error())
		assert.Equal(expectedString, out)
		expectedAns := strings.Replace(c.input, "\r", "", -1)
		expectedAns = strings.Replace(expectedAns, "\n", "", -1)
		expectedAns = strings.Trim(expectedAns, c.trim)
		assert.Equal(expectedAns, res)
	}
}