Ejemplo n.º 1
0
Archivo: quest.go Proyecto: fd/terminal
// New returns a Question with the given arguments.
//
// prefix is the text placed before of the prompt, errPrefix is placed before of
// show any error.
//
// trueString and falseString are the strings to be showed when the question
// needs a boolean like answer and it is being used a default value.
// It is already handled the next strings like boolean values (from validate.Atob):
//   1, t, T, TRUE, true, True, y, Y, yes, YES, Yes
//   0, f, F, FALSE, false, False, n, N, no, NO, No
func New(prefix, errPrefix, trueString, falseString string) *Question {
	term, err := terminal.New(editline.InputFd)
	if err != nil {
		panic(err)
	}

	extraBool := make(map[string]bool)
	val := validate.New(validate.Bool, validate.NONE)

	// Add strings of boolean values if there are not validated like boolean.
	if _, err = val.Atob(trueString); err != nil {
		extraBool = map[string]bool{
			strings.ToLower(trueString): true,
			strings.ToUpper(trueString): true,
			strings.Title(trueString):   true,
		}
	}
	if _, err = val.Atob(falseString); err != nil {
		extraBool[strings.ToLower(falseString)] = false
		extraBool[strings.ToUpper(falseString)] = false
		extraBool[strings.Title(falseString)] = false
	}

	return &Question{
		false,
		NONE,

		prefix,
		errPrefix,
		"",

		"",
		nil,

		trueString,
		falseString,
		extraBool,

		0,

		term.Fd(),
		term.OriginalState(),
	}
}
Ejemplo n.º 2
0
// New returns a Question with the given arguments.
//
// prefix is the text placed before of the prompt, errPrefix is placed before of
// show any error.
//
// trueString and falseString are the strings to be showed when the question
// needs a boolean like answer and it is being used a default value.
// It is already handled the next strings like boolean values (from validate.Atob):
//   1, t, T, TRUE, true, True, y, Y, yes, YES, Yes
//   0, f, F, FALSE, false, False, n, N, no, NO, No
func New(prefix, errPrefix, trueString, falseString string) *Question {
	ter, err := term.New()
	if err != nil {
		panic(err)
	}

	extraBool := make(map[string]bool)
	val := validate.New(validate.Bool, validate.None)

	// Add strings of boolean values if there are not validated like boolean.
	if _, err = val.Atob(trueString); err != nil {
		extraBool = map[string]bool{
			strings.ToLower(trueString): true,
			strings.ToUpper(trueString): true,
			strings.Title(trueString):   true,
		}
	}
	if _, err = val.Atob(falseString); err != nil {
		extraBool[strings.ToLower(falseString)] = false
		extraBool[strings.ToUpper(falseString)] = false
		extraBool[strings.Title(falseString)] = false
	}

	return &Question{
		new(validate.Validate),
		ter,

		extraBool,
		trueString,
		falseString,

		prefix,
		errPrefix,
		"",

		"",
		nil,

		false,

		validate.None,
	}
}
Ejemplo n.º 3
0
// readType is the base to read the basic types.
func (q *Question) readType(kind validate.Kind) (value interface{}, err error) {
	valida := validate.New(kind, q.mod)

	if kind == validate.Bool {
		valida.SetBoolString(q.extraBool)
	}

	// Default value
	if q.defValue != nil {
		valida.SetDefault(q.defValue)

		if kind != validate.Bool {
			q.defString = defaultToPrint(q.defValue)
		} else {
			q.defString = q.defaultBoolToPrint(q.defValue.(bool))
		}
	}

	value, err = q.read(q.newLine(), valida)
	q.clean()
	return
}