Home  >  Article  >  Backend Development  >  GoLang: gocui border color

GoLang: gocui border color

WBOY
WBOYforward
2024-02-13 23:54:09951browse

GoLang: gocui border color

php editor Xiaoxin introduces to you the gocui border color setting in GoLang. gocui is a powerful Go language library for creating interactive applications with command line interfaces. In gocui, you can increase the beauty and readability of the interface by setting the border color. With simple code modifications, you can add custom colors to the borders of the interface to make your application more attractive. Next, let us learn how to use the gocui library in GoLang to set the border color!

Question content

There is a problem with the artificial intelligence robot, so I ask here:

I have this code, it only outputs two windows, instead of background, I want the border to be green.

I've tried:

g.highlight = true

Every document still doesn't work.

This is my complete 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
}

Workaround

The frame of view is handled by the gui that renders the frame. You have set highlighting to true for gui, but not 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

Add g.selfgcolor = gocui.colorgreen by changing the beginning of the layout to the following:

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

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

Then you will get a green border with a black background:

If you don't want the background to turn green as well, remove this line:

current.bgcolor = gocui.colorgreen

It's a little confusing at first because view and gui both have selfgcolor selbgcolor and highlight, but The properties of view control the selected text in the view, not the border of the current view in the gui.

The above is the detailed content of GoLang: gocui border color. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete