GoLang: gocui 边框颜色

huangapple go评论108阅读模式
英文:

GoLang: gocui border color

问题

AI机器人出现问题,所以我在这里提问:

我有这段代码,它只输出两个窗口,但是我想要将边框的颜色改为绿色,而不是背景

我尝试了:

  1. g.Highlight = true

根据文档,但仍然不起作用。

这是我的整个代码:

  1. package main
  2. import (
  3. // "fmt"
  4. "log"
  5. "github.com/jroimartin/gocui"
  6. )
  7. func main() {
  8. // 创建一个新的gocui视图
  9. g, err := gocui.NewGui(gocui.OutputNormal)
  10. if err != nil {
  11. log.Panicln(err)
  12. }
  13. defer g.Close()
  14. g.SetManagerFunc(layout)
  15. if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
  16. log.Panicln(err)
  17. }
  18. if err := g.SetKeybinding("", '1', gocui.ModNone, view1); err != nil {
  19. log.Panicln(err)
  20. }
  21. if err := g.SetKeybinding("", '2', gocui.ModNone, view2); err != nil {
  22. log.Panicln(err)
  23. }
  24. if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
  25. log.Panicln(err)
  26. }
  27. }
  28. func updateView(g *gocui.Gui, v *gocui.View) error {
  29. // 检查视图是否处于活动状态
  30. if v != nil && v == g.CurrentView() {
  31. // 如果视图处于活动状态,则将其背景颜色设置为黄色
  32. v.BgColor = gocui.ColorYellow
  33. } else {
  34. // 如果视图不处于活动状态,则将其背景颜色设置为默认(无)
  35. v.BgColor = gocui.ColorDefault
  36. }
  37. return nil
  38. }
  39. func layout(g *gocui.Gui) error {
  40. // maxX, maxY := g.Size()
  41. g.Highlight = true
  42. // 创建一个名为"view1"的新视图
  43. if v, err := g.SetView("view1", 0, 0, 20, 10); err != nil {
  44. if err != gocui.ErrUnknownView {
  45. log.Panicln(err)
  46. }
  47. // 将视图的默认背景颜色设置为无
  48. v.BgColor = gocui.ColorDefault
  49. v.Title = "View 1"
  50. v.Wrap = false
  51. // fmt.Fprintln(v, "View 1")
  52. }
  53. // 创建一个名为"view2"的第二个视图
  54. if v, err := g.SetView("view2", 25, 0, 45, 10); err != nil {
  55. if err != gocui.ErrUnknownView {
  56. log.Panicln(err)
  57. }
  58. // 将视图的默认背景颜色设置为无
  59. v.BgColor = gocui.ColorDefault
  60. v.Title = "View 2"
  61. v.Wrap = false
  62. // fmt.Fprintln(v, "View 2")
  63. }
  64. return nil
  65. }
  66. func view1(g *gocui.Gui, v *gocui.View) error {
  67. if _, err := g.SetCurrentView("view1"); err != nil {
  68. return err
  69. }
  70. updateHighlighting(g, v)
  71. return nil
  72. }
  73. func view2(g *gocui.Gui, v *gocui.View) error {
  74. if _, err := g.SetCurrentView("view2"); err != nil {
  75. return err
  76. }
  77. updateHighlighting(g, v)
  78. return nil
  79. }
  80. func updateHighlighting(g *gocui.Gui, v *gocui.View) error {
  81. current := g.CurrentView()
  82. for _, view := range g.Views() {
  83. if view == current {
  84. current.BgColor = gocui.ColorGreen
  85. } else {
  86. view.BgColor = gocui.ColorDefault
  87. }
  88. }
  89. return nil
  90. }
  91. func quit(g *gocui.Gui, v *gocui.View) error {
  92. return gocui.ErrQuit
  93. }
英文:

The AI bot is acting up so I'm asking here:

I have this code which just outputs two windows and instead of the background, I want the border to be green.

I've tried:

  1. g.Highlight = true

per docs that still doesn't work.

GoLang: gocui 边框颜色

