github.com/swaros/contxt/module/runner@v0.0.0-20240305083542-3dbd4436ac40/iactive.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the Software), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 // AINC-NOTE-0815 24 25 package runner 26 27 import ( 28 "strings" 29 30 "github.com/gdamore/tcell/v2" 31 "github.com/swaros/contxt/module/ctxout" 32 "github.com/swaros/contxt/module/ctxtcell" 33 ) 34 35 func injectedTcell(c *CmdExecutorImpl, initFn func(caller *ctxtcell.CtCell, c *CmdExecutorImpl)) (*ctxtcell.CtCell, error) { 36 tc := ctxtcell.NewTcell() 37 tc.SetMouse(true).SetNoClearScreen(false) 38 outHndl := ctxtcell.NewCtOutput(tc) 39 c.session.OutPutHdnl = outHndl 40 c.session.Printer = nil 41 if err := tc.Init(); err != nil { 42 return nil, err 43 } 44 initFn(tc, c) 45 if err := tc.Run(); err != nil { 46 return nil, err 47 } 48 return tc, nil 49 } 50 51 func MainScreen(c *CmdExecutorImpl) (*ctxtcell.CtCell, error) { 52 return injectedTcell(c, func(caller *ctxtcell.CtCell, c *CmdExecutorImpl) { 53 MainMenu(caller, c) 54 }) 55 } 56 57 func MainMenu(tc *ctxtcell.CtCell, c *CmdExecutorImpl) { 58 menu := tc.NewMenu() 59 60 // top bar 61 contxtTopMenu := tc.ActiveText("contxt") 62 contxtTopMenu.SetPos(1, 0).SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGoldenrod).Background(tcell.ColorBlack)) 63 contxtTopMenu.OnSelect = func(selected bool) { 64 menu.SetVisible(!menu.IsVisible()) 65 } 66 tc.AddElement(contxtTopMenu) 67 68 exitTopMenu := tc.ActiveText("exit") 69 exitTopMenu.SetPosProcentage(100, 0). 70 SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGoldenrod).Background(tcell.ColorBlack)) 71 72 exitTopMenu.GetPos().SetMargin(-5, 0) 73 exitTopMenu.OnSelect = func(selected bool) { 74 tc.Stop() 75 } 76 tc.AddElement(exitTopMenu) 77 78 menu.SetTopLeft(1, 1).SetBottomRight(20, 10) 79 80 menu.AddItem("PrintPaths", func(itm *ctxtcell.MenuElement) { 81 itm.GetText().SetText("PrintPaths RUNS") 82 c.GetLogger().Debug("run command: PrintPaths") 83 c.Println("run command: ", ctxout.ForeLightBlue, "PrintPaths") 84 runInfos := ctxout.GetRunInfos() 85 c.PrintPaths(false, false) 86 addTxt := strings.Join(runInfos, "|") 87 itm.GetText().SetText("PrintPaths:" + addTxt) 88 c.Println(addTxt) 89 }) 90 91 // add cobra commands to menu 92 /* 93 for _, cmd := range c.session.Cobra.RootCmd.Commands() { 94 menu.AddItemWithRef(cmd.Name(), cmd, func(itm *ctxtcell.MenuElement) { 95 itm.GetText().SetText("RUNS") 96 cmdIntern := itm.GetReference().(*cobra.Command) 97 ctxout.CtxOut(c.session.OutPutHdnl, "run command: ", ctxout.ForeLightBlue, cmdIntern.Name()) 98 cmdIntern.Run(cmdIntern, []string{}) 99 itm.GetText().SetText(cmdIntern.Name() + " done") 100 }) 101 } 102 */ 103 menu.SetVisible(false) 104 tc.AddElement(menu) 105 }