[TOC] > [github](https://github.com/jroimartin/gocui/) ## 特性案例 ## view 中填入内容 ``` if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { if err != gocui.ErrUnknownView { return err } fmt.Fprintln(v, "Hello world!") } ``` ### 退出 ``` if err := g.SetKeybinding("main", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } ``` ### 切换活动窗口 > [github demo](https://github.com/jroimartin/gocui/blob/master/_examples/active.go) ``` if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { log.Panicln(err) } func nextView(g *gocui.Gui, v *gocui.View) error { nextIndex := (active + 1) % len(viewArr) name := viewArr[nextIndex] out, err := g.View("v2") if err != nil { return err } fmt.Fprintln(out, "Going from view "+v.Name()+" to "+name) if _, err := setCurrentViewOnTop(g, name); err != nil { return err } if nextIndex == 0 || nextIndex == 3 { g.Cursor = true } else { g.Cursor = false } active = nextIndex return nil } ``` ### 字体颜色 > [github demo](https://github.com/jroimartin/gocui/blob/master/_examples/colors.go) 方式一 ``` func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("colors", maxX/2-7, maxY/2-12, maxX/2+7, maxY/2+13); err != nil { if err != gocui.ErrUnknownView { return err } for i := 0; i <= 7; i++ { for _, j := range []int{1, 4, 7} { fmt.Fprintf(v, "Hello \033[3%d;%dmcolors!\033[0m\n", i, j) //颜色 } } } return nil } ``` 方式二 ``` if v, err := g.SetView("but1", 2, 2, 22, 7); err != nil { if err != gocui.ErrUnknownView { return err } v.Highlight = true v.SelBgColor = gocui.ColorGreen //背景颜色 v.SelFgColor = gocui.ColorBlack //字体颜色 fmt.Fprintln(v, "Button 1 - line 1") } ``` ### 绑定键盘事件 ``` if err := g.SetKeybinding("side", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil { return err } ``` ## 定位当前 view ``` if v, err := g.SetView("input", 0, 0, maxX-24, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } if _, err := g.SetCurrentView("input"); err != nil { return err } v.Editable = true v.Wrap = true } ``` ## 鼠标点击定位当前 view > [git demo](https://github.com/jroimartin/gocui/blob/master/_examples/mouse.go) ``` if err := g.SetKeybinding(n, gocui.MouseLeft, gocui.ModNone, showMsg); err != nil { return err } func showMsg(g *gocui.Gui, v *gocui.View) error { var l string //点击选中的文字 var err error if _, err := g.SetCurrentView(v.Name()); err != nil { return err } _, cy := v.Cursor()//鼠标点击的位置 if l, err = v.Line(cy); err != nil { //点击位置读取文字 l = "" } maxX, maxY := g.Size() if v, err := g.SetView("msg", maxX/2-10, maxY/2, maxX/2+10, maxY/2+2); err != nil { if err != gocui.ErrUnknownView { return err } fmt.Fprintln(v, l) } return nil } ``` ### 对叠加的 view 设置置顶 > [github demo](https://github.com/jroimartin/gocui/blob/master/_examples/ontop.go) ``` err = g.SetKeybinding("", '1', gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { _, err := g.SetViewOnTop("v1") return err }) if err != nil { return err } ``` ## view 中文字是否换行 ``` if v, err := g.SetView("main", 1, 1, maxX-1, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Wrap = false line := strings.Repeat("This is a long line -- ", 10) fmt.Fprintf(v, "%s\n\n", line) fmt.Fprintln(v, "Short") } return nil ```