From 816ee16b670274afbba7770fe826a247d9fde371 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 25 Apr 2023 07:47:50 -0500 Subject: [PATCH] golang: finally doing struct defaults right Signed-off-by: Jeff Carr --- toolkit/gocui/main.go | 9 +-------- toolkit/gocui/structs.go | 38 ++++++++++++++++++++------------------ toolkit/gocui/tab.go | 4 ++-- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/toolkit/gocui/main.go b/toolkit/gocui/main.go index 34cea92..273da4b 100644 --- a/toolkit/gocui/main.go +++ b/toolkit/gocui/main.go @@ -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 diff --git a/toolkit/gocui/structs.go b/toolkit/gocui/structs.go index a9ceae9..5eea138 100644 --- a/toolkit/gocui/structs.go +++ b/toolkit/gocui/structs.go @@ -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 diff --git a/toolkit/gocui/tab.go b/toolkit/gocui/tab.go index 80447aa..7e25545 100644 --- a/toolkit/gocui/tab.go +++ b/toolkit/gocui/tab.go @@ -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