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