golang: finally doing struct defaults right
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
a8b4c45eb7
commit
816ee16b67
|
@ -13,11 +13,7 @@ import (
|
|||
// to this toolkit from the wit/gui golang package
|
||||
func init() {
|
||||
log(logInfo, "Init() of awesome-gocui")
|
||||
var test config
|
||||
Set(&test, "default")
|
||||
log(logNow, "Init() me.rawW", me.rawW)
|
||||
// exit("test init()")
|
||||
me.defaultBehavior = true
|
||||
Set(&me, "default")
|
||||
|
||||
me.groupPadding = 4
|
||||
me.buttonPadding = 3
|
||||
|
@ -26,9 +22,6 @@ func init() {
|
|||
me.rawW = 7
|
||||
me.rawH = 3
|
||||
|
||||
me.padW = 3
|
||||
me.padH = 3
|
||||
|
||||
// todo, remove all of these
|
||||
me.defaultWidth = 10
|
||||
me.defaultHeight = 2 // this means by default one line of text in a button
|
||||
|
|
|
@ -34,16 +34,18 @@ type config struct {
|
|||
|
||||
helpLabel *gocui.View
|
||||
|
||||
defaultBehavior bool
|
||||
DefaultBehavior bool `default:"true"`
|
||||
defaultWidth int
|
||||
defaultHeight int
|
||||
// nextW int // where the next window or tab flag should go
|
||||
|
||||
padW int `default:"3" dense:"2"`
|
||||
padH int
|
||||
// the amount to put between winodw tab widgets
|
||||
TabPadW int `default:"4" dense:"2"`
|
||||
// PadH int `default:"3" dense:"2"`
|
||||
|
||||
// the raw beginning of each window (or tab)
|
||||
rawW int `default:"7"`
|
||||
JWC int `default:"7"`
|
||||
rawH int `default:"3"`
|
||||
|
||||
bookshelf bool // do you want things arranged in the box like a bookshelf or a stack?
|
||||
|
@ -162,7 +164,7 @@ func (w *cuiWidget) Write(p []byte) (n int, err error) {
|
|||
|
||||
func Set(ptr interface{}, tag string) error {
|
||||
if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
|
||||
log("Set() Not a pointer", ptr, "with tag =", tag)
|
||||
log(logError, "Set() Not a pointer", ptr, "with tag =", tag)
|
||||
return fmt.Errorf("Not a pointer")
|
||||
}
|
||||
|
||||
|
@ -170,15 +172,10 @@ func Set(ptr interface{}, tag string) error {
|
|||
t := v.Type()
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
log("Set() i =", i, t.Field(i))
|
||||
if defaultVal := t.Field(i).Tag.Get(tag); defaultVal != "-" {
|
||||
log("Set() tried something")
|
||||
if err := setField(v.Field(i), defaultVal); err != nil {
|
||||
log("Set() failed", err)
|
||||
// return err
|
||||
}
|
||||
|
||||
}
|
||||
defaultVal := t.Field(i).Tag.Get(tag)
|
||||
// name := t.Field(i).Name
|
||||
// log("Set() try name =", name, "defaultVal =", defaultVal)
|
||||
setField(v.Field(i), defaultVal)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -186,19 +183,24 @@ func Set(ptr interface{}, tag string) error {
|
|||
func setField(field reflect.Value, defaultVal string) error {
|
||||
|
||||
if !field.CanSet() {
|
||||
log("setField() Can't set value", field, defaultVal)
|
||||
// log("setField() Can't set value", field, defaultVal)
|
||||
return fmt.Errorf("Can't set value\n")
|
||||
} else {
|
||||
log("setField() Can set value", field, defaultVal)
|
||||
}
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.Int:
|
||||
if val, err := strconv.ParseInt(defaultVal, 1, 64); err == nil {
|
||||
field.Set(reflect.ValueOf(int(val)).Convert(field.Type()))
|
||||
}
|
||||
val, _ := strconv.Atoi(defaultVal)
|
||||
field.Set(reflect.ValueOf(int(val)).Convert(field.Type()))
|
||||
case reflect.String:
|
||||
field.Set(reflect.ValueOf(defaultVal).Convert(field.Type()))
|
||||
case reflect.Bool:
|
||||
field.Set(reflect.ValueOf(defaultVal).Convert(field.Type()))
|
||||
if defaultVal == "true" {
|
||||
field.Set(reflect.ValueOf(true))
|
||||
} else {
|
||||
field.Set(reflect.ValueOf(false))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -61,7 +61,7 @@ func (w *cuiWidget) setTabWH() {
|
|||
w.gocuiSize.h0 = me.rootNode.nextH
|
||||
|
||||
// move the rootNode width over for the next window or tab
|
||||
me.rootNode.nextW += w.gocuiSize.width + me.padW
|
||||
me.rootNode.nextW += w.gocuiSize.width + me.TabPadW
|
||||
|
||||
w.startW = me.rawW
|
||||
w.startH = me.rawH
|
||||
|
@ -81,7 +81,7 @@ func (w *cuiWidget) setLabel() {
|
|||
w.gocuiSize.h0 = me.rootNode.nextH
|
||||
|
||||
// move the rootNode width over for the next window or tab
|
||||
me.rootNode.nextW += w.gocuiSize.width + me.padW
|
||||
me.rootNode.nextW += w.gocuiSize.width
|
||||
|
||||
w.startW = me.rawW
|
||||
w.startH = me.rawH
|
||||
|
|
Loading…
Reference in New Issue