Here is my entire code:

  1. package main
  2. import (
  3. // "fmt"
  4. "log"
  5. "github.com/jroimartin/gocui"
  6. )
  7. func main() {
  8. // Create a new gocui view
  9. g, err := gocui.NewGui(gocui.OutputNormal)
  10. if err != nil {
  11. log.Panicln(err)
  12. }
  13. defer g.Close()
  14. g.SetManagerFunc(layout)
  15. if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
  16. log.Panicln(err)
  17. }
  18. if err := g.SetKeybinding("", '1', gocui.ModNone, view1); err != nil {
  19. log.Panicln(err)
  20. }
  21. if err := g.SetKeybinding("", '2', gocui.ModNone, view2); err != nil {
  22. log.Panicln(err)
  23. }
  24. if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
  25. log.Panicln(err)
  26. }
  27. }
  28. func updateView(g *gocui.Gui, v *gocui.View) error {
  29. // Check if the view is active
  30. if v != nil && v == g.CurrentView() {
  31. // If the view is active, set its background color to yellow
  32. v.BgColor = gocui.ColorYellow
  33. } else {
  34. // If the view is not active, set its background color to default (nothing)
  35. v.BgColor = gocui.ColorDefault
  36. }
  37. return nil
  38. }
  39. func layout(g *gocui.Gui) error {
  40. // maxX, maxY := g.Size()
  41. g.Highlight = true
  42. // Create a new view with the name "myView"
  43. if v, err := g.SetView("view1", 0, 0, 20, 10); err != nil {
  44. if err != gocui.ErrUnknownView {
  45. log.Panicln(err)
  46. }
  47. // Set the default background color of the view to nothing
  48. v.BgColor = gocui.ColorDefault
  49. v.Title = "View 1"
  50. v.Wrap = false
  51. // fmt.Fprintln(v, "View 1")
  52. }
  53. // Set a second view with the name "myOtherView"
  54. if v, err := g.SetView("view2", 25, 0, 45, 10); err != nil {
  55. if err != gocui.ErrUnknownView {
  56. log.Panicln(err)
  57. }
  58. // Set the default background color of the view to nothing
  59. v.BgColor = gocui.ColorDefault
  60. v.Title = "View 2"
  61. v.Wrap = false
  62. // fmt.Fprintln(v, "View 2")
  63. }
  64. return nil
  65. }
  66. func view1(g *gocui.Gui, v *gocui.View) error {
  67. if _, err := g.SetCurrentView("view1"); err != nil {
  68. return err
  69. }
  70. updateHighlighting(g, v)
  71. return nil
  72. }
  73. func view2(g *gocui.Gui, v *gocui.View) error {
  74. if _, err := g.SetCurrentView("view2"); err != nil {
  75. return err
  76. }
  77. updateHighlighting(g, v)
  78. return nil
  79. }
  80. func updateHighlighting(g *gocui.Gui, v *gocui.View) error {
  81. current := g.CurrentView()
  82. for _, view := range g.Views() {
  83. if view == current {
  84. current.BgColor = gocui.ColorGreen
  85. } else {
  86. view.BgColor = gocui.ColorDefault
  87. }
  88. }
  89. return nil
  90. }
  91. func quit(g *gocui.Gui, v *gocui.View) error {
  92. return gocui.ErrQuit
  93. }

答案1

得分: 1

一个View的框架由渲染该框架的Gui处理。你已经将你的Gui的高亮设置为true,但是你没有设置SelBgColor或者SelFgColor

从https://pkg.go.dev/github.com/jroimartin/gocui#Gui中可以看到:

  1. // SelBgColor和SelFgColor允许配置当前视图的框架的背景和前景颜色。
  2. SelBgColor, SelFgColor Attribute
  3. // 如果Highlight为true,则Sel{Bg,Fg}Colors将用于绘制当前视图的框架。
  4. Highlight bool

通过将你的布局的开头更改为以下内容,并添加g.SelFgColor = gocui.ColorGreen

  1. func layout(g *gocui.Gui) error {
  2. // maxX, maxY := g.Size()
  3. g.Highlight = true
  4. g.SelFgColor = gocui.ColorGreen
  5. ...
  6. ...

然后你将得到一个带有黑色背景的绿色边框:
GoLang: gocui 边框颜色

如果你不想让背景也是绿色的,请将这一行删除:

current.BgColor = gocui.ColorGreen

GoLang: gocui 边框颜色

起初可能有点困惑,因为ViewGui都有SelFgColorSelBgColorHighlight,但是View的属性控制的是视图中选定的文本,而不是Gui中当前视图的边框。

英文:

A View's frame is handled by the Gui that renders that frame. You have your Gui's highlight set to true already, but you don't set SelBgColor or SelFgColor.

From https://pkg.go.dev/github.com/jroimartin/gocui#Gui:

  1. // SelBgColor and SelFgColor allow to configure the background and
  2. // foreground colors of the frame of the current view.
  3. SelBgColor, SelFgColor Attribute
  4. // If Highlight is true, Sel{Bg,Fg}Colors will be used to draw the
  5. // frame of the current view.
  6. Highlight bool

By changing the beginning of your layout to the following, adding g.SelFgColor = gocui.ColorGreen:

  1. func layout(g *gocui.Gui) error {
  2. // maxX, maxY := g.Size()
  3. g.Highlight = true
  4. g.SelFgColor = gocui.ColorGreen
  5. ...
  6. ...

Then you'll get a green border with black background:
GoLang: gocui 边框颜色

If you don't want the background to be green also, take this line out:

> current.BgColor = gocui.ColorGreen

GoLang: gocui 边框颜色

It's a little confusing at first because both View and Gui have SelFgColor SelBgColor and Highlight, but View's attributes control the selected text in the view, not the border of the current view in the Gui.

huangapple
  • 本文由 发表于 2023年3月11日 01:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75699151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定