卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

GoLang:gocui边框颜色

golang:gocui边框颜色

php小编小新为您介绍GoLang中的gocui边框颜色设置。gocui是一个强大的Go语言库,用于创建命令行界面的交互式应用程序。在gocui中,可以通过设置边框颜色来增加界面的美观性和可读性。通过简单的代码修改,您可以为界面的边框添加自定义的颜色,使得应用程序更加吸引人。接下来,让我们一起来了解如何在GoLang中使用gocui库来实现边框颜色的设置吧!

问题内容

人工智能机器人出现问题,所以我在这里问:

我有这个代码,它只输出两个窗口,而不是背景,我希望边框是绿色的。

我已经尝试过:

g.highlight = true

每个文档仍然不起作用。

这是我的完整代码:

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
}

解决方法

view 的框架由呈现该框架的 gui 处理。您已将 gui 的突出显示设置为 true,但未设置 selbgcolorselfgcolor

来自 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

通过将布局的开头更改为以下内容,添加 g.selfgcolor = gocui.colorgreen

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

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

然后你会得到一个黑色背景的绿色边框:

如果您不希望背景也变成绿色,请删除此行:

current.bgcolor = gocui.colorgreen

一开始有点混乱,因为 viewgui 都有 selfgcolor selbgcolorhighlight,但是 view 的属性控制的是视图中的选定文本,而不是当前视图的边框在 gui 中。

卓越飞翔博客
上一篇: 根据字节部分解析文件
下一篇: spanner.Mutation 如何理解要更新哪一行
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