首页 > 后端开发 > Golang > 正文

GoLang:gocui边框颜色

WBOY
发布: 2024-02-13 23:54:09
转载
1007人浏览过

golang:gocui边框颜色

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

问题内容

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

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

我已经尝试过:

立即学习go语言免费学习笔记(深入)”;

g.highlight = true
登录后复制

每个文档仍然不起作用。

这是我的完整代码:

Cutout老照片上色
Cutout老照片上色

Cutout.Pro推出的黑白图片上色

Cutout老照片上色 20
查看详情 Cutout老照片上色
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 中。

以上就是GoLang:gocui边框颜色的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号