mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 02:57:52 +08:00
eval/vartypes: Add numVar and boolVar.
This commit is contained in:
parent
d94410f8a1
commit
eb5c813180
|
@ -84,9 +84,13 @@ func Rprompt(ed Editor) eval.Callable {
|
|||
|
||||
// Implementation for $rprompt-persistent.
|
||||
|
||||
// TODO Keep the underlying boolean and float64 values somewhere within the
|
||||
// Editor to avoid casts.
|
||||
|
||||
// RpromptPersistentVariable returns a variable for $edit:rprompt-persistent.
|
||||
func RpromptPersistentVariable() vartypes.Variable {
|
||||
return vartypes.NewValidatedPtrVariable(types.Bool(false), vartypes.ShouldBeBool)
|
||||
b := false
|
||||
return vartypes.NewBool(&b)
|
||||
}
|
||||
|
||||
// RpromptPersistent extracts $edit:rprompt-persistent.
|
||||
|
@ -96,7 +100,8 @@ func RpromptPersistent(ed Editor) bool {
|
|||
|
||||
// MaxWaitVariable returns a variable for $edit:-prompts-max-wait.
|
||||
func MaxWaitVariable() vartypes.Variable {
|
||||
return vartypes.NewValidatedPtrVariable(types.String("+Inf"), vartypes.ShouldBeNumber)
|
||||
f := math.Inf(1)
|
||||
return vartypes.NewNumber(&f)
|
||||
}
|
||||
|
||||
// MaxWait extracts $edit:-prompts-max-wait.
|
||||
|
|
29
eval/vartypes/bool.go
Normal file
29
eval/vartypes/bool.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package vartypes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/elves/elvish/eval/types"
|
||||
)
|
||||
|
||||
var errMustBeBool = errors.New("must be bool")
|
||||
|
||||
type boolVar struct {
|
||||
ptr *bool
|
||||
}
|
||||
|
||||
func NewBool(ptr *bool) Variable {
|
||||
return boolVar{ptr}
|
||||
}
|
||||
|
||||
func (bv boolVar) Get() types.Value {
|
||||
return types.Bool(*bv.ptr)
|
||||
}
|
||||
|
||||
func (bv boolVar) Set(v types.Value) error {
|
||||
if b, ok := v.(types.Bool); ok {
|
||||
*bv.ptr = bool(b)
|
||||
return nil
|
||||
}
|
||||
return errMustBeBool
|
||||
}
|
32
eval/vartypes/number.go
Normal file
32
eval/vartypes/number.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package vartypes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/elves/elvish/eval/types"
|
||||
)
|
||||
|
||||
var errMustBeNumber = errors.New("must be number")
|
||||
|
||||
type numberVar struct {
|
||||
ptr *float64
|
||||
}
|
||||
|
||||
func NewNumber(ptr *float64) Variable {
|
||||
return numberVar{ptr}
|
||||
}
|
||||
|
||||
func (nv numberVar) Get() types.Value {
|
||||
return types.String(strconv.FormatFloat(*nv.ptr, 'E', -1, 64))
|
||||
}
|
||||
|
||||
func (nv numberVar) Set(v types.Value) error {
|
||||
if s, ok := v.(types.String); ok {
|
||||
if num, err := strconv.ParseFloat(string(s), 64); err == nil {
|
||||
*nv.ptr = num
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errMustBeNumber
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/elves/elvish/eval/types"
|
||||
)
|
||||
|
||||
var errMustBeString = errors.New("must be list")
|
||||
var errMustBeString = errors.New("must be string")
|
||||
|
||||
type stringVar struct {
|
||||
ptr *string
|
||||
|
|
Loading…
Reference in New Issue
Block a user