Rumah  >  Artikel  >  pembangunan bahagian belakang  >  GoLang: warna sempadan gocui

GoLang: warna sempadan gocui

WBOY
WBOYke hadapan
2024-02-13 23:54:09885semak imbas

GoLang: warna sempadan gocui

Editor php Xiaoxin memperkenalkan kepada anda tetapan warna sempadan gocui dalam GoLang. gocui ialah perpustakaan bahasa Go yang berkuasa untuk mencipta aplikasi interaktif dengan antara muka baris arahan. Dalam gocui, anda boleh meningkatkan keindahan dan kebolehbacaan antara muka dengan menetapkan warna sempadan. Dengan pengubahsuaian kod mudah, anda boleh menambah warna tersuai pada sempadan antara muka untuk menjadikan aplikasi anda lebih menarik. Seterusnya, mari kita pelajari cara menggunakan perpustakaan gocui dalam GoLang untuk menetapkan warna jidar!

Kandungan soalan

Ada masalah dengan robot kecerdasan buatan, jadi saya bertanya di sini:

Saya mempunyai kod ini, ia hanya mengeluarkan dua tetingkap dan bukannya latar belakang, saya mahu sempadan menjadi hijau.

Saya dah cuba:

g.highlight = true

Setiap dokumen masih tidak berfungsi.

Ini kod lengkap saya:

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
}

Penyelesaian

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

Dari 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

Tambah g.selfgcolor = gocui.colorgreen dengan menukar permulaan reka letak kepada yang berikut:

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

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

Kemudian anda akan mendapat sempadan hijau pada latar belakang hitam:

Jika anda tidak mahu latar belakang bertukar hijau juga, alih keluar baris ini:

current.bgcolor = gocui.colorgreen

Ia agak mengelirukan pada mulanya kerana sifat viewgui 都有 selfgcolor selbgcolorhighlight,但是 view mengawal teks yang dipilih dalam paparan, bukan sempadan paparan semasa dalam gui.

Atas ialah kandungan terperinci GoLang: warna sempadan gocui. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:stackoverflow.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam