From 84a78cc106db59d4e28a197222dd3e582abc0b48 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Wed, 23 Dec 2020 17:53:30 +0100 Subject: [PATCH 1/2] Fix circleci build error --- .circleci/config.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 89d5f0ad..e275579a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,10 +1,10 @@ version: 2 jobs: build: - working_directory: /go/src/github.com/glvr182/git-profile - + working_directory: /go/src/github.com/awesome-gocui/gocui + docker: - - image: circleci/golang:1.12 + - image: circleci/golang:1.15 steps: - checkout @@ -13,7 +13,7 @@ jobs: command: go mod tidy - run: name: go fmt - command: | + command: | if [ $(find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \;|wc -l) -gt 0 ]; then find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \; exit 1; @@ -24,11 +24,11 @@ jobs: cd _examples/ for file in *.go do - go build $file + go build $file done workflows: version: 2 build: jobs: - - build \ No newline at end of file + - build From 05b9329b5b69579d0ea5e9b05d93a2e7a3e83c45 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Wed, 23 Dec 2020 18:56:51 +0100 Subject: [PATCH 2/2] Ran gofmt over the whole project --- _examples/custom_frame.go | 282 ++++++++++++++++---------------- attribute.go | 330 +++++++++++++++++++------------------- 2 files changed, 306 insertions(+), 306 deletions(-) diff --git a/_examples/custom_frame.go b/_examples/custom_frame.go index 6087bac8..ef4ae9f2 100644 --- a/_examples/custom_frame.go +++ b/_examples/custom_frame.go @@ -1,141 +1,141 @@ -package main - -import ( - "fmt" - "log" - - "github.com/awesome-gocui/gocui" -) - -var ( - viewArr = []string{"v1", "v2", "v3", "v4"} - active = 0 -) - -func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { - if _, err := g.SetCurrentView(name); err != nil { - return nil, err - } - return g.SetViewOnTop(name) -} - -func nextView(g *gocui.Gui, v *gocui.View) error { - nextIndex := (active + 1) % len(viewArr) - name := viewArr[nextIndex] - - out, err := g.View("v1") - 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 == 3 { - g.Cursor = true - } else { - g.Cursor = false - } - - active = nextIndex - return nil -} - -func layout(g *gocui.Gui) error { - maxX, maxY := g.Size() - if v, err := g.SetView("v1", 0, 0, maxX/2-1, maxY/2-1, gocui.RIGHT); err != nil { - if !gocui.IsUnknownView(err) { - return err - } - v.Title = "v1" - v.Autoscroll = true - fmt.Fprintln(v, "View with default frame color") - fmt.Fprintln(v, "It's connected to v2 with overlay RIGHT.\n") - if _, err = setCurrentViewOnTop(g, "v1"); err != nil { - return err - } - } - - if v, err := g.SetView("v2", maxX/2-1, 0, maxX-1, maxY/2-1, gocui.LEFT); err != nil { - if !gocui.IsUnknownView(err) { - return err - } - v.Title = "v2" - v.Wrap = true - v.FrameColor = gocui.ColorMagenta - v.FrameRunes = []rune{'═', '│'} - fmt.Fprintln(v, "View with minimum frame customization and colored frame.") - fmt.Fprintln(v, "It's connected to v1 with overlay LEFT.\n") - fmt.Fprintln(v, "\033[35;1mInstructions:\033[0m") - fmt.Fprintln(v, "Press TAB to change current view") - fmt.Fprintln(v, "Press Ctrl+O to toggle gocui.SupportOverlap\n") - fmt.Fprintln(v, "\033[32;2mSelected frame is highlighted with green color\033[0m") - } - if v, err := g.SetView("v3", 0, maxY/2, maxX/2-1, maxY-1, 0); err != nil { - if !gocui.IsUnknownView(err) { - return err - } - v.Title = "v3" - v.Wrap = true - v.Autoscroll = true - v.FrameColor = gocui.ColorCyan - v.TitleColor = gocui.ColorCyan - v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝'} - fmt.Fprintln(v, "View with basic frame customization and colored frame and title") - fmt.Fprintln(v, "It's not connected to any view.") - } - if v, err := g.SetView("v4", maxX/2, maxY/2, maxX-1, maxY-1, gocui.LEFT); err != nil { - if !gocui.IsUnknownView(err) { - return err - } - v.Title = "v4" - v.Subtitle = "(editable)" - v.Editable = true - v.TitleColor = gocui.ColorYellow - v.FrameColor = gocui.ColorRed - v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩', '╬'} - fmt.Fprintln(v, "View with fully customized frame and colored title differently.") - fmt.Fprintln(v, "It's connected to v3 with overlay LEFT.\n") - v.SetCursor(0, 3) - } - return nil -} - -func quit(g *gocui.Gui, v *gocui.View) error { - return gocui.ErrQuit -} - -func toggleOverlap(g *gocui.Gui, v *gocui.View) error { - g.SupportOverlaps = !g.SupportOverlaps - return nil -} - -func main() { - g, err := gocui.NewGui(gocui.OutputNormal, true) - if err != nil { - log.Panicln(err) - } - defer g.Close() - - g.Highlight = true - g.SelFgColor = gocui.ColorGreen - g.SelFrameColor = gocui.ColorGreen - - g.SetManagerFunc(layout) - - if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { - log.Panicln(err) - } - if err := g.SetKeybinding("", gocui.KeyCtrlO, gocui.ModNone, toggleOverlap); err != nil { - log.Panicln(err) - } - if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { - log.Panicln(err) - } - - if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) { - log.Panicln(err) - } -} +package main + +import ( + "fmt" + "log" + + "github.com/awesome-gocui/gocui" +) + +var ( + viewArr = []string{"v1", "v2", "v3", "v4"} + active = 0 +) + +func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { + if _, err := g.SetCurrentView(name); err != nil { + return nil, err + } + return g.SetViewOnTop(name) +} + +func nextView(g *gocui.Gui, v *gocui.View) error { + nextIndex := (active + 1) % len(viewArr) + name := viewArr[nextIndex] + + out, err := g.View("v1") + 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 == 3 { + g.Cursor = true + } else { + g.Cursor = false + } + + active = nextIndex + return nil +} + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("v1", 0, 0, maxX/2-1, maxY/2-1, gocui.RIGHT); err != nil { + if !gocui.IsUnknownView(err) { + return err + } + v.Title = "v1" + v.Autoscroll = true + fmt.Fprintln(v, "View with default frame color") + fmt.Fprintln(v, "It's connected to v2 with overlay RIGHT.\n") + if _, err = setCurrentViewOnTop(g, "v1"); err != nil { + return err + } + } + + if v, err := g.SetView("v2", maxX/2-1, 0, maxX-1, maxY/2-1, gocui.LEFT); err != nil { + if !gocui.IsUnknownView(err) { + return err + } + v.Title = "v2" + v.Wrap = true + v.FrameColor = gocui.ColorMagenta + v.FrameRunes = []rune{'═', '│'} + fmt.Fprintln(v, "View with minimum frame customization and colored frame.") + fmt.Fprintln(v, "It's connected to v1 with overlay LEFT.\n") + fmt.Fprintln(v, "\033[35;1mInstructions:\033[0m") + fmt.Fprintln(v, "Press TAB to change current view") + fmt.Fprintln(v, "Press Ctrl+O to toggle gocui.SupportOverlap\n") + fmt.Fprintln(v, "\033[32;2mSelected frame is highlighted with green color\033[0m") + } + if v, err := g.SetView("v3", 0, maxY/2, maxX/2-1, maxY-1, 0); err != nil { + if !gocui.IsUnknownView(err) { + return err + } + v.Title = "v3" + v.Wrap = true + v.Autoscroll = true + v.FrameColor = gocui.ColorCyan + v.TitleColor = gocui.ColorCyan + v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝'} + fmt.Fprintln(v, "View with basic frame customization and colored frame and title") + fmt.Fprintln(v, "It's not connected to any view.") + } + if v, err := g.SetView("v4", maxX/2, maxY/2, maxX-1, maxY-1, gocui.LEFT); err != nil { + if !gocui.IsUnknownView(err) { + return err + } + v.Title = "v4" + v.Subtitle = "(editable)" + v.Editable = true + v.TitleColor = gocui.ColorYellow + v.FrameColor = gocui.ColorRed + v.FrameRunes = []rune{'═', '║', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩', '╬'} + fmt.Fprintln(v, "View with fully customized frame and colored title differently.") + fmt.Fprintln(v, "It's connected to v3 with overlay LEFT.\n") + v.SetCursor(0, 3) + } + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +} + +func toggleOverlap(g *gocui.Gui, v *gocui.View) error { + g.SupportOverlaps = !g.SupportOverlaps + return nil +} + +func main() { + g, err := gocui.NewGui(gocui.OutputNormal, true) + if err != nil { + log.Panicln(err) + } + defer g.Close() + + g.Highlight = true + g.SelFgColor = gocui.ColorGreen + g.SelFrameColor = gocui.ColorGreen + + g.SetManagerFunc(layout) + + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + if err := g.SetKeybinding("", gocui.KeyCtrlO, gocui.ModNone, toggleOverlap); err != nil { + log.Panicln(err) + } + if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { + log.Panicln(err) + } + + if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) { + log.Panicln(err) + } +} diff --git a/attribute.go b/attribute.go index adc933ff..54e39fc2 100644 --- a/attribute.go +++ b/attribute.go @@ -1,165 +1,165 @@ -// Copyright 2020 The gocui Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package gocui - -import "github.com/gdamore/tcell/v2" - -// Attribute affects the presentation of characters, such as color, boldness, etc. -type Attribute uint64 - -const ( - // ColorDefault is used to leave the Color unchanged from whatever system or teminal default may exist. - ColorDefault = Attribute(tcell.ColorDefault) - - // AttrIsValidColor is used to indicate the color value is actually - // valid (initialized). This is useful to permit the zero value - // to be treated as the default. - AttrIsValidColor = Attribute(tcell.ColorValid) - - // AttrIsRGBColor is used to indicate that the Attribute value is RGB value of color. - // The lower order 3 bytes are RGB. - // (It's not a color in basic ANSI range 256). - AttrIsRGBColor = Attribute(tcell.ColorIsRGB) - - // AttrColorBits is a mask where color is located in Attribute - AttrColorBits = 0xffffffffff // roughly 5 bytes, tcell uses 4 bytes and half-byte as a special flags for color (rest is reserved for future) - - // AttrStyleBits is a mask where character attributes (e.g.: bold, italic, underline) are located in Attribute - AttrStyleBits = 0xffffff0000000000 // remaining 3 bytes in the 8 bytes Attribute (tcell is not using it, so we should be fine) -) - -// Color attributes. These colors are compatible with tcell.Color type and can be expanded like: -// g.FgColor := gocui.Attribute(tcell.ColorLime) -const ( - ColorBlack Attribute = AttrIsValidColor + iota - ColorRed - ColorGreen - ColorYellow - ColorBlue - ColorMagenta - ColorCyan - ColorWhite -) - -// grayscale indexes (for backward compatibility with termbox-go original grayscale) -var grayscale = []tcell.Color{ - 16, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, - 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 231, -} - -// Attributes are not colors, but effects (e.g.: bold, dim) which affect the display of text. -// They can be combined. -const ( - AttrBold Attribute = 1 << (40 + iota) - AttrBlink - AttrReverse - AttrUnderline - AttrDim - AttrItalic - AttrStrikeThrough - AttrNone Attribute = 0 // Just normal text. -) - -// AttrAll represents all the text effect attributes turned on -const AttrAll = AttrBold | AttrBlink | AttrReverse | AttrUnderline | AttrDim | AttrItalic - -// IsValidColor indicates if the Attribute is a valid color value (has been set). -func (a Attribute) IsValidColor() bool { - return a&AttrIsValidColor != 0 -} - -// Hex returns the color's hexadecimal RGB 24-bit value with each component -// consisting of a single byte, ala R << 16 | G << 8 | B. If the color -// is unknown or unset, -1 is returned. -// -// This function produce the same output as `tcell.Hex()` with additional -// support for `termbox-go` colors (to 256). -func (a Attribute) Hex() int32 { - if !a.IsValidColor() { - return -1 - } - tc := getTcellColor(a, OutputTrue) - return tc.Hex() -} - -// RGB returns the red, green, and blue components of the color, with -// each component represented as a value 0-255. If the color -// is unknown or unset, -1 is returned for each component. -// -// This function produce the same output as `tcell.RGB()` with additional -// support for `termbox-go` colors (to 256). -func (a Attribute) RGB() (int32, int32, int32) { - v := a.Hex() - if v < 0 { - return -1, -1, -1 - } - return (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff -} - -// GetColor creates a Color from a color name (W3C name). A hex value may -// be supplied as a string in the format "#ffffff". -func GetColor(color string) Attribute { - return Attribute(tcell.GetColor(color)) -} - -// Get256Color creates Attribute which stores ANSI color (0-255) -func Get256Color(color int32) Attribute { - return Attribute(color) | AttrIsValidColor -} - -// GetRGBColor creates Attribute which stores RGB color. -// Color is passed as 24bit RGB value, where R << 16 | G << 8 | B -func GetRGBColor(color int32) Attribute { - return Attribute(color) | AttrIsValidColor | AttrIsRGBColor -} - -// NewRGBColor creates Attribute which stores RGB color. -func NewRGBColor(r, g, b int32) Attribute { - return Attribute(tcell.NewRGBColor(r, g, b)) -} - -// getTcellColor transform Attribute into tcell.Color -func getTcellColor(c Attribute, omode OutputMode) tcell.Color { - c = c & AttrColorBits - // Default color is 0 in tcell/v2 and was 0 in termbox-go, so we are good here - if c == ColorDefault { - return tcell.ColorDefault - } - - tc := tcell.ColorDefault - // Check if we have valid color - if c.IsValidColor() { - tc = tcell.Color(c) - } else if c > 0 && c <= 256 { - // It's not valid color, but it has value in range 1-256 - // This is old Attribute style of color from termbox-go (black=1, etc.) - // convert to tcell color (black=0|ColorValid) - tc = tcell.Color(c-1) | tcell.ColorValid - } - - switch omode { - case OutputTrue: - return tc - case OutputNormal: - tc &= tcell.Color(0xf) | tcell.ColorValid - case Output256: - tc &= tcell.Color(0xff) | tcell.ColorValid - case Output216: - tc &= tcell.Color(0xff) - if tc > 215 { - return tcell.ColorDefault - } - tc += tcell.Color(16) | tcell.ColorValid - case OutputGrayscale: - tc &= tcell.Color(0x1f) - if tc > 26 { - return tcell.ColorDefault - } - tc = grayscale[tc] | tcell.ColorValid - default: - return tcell.ColorDefault - } - return tc -} +// Copyright 2020 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package gocui + +import "github.com/gdamore/tcell/v2" + +// Attribute affects the presentation of characters, such as color, boldness, etc. +type Attribute uint64 + +const ( + // ColorDefault is used to leave the Color unchanged from whatever system or teminal default may exist. + ColorDefault = Attribute(tcell.ColorDefault) + + // AttrIsValidColor is used to indicate the color value is actually + // valid (initialized). This is useful to permit the zero value + // to be treated as the default. + AttrIsValidColor = Attribute(tcell.ColorValid) + + // AttrIsRGBColor is used to indicate that the Attribute value is RGB value of color. + // The lower order 3 bytes are RGB. + // (It's not a color in basic ANSI range 256). + AttrIsRGBColor = Attribute(tcell.ColorIsRGB) + + // AttrColorBits is a mask where color is located in Attribute + AttrColorBits = 0xffffffffff // roughly 5 bytes, tcell uses 4 bytes and half-byte as a special flags for color (rest is reserved for future) + + // AttrStyleBits is a mask where character attributes (e.g.: bold, italic, underline) are located in Attribute + AttrStyleBits = 0xffffff0000000000 // remaining 3 bytes in the 8 bytes Attribute (tcell is not using it, so we should be fine) +) + +// Color attributes. These colors are compatible with tcell.Color type and can be expanded like: +// g.FgColor := gocui.Attribute(tcell.ColorLime) +const ( + ColorBlack Attribute = AttrIsValidColor + iota + ColorRed + ColorGreen + ColorYellow + ColorBlue + ColorMagenta + ColorCyan + ColorWhite +) + +// grayscale indexes (for backward compatibility with termbox-go original grayscale) +var grayscale = []tcell.Color{ + 16, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, + 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 231, +} + +// Attributes are not colors, but effects (e.g.: bold, dim) which affect the display of text. +// They can be combined. +const ( + AttrBold Attribute = 1 << (40 + iota) + AttrBlink + AttrReverse + AttrUnderline + AttrDim + AttrItalic + AttrStrikeThrough + AttrNone Attribute = 0 // Just normal text. +) + +// AttrAll represents all the text effect attributes turned on +const AttrAll = AttrBold | AttrBlink | AttrReverse | AttrUnderline | AttrDim | AttrItalic + +// IsValidColor indicates if the Attribute is a valid color value (has been set). +func (a Attribute) IsValidColor() bool { + return a&AttrIsValidColor != 0 +} + +// Hex returns the color's hexadecimal RGB 24-bit value with each component +// consisting of a single byte, ala R << 16 | G << 8 | B. If the color +// is unknown or unset, -1 is returned. +// +// This function produce the same output as `tcell.Hex()` with additional +// support for `termbox-go` colors (to 256). +func (a Attribute) Hex() int32 { + if !a.IsValidColor() { + return -1 + } + tc := getTcellColor(a, OutputTrue) + return tc.Hex() +} + +// RGB returns the red, green, and blue components of the color, with +// each component represented as a value 0-255. If the color +// is unknown or unset, -1 is returned for each component. +// +// This function produce the same output as `tcell.RGB()` with additional +// support for `termbox-go` colors (to 256). +func (a Attribute) RGB() (int32, int32, int32) { + v := a.Hex() + if v < 0 { + return -1, -1, -1 + } + return (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff +} + +// GetColor creates a Color from a color name (W3C name). A hex value may +// be supplied as a string in the format "#ffffff". +func GetColor(color string) Attribute { + return Attribute(tcell.GetColor(color)) +} + +// Get256Color creates Attribute which stores ANSI color (0-255) +func Get256Color(color int32) Attribute { + return Attribute(color) | AttrIsValidColor +} + +// GetRGBColor creates Attribute which stores RGB color. +// Color is passed as 24bit RGB value, where R << 16 | G << 8 | B +func GetRGBColor(color int32) Attribute { + return Attribute(color) | AttrIsValidColor | AttrIsRGBColor +} + +// NewRGBColor creates Attribute which stores RGB color. +func NewRGBColor(r, g, b int32) Attribute { + return Attribute(tcell.NewRGBColor(r, g, b)) +} + +// getTcellColor transform Attribute into tcell.Color +func getTcellColor(c Attribute, omode OutputMode) tcell.Color { + c = c & AttrColorBits + // Default color is 0 in tcell/v2 and was 0 in termbox-go, so we are good here + if c == ColorDefault { + return tcell.ColorDefault + } + + tc := tcell.ColorDefault + // Check if we have valid color + if c.IsValidColor() { + tc = tcell.Color(c) + } else if c > 0 && c <= 256 { + // It's not valid color, but it has value in range 1-256 + // This is old Attribute style of color from termbox-go (black=1, etc.) + // convert to tcell color (black=0|ColorValid) + tc = tcell.Color(c-1) | tcell.ColorValid + } + + switch omode { + case OutputTrue: + return tc + case OutputNormal: + tc &= tcell.Color(0xf) | tcell.ColorValid + case Output256: + tc &= tcell.Color(0xff) | tcell.ColorValid + case Output216: + tc &= tcell.Color(0xff) + if tc > 215 { + return tcell.ColorDefault + } + tc += tcell.Color(16) | tcell.ColorValid + case OutputGrayscale: + tc &= tcell.Color(0x1f) + if tc > 26 { + return tcell.ColorDefault + } + tc = grayscale[tc] | tcell.ColorValid + default: + return tcell.ColorDefault + } + return tc +}