php小編小新為您介紹GoLang中的gocui邊框顏色設(shè)置。gocui是一個(gè)強(qiáng)大的Go語(yǔ)言庫(kù),用于創(chuàng)建命令行界面的交互式應(yīng)用程序。在gocui中,可以通過(guò)設(shè)置邊框顏色來(lái)增加界面的美觀性和可讀性。通過(guò)簡(jiǎn)單的代碼修改,您可以為界面的邊框添加自定義的顏色,使得應(yīng)用程序更加吸引人。接下來(lái),讓我們一起來(lái)了解如何在GoLang中使用gocui庫(kù)來(lái)實(shí)現(xiàn)邊框顏色的設(shè)置吧!
問(wèn)題內(nèi)容
人工智能機(jī)器人出現(xiàn)問(wèn)題,所以我在這里問(wèn):
我有這個(gè)代碼,它只輸出兩個(gè)窗口,而不是背景,我希望邊框是綠色的。
我已經(jīng)嘗試過(guò):
g.highlight = true
登錄后復(fù)制
每個(gè)文檔仍然不起作用。
這是我的完整代碼:
package main import ( // "fmt" "log" "github.com/jroimartin/gocui" ) func main() { // Create a new gocui view g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) } defer g.Close() g.SetManagerFunc(layout) if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil { log.Panicln(err) } if err := g.SetKeybinding("", '1', gocui.ModNone, view1); err != nil { log.Panicln(err) } if err := g.SetKeybinding("", '2', gocui.ModNone, view2); err != nil { log.Panicln(err) } if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } func updateView(g *gocui.Gui, v *gocui.View) error { // Check if the view is active if v != nil && v == g.CurrentView() { // If the view is active, set its background color to yellow v.BgColor = gocui.ColorYellow } else { // If the view is not active, set its background color to default (nothing) v.BgColor = gocui.ColorDefault } return nil } func layout(g *gocui.Gui) error { // maxX, maxY := g.Size() g.Highlight = true // Create a new view with the name "myView" if v, err := g.SetView("view1", 0, 0, 20, 10); err != nil { if err != gocui.ErrUnknownView { log.Panicln(err) } // Set the default background color of the view to nothing v.BgColor = gocui.ColorDefault v.Title = "View 1" v.Wrap = false // fmt.Fprintln(v, "View 1") } // Set a second view with the name "myOtherView" if v, err := g.SetView("view2", 25, 0, 45, 10); err != nil { if err != gocui.ErrUnknownView { log.Panicln(err) } // Set the default background color of the view to nothing v.BgColor = gocui.ColorDefault v.Title = "View 2" v.Wrap = false // fmt.Fprintln(v, "View 2") } return nil } func view1(g *gocui.Gui, v *gocui.View) error { if _, err := g.SetCurrentView("view1"); err != nil { return err } updateHighlighting(g, v) return nil } func view2(g *gocui.Gui, v *gocui.View) error { if _, err := g.SetCurrentView("view2"); err != nil { return err } updateHighlighting(g, v) return nil } func updateHighlighting(g *gocui.Gui, v *gocui.View) error { current := g.CurrentView() for _, view := range g.Views() { if view == current { current.BgColor = gocui.ColorGreen } else { view.BgColor = gocui.ColorDefault } } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit }
登錄后復(fù)制
解決方法
view
的框架由呈現(xiàn)該框架的 gui
處理。您已將 gui
的突出顯示設(shè)置為 true,但未設(shè)置 selbgcolor
或 selfgcolor
。
來(lái)自 https://pkg.go.dev/github.com/ jroimartin/gocui#gui:
// selbgcolor and selfgcolor allow to configure the background and // foreground colors of the frame of the current view. selbgcolor, selfgcolor attribute // if highlight is true, sel{bg,fg}colors will be used to draw the // frame of the current view. highlight bool
登錄后復(fù)制
通過(guò)將布局的開(kāi)頭更改為以下內(nèi)容,添加 g.selfgcolor = gocui.colorgreen
:
func layout(g *gocui.Gui) error { // maxX, maxY := g.Size() g.Highlight = true g.SelFgColor = gocui.ColorGreen ... ...
登錄后復(fù)制
然后你會(huì)得到一個(gè)黑色背景的綠色邊框:
如果您不希望背景也變成綠色,請(qǐng)刪除此行:
current.bgcolor = gocui.colorgreen
一開(kāi)始有點(diǎn)混亂,因?yàn)?view
和 gui
都有 selfgcolor
selbgcolor
和 highlight
,但是 view
的屬性控制的是視圖中的選定文本,而不是當(dāng)前視圖的邊框在 gui 中。