diff --git a/cmd/test/main.go b/cmd/test/main.go index b5598b4..bc30ddb 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -106,6 +106,21 @@ var ( ).Reply() ) +var theNode = tg.NewNode( + "/", tg.WidgetFunc(func(c *tg.Context){ + c.Go("/start") + }), + tg.NewNode( + "start", tg.WidgetFunc(func(c *tg.Context){}), + tg.NewNode( + "profile", tg.WidgetFunc(func(c *tg.Context){}), + ), + tg.NewNode( + "upper-case", tg.WidgetFunc(func(c *tg.Context){}), + ), + ), +) + var beh = tg.NewBehaviour(). WithInitFunc(func(c *tg.Context) { // The session initialization. @@ -224,6 +239,8 @@ var gBeh = tg.NewGroupBehaviour(). ) func main() { + log.Println(theNode.ScreenMap()) + return token := os.Getenv("BOT_TOKEN") bot, err := tg.NewBot(token) diff --git a/tg/screen.go b/tg/screen.go index a740c6e..9684177 100644 --- a/tg/screen.go +++ b/tg/screen.go @@ -13,6 +13,54 @@ type Screen struct { Widget Widget } +// The node is a simple way to represent +// tree-like structured applications. +type Node struct { + Screen *Screen + Subs []*Node +} + +func NewNode(id ScreenId, widget Widget, subs ...*Node) *Node { + ret := &Node{} + ret.Screen = NewScreen(id, widget) + ret.Subs = subs + return ret +} + +func (n *Node) ScreenMap() ScreenMap { + m := make(ScreenMap) + id := n.Screen.Id + m[id] = n.Screen + n.Screen.Id = id + var root ScreenId + if id == "/" { + root = "" + } else { + root = id + } + for _, sub := range n.Subs { + buf := sub.screenMap(root + "/") + for k, v := range buf { + m[k] = v + } + } + return m +} + +func (n *Node) screenMap(root ScreenId) ScreenMap { + m := make(ScreenMap) + id := root+ n.Screen.Id + m[id] = n.Screen + n.Screen.Id = id + for _, sub := range n.Subs { + buf := sub.screenMap(id + "/") + for k, v := range buf { + m[k] = v + } + } + return m +} + // Map structure for the screens. type ScreenMap map[ScreenId]*Screen