GoLang: gocui 边框颜色

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

GoLang: gocui border color

问题

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

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

我尝试了:

g.Highlight = true

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

这是我的整个代码:

package main

import (
	// "fmt"
	"log"

	"github.com/jroimartin/gocui"
)

func main() {
	// 创建一个新的gocui视图
	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 {
	// 检查视图是否处于活动状态
	if v != nil && v == g.CurrentView() {
		// 如果视图处于活动状态,则将其背景颜色设置为黄色
		v.BgColor = gocui.ColorYellow
	} else {
		// 如果视图不处于活动状态,则将其背景颜色设置为默认(无)
		v.BgColor = gocui.ColorDefault
	}

	return nil
}

func layout(g *gocui.Gui) error {
	// maxX, maxY := g.Size()

	g.Highlight = true

	// 创建一个名为"view1"的新视图
	if v, err := g.SetView("view1", 0, 0, 20, 10); err != nil {
		if err != gocui.ErrUnknownView {
			log.Panicln(err)
		}

		// 将视图的默认背景颜色设置为无
		v.BgColor = gocui.ColorDefault
		v.Title = "View 1"
		v.Wrap = false
		// fmt.Fprintln(v, "View 1")
	}

	// 创建一个名为"view2"的第二个视图
	if v, err := g.SetView("view2", 25, 0, 45, 10); err != nil {
		if err != gocui.ErrUnknownView {
			log.Panicln(err)
		}

		// 将视图的默认背景颜色设置为无
		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
}
英文:

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:

g.Highlight = true

per docs that still doesn't work.

GoLang: gocui 边框颜色

Here is my entire code:

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
}

答案1

得分: 1

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

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

	// SelBgColor和SelFgColor允许配置当前视图的框架的背景和前景颜色。
	SelBgColor, SelFgColor Attribute

	// 如果Highlight为true,则Sel{Bg,Fg}Colors将用于绘制当前视图的框架。
	Highlight bool

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

func layout(g *gocui.Gui) error {
    // maxX, maxY := g.Size()

    g.Highlight = true
    g.SelFgColor = gocui.ColorGreen
    ...
    ...

然后你将得到一个带有黑色背景的绿色边框:
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:

	// 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

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

func layout(g *gocui.Gui) error {
// maxX, maxY := g.Size()
g.Highlight = true
g.SelFgColor = gocui.ColorGreen
...
...

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:

确定