mirror of https://github.com/miguelmota/cointop
clean up
parent
65faa8b0a3
commit
19684e3207
@ -0,0 +1,114 @@
|
||||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/anaskhan96/soup"
|
||||
packages = ["."]
|
||||
revision = "cd07662ec9300d16d2ece1c24b0b29ad89f65ff4"
|
||||
version = "v1.1"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/bradfitz/slice"
|
||||
packages = ["."]
|
||||
revision = "d9036e2120b5ddfa53f3ebccd618c4af275f47da"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/dustin/go-humanize"
|
||||
packages = ["."]
|
||||
revision = "bb3d318650d48840a39aa21a027c6630e198e626"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/fatih/color"
|
||||
packages = ["."]
|
||||
revision = "507f6050b8568533fb3f5504de8e5205fa62a114"
|
||||
version = "v1.6.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/gizak/termui"
|
||||
packages = ["."]
|
||||
revision = "24acd523c756fd9728824cdfac66aad9d8982fb7"
|
||||
version = "v2.2.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/jroimartin/gocui"
|
||||
packages = ["."]
|
||||
revision = "4f518eddb04b8f73870836b6d1941e8aa3c06637"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/maruel/panicparse"
|
||||
packages = ["stack"]
|
||||
revision = "785840568bdc7faa0dfb1cd6c643207f03271f64"
|
||||
version = "v1.1.1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/mattn/go-colorable"
|
||||
packages = ["."]
|
||||
revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072"
|
||||
version = "v0.0.9"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/mattn/go-isatty"
|
||||
packages = ["."]
|
||||
revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39"
|
||||
version = "v0.0.3"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/mattn/go-runewidth"
|
||||
packages = ["."]
|
||||
revision = "9e777a8366cce605130a531d2cd6363d07ad7317"
|
||||
version = "v0.0.2"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/miguelmota/go-coinmarketcap"
|
||||
packages = ["."]
|
||||
revision = "05fd2d0c45f763462f90e630da2ddc0392d2cdd3"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/mitchellh/go-wordwrap"
|
||||
packages = ["."]
|
||||
revision = "ad45545899c7b13c020ea92b2072220eefad42b8"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/nsf/termbox-go"
|
||||
packages = ["."]
|
||||
revision = "e2050e41c8847748ec5288741c0b19a8cb26d084"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/willf/pad"
|
||||
packages = ["."]
|
||||
revision = "b3d7806010228b1a954b4d9c4ee4cf6cb476b063"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "go4.org"
|
||||
packages = ["reflectutil"]
|
||||
revision = "e6a7f04a962e05f288f348eec6de20fe93b6b292"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/net"
|
||||
packages = [
|
||||
"html",
|
||||
"html/atom"
|
||||
]
|
||||
revision = "6078986fec03a1dcc236c34816c71b0e05018fda"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/sys"
|
||||
packages = ["unix"]
|
||||
revision = "378d26f46672a356c46195c28f61bdb4c0a781dd"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "ff54fa52bac3cde3f10f5c237dad28da497594584e4291356b5efd040501fe4e"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
@ -0,0 +1,58 @@
|
||||
# Gopkg.toml example
|
||||
#
|
||||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
||||
# for detailed Gopkg.toml documentation.
|
||||
#
|
||||
# required = ["github.com/user/thing/cmd/thing"]
|
||||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project"
|
||||
# version = "1.0.0"
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project2"
|
||||
# branch = "dev"
|
||||
# source = "github.com/myfork/project2"
|
||||
#
|
||||
# [[override]]
|
||||
# name = "github.com/x/y"
|
||||
# version = "2.4.0"
|
||||
#
|
||||
# [prune]
|
||||
# non-go = false
|
||||
# go-tests = true
|
||||
# unused-packages = true
|
||||
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/bradfitz/slice"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/dustin/go-humanize"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/fatih/color"
|
||||
version = "1.6.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/gizak/termui"
|
||||
version = "2.2.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/jroimartin/gocui"
|
||||
branch = "master"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/miguelmota/go-coinmarketcap"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/willf/pad"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
unused-packages = true
|
@ -0,0 +1,500 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bradfitz/slice"
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gizak/termui"
|
||||
"github.com/jroimartin/gocui"
|
||||
"github.com/miguelmota/cointop/table"
|
||||
cmc "github.com/miguelmota/go-coinmarketcap"
|
||||
"github.com/willf/pad"
|
||||
)
|
||||
|
||||
var (
|
||||
white = color.New(color.FgWhite).SprintFunc()
|
||||
green = color.New(color.FgGreen).SprintFunc()
|
||||
red = color.New(color.FgRed).SprintFunc()
|
||||
cyan = color.New(color.FgCyan).SprintFunc()
|
||||
)
|
||||
|
||||
var (
|
||||
oneMinute int64 = 60
|
||||
oneHour = oneMinute * 60
|
||||
oneDay = oneHour * 24
|
||||
oneWeek = oneDay * 7
|
||||
oneMonth = oneDay * 30
|
||||
oneYear = oneDay * 365
|
||||
)
|
||||
|
||||
// Cointop cointop
|
||||
type Cointop struct {
|
||||
g *gocui.Gui
|
||||
chartview *gocui.View
|
||||
chartpoints [][]termui.Cell
|
||||
headersview *gocui.View
|
||||
tableview *gocui.View
|
||||
table *table.Table
|
||||
sortdesc bool
|
||||
currentsort string
|
||||
coins []*cmc.Coin
|
||||
}
|
||||
|
||||
func (ct *Cointop) chartPoints(maxX int, coin string) error {
|
||||
chart := termui.NewLineChart()
|
||||
chart.Height = 10
|
||||
chart.AxesColor = termui.ColorWhite
|
||||
chart.LineColor = termui.ColorCyan
|
||||
chart.Border = false
|
||||
|
||||
now := time.Now()
|
||||
secs := now.Unix()
|
||||
start := secs - oneDay
|
||||
end := secs
|
||||
|
||||
_ = coin
|
||||
//graphData, err := cmc.GetCoinGraphData(coin, start, end)
|
||||
graphData, err := cmc.GetGlobalMarketGraphData(start, end)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var data []float64
|
||||
/*
|
||||
for i := range graphData.PriceUSD {
|
||||
data = append(data, graphData.PriceUSD[i][1])
|
||||
}
|
||||
*/
|
||||
for i := range graphData.MarketCapByAvailableSupply {
|
||||
data = append(data, graphData.MarketCapByAvailableSupply[i][1])
|
||||
}
|
||||
chart.Data = data
|
||||
termui.Body = termui.NewGrid()
|
||||
termui.Body.Width = maxX
|
||||
termui.Body.AddRows(
|
||||
termui.NewRow(
|
||||
termui.NewCol(12, 0, chart),
|
||||
),
|
||||
)
|
||||
|
||||
var points [][]termui.Cell
|
||||
// calculate layout
|
||||
termui.Body.Align()
|
||||
w := termui.Body.Width
|
||||
h := 10
|
||||
row := termui.Body.Rows[0]
|
||||
b := row.Buffer()
|
||||
for i := 0; i < h; i = i + 1 {
|
||||
var rowpoints []termui.Cell
|
||||
for j := 0; j < w; j = j + 1 {
|
||||
p := b.At(j, i)
|
||||
rowpoints = append(rowpoints, p)
|
||||
}
|
||||
points = append(points, rowpoints)
|
||||
}
|
||||
|
||||
ct.chartpoints = points
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
g, err := gocui.NewGui(gocui.Output256)
|
||||
if err != nil {
|
||||
log.Fatalf("new gocui: %v", err)
|
||||
}
|
||||
defer g.Close()
|
||||
g.Cursor = true
|
||||
g.Mouse = true
|
||||
g.Highlight = true
|
||||
|
||||
ct := Cointop{
|
||||
g: g,
|
||||
}
|
||||
g.SetManagerFunc(ct.layout)
|
||||
|
||||
if err := ct.keybindings(g); err != nil {
|
||||
log.Fatalf("keybindings: %v", err)
|
||||
}
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Fatalf("main loop: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (ct *Cointop) quit(g *gocui.Gui, v *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
func (ct *Cointop) keybindings(g *gocui.Gui) error {
|
||||
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, ct.cursorDown); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'j', gocui.ModNone, ct.cursorDown); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, ct.cursorUp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'k', gocui.ModNone, ct.cursorUp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone, ct.pageDown); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlU, gocui.ModNone, ct.pageUp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'r', gocui.ModNone, ct.sort("rank", false)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'n', gocui.ModNone, ct.sort("name", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 's', gocui.ModNone, ct.sort("symbol", false)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'p', gocui.ModNone, ct.sort("price", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'm', gocui.ModNone, ct.sort("marketcap", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'v', gocui.ModNone, ct.sort("24hvolume", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '1', gocui.ModNone, ct.sort("1hchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '2', gocui.ModNone, ct.sort("24hchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '7', gocui.ModNone, ct.sort("7dchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 't', gocui.ModNone, ct.sort("totalsupply", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'a', gocui.ModNone, ct.sort("availablesupply", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'l', gocui.ModNone, ct.sort("lastupdated", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, ct.quit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'q', gocui.ModNone, ct.quit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyEsc, gocui.ModNone, ct.quit); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) sort(sortby string, desc bool) func(g *gocui.Gui, v *gocui.View) error {
|
||||
return func(g *gocui.Gui, v *gocui.View) error {
|
||||
if ct.currentsort == sortby {
|
||||
ct.sortdesc = !ct.sortdesc
|
||||
} else {
|
||||
ct.currentsort = sortby
|
||||
ct.sortdesc = desc
|
||||
}
|
||||
slice.Sort(ct.coins[:], func(i, j int) bool {
|
||||
if ct.sortdesc {
|
||||
i, j = j, i
|
||||
}
|
||||
a := ct.coins[i]
|
||||
b := ct.coins[j]
|
||||
switch sortby {
|
||||
case "rank":
|
||||
return a.Rank < b.Rank
|
||||
case "name":
|
||||
return a.Name < b.Name
|
||||
case "symbol":
|
||||
return a.Symbol < b.Symbol
|
||||
case "price":
|
||||
return a.PriceUSD < b.PriceUSD
|
||||
case "marketcap":
|
||||
return a.MarketCapUSD < b.MarketCapUSD
|
||||
case "24hvolume":
|
||||
return a.USD24HVolume < b.USD24HVolume
|
||||
case "1hchange":
|
||||
return a.PercentChange1H < b.PercentChange1H
|
||||
case "24hchange":
|
||||
return a.PercentChange24H < b.PercentChange24H
|
||||
case "7dchange":
|
||||
return a.PercentChange7D < b.PercentChange7D
|
||||
case "totalsupply":
|
||||
return a.TotalSupply < b.TotalSupply
|
||||
case "availablesupply":
|
||||
return a.AvailableSupply < b.AvailableSupply
|
||||
case "lastupdated":
|
||||
return a.LastUpdated < b.LastUpdated
|
||||
default:
|
||||
return a.Rank < b.Rank
|
||||
}
|
||||
})
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
ct.tableview.Clear()
|
||||
ct.setTable()
|
||||
return nil
|
||||
})
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
ct.chartview.Clear()
|
||||
maxX, _ := g.Size()
|
||||
_, cy := ct.chartview.Cursor()
|
||||
coin := "ethereum"
|
||||
ct.chartPoints(maxX, coin)
|
||||
ct.setChart()
|
||||
fmt.Fprint(v, cy)
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (ct *Cointop) cursorDown(g *gocui.Gui, v *gocui.View) error {
|
||||
if ct.tableview == nil {
|
||||
return nil
|
||||
}
|
||||
_, y := ct.tableview.Origin()
|
||||
cx, cy := ct.tableview.Cursor()
|
||||
numRows := len(ct.coins) - 1
|
||||
//fmt.Fprint(v, cy)
|
||||
if (cy + y + 1) > numRows {
|
||||
return nil
|
||||
}
|
||||
if err := ct.tableview.SetCursor(cx, cy+1); err != nil {
|
||||
ox, oy := ct.tableview.Origin()
|
||||
if err := ct.tableview.SetOrigin(ox, oy+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) cursorUp(g *gocui.Gui, v *gocui.View) error {
|
||||
if ct.tableview == nil {
|
||||
return nil
|
||||
}
|
||||
ox, oy := ct.tableview.Origin()
|
||||
cx, cy := ct.tableview.Cursor()
|
||||
//fmt.Fprint(v, oy)
|
||||
if err := ct.tableview.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
||||
if err := ct.tableview.SetOrigin(ox, oy-1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) pageDown(g *gocui.Gui, v *gocui.View) error {
|
||||
if ct.tableview == nil {
|
||||
return nil
|
||||
}
|
||||
_, y := ct.tableview.Origin()
|
||||
cx, cy := ct.tableview.Cursor()
|
||||
numRows := len(ct.coins) - 1
|
||||
_, sy := ct.tableview.Size()
|
||||
rows := sy
|
||||
if (cy + y + rows) > numRows {
|
||||
// go to last row
|
||||
ct.tableview.SetCursor(cx, numRows)
|
||||
ox, _ := ct.tableview.Origin()
|
||||
ct.tableview.SetOrigin(ox, numRows)
|
||||
return nil
|
||||
}
|
||||
if err := ct.tableview.SetCursor(cx, cy+rows); err != nil {
|
||||
ox, oy := ct.tableview.Origin()
|
||||
if err := ct.tableview.SetOrigin(ox, oy+rows); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) pageUp(g *gocui.Gui, v *gocui.View) error {
|
||||
if ct.tableview == nil {
|
||||
return nil
|
||||
}
|
||||
ox, oy := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
_, sy := v.Size()
|
||||
rows := sy
|
||||
//fmt.Fprint(v, oy)
|
||||
if err := v.SetCursor(cx, cy-rows); err != nil && oy > 0 {
|
||||
if err := v.SetOrigin(ox, oy-rows); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchData() ([]*cmc.Coin, error) {
|
||||
limit := 100
|
||||
result := []*cmc.Coin{}
|
||||
coins, err := cmc.GetAllCoinData(int(limit))
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
for i := range coins {
|
||||
coin := coins[i]
|
||||
result = append(result, &coin)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
maxX, maxY := g.Size()
|
||||
|
||||
chartHeight := 10
|
||||
if v, err := g.SetView("chart", 0, 0, maxX, chartHeight); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
v.Frame = false
|
||||
ct.chartview = v
|
||||
ct.setChart()
|
||||
}
|
||||
|
||||
if v, err := g.SetView("header", 0, chartHeight, maxX, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
t := table.New().SetWidth(maxX)
|
||||
|
||||
headers := []string{
|
||||
pad.Right("[r]ank", 13, " "),
|
||||
pad.Right("[n]ame", 13, " "),
|
||||
pad.Right("[s]ymbol", 8, " "),
|
||||
pad.Left("[p]rice", 10, " "),
|
||||
pad.Left("[m]arket cap", 17, " "),
|
||||
pad.Left("24H [v]olume", 15, " "),
|
||||
pad.Left("[1]H%", 9, " "),
|
||||
pad.Left("[2]4H%", 9, " "),
|
||||
pad.Left("[7]D%", 9, " "),
|
||||
pad.Left("[t]otal supply", 20, " "),
|
||||
pad.Left("[a]vailable supply", 19, " "),
|
||||
pad.Left("[l]ast updated", 17, " "),
|
||||
}
|
||||
for _, h := range headers {
|
||||
t.AddCol(h)
|
||||
}
|
||||
|
||||
t.Format().Fprint(v)
|
||||
v.Highlight = true
|
||||
v.SelBgColor = gocui.ColorGreen
|
||||
v.SelFgColor = gocui.ColorBlack
|
||||
v.Frame = false
|
||||
}
|
||||
|
||||
if v, err := g.SetView("table", 0, chartHeight+1, maxX, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
v.Highlight = true
|
||||
v.SelBgColor = gocui.ColorCyan
|
||||
v.SelFgColor = gocui.ColorBlack
|
||||
v.Frame = false
|
||||
ct.tableview = v
|
||||
ct.setTable()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) setChart() error {
|
||||
maxX, _ := ct.g.Size()
|
||||
if len(ct.chartpoints) == 0 {
|
||||
ct.chartPoints(maxX, "bitcoin")
|
||||
}
|
||||
|
||||
for i := range ct.chartpoints {
|
||||
var s string
|
||||
for j := range ct.chartpoints[i] {
|
||||
p := ct.chartpoints[i][j]
|
||||
s = fmt.Sprintf("%s%c", s, p.Ch)
|
||||
}
|
||||
fmt.Fprintln(ct.chartview, s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) setTable() error {
|
||||
maxX, _ := ct.g.Size()
|
||||
ct.table = table.New().SetWidth(maxX)
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.AddCol("")
|
||||
ct.table.HideColumHeaders = true
|
||||
var err error
|
||||
if len(ct.coins) == 0 {
|
||||
ct.coins, err = fetchData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, coin := range ct.coins {
|
||||
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
|
||||
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
|
||||
colorprice := cyan
|
||||
color1h := white
|
||||
color24h := white
|
||||
color7d := white
|
||||
if coin.PercentChange1H > 0 {
|
||||
color1h = green
|
||||
}
|
||||
if coin.PercentChange1H < 0 {
|
||||
color1h = red
|
||||
}
|
||||
if coin.PercentChange24H > 0 {
|
||||
color24h = green
|
||||
}
|
||||
if coin.PercentChange24H < 0 {
|
||||
color24h = red
|
||||
}
|
||||
if coin.PercentChange7D > 0 {
|
||||
color7d = green
|
||||
}
|
||||
if coin.PercentChange7D < 0 {
|
||||
color7d = red
|
||||
}
|
||||
ct.table.AddRow(
|
||||
pad.Left(fmt.Sprint(coin.Rank), 4, " "),
|
||||
pad.Right(coin.Name, 22, " "),
|
||||
pad.Right(coin.Symbol, 6, " "),
|
||||
colorprice(pad.Left(humanize.Commaf(coin.PriceUSD), 12, " ")),
|
||||
pad.Left(humanize.Commaf(coin.MarketCapUSD), 17, " "),
|
||||
pad.Left(humanize.Commaf(coin.USD24HVolume), 15, " "),
|
||||
color1h(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange1H), 9, " ")),
|
||||
color24h(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange24H), 9, " ")),
|
||||
color7d(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange7D), 9, " ")),
|
||||
pad.Left(humanize.Commaf(coin.TotalSupply), 20, " "),
|
||||
pad.Left(humanize.Commaf(coin.AvailableSupply), 18, " "),
|
||||
pad.Left(fmt.Sprintf("%s", lastUpdated), 18, " "),
|
||||
// add %percent of cap
|
||||
)
|
||||
}
|
||||
|
||||
ct.table.Format().Fprint(ct.tableview)
|
||||
return nil
|
||||
}
|
@ -1,614 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bradfitz/slice"
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gizak/termui"
|
||||
"github.com/jroimartin/gocui"
|
||||
"github.com/miguelmota/cointop/table"
|
||||
cmc "github.com/miguelmota/go-coinmarketcap"
|
||||
"github.com/willf/pad"
|
||||
)
|
||||
|
||||
var white = color.New(color.FgWhite).SprintFunc()
|
||||
var cyan = color.New(color.FgCyan).SprintFunc()
|
||||
var red = color.New(color.FgRed).SprintFunc()
|
||||
var green = color.New(color.FgGreen).SprintFunc()
|
||||
|
||||
func fmtTime(v interface{}) string {
|
||||
t := v.(time.Time)
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func ltTime(a interface{}, b interface{}) bool {
|
||||
return a.(time.Time).Before(b.(time.Time))
|
||||
}
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
func toFixed(num float64, precision int) float64 {
|
||||
output := math.Pow(10, float64(precision))
|
||||
return float64(round(num*output)) / output
|
||||
}
|
||||
|
||||
func chartCells(maxX int, coin string) [][]termui.Cell {
|
||||
//_ = termui.Init()
|
||||
//defer termui.Close()
|
||||
|
||||
// quit on Ctrl-c
|
||||
/*
|
||||
termui.Handle("/sys/kbd/C-c", func(termui.Event) {
|
||||
termui.StopLoop()
|
||||
})
|
||||
*/
|
||||
chart := termui.NewLineChart()
|
||||
//chart.DataLabels = []string{""}
|
||||
chart.Height = 10
|
||||
chart.AxesColor = termui.ColorWhite
|
||||
chart.LineColor = termui.ColorCyan //| termui.AttrBold
|
||||
chart.Border = false
|
||||
|
||||
var (
|
||||
oneMinute int64 = 60
|
||||
oneHour = oneMinute * 60
|
||||
oneDay = oneHour * 24
|
||||
//oneWeek = oneDay * 7
|
||||
//oneMonth = oneDay * 30
|
||||
//oneYear = oneDay * 365
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
secs := now.Unix()
|
||||
start := secs - oneDay
|
||||
end := secs
|
||||
|
||||
_ = coin
|
||||
//graphData, err := cmc.GetCoinGraphData(coin, start, end)
|
||||
graphData, err := cmc.GetGlobalMarketGraphData(start, end)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var data []float64
|
||||
/*
|
||||
for i := range graphData.PriceUSD {
|
||||
data = append(data, graphData.PriceUSD[i][1])
|
||||
}
|
||||
*/
|
||||
for i := range graphData.MarketCapByAvailableSupply {
|
||||
data = append(data, graphData.MarketCapByAvailableSupply[i][1])
|
||||
}
|
||||
chart.Data = data
|
||||
//h := chart.InnerHeight()
|
||||
//w := chart.InnerWidth()
|
||||
|
||||
// add grid rows and columns
|
||||
termui.Body = termui.NewGrid()
|
||||
//termui.Body.Rows = termui.Body.Rows[:0]
|
||||
termui.Body.Width = maxX
|
||||
termui.Body.AddRows(
|
||||
termui.NewRow(
|
||||
termui.NewCol(12, 0, chart),
|
||||
),
|
||||
)
|
||||
|
||||
var points [][]termui.Cell
|
||||
|
||||
// calculate layout
|
||||
termui.Body.Align()
|
||||
// render to terminal
|
||||
//termui.Render(termui.Body)
|
||||
w := termui.Body.Width
|
||||
h := 10
|
||||
row := termui.Body.Rows[0]
|
||||
b := row.Buffer()
|
||||
for i := 0; i < h; i = i + 1 {
|
||||
var rowpoints []termui.Cell
|
||||
for j := 0; j < w; j = j + 1 {
|
||||
_ = b
|
||||
p := b.At(j, i)
|
||||
c := p
|
||||
rowpoints = append(rowpoints, c)
|
||||
}
|
||||
points = append(points, rowpoints)
|
||||
}
|
||||
//termui.Loop()
|
||||
|
||||
return points
|
||||
}
|
||||
|
||||
var points [][]termui.Cell
|
||||
|
||||
func main() {
|
||||
g, err := gocui.NewGui(gocui.Output256)
|
||||
if err != nil {
|
||||
log.Fatalf("new gocui: %v", err)
|
||||
}
|
||||
defer g.Close()
|
||||
|
||||
g.Cursor = true
|
||||
g.Mouse = true
|
||||
g.Highlight = true
|
||||
g.SetManagerFunc(layout)
|
||||
|
||||
if err := keybindings(g); err != nil {
|
||||
log.Fatalf("keybindings: %v", err)
|
||||
}
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Fatalf("main loop: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func quit(g *gocui.Gui, v *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
func keybindings(g *gocui.Gui) error {
|
||||
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'j', gocui.ModNone, cursorDown); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'k', gocui.ModNone, cursorUp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone, pageDown); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlU, gocui.ModNone, pageUp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'r', gocui.ModNone, sort("rank", false)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'n', gocui.ModNone, sort("name", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 's', gocui.ModNone, sort("symbol", false)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'p', gocui.ModNone, sort("price", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'm', gocui.ModNone, sort("marketcap", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'v', gocui.ModNone, sort("24hvolume", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '1', gocui.ModNone, sort("1hchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '2', gocui.ModNone, sort("24hchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", '7', gocui.ModNone, sort("7dchange", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 't', gocui.ModNone, sort("totalsupply", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'a', gocui.ModNone, sort("availablesupply", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'l', gocui.ModNone, sort("lastupdated", true)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.SetKeybinding("", gocui.KeyEsc, gocui.ModNone, quit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type strategy func(g *gocui.Gui, v *gocui.View) error
|
||||
|
||||
var sortDesc bool
|
||||
var currentsort string
|
||||
|
||||
func sort(sortBy string, desc bool) strategy {
|
||||
return func(g *gocui.Gui, v *gocui.View) error {
|
||||
|
||||
if currentsort == sortBy {
|
||||
sortDesc = !sortDesc
|
||||
} else {
|
||||
currentsort = sortBy
|
||||
sortDesc = desc
|
||||
}
|
||||
|
||||
slice.Sort(coins[:], func(i, j int) bool {
|
||||
if sortDesc == true {
|
||||
i, j = j, i
|
||||
}
|
||||
a := coins[i]
|
||||
b := coins[j]
|
||||
switch sortBy {
|
||||
case "rank":
|
||||
return a.Rank < b.Rank
|
||||
case "name":
|
||||
return a.Name < b.Name
|
||||
case "symbol":
|
||||
return a.Symbol < b.Symbol
|
||||
case "price":
|
||||
return a.PriceUSD < b.PriceUSD
|
||||
case "marketcap":
|
||||
return a.MarketCapUSD < b.MarketCapUSD
|
||||
case "24hvolume":
|
||||
return a.USD24HVolume < b.USD24HVolume
|
||||
case "1hchange":
|
||||
return a.PercentChange1H < b.PercentChange1H
|
||||
case "24hchange":
|
||||
return a.PercentChange24H < b.PercentChange24H
|
||||
case "7dchange":
|
||||
return a.PercentChange7D < b.PercentChange7D
|
||||
case "totalsupply":
|
||||
return a.TotalSupply < b.TotalSupply
|
||||
case "availablesupply":
|
||||
return a.AvailableSupply < b.AvailableSupply
|
||||
case "lastupdated":
|
||||
return a.LastUpdated < b.LastUpdated
|
||||
default:
|
||||
return a.Rank < b.Rank
|
||||
}
|
||||
})
|
||||
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
v, _ = g.View("table")
|
||||
v.Clear()
|
||||
setTable(g)
|
||||
return nil
|
||||
})
|
||||
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
v, _ = g.View("chart")
|
||||
v.Clear()
|
||||
|
||||
maxX, _ := g.Size()
|
||||
_, cy := v.Cursor()
|
||||
coin := "ethereum"
|
||||
points = chartCells(maxX, coin)
|
||||
setChart(g, v)
|
||||
fmt.Fprint(v, cy)
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var coins []*cmc.Coin
|
||||
|
||||
func cursorDown(g *gocui.Gui, v *gocui.View) error {
|
||||
v, _ = g.View("table")
|
||||
if v != nil {
|
||||
_, y := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
numRows := len(coins) - 1
|
||||
//fmt.Fprint(v, cy)
|
||||
if (cy + y + 1) > numRows {
|
||||
return nil
|
||||
}
|
||||
if err := v.SetCursor(cx, cy+1); err != nil {
|
||||
ox, oy := v.Origin()
|
||||
if err := v.SetOrigin(ox, oy+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cursorUp(g *gocui.Gui, v *gocui.View) error {
|
||||
v, _ = g.View("table")
|
||||
if v != nil {
|
||||
ox, oy := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
//fmt.Fprint(v, oy)
|
||||
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
||||
if err := v.SetOrigin(ox, oy-1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pageDown(g *gocui.Gui, v *gocui.View) error {
|
||||
v, _ = g.View("table")
|
||||
if v != nil {
|
||||
_, y := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
numRows := len(coins) - 1
|
||||
_, sy := v.Size()
|
||||
rows := sy
|
||||
if (cy + y + rows) > numRows {
|
||||
// go to last row
|
||||
v.SetCursor(cx, numRows)
|
||||
ox, _ := v.Origin()
|
||||
v.SetOrigin(ox, numRows)
|
||||
return nil
|
||||
}
|
||||
if err := v.SetCursor(cx, cy+rows); err != nil {
|
||||
ox, oy := v.Origin()
|
||||
if err := v.SetOrigin(ox, oy+rows); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pageUp(g *gocui.Gui, v *gocui.View) error {
|
||||
v, _ = g.View("table")
|
||||
if v != nil {
|
||||
ox, oy := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
_, sy := v.Size()
|
||||
rows := sy
|
||||
//fmt.Fprint(v, oy)
|
||||
if err := v.SetCursor(cx, cy-rows); err != nil && oy > 0 {
|
||||
if err := v.SetOrigin(ox, oy-rows); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchData() ([]*cmc.Coin, error) {
|
||||
limit := 100
|
||||
result := []*cmc.Coin{}
|
||||
coins, err := cmc.GetAllCoinData(int(limit))
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
for i := range coins {
|
||||
coin := coins[i]
|
||||
result = append(result, &coin)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func layout(g *gocui.Gui) error {
|
||||
maxX, maxY := g.Size()
|
||||
|
||||
chartHeight := 10
|
||||
if v, err := g.SetView("chart", 0, 0, maxX, chartHeight); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
//v.Frame = false
|
||||
setChart(g, v)
|
||||
}
|
||||
|
||||
if v, err := g.SetView("header", 0, chartHeight, maxX, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
t := table.New().SetWidth(maxX)
|
||||
|
||||
headers := []string{
|
||||
pad.Right("[r]ank", 13, " "),
|
||||
pad.Right("[n]ame", 13, " "),
|
||||
pad.Right("[s]ymbol", 8, " "),
|
||||
pad.Left("[p]rice", 10, " "),
|
||||
pad.Left("[m]arket cap", 17, " "),
|
||||
pad.Left("24H [v]olume", 15, " "),
|
||||
pad.Left("[1]H%", 9, " "),
|
||||
pad.Left("[2]4H%", 9, " "),
|
||||
pad.Left("[7]D%", 9, " "),
|
||||
pad.Left("[t]otal supply", 20, " "),
|
||||
pad.Left("[a]vailable supply", 19, " "),
|
||||
pad.Left("[l]ast updated", 17, " "),
|
||||
}
|
||||
for _, h := range headers {
|
||||
t.AddCol(h)
|
||||
}
|
||||
|
||||
t.Format().Fprint(v)
|
||||
v.Highlight = true
|
||||
v.SelBgColor = gocui.ColorGreen
|
||||
v.SelFgColor = gocui.ColorBlack
|
||||
v.Frame = false
|
||||
}
|
||||
|
||||
if v, err := g.SetView("table", 0, chartHeight+1, maxX, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
_ = v
|
||||
_ = maxY
|
||||
setTable(g)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setChart(g *gocui.Gui, v *gocui.View) error {
|
||||
v.Frame = false
|
||||
/*
|
||||
tm.Clear()
|
||||
tm.MoveCursor(0, 0)
|
||||
chart := tm.NewLineChart(maxX/2, 9)
|
||||
//chart.Flags = tm.DRAW_INDEPENDENT
|
||||
chart.Flags = tm.DRAW_RELATIVE
|
||||
data := new(tm.DataTable)
|
||||
data.AddColumn("")
|
||||
data.AddColumn("")
|
||||
|
||||
var (
|
||||
oneMinute int64 = 60
|
||||
oneHour = oneMinute * 60
|
||||
oneDay = oneHour * 24
|
||||
//oneWeek = oneDay * 7
|
||||
oneMonth = oneDay * 30
|
||||
//oneYear = oneDay * 365
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
secs := now.Unix()
|
||||
//start := secs - oneDay
|
||||
start := secs - oneMonth
|
||||
end := secs
|
||||
|
||||
coin := "ethereum"
|
||||
graphData, err := cmc.GetCoinGraphData(coin, start, end)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var dt []float64
|
||||
for i := range graphData.PriceUSD {
|
||||
dt = append(dt, graphData.PriceUSD[i][1])
|
||||
}
|
||||
|
||||
for i, d := range dt {
|
||||
_ = d
|
||||
_ = i
|
||||
data.AddRow(float64(i), d)
|
||||
}
|
||||
_ = dt
|
||||
|
||||
out := chart.Draw(data, []int{0, 6})
|
||||
fmt.Fprint(v, out)
|
||||
//tm.Println(out)
|
||||
var buf bytes.Buffer
|
||||
tm.Output = bufio.NewWriter(&buf)
|
||||
//tm.Output = bufio.NewWriter(v)
|
||||
_ = buf
|
||||
tm.Flush()
|
||||
_ = v
|
||||
//buf.WriteTo(v)
|
||||
//buf.WriteTo(os.Stdout)
|
||||
*/
|
||||
|
||||
maxX, _ := g.Size()
|
||||
if len(points) == 0 {
|
||||
points = chartCells(maxX, "bitcoin")
|
||||
}
|
||||
|
||||
for i := range points {
|
||||
var s string
|
||||
for j := range points[i] {
|
||||
p := points[i][j]
|
||||
s = fmt.Sprintf("%s%c", s, p.Ch)
|
||||
}
|
||||
fmt.Fprintln(v, s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setTable(g *gocui.Gui) error {
|
||||
maxX, _ := g.Size()
|
||||
v, _ := g.View("table")
|
||||
t := table.New().SetWidth(maxX)
|
||||
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.AddCol("")
|
||||
t.HideColumHeaders = true
|
||||
|
||||
var err error
|
||||
if len(coins) == 0 {
|
||||
coins, err = fetchData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, coin := range coins {
|
||||
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
|
||||
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
|
||||
colorprice := cyan
|
||||
color1h := white
|
||||
color24h := white
|
||||
color7d := white
|
||||
if coin.PercentChange1H > 0 {
|
||||
color1h = green
|
||||
}
|
||||
if coin.PercentChange1H < 0 {
|
||||
color1h = red
|
||||
}
|
||||
if coin.PercentChange24H > 0 {
|
||||
color24h = green
|
||||
}
|
||||
if coin.PercentChange24H < 0 {
|
||||
color24h = red
|
||||
}
|
||||
if coin.PercentChange7D > 0 {
|
||||
color7d = green
|
||||
}
|
||||
if coin.PercentChange7D < 0 {
|
||||
color7d = red
|
||||
}
|
||||
_ = color7d
|
||||
t.AddRow(
|
||||
pad.Left(fmt.Sprint(coin.Rank), 4, " "),
|
||||
pad.Right(coin.Name, 22, " "),
|
||||
pad.Right(coin.Symbol, 6, " "),
|
||||
colorprice(pad.Left(humanize.Commaf(coin.PriceUSD), 12, " ")),
|
||||
pad.Left(humanize.Commaf(coin.MarketCapUSD), 17, " "),
|
||||
pad.Left(humanize.Commaf(coin.USD24HVolume), 15, " "),
|
||||
color1h(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange1H), 9, " ")),
|
||||
color24h(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange24H), 9, " ")),
|
||||
color7d(pad.Left(fmt.Sprintf("%.2f%%", coin.PercentChange7D), 9, " ")),
|
||||
pad.Left(humanize.Commaf(coin.TotalSupply), 20, " "),
|
||||
pad.Left(humanize.Commaf(coin.AvailableSupply), 18, " "),
|
||||
pad.Left(fmt.Sprintf("%s", lastUpdated), 18, " "),
|
||||
// add %percent of cap
|
||||
)
|
||||
}
|
||||
|
||||
//t.SortAsc("Name").SortDesc("Age").Sort().Format().Fprint(v)
|
||||
t.Format().Fprint(v)
|
||||
//v.Editable = true
|
||||
//v.SetCursor(0, 2)
|
||||
v.Highlight = true
|
||||
v.SelBgColor = gocui.ColorCyan
|
||||
v.SelFgColor = gocui.ColorBlack
|
||||
v.Frame = false
|
||||
//v.Autoscroll = true
|
||||
|
||||
//buffer := ln.Buffer()
|
||||
//buffer.
|
||||
|
||||
// Sort by time
|
||||
// t.SortAscFn("Created", ltTime).Sort().Format().Fprint(v)
|
||||
return nil
|
||||
}
|
@ -0,0 +1 @@
|
||||
.idea/
|
@ -0,0 +1,9 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.6.x
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
|
||||
script:
|
||||
- go test
|
@ -0,0 +1,15 @@
|
||||
## v1.1
|
||||
|
||||
### Added
|
||||
|
||||
- Cookies can be added to the HTTP request, either via the `Cookies` map or the `Cookie()` function
|
||||
- Function `GetWithClient()` provides the ability to send the request with a custom HTTP client
|
||||
- Function `FindStrict()` finds the first instance of the mentioned tag with the exact matching values of the provided attribute (previously `Find()`)
|
||||
- Function `FindAllStrict()` finds all the instances of the mentioned tag with the exact matching values of the attributes (previously `FindAll()`)
|
||||
|
||||
## Changed
|
||||
|
||||
- Function `Find()` now finds the first instance of the mentioned tag with any matching values of the provided attribute.
|
||||
- Function `FindAll()` now finds all the instances of the mentioned tag with any matching values of the provided attribute.
|
||||
|
||||
---
|
@ -0,0 +1,70 @@
|
||||
# soup
|
||||
[![Build Status](https://travis-ci.org/anaskhan96/soup.svg?branch=master)](https://travis-ci.org/anaskhan96/soup)
|
||||
[![GoDoc](https://godoc.org/github.com/anaskhan96/soup?status.svg)](https://godoc.org/github.com/anaskhan96/soup)
|
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/anaskhan96/soup)](https://goreportcard.com/report/github.com/anaskhan96/soup)
|
||||
|
||||
**Web Scraper in Go, similar to BeautifulSoup**
|
||||
|
||||
*soup* is a small web scraper package for Go, with its interface highly similar to that of BeautifulSoup.
|
||||
|
||||
Exported variables and functions implemented till now :
|
||||
```go
|
||||
var Headers map[string]string // Set headers as a map of key-value pairs, an alternative to calling Header() individually
|
||||
var Cookies map[string]string // Set cookies as a map of key-value pairs, an alternative to calling Cookie() individually
|
||||
func Get(string) (string,error) // Takes the url as an argument, returns HTML string
|
||||
func GetWithClient(string, *http.Client) // Takes the url and a custom HTTP client as arguments, returns HTML string
|
||||
func Header(string, string) // Takes key,value pair to set as headers for the HTTP request made in Get()
|
||||
func Cookie(string, string) // Takes key, value pair to set as cookies to be sent with the HTTP request in Get()
|
||||
func HTMLParse(string) Root // Takes the HTML string as an argument, returns a pointer to the DOM constructed
|
||||
func Find([]string) Root // Element tag,(attribute key-value pair) as argument, pointer to first occurence returned
|
||||
func FindAll([]string) []Root // Same as Find(), but pointers to all occurrences returned
|
||||
func FindStrict([]string) Root // Element tag,(attribute key-value pair) as argument, pointer to first occurence returned with exact matching values
|
||||
func FindAllStrict([]string) []Root // Same as FindStrict(), but pointers to all occurrences returned
|
||||
func FindNextSibling() Root // Pointer to the next sibling of the Element in the DOM returned
|
||||
func FindNextElementSibling() Root // Pointer to the next element sibling of the Element in the DOM returned
|
||||
func FindPrevSibling() Root // Pointer to the previous sibling of the Element in the DOM returned
|
||||
func FindPrevElementSibling() Root // Pointer to the previous element sibling of the Element in the DOM returned
|
||||
func Attrs() map[string]string // Map returned with all the attributes of the Element as lookup to their respective values
|
||||
func Text() string // Full text inside a non-nested tag returned
|
||||
func SetDebug(bool) // Sets the debug mode to true or false; false by default
|
||||
```
|
||||
|
||||
`Root` is a struct, containing three fields :
|
||||
* `Pointer` containing the pointer to the current html node
|
||||
* `NodeValue` containing the current html node's value, i.e. the tag name for an ElementNode, or the text in case of a TextNode
|
||||
* `Error` containing an error if one occurrs, else `nil` is returned.
|
||||
|
||||
## Installation
|
||||
Install the package using the command
|
||||
```bash
|
||||
go get github.com/anaskhan96/soup
|
||||
```
|
||||
|
||||
## Example
|
||||
An example code is given below to scrape the "Comics I Enjoy" part (text and its links) from [xkcd](https://xkcd.com).
|
||||
|
||||
[More Examples](https://github.com/anaskhan96/soup/tree/master/examples)
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/anaskhan96/soup"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resp, err := soup.Get("https://xkcd.com")
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
doc := soup.HTMLParse(resp)
|
||||
links := doc.Find("div", "id", "comicLinks").FindAll("a")
|
||||
for _, link := range links {
|
||||
fmt.Println(link.Text(), "| Link :", link.Attrs()["href"])
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Contributions
|
||||
This package was developed in my free time. However, contributions from everybody in the community are welcome, to make it a better web scraper. If you think there should be a particular feature or function included in the package, feel free to open up a new issue or pull request.
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Anas Khan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,373 @@
|
||||
/* soup package implements a simple web scraper for Go,
|
||||
keeping it as similar as possible to BeautifulSoup
|
||||
*/
|
||||
|
||||
package soup
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// Root is a structure containing a pointer to an html node, the node value, and an error variable to return an error if occurred
|
||||
type Root struct {
|
||||
Pointer *html.Node
|
||||
NodeValue string
|
||||
Error error
|
||||
}
|
||||
|
||||
var debug = false
|
||||
|
||||
// Headers contains all HTTP headers to send
|
||||
var Headers = make(map[string]string)
|
||||
|
||||
// Cookies contains all HTTP cookies to send
|
||||
var Cookies = make(map[string]string)
|
||||
|
||||
// SetDebug sets the debug status
|
||||
// Setting this to true causes the panics to be thrown and logged onto the console.
|
||||
// Setting this to false causes the errors to be saved in the Error field in the returned struct.
|
||||
func SetDebug(d bool) {
|
||||
debug = d
|
||||
}
|
||||
|
||||
// Header sets a new HTTP header
|
||||
func Header(n string, v string) {
|
||||
Headers[n] = v
|
||||
}
|
||||
|
||||
func Cookie(n string, v string) {
|
||||
Cookies[n] = v
|
||||
}
|
||||
|
||||
// GetWithClient returns the HTML returned by the url using a provided HTTP client
|
||||
func GetWithClient(url string, client *http.Client) (string, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
if debug {
|
||||
panic("Couldn't perform GET request to " + url)
|
||||
}
|
||||
return "", errors.New("couldn't perform GET request to " + url)
|
||||
}
|
||||
// Set headers
|
||||
for hName, hValue := range Headers {
|
||||
req.Header.Set(hName, hValue)
|
||||
}
|
||||
// Set cookies
|
||||
for cName, cValue := range Cookies {
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: cName,
|
||||
Value: cValue,
|
||||
})
|
||||
}
|
||||
// Perform request
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
if debug {
|
||||
panic("Couldn't perform GET request to " + url)
|
||||
}
|
||||
return "", errors.New("couldn't perform GET request to " + url)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
if debug {
|
||||
panic("Unable to read the response body")
|
||||
}
|
||||
return "", errors.New("unable to read the response body")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
// Get returns the HTML returned by the url in string using the default HTTP client
|
||||
func Get(url string) (string, error) {
|
||||
// Init a new HTTP client
|
||||
client := &http.Client{}
|
||||
return GetWithClient(url, client)
|
||||
}
|
||||
|
||||
// HTMLParse parses the HTML returning a start pointer to the DOM
|
||||
func HTMLParse(s string) Root {
|
||||
r, err := html.Parse(strings.NewReader(s))
|
||||
if err != nil {
|
||||
if debug {
|
||||
panic("Unable to parse the HTML")
|
||||
}
|
||||
return Root{nil, "", errors.New("unable to parse the HTML")}
|
||||
}
|
||||
for r.Type != html.ElementNode {
|
||||
switch r.Type {
|
||||
case html.DocumentNode:
|
||||
r = r.FirstChild
|
||||
case html.DoctypeNode:
|
||||
r = r.NextSibling
|
||||
case html.CommentNode:
|
||||
r = r.NextSibling
|
||||
}
|
||||
}
|
||||
return Root{r, r.Data, nil}
|
||||
}
|
||||
|
||||
// Find finds the first occurrence of the given tag name,
|
||||
// with or without attribute key and value specified,
|
||||
// and returns a struct with a pointer to it
|
||||
func (r Root) Find(args ...string) Root {
|
||||
temp, ok := findOnce(r.Pointer, args, false, false)
|
||||
if ok == false {
|
||||
if debug {
|
||||
panic("Element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")
|
||||
}
|
||||
return Root{nil, "", errors.New("element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")}
|
||||
}
|
||||
return Root{temp, temp.Data, nil}
|
||||
}
|
||||
|
||||
// FindAll finds all occurrences of the given tag name,
|
||||
// with or without key and value specified,
|
||||
// and returns an array of structs, each having
|
||||
// the respective pointers
|
||||
func (r Root) FindAll(args ...string) []Root {
|
||||
temp := findAllofem(r.Pointer, args, false)
|
||||
if len(temp) == 0 {
|
||||
if debug {
|
||||
panic("Element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")
|
||||
}
|
||||
return []Root{}
|
||||
}
|
||||
pointers := make([]Root, 0, len(temp))
|
||||
for i := 0; i < len(temp); i++ {
|
||||
pointers = append(pointers, Root{temp[i], temp[i].Data, nil})
|
||||
}
|
||||
return pointers
|
||||
}
|
||||
|
||||
// FindStrict finds the first occurrence of the given tag name
|
||||
// only if all the values of the provided attribute are an exact match
|
||||
func (r Root) FindStrict(args ...string) Root {
|
||||
temp, ok := findOnce(r.Pointer, args, false, true)
|
||||
if ok == false {
|
||||
if debug {
|
||||
panic("Element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")
|
||||
}
|
||||
return Root{nil, "", errors.New("element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")}
|
||||
}
|
||||
return Root{temp, temp.Data, nil}
|
||||
}
|
||||
|
||||
// FindAllStrict finds all occurrences of the given tag name
|
||||
// only if all the values of the provided attribute are an exact match
|
||||
func (r Root) FindAllStrict(args ...string) []Root {
|
||||
temp := findAllofem(r.Pointer, args, true)
|
||||
if len(temp) == 0 {
|
||||
if debug {
|
||||
panic("Element `" + args[0] + "` with attributes `" + strings.Join(args[1:], " ") + "` not found")
|
||||
}
|
||||
return []Root{}
|
||||
}
|
||||
pointers := make([]Root, 0, len(temp))
|
||||
for i := 0; i < len(temp); i++ {
|
||||
pointers = append(pointers, Root{temp[i], temp[i].Data, nil})
|
||||
}
|
||||
return pointers
|
||||
}
|
||||
|
||||
// FindNextSibling finds the next sibling of the pointer in the DOM
|
||||
// returning a struct with a pointer to it
|
||||
func (r Root) FindNextSibling() Root {
|
||||
nextSibling := r.Pointer.NextSibling
|
||||
if nextSibling == nil {
|
||||
if debug {
|
||||
panic("No next sibling found")
|
||||
}
|
||||
return Root{nil, "", errors.New("no next sibling found")}
|
||||
}
|
||||
return Root{nextSibling, nextSibling.Data, nil}
|
||||
}
|
||||
|
||||
// FindPrevSibling finds the previous sibling of the pointer in the DOM
|
||||
// returning a struct with a pointer to it
|
||||
func (r Root) FindPrevSibling() Root {
|
||||
prevSibling := r.Pointer.PrevSibling
|
||||
if prevSibling == nil {
|
||||
if debug {
|
||||
panic("No previous sibling found")
|
||||
}
|
||||
return Root{nil, "", errors.New("no previous sibling found")}
|
||||
}
|
||||
return Root{prevSibling, prevSibling.Data, nil}
|
||||
}
|
||||
|
||||
// FindNextElementSibling finds the next element sibling of the pointer in the DOM
|
||||
// returning a struct with a pointer to it
|
||||
func (r Root) FindNextElementSibling() Root {
|
||||
nextSibling := r.Pointer.NextSibling
|
||||
if nextSibling == nil {
|
||||
if debug {
|
||||
panic("No next element sibling found")
|
||||
}
|
||||
return Root{nil, "", errors.New("no next element sibling found")}
|
||||
}
|
||||
if nextSibling.Type == html.ElementNode {
|
||||
return Root{nextSibling, nextSibling.Data, nil}
|
||||
}
|
||||
p := Root{nextSibling, nextSibling.Data, nil}
|
||||
return p.FindNextElementSibling()
|
||||
}
|
||||
|
||||
// FindPrevElementSibling finds the previous element sibling of the pointer in the DOM
|
||||
// returning a struct with a pointer to it
|
||||
func (r Root) FindPrevElementSibling() Root {
|
||||
prevSibling := r.Pointer.PrevSibling
|
||||
if prevSibling == nil {
|
||||
if debug {
|
||||
panic("No previous element sibling found")
|
||||
}
|
||||
return Root{nil, "", errors.New("no previous element sibling found")}
|
||||
}
|
||||
if prevSibling.Type == html.ElementNode {
|
||||
return Root{prevSibling, prevSibling.Data, nil}
|
||||
}
|
||||
p := Root{prevSibling, prevSibling.Data, nil}
|
||||
return p.FindPrevElementSibling()
|
||||
}
|
||||
|
||||
// Attrs returns a map containing all attributes
|
||||
func (r Root) Attrs() map[string]string {
|
||||
if r.Pointer.Type != html.ElementNode {
|
||||
if debug {
|
||||
panic("Not an ElementNode")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if len(r.Pointer.Attr) == 0 {
|
||||
return nil
|
||||
}
|
||||
return getKeyValue(r.Pointer.Attr)
|
||||
}
|
||||
|
||||
// Text returns the string inside a non-nested element
|
||||
func (r Root) Text() string {
|
||||
k := r.Pointer.FirstChild
|
||||
checkNode:
|
||||
if k.Type != html.TextNode {
|
||||
k = k.NextSibling
|
||||
if k == nil {
|
||||
if debug {
|
||||
panic("No text node found")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
goto checkNode
|
||||
}
|
||||
if k != nil {
|
||||
r, _ := regexp.Compile(`^\s+$`)
|
||||
if ok := r.MatchString(k.Data); ok {
|
||||
k = k.NextSibling
|
||||
if k == nil {
|
||||
if debug {
|
||||
panic("No text node found")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
goto checkNode
|
||||
}
|
||||
return k.Data
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Using depth first search to find the first occurrence and return
|
||||
func findOnce(n *html.Node, args []string, uni bool, strict bool) (*html.Node, bool) {
|
||||
if uni == true {
|
||||
if n.Type == html.ElementNode && n.Data == args[0] {
|
||||
if len(args) > 1 && len(args) < 4 {
|
||||
for i := 0; i < len(n.Attr); i++ {
|
||||
attr := n.Attr[i]
|
||||
searchAttrName := args[1]
|
||||
searchAttrVal := args[2]
|
||||
if (strict && attributeAndValueEquals(attr, searchAttrName, searchAttrVal)) ||
|
||||
(!strict && attributeContainsValue(attr, searchAttrName, searchAttrVal)) {
|
||||
return n, true
|
||||
}
|
||||
}
|
||||
} else if len(args) == 1 {
|
||||
return n, true
|
||||
}
|
||||
}
|
||||
}
|
||||
uni = true
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
p, q := findOnce(c, args, true, strict)
|
||||
if q != false {
|
||||
return p, q
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Using depth first search to find all occurrences and return
|
||||
func findAllofem(n *html.Node, args []string, strict bool) []*html.Node {
|
||||
var nodeLinks = make([]*html.Node, 0, 10)
|
||||
var f func(*html.Node, []string, bool)
|
||||
f = func(n *html.Node, args []string, uni bool) {
|
||||
if uni == true {
|
||||
if n.Data == args[0] {
|
||||
if len(args) > 1 && len(args) < 4 {
|
||||
for i := 0; i < len(n.Attr); i++ {
|
||||
attr := n.Attr[i]
|
||||
searchAttrName := args[1]
|
||||
searchAttrVal := args[2]
|
||||
if (strict && attributeAndValueEquals(attr, searchAttrName, searchAttrVal)) ||
|
||||
(!strict && attributeContainsValue(attr, searchAttrName, searchAttrVal)) {
|
||||
nodeLinks = append(nodeLinks, n)
|
||||
}
|
||||
}
|
||||
} else if len(args) == 1 {
|
||||
nodeLinks = append(nodeLinks, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
uni = true
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
f(c, args, true)
|
||||
}
|
||||
}
|
||||
f(n, args, false)
|
||||
return nodeLinks
|
||||
}
|
||||
|
||||
// attributeAndValueEquals reports when the html.Attribute attr has the same attribute name and value as from
|
||||
// provided arguments
|
||||
func attributeAndValueEquals(attr html.Attribute, attribute, value string) bool {
|
||||
return attr.Key == attribute && attr.Val == value
|
||||
}
|
||||
|
||||
// attributeContainsValue reports when the html.Attribute attr has the same attribute name as from provided
|
||||
// attribute argument and compares if it has the same value in its values parameter
|
||||
func attributeContainsValue(attr html.Attribute, attribute, value string) bool {
|
||||
if attr.Key == attribute {
|
||||
for _, attrVal := range strings.Fields(attr.Val) {
|
||||
if attrVal == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns a key pair value (like a dictionary) for each attribute
|
||||
func getKeyValue(attributes []html.Attribute) map[string]string {
|
||||
var keyvalues = make(map[string]string)
|
||||
for i := 0; i < len(attributes); i++ {
|
||||
_, exists := keyvalues[attributes[i].Key]
|
||||
if exists == false {
|
||||
keyvalues[attributes[i].Key] = attributes[i].Val
|
||||
}
|
||||
}
|
||||
return keyvalues
|
||||
}
|
@ -0,0 +1 @@
|
||||
*~
|
@ -0,0 +1,3 @@
|
||||
This package is licensed under the same terms as Go itself and
|
||||
has the same contribution / CLA requirements.
|
||||
|
@ -0,0 +1,27 @@
|
||||
Copyright (c) 2012 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1 @@
|
||||
See https://godoc.org/github.com/bradfitz/slice
|
@ -0,0 +1,44 @@
|
||||
// Copyright 2014 The Go 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 slice provides a slice sorting function.
|
||||
package slice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"go4.org/reflectutil"
|
||||
)
|
||||
|
||||
// Sort sorts the provided slice using the function less.
|
||||
// If slice is not a slice, Sort panics.
|
||||
func Sort(slice interface{}, less func(i, j int) bool) {
|
||||
sort.Sort(SortInterface(slice, less))
|
||||
}
|
||||
|
||||
// SortInterface returns a sort.Interface to sort the provided slice
|
||||
// using the function less.
|
||||
func SortInterface(slice interface{}, less func(i, j int) bool) sort.Interface {
|
||||
sv := reflect.ValueOf(slice)
|
||||
if sv.Kind() != reflect.Slice {
|
||||
panic(fmt.Sprintf("slice.Sort called with non-slice value of type %T", slice))
|
||||
}
|
||||
return &funcs{
|
||||
length: sv.Len(),
|
||||
less: less,
|
||||
swap: reflectutil.Swapper(slice),
|
||||
}
|
||||
}
|
||||
|
||||
type funcs struct {
|
||||
length int
|
||||
less func(i, j int) bool
|
||||
swap func(i, j int)
|
||||
}
|
||||
|
||||
func (f *funcs) Len() int { return f.length }
|
||||
func (f *funcs) Less(i, j int) bool { return f.less(i, j) }
|
||||
func (f *funcs) Swap(i, j int) { f.swap(i, j) }
|
@ -0,0 +1,21 @@
|
||||
sudo: false
|
||||
language: go
|
||||
go:
|
||||
- 1.3.x
|
||||
- 1.5.x
|
||||
- 1.6.x
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
- 1.9.x
|
||||
- master
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: master
|
||||
fast_finish: true
|
||||
install:
|
||||
- # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step).
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -u <(echo -n) <(gofmt -d -s .)
|
||||
- go tool vet .
|
||||
- go test -v -race ./...
|
@ -0,0 +1,21 @@
|
||||
Copyright (c) 2005-2008 Dustin Sallings <dustin@spy.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
<http://www.opensource.org/licenses/mit-license.php>
|
@ -0,0 +1,124 @@
|
||||
# Humane Units [![Build Status](https://travis-ci.org/dustin/go-humanize.svg?branch=master)](https://travis-ci.org/dustin/go-humanize) [![GoDoc](https://godoc.org/github.com/dustin/go-humanize?status.svg)](https://godoc.org/github.com/dustin/go-humanize)
|
||||
|
||||
Just a few functions for helping humanize times and sizes.
|
||||
|
||||
`go get` it as `github.com/dustin/go-humanize`, import it as
|
||||
`"github.com/dustin/go-humanize"`, use it as `humanize`.
|
||||
|
||||
See [godoc](https://godoc.org/github.com/dustin/go-humanize) for
|
||||
complete documentation.
|
||||
|
||||
## Sizes
|
||||
|
||||
This lets you take numbers like `82854982` and convert them to useful
|
||||
strings like, `83 MB` or `79 MiB` (whichever you prefer).
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
fmt.Printf("That file is %s.", humanize.Bytes(82854982)) // That file is 83 MB.
|
||||
```
|
||||
|
||||
## Times
|
||||
|
||||
This lets you take a `time.Time` and spit it out in relative terms.
|
||||
For example, `12 seconds ago` or `3 days from now`.
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
fmt.Printf("This was touched %s.", humanize.Time(someTimeInstance)) // This was touched 7 hours ago.
|
||||
```
|
||||
|
||||
Thanks to Kyle Lemons for the time implementation from an IRC
|
||||
conversation one day. It's pretty neat.
|
||||
|
||||
## Ordinals
|
||||
|
||||
From a [mailing list discussion][odisc] where a user wanted to be able
|
||||
to label ordinals.
|
||||
|
||||
0 -> 0th
|
||||
1 -> 1st
|
||||
2 -> 2nd
|
||||
3 -> 3rd
|
||||
4 -> 4th
|
||||
[...]
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
fmt.Printf("You're my %s best friend.", humanize.Ordinal(193)) // You are my 193rd best friend.
|
||||
```
|
||||
|
||||
## Commas
|
||||
|
||||
Want to shove commas into numbers? Be my guest.
|
||||
|
||||
0 -> 0
|
||||
100 -> 100
|
||||
1000 -> 1,000
|
||||
1000000000 -> 1,000,000,000
|
||||
-100000 -> -100,000
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
fmt.Printf("You owe $%s.\n", humanize.Comma(6582491)) // You owe $6,582,491.
|
||||
```
|
||||
|
||||
## Ftoa
|
||||
|
||||
Nicer float64 formatter that removes trailing zeros.
|
||||
|
||||
```go
|
||||
fmt.Printf("%f", 2.24) // 2.240000
|
||||
fmt.Printf("%s", humanize.Ftoa(2.24)) // 2.24
|
||||
fmt.Printf("%f", 2.0) // 2.000000
|
||||
fmt.Printf("%s", humanize.Ftoa(2.0)) // 2
|
||||
```
|
||||
|
||||
## SI notation
|
||||
|
||||
Format numbers with [SI notation][sinotation].
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
humanize.SI(0.00000000223, "M") // 2.23 nM
|
||||
```
|
||||
|
||||
## English-specific functions
|
||||
|
||||
The following functions are in the `humanize/english` subpackage.
|
||||
|
||||
### Plurals
|
||||
|
||||
Simple English pluralization
|
||||
|
||||
```go
|
||||
english.PluralWord(1, "object", "") // object
|
||||
english.PluralWord(42, "object", "") // objects
|
||||
english.PluralWord(2, "bus", "") // buses
|
||||
english.PluralWord(99, "locus", "loci") // loci
|
||||
|
||||
english.Plural(1, "object", "") // 1 object
|
||||
english.Plural(42, "object", "") // 42 objects
|
||||
english.Plural(2, "bus", "") // 2 buses
|
||||
english.Plural(99, "locus", "loci") // 99 loci
|
||||
```
|
||||
|
||||
### Word series
|
||||
|
||||
Format comma-separated words lists with conjuctions:
|
||||
|
||||
```go
|
||||
english.WordSeries([]string{"foo"}, "and") // foo
|
||||
english.WordSeries([]string{"foo", "bar"}, "and") // foo and bar
|
||||
english.WordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar and baz
|
||||
|
||||
english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar, and baz
|
||||
```
|
||||
|
||||
[odisc]: https://groups.google.com/d/topic/golang-nuts/l8NhI74jl-4/discussion
|
||||
[sinotation]: http://en.wikipedia.org/wiki/Metric_prefix
|
@ -0,0 +1,31 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// order of magnitude (to a max order)
|
||||
func oomm(n, b *big.Int, maxmag int) (float64, int) {
|
||||
mag := 0
|
||||
m := &big.Int{}
|
||||
for n.Cmp(b) >= 0 {
|
||||
n.DivMod(n, b, m)
|
||||
mag++
|
||||
if mag == maxmag && maxmag >= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
|
||||
}
|
||||
|
||||
// total order of magnitude
|
||||
// (same as above, but with no upper limit)
|
||||
func oom(n, b *big.Int) (float64, int) {
|
||||
mag := 0
|
||||
m := &big.Int{}
|
||||
for n.Cmp(b) >= 0 {
|
||||
n.DivMod(n, b, m)
|
||||
mag++
|
||||
}
|
||||
return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var (
|
||||
bigIECExp = big.NewInt(1024)
|
||||
|
||||
// BigByte is one byte in bit.Ints
|
||||
BigByte = big.NewInt(1)
|
||||
// BigKiByte is 1,024 bytes in bit.Ints
|
||||
BigKiByte = (&big.Int{}).Mul(BigByte, bigIECExp)
|
||||
// BigMiByte is 1,024 k bytes in bit.Ints
|
||||
BigMiByte = (&big.Int{}).Mul(BigKiByte, bigIECExp)
|
||||
// BigGiByte is 1,024 m bytes in bit.Ints
|
||||
BigGiByte = (&big.Int{}).Mul(BigMiByte, bigIECExp)
|
||||
// BigTiByte is 1,024 g bytes in bit.Ints
|
||||
BigTiByte = (&big.Int{}).Mul(BigGiByte, bigIECExp)
|
||||
// BigPiByte is 1,024 t bytes in bit.Ints
|
||||
BigPiByte = (&big.Int{}).Mul(BigTiByte, bigIECExp)
|
||||
// BigEiByte is 1,024 p bytes in bit.Ints
|
||||
BigEiByte = (&big.Int{}).Mul(BigPiByte, bigIECExp)
|
||||
// BigZiByte is 1,024 e bytes in bit.Ints
|
||||
BigZiByte = (&big.Int{}).Mul(BigEiByte, bigIECExp)
|
||||
// BigYiByte is 1,024 z bytes in bit.Ints
|
||||
BigYiByte = (&big.Int{}).Mul(BigZiByte, bigIECExp)
|
||||
)
|
||||
|
||||
var (
|
||||
bigSIExp = big.NewInt(1000)
|
||||
|
||||
// BigSIByte is one SI byte in big.Ints
|
||||
BigSIByte = big.NewInt(1)
|
||||
// BigKByte is 1,000 SI bytes in big.Ints
|
||||
BigKByte = (&big.Int{}).Mul(BigSIByte, bigSIExp)
|
||||
// BigMByte is 1,000 SI k bytes in big.Ints
|
||||
BigMByte = (&big.Int{}).Mul(BigKByte, bigSIExp)
|
||||
// BigGByte is 1,000 SI m bytes in big.Ints
|
||||
BigGByte = (&big.Int{}).Mul(BigMByte, bigSIExp)
|
||||
// BigTByte is 1,000 SI g bytes in big.Ints
|
||||
BigTByte = (&big.Int{}).Mul(BigGByte, bigSIExp)
|
||||
// BigPByte is 1,000 SI t bytes in big.Ints
|
||||
BigPByte = (&big.Int{}).Mul(BigTByte, bigSIExp)
|
||||
// BigEByte is 1,000 SI p bytes in big.Ints
|
||||
BigEByte = (&big.Int{}).Mul(BigPByte, bigSIExp)
|
||||
// BigZByte is 1,000 SI e bytes in big.Ints
|
||||
BigZByte = (&big.Int{}).Mul(BigEByte, bigSIExp)
|
||||
// BigYByte is 1,000 SI z bytes in big.Ints
|
||||
BigYByte = (&big.Int{}).Mul(BigZByte, bigSIExp)
|
||||
)
|
||||
|
||||
var bigBytesSizeTable = map[string]*big.Int{
|
||||
"b": BigByte,
|
||||
"kib": BigKiByte,
|
||||
"kb": BigKByte,
|
||||
"mib": BigMiByte,
|
||||
"mb": BigMByte,
|
||||
"gib": BigGiByte,
|
||||
"gb": BigGByte,
|
||||
"tib": BigTiByte,
|
||||
"tb": BigTByte,
|
||||
"pib": BigPiByte,
|
||||
"pb": BigPByte,
|
||||
"eib": BigEiByte,
|
||||
"eb": BigEByte,
|
||||
"zib": BigZiByte,
|
||||
"zb": BigZByte,
|
||||
"yib": BigYiByte,
|
||||
"yb": BigYByte,
|
||||
// Without suffix
|
||||
"": BigByte,
|
||||
"ki": BigKiByte,
|
||||
"k": BigKByte,
|
||||
"mi": BigMiByte,
|
||||
"m": BigMByte,
|
||||
"gi": BigGiByte,
|
||||
"g": BigGByte,
|
||||
"ti": BigTiByte,
|
||||
"t": BigTByte,
|
||||
"pi": BigPiByte,
|
||||
"p": BigPByte,
|
||||
"ei": BigEiByte,
|
||||
"e": BigEByte,
|
||||
"z": BigZByte,
|
||||
"zi": BigZiByte,
|
||||
"y": BigYByte,
|
||||
"yi": BigYiByte,
|
||||
}
|
||||
|
||||
var ten = big.NewInt(10)
|
||||
|
||||
func humanateBigBytes(s, base *big.Int, sizes []string) string {
|
||||
if s.Cmp(ten) < 0 {
|
||||
return fmt.Sprintf("%d B", s)
|
||||
}
|
||||
c := (&big.Int{}).Set(s)
|
||||
val, mag := oomm(c, base, len(sizes)-1)
|
||||
suffix := sizes[mag]
|
||||
f := "%.0f %s"
|
||||
if val < 10 {
|
||||
f = "%.1f %s"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(f, val, suffix)
|
||||
|
||||
}
|
||||
|
||||
// BigBytes produces a human readable representation of an SI size.
|
||||
//
|
||||
// See also: ParseBigBytes.
|
||||
//
|
||||
// BigBytes(82854982) -> 83 MB
|
||||
func BigBytes(s *big.Int) string {
|
||||
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||||
return humanateBigBytes(s, bigSIExp, sizes)
|
||||
}
|
||||
|
||||
// BigIBytes produces a human readable representation of an IEC size.
|
||||
//
|
||||
// See also: ParseBigBytes.
|
||||
//
|
||||
// BigIBytes(82854982) -> 79 MiB
|
||||
func BigIBytes(s *big.Int) string {
|
||||
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
||||
return humanateBigBytes(s, bigIECExp, sizes)
|
||||
}
|
||||
|
||||
// ParseBigBytes parses a string representation of bytes into the number
|
||||
// of bytes it represents.
|
||||
//
|
||||
// See also: BigBytes, BigIBytes.
|
||||
//
|
||||
// ParseBigBytes("42 MB") -> 42000000, nil
|
||||
// ParseBigBytes("42 mib") -> 44040192, nil
|
||||
func ParseBigBytes(s string) (*big.Int, error) {
|
||||
lastDigit := 0
|
||||
hasComma := false
|
||||
for _, r := range s {
|
||||
if !(unicode.IsDigit(r) || r == '.' || r == ',') {
|
||||
break
|
||||
}
|
||||
if r == ',' {
|
||||
hasComma = true
|
||||
}
|
||||
lastDigit++
|
||||
}
|
||||
|
||||
num := s[:lastDigit]
|
||||
if hasComma {
|
||||
num = strings.Replace(num, ",", "", -1)
|
||||
}
|
||||
|
||||
val := &big.Rat{}
|
||||
_, err := fmt.Sscanf(num, "%f", val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
|
||||
if m, ok := bigBytesSizeTable[extra]; ok {
|
||||
mv := (&big.Rat{}).SetInt(m)
|
||||
val.Mul(val, mv)
|
||||
rv := &big.Int{}
|
||||
rv.Div(val.Num(), val.Denom())
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unhandled size name: %v", extra)
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// IEC Sizes.
|
||||
// kibis of bits
|
||||
const (
|
||||
Byte = 1 << (iota * 10)
|
||||
KiByte
|
||||
MiByte
|
||||
GiByte
|
||||
TiByte
|
||||
PiByte
|
||||
EiByte
|
||||
)
|
||||
|
||||
// SI Sizes.
|
||||
const (
|
||||
IByte = 1
|
||||
KByte = IByte * 1000
|
||||
MByte = KByte * 1000
|
||||
GByte = MByte * 1000
|
||||
TByte = GByte * 1000
|
||||
PByte = TByte * 1000
|
||||
EByte = PByte * 1000
|
||||
)
|
||||
|
||||
var bytesSizeTable = map[string]uint64{
|
||||
"b": Byte,
|
||||
"kib": KiByte,
|
||||
"kb": KByte,
|
||||
"mib": MiByte,
|
||||
"mb": MByte,
|
||||
"gib": GiByte,
|
||||
"gb": GByte,
|
||||
"tib": TiByte,
|
||||
"tb": TByte,
|
||||
"pib": PiByte,
|
||||
"pb": PByte,
|
||||
"eib": EiByte,
|
||||
"eb": EByte,
|
||||
// Without suffix
|
||||
"": Byte,
|
||||
"ki": KiByte,
|
||||
"k": KByte,
|
||||
"mi": MiByte,
|
||||
"m": MByte,
|
||||
"gi": GiByte,
|
||||
"g": GByte,
|
||||
"ti": TiByte,
|
||||
"t": TByte,
|
||||
"pi": PiByte,
|
||||
"p": PByte,
|
||||
"ei": EiByte,
|
||||
"e": EByte,
|
||||
}
|
||||
|
||||
func logn(n, b float64) float64 {
|
||||
return math.Log(n) / math.Log(b)
|
||||
}
|
||||
|
||||
func humanateBytes(s uint64, base float64, sizes []string) string {
|
||||
if s < 10 {
|
||||
return fmt.Sprintf("%d B", s)
|
||||
}
|
||||
e := math.Floor(logn(float64(s), base))
|
||||
suffix := sizes[int(e)]
|
||||
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
|
||||
f := "%.0f %s"
|
||||
if val < 10 {
|
||||
f = "%.1f %s"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(f, val, suffix)
|
||||
}
|
||||
|
||||
// Bytes produces a human readable representation of an SI size.
|
||||
//
|
||||
// See also: ParseBytes.
|
||||
//
|
||||
// Bytes(82854982) -> 83 MB
|
||||
func Bytes(s uint64) string {
|
||||
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
|
||||
return humanateBytes(s, 1000, sizes)
|
||||
}
|
||||
|
||||
// IBytes produces a human readable representation of an IEC size.
|
||||
//
|
||||
// See also: ParseBytes.
|
||||
//
|
||||
// IBytes(82854982) -> 79 MiB
|
||||
func IBytes(s uint64) string {
|
||||
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
||||
return humanateBytes(s, 1024, sizes)
|
||||
}
|
||||
|
||||
// ParseBytes parses a string representation of bytes into the number
|
||||
// of bytes it represents.
|
||||
//
|
||||
// See Also: Bytes, IBytes.
|
||||
//
|
||||
// ParseBytes("42 MB") -> 42000000, nil
|
||||
// ParseBytes("42 mib") -> 44040192, nil
|
||||
func ParseBytes(s string) (uint64, error) {
|
||||
lastDigit := 0
|
||||
hasComma := false
|
||||
for _, r := range s {
|
||||
if !(unicode.IsDigit(r) || r == '.' || r == ',') {
|
||||
break
|
||||
}
|
||||
if r == ',' {
|
||||
hasComma = true
|
||||
}
|
||||
lastDigit++
|
||||
}
|
||||
|
||||
num := s[:lastDigit]
|
||||
if hasComma {
|
||||
num = strings.Replace(num, ",", "", -1)
|
||||
}
|
||||
|
||||
f, err := strconv.ParseFloat(num, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
|
||||
if m, ok := bytesSizeTable[extra]; ok {
|
||||
f *= float64(m)
|
||||
if f >= math.MaxUint64 {
|
||||
return 0, fmt.Errorf("too large: %v", s)
|
||||
}
|
||||
return uint64(f), nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("unhandled size name: %v", extra)
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Comma produces a string form of the given number in base 10 with
|
||||
// commas after every three orders of magnitude.
|
||||
//
|
||||
// e.g. Comma(834142) -> 834,142
|
||||
func Comma(v int64) string {
|
||||
sign := ""
|
||||
|
||||
// Min int64 can't be negated to a usable value, so it has to be special cased.
|
||||
if v == math.MinInt64 {
|
||||
return "-9,223,372,036,854,775,808"
|
||||
}
|
||||
|
||||
if v < 0 {
|
||||
sign = "-"
|
||||
v = 0 - v
|
||||
}
|
||||
|
||||
parts := []string{"", "", "", "", "", "", ""}
|
||||
j := len(parts) - 1
|
||||
|
||||
for v > 999 {
|
||||
parts[j] = strconv.FormatInt(v%1000, 10)
|
||||
switch len(parts[j]) {
|
||||
case 2:
|
||||
parts[j] = "0" + parts[j]
|
||||
case 1:
|
||||
parts[j] = "00" + parts[j]
|
||||
}
|
||||
v = v / 1000
|
||||
j--
|
||||
}
|
||||
parts[j] = strconv.Itoa(int(v))
|
||||
return sign + strings.Join(parts[j:], ",")
|
||||
}
|
||||
|
||||
// Commaf produces a string form of the given number in base 10 with
|
||||
// commas after every three orders of magnitude.
|
||||
//
|
||||
// e.g. Commaf(834142.32) -> 834,142.32
|
||||
func Commaf(v float64) string {
|
||||
buf := &bytes.Buffer{}
|
||||
if v < 0 {
|
||||
buf.Write([]byte{'-'})
|
||||
v = 0 - v
|
||||
}
|
||||
|
||||
comma := []byte{','}
|
||||
|
||||
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
|
||||
pos := 0
|
||||
if len(parts[0])%3 != 0 {
|
||||
pos += len(parts[0]) % 3
|
||||
buf.WriteString(parts[0][:pos])
|
||||
buf.Write(comma)
|
||||
}
|
||||
for ; pos < len(parts[0]); pos += 3 {
|
||||
buf.WriteString(parts[0][pos : pos+3])
|
||||
buf.Write(comma)
|
||||
}
|
||||
buf.Truncate(buf.Len() - 1)
|
||||
|
||||
if len(parts) > 1 {
|
||||
buf.Write([]byte{'.'})
|
||||
buf.WriteString(parts[1])
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// BigComma produces a string form of the given big.Int in base 10
|
||||
// with commas after every three orders of magnitude.
|
||||
func BigComma(b *big.Int) string {
|
||||
sign := ""
|
||||
if b.Sign() < 0 {
|
||||
sign = "-"
|
||||
b.Abs(b)
|
||||
}
|
||||
|
||||
athousand := big.NewInt(1000)
|
||||
c := (&big.Int{}).Set(b)
|
||||
_, m := oom(c, athousand)
|
||||
parts := make([]string, m+1)
|
||||
j := len(parts) - 1
|
||||
|
||||
mod := &big.Int{}
|
||||
for b.Cmp(athousand) >= 0 {
|
||||
b.DivMod(b, athousand, mod)
|
||||
parts[j] = strconv.FormatInt(mod.Int64(), 10)
|
||||
switch len(parts[j]) {
|
||||
case 2:
|
||||
parts[j] = "0" + parts[j]
|
||||
case 1:
|
||||
parts[j] = "00" + parts[j]
|
||||
}
|
||||
j--
|
||||
}
|
||||
parts[j] = strconv.Itoa(int(b.Int64()))
|
||||
return sign + strings.Join(parts[j:], ",")
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
// +build go1.6
|
||||
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// BigCommaf produces a string form of the given big.Float in base 10
|
||||
// with commas after every three orders of magnitude.
|
||||
func BigCommaf(v *big.Float) string {
|
||||
buf := &bytes.Buffer{}
|
||||
if v.Sign() < 0 {
|
||||
buf.Write([]byte{'-'})
|
||||
v.Abs(v)
|
||||
}
|
||||
|
||||
comma := []byte{','}
|
||||
|
||||
parts := strings.Split(v.Text('f', -1), ".")
|
||||
pos := 0
|
||||
if len(parts[0])%3 != 0 {
|
||||
pos += len(parts[0]) % 3
|
||||
buf.WriteString(parts[0][:pos])
|
||||
buf.Write(comma)
|
||||
}
|
||||
for ; pos < len(parts[0]); pos += 3 {
|
||||
buf.WriteString(parts[0][pos : pos+3])
|
||||
buf.Write(comma)
|
||||
}
|
||||
buf.Truncate(buf.Len() - 1)
|
||||
|
||||
if len(parts) > 1 {
|
||||
buf.Write([]byte{'.'})
|
||||
buf.WriteString(parts[1])
|
||||
}
|
||||
return buf.String()
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package humanize
|
||||
|
||||
import "strconv"
|
||||
|
||||
func stripTrailingZeros(s string) string {
|
||||
offset := len(s) - 1
|
||||
for offset > 0 {
|
||||
if s[offset] == '.' {
|
||||
offset--
|
||||
break
|
||||
}
|
||||
if s[offset] != '0' {
|
||||
break
|
||||
}
|
||||
offset--
|
||||
}
|
||||
return s[:offset+1]
|
||||
}
|
||||
|
||||
// Ftoa converts a float to a string with no trailing zeros.
|
||||
func Ftoa(num float64) string {
|
||||
return stripTrailingZeros(strconv.FormatFloat(num, 'f', 6, 64))
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
/*
|
||||
Package humanize converts boring ugly numbers to human-friendly strings and back.
|
||||
|
||||
Durations can be turned into strings such as "3 days ago", numbers
|
||||
representing sizes like 82854982 into useful strings like, "83 MB" or
|
||||
"79 MiB" (whichever you prefer).
|
||||
*/
|
||||
package humanize
|
@ -0,0 +1,25 @@
|
||||
package humanize
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Ordinal gives you the input number in a rank/ordinal format.
|
||||
//
|
||||
// Ordinal(3) -> 3rd
|
||||
func Ordinal(x int) string {
|
||||
suffix := "th"
|
||||
switch x % 10 {
|
||||
case 1:
|
||||
if x%100 != 11 {
|
||||
suffix = "st"
|
||||
}
|
||||
case 2:
|
||||
if x%100 != 12 {
|
||||
suffix = "nd"
|
||||
}
|
||||
case 3:
|
||||
if x%100 != 13 {
|
||||
suffix = "rd"
|
||||
}
|
||||
}
|
||||
return strconv.Itoa(x) + suffix
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var siPrefixTable = map[float64]string{
|
||||
-24: "y", // yocto
|
||||
-21: "z", // zepto
|
||||
-18: "a", // atto
|
||||
-15: "f", // femto
|
||||
-12: "p", // pico
|
||||
-9: "n", // nano
|
||||
-6: "µ", // micro
|
||||
-3: "m", // milli
|
||||
0: "",
|
||||
3: "k", // kilo
|
||||
6: "M", // mega
|
||||
9: "G", // giga
|
||||
12: "T", // tera
|
||||
15: "P", // peta
|
||||
18: "E", // exa
|
||||
21: "Z", // zetta
|
||||
24: "Y", // yotta
|
||||
}
|
||||
|
||||
var revSIPrefixTable = revfmap(siPrefixTable)
|
||||
|
||||
// revfmap reverses the map and precomputes the power multiplier
|
||||
func revfmap(in map[float64]string) map[string]float64 {
|
||||
rv := map[string]float64{}
|
||||
for k, v := range in {
|
||||
rv[v] = math.Pow(10, k)
|
||||
}
|
||||
return rv
|
||||
}
|
||||
|
||||
var riParseRegex *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
ri := `^([\-0-9.]+)\s?([`
|
||||
for _, v := range siPrefixTable {
|
||||
ri += v
|
||||
}
|
||||
ri += `]?)(.*)`
|
||||
|
||||
riParseRegex = regexp.MustCompile(ri)
|
||||
}
|
||||
|
||||
// ComputeSI finds the most appropriate SI prefix for the given number
|
||||
// and returns the prefix along with the value adjusted to be within
|
||||
// that prefix.
|
||||
//
|
||||
// See also: SI, ParseSI.
|
||||
//
|
||||
// e.g. ComputeSI(2.2345e-12) -> (2.2345, "p")
|
||||
func ComputeSI(input float64) (float64, string) {
|
||||
if input == 0 {
|
||||
return 0, ""
|
||||
}
|
||||
mag := math.Abs(input)
|
||||
exponent := math.Floor(logn(mag, 10))
|
||||
exponent = math.Floor(exponent/3) * 3
|
||||
|
||||
value := mag / math.Pow(10, exponent)
|
||||
|
||||
// Handle special case where value is exactly 1000.0
|
||||
// Should return 1 M instead of 1000 k
|
||||
if value == 1000.0 {
|
||||
exponent += 3
|
||||
value = mag / math.Pow(10, exponent)
|
||||
}
|
||||
|
||||
value = math.Copysign(value, input)
|
||||
|
||||
prefix := siPrefixTable[exponent]
|
||||
return value, prefix
|
||||
}
|
||||
|
||||
// SI returns a string with default formatting.
|
||||
//
|
||||
// SI uses Ftoa to format float value, removing trailing zeros.
|
||||
//
|
||||
// See also: ComputeSI, ParseSI.
|
||||
//
|
||||
// e.g. SI(1000000, "B") -> 1 MB
|
||||
// e.g. SI(2.2345e-12, "F") -> 2.2345 pF
|
||||
func SI(input float64, unit string) string {
|
||||
value, prefix := ComputeSI(input)
|
||||
return Ftoa(value) + " " + prefix + unit
|
||||
}
|
||||
|
||||
var errInvalid = errors.New("invalid input")
|
||||
|
||||
// ParseSI parses an SI string back into the number and unit.
|
||||
//
|
||||
// See also: SI, ComputeSI.
|
||||
//
|
||||
// e.g. ParseSI("2.2345 pF") -> (2.2345e-12, "F", nil)
|
||||
func ParseSI(input string) (float64, string, error) {
|
||||
found := riParseRegex.FindStringSubmatch(input)
|
||||
if len(found) != 4 {
|
||||
return 0, "", errInvalid
|
||||
}
|
||||
mag := revSIPrefixTable[found[2]]
|
||||
unit := found[3]
|
||||
|
||||
base, err := strconv.ParseFloat(found[1], 64)
|
||||
return base * mag, unit, err
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package humanize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Seconds-based time units
|
||||
const (
|
||||
Day = 24 * time.Hour
|
||||
Week = 7 * Day
|
||||
Month = 30 * Day
|
||||
Year = 12 * Month
|
||||
LongTime = 37 * Year
|
||||
)
|
||||
|
||||
// Time formats a time into a relative string.
|
||||
//
|
||||
// Time(someT) -> "3 weeks ago"
|
||||
func Time(then time.Time) string {
|
||||
return RelTime(then, time.Now(), "ago", "from now")
|
||||
}
|
||||
|
||||
// A RelTimeMagnitude struct contains a relative time point at which
|
||||
// the relative format of time will switch to a new format string. A
|
||||
// slice of these in ascending order by their "D" field is passed to
|
||||
// CustomRelTime to format durations.
|
||||
//
|
||||
// The Format field is a string that may contain a "%s" which will be
|
||||
// replaced with the appropriate signed label (e.g. "ago" or "from
|
||||
// now") and a "%d" that will be replaced by the quantity.
|
||||
//
|
||||
// The DivBy field is the amount of time the time difference must be
|
||||
// divided by in order to display correctly.
|
||||
//
|
||||
// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"
|
||||
// DivBy should be time.Minute so whatever the duration is will be
|
||||
// expressed in minutes.
|
||||
type RelTimeMagnitude struct {
|
||||
D time.Duration
|
||||
Format string
|
||||
DivBy time.Duration
|
||||
}
|
||||
|
||||
var defaultMagnitudes = []RelTimeMagnitude{
|
||||
{time.Second, "now", time.Second},
|
||||
{2 * time.Second, "1 second %s", 1},
|
||||
{time.Minute, "%d seconds %s", time.Second},
|
||||
{2 * time.Minute, "1 minute %s", 1},
|
||||
{time.Hour, "%d minutes %s", time.Minute},
|
||||
{2 * time.Hour, "1 hour %s", 1},
|
||||
{Day, "%d hours %s", time.Hour},
|
||||
{2 * Day, "1 day %s", 1},
|
||||
{Week, "%d days %s", Day},
|
||||
{2 * Week, "1 week %s", 1},
|
||||
{Month, "%d weeks %s", Week},
|
||||
{2 * Month, "1 month %s", 1},
|
||||
{Year, "%d months %s", Month},
|
||||
{18 * Month, "1 year %s", 1},
|
||||
{2 * Year, "2 years %s", 1},
|
||||
{LongTime, "%d years %s", Year},
|
||||
{math.MaxInt64, "a long while %s", 1},
|
||||
}
|
||||
|
||||
// RelTime formats a time into a relative string.
|
||||
//
|
||||
// It takes two times and two labels. In addition to the generic time
|
||||
// delta string (e.g. 5 minutes), the labels are used applied so that
|
||||
// the label corresponding to the smaller time is applied.
|
||||
//
|
||||
// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
|
||||
func RelTime(a, b time.Time, albl, blbl string) string {
|
||||
return CustomRelTime(a, b, albl, blbl, defaultMagnitudes)
|
||||
}
|
||||
|
||||
// CustomRelTime formats a time into a relative string.
|
||||
//
|
||||
// It takes two times two labels and a table of relative time formats.
|
||||
// In addition to the generic time delta string (e.g. 5 minutes), the
|
||||
// labels are used applied so that the label corresponding to the
|
||||
// smaller time is applied.
|
||||
func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnitude) string {
|
||||
lbl := albl
|
||||
diff := b.Sub(a)
|
||||
|
||||
if a.After(b) {
|
||||
lbl = blbl
|
||||
diff = a.Sub(b)
|
||||
}
|
||||
|
||||
n := sort.Search(len(magnitudes), func(i int) bool {
|
||||
return magnitudes[i].D > diff
|
||||
})
|
||||
|
||||
if n >= len(magnitudes) {
|
||||
n = len(magnitudes) - 1
|
||||
}
|
||||
mag := magnitudes[n]
|
||||
args := []interface{}{}
|
||||
escaped := false
|
||||
for _, ch := range mag.Format {
|
||||
if escaped {
|
||||
switch ch {
|
||||
case 's':
|
||||
args = append(args, lbl)
|
||||
case 'd':
|
||||
args = append(args, diff/mag.DivBy)
|
||||
}
|
||||
escaped = false
|
||||
} else {
|
||||
escaped = ch == '%'
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf(mag.Format, args...)
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.8.x
|
||||
- tip
|
||||
|
@ -0,0 +1,27 @@
|
||||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/mattn/go-colorable"
|
||||
packages = ["."]
|
||||
revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072"
|
||||
version = "v0.0.9"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/mattn/go-isatty"
|
||||
packages = ["."]
|
||||
revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39"
|
||||
version = "v0.0.3"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/sys"
|
||||
packages = ["unix"]
|
||||
revision = "37707fdb30a5b38865cfb95e5aab41707daec7fd"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "e8a50671c3cb93ea935bf210b1cd20702876b9d9226129be581ef646d1565cdc"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
@ -0,0 +1,30 @@
|
||||
|
||||
# Gopkg.toml example
|
||||
#
|
||||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
||||
# for detailed Gopkg.toml documentation.
|
||||
#
|
||||
# required = ["github.com/user/thing/cmd/thing"]
|
||||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project"
|
||||
# version = "1.0.0"
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project2"
|
||||
# branch = "dev"
|
||||
# source = "github.com/myfork/project2"
|
||||
#
|
||||
# [[override]]
|
||||
# name = "github.com/x/y"
|
||||
# version = "2.4.0"
|
||||
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/mattn/go-colorable"
|
||||
version = "0.0.9"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/mattn/go-isatty"
|
||||
version = "0.0.3"
|
@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Fatih Arslan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,179 @@
|
||||
# Color [![GoDoc](https://godoc.org/github.com/fatih/color?status.svg)](https://godoc.org/github.com/fatih/color) [![Build Status](https://img.shields.io/travis/fatih/color.svg?style=flat-square)](https://travis-ci.org/fatih/color)
|
||||
|
||||
|
||||
|
||||
Color lets you use colorized outputs in terms of [ANSI Escape
|
||||
Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) in Go (Golang). It
|
||||
has support for Windows too! The API can be used in several ways, pick one that
|
||||
suits you.
|
||||
|
||||
|
||||
![Color](https://i.imgur.com/c1JI0lA.png)
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get github.com/fatih/color
|
||||
```
|
||||
|
||||
Note that the `vendor` folder is here for stability. Remove the folder if you
|
||||
already have the dependencies in your GOPATH.
|
||||
|
||||
## Examples
|
||||
|
||||
### Standard colors
|
||||
|
||||
```go
|
||||
// Print with default helper functions
|
||||
color.Cyan("Prints text in cyan.")
|
||||
|
||||
// A newline will be appended automatically
|
||||
color.Blue("Prints %s in blue.", "text")
|
||||
|
||||
// These are using the default foreground colors
|
||||
color.Red("We have red")
|
||||
color.Magenta("And many others ..")
|
||||
|
||||
```
|
||||
|
||||
### Mix and reuse colors
|
||||
|
||||
```go
|
||||
// Create a new color object
|
||||
c := color.New(color.FgCyan).Add(color.Underline)
|
||||
c.Println("Prints cyan text with an underline.")
|
||||
|
||||
// Or just add them to New()
|
||||
d := color.New(color.FgCyan, color.Bold)
|
||||
d.Printf("This prints bold cyan %s\n", "too!.")
|
||||
|
||||
// Mix up foreground and background colors, create new mixes!
|
||||
red := color.New(color.FgRed)
|
||||
|
||||
boldRed := red.Add(color.Bold)
|
||||
boldRed.Println("This will print text in bold red.")
|
||||
|
||||
whiteBackground := red.Add(color.BgWhite)
|
||||
whiteBackground.Println("Red text with white background.")
|
||||
```
|
||||
|
||||
### Use your own output (io.Writer)
|
||||
|
||||
```go
|
||||
// Use your own io.Writer output
|
||||
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
|
||||
|
||||
blue := color.New(color.FgBlue)
|
||||
blue.Fprint(writer, "This will print text in blue.")
|
||||
```
|
||||
|
||||
### Custom print functions (PrintFunc)
|
||||
|
||||
```go
|
||||
// Create a custom print function for convenience
|
||||
red := color.New(color.FgRed).PrintfFunc()
|
||||
red("Warning")
|
||||
red("Error: %s", err)
|
||||
|
||||
// Mix up multiple attributes
|
||||
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
|
||||
notice("Don't forget this...")
|
||||
```
|
||||
|
||||
### Custom fprint functions (FprintFunc)
|
||||
|
||||
```go
|
||||
blue := color.New(FgBlue).FprintfFunc()
|
||||
blue(myWriter, "important notice: %s", stars)
|
||||
|
||||
// Mix up with multiple attributes
|
||||
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
|
||||
success(myWriter, "Don't forget this...")
|
||||
```
|
||||
|
||||
### Insert into noncolor strings (SprintFunc)
|
||||
|
||||
```go
|
||||
// Create SprintXxx functions to mix strings with other non-colorized strings:
|
||||
yellow := color.New(color.FgYellow).SprintFunc()
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
|
||||
|
||||
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
|
||||
fmt.Printf("This %s rocks!\n", info("package"))
|
||||
|
||||
// Use helper functions
|
||||
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
|
||||
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
|
||||
|
||||
// Windows supported too! Just don't forget to change the output to color.Output
|
||||
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
|
||||
```
|
||||
|
||||
### Plug into existing code
|
||||
|
||||
```go
|
||||
// Use handy standard colors
|
||||
color.Set(color.FgYellow)
|
||||
|
||||
fmt.Println("Existing text will now be in yellow")
|
||||
fmt.Printf("This one %s\n", "too")
|
||||
|
||||
color.Unset() // Don't forget to unset
|
||||
|
||||
// You can mix up parameters
|
||||
color.Set(color.FgMagenta, color.Bold)
|
||||
defer color.Unset() // Use it in your function
|
||||
|
||||
fmt.Println("All text will now be bold magenta.")
|
||||
```
|
||||
|
||||
### Disable/Enable color
|
||||
|
||||
There might be a case where you want to explicitly disable/enable color output. the
|
||||
`go-isatty` package will automatically disable color output for non-tty output streams
|
||||
(for example if the output were piped directly to `less`)
|
||||
|
||||
`Color` has support to disable/enable colors both globally and for single color
|
||||
definitions. For example suppose you have a CLI app and a `--no-color` bool flag. You
|
||||
can easily disable the color output with:
|
||||
|
||||
```go
|
||||
|
||||
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
|
||||
|
||||
if *flagNoColor {
|
||||
color.NoColor = true // disables colorized output
|
||||
}
|
||||
```
|
||||
|
||||
It also has support for single color definitions (local). You can
|
||||
disable/enable color output on the fly:
|
||||
|
||||
```go
|
||||
c := color.New(color.FgCyan)
|
||||
c.Println("Prints cyan text")
|
||||
|
||||
c.DisableColor()
|
||||
c.Println("This is printed without any color")
|
||||
|
||||
c.EnableColor()
|
||||
c.Println("This prints again cyan...")
|
||||
```
|
||||
|
||||
## Todo
|
||||
|
||||
* Save/Return previous values
|
||||
* Evaluate fmt.Formatter interface
|
||||
|
||||
|
||||
## Credits
|
||||
|
||||
* [Fatih Arslan](https://github.com/fatih)
|
||||
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT) - see [`LICENSE.md`](https://github.com/fatih/color/blob/master/LICENSE.md) for more details
|
||||
|
@ -0,0 +1,600 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mattn/go-colorable"
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
var (
|
||||
// NoColor defines if the output is colorized or not. It's dynamically set to
|
||||
// false or true based on the stdout's file descriptor referring to a terminal
|
||||
// or not. This is a global option and affects all colors. For more control
|
||||
// over each color block use the methods DisableColor() individually.
|
||||
NoColor = os.Getenv("TERM") == "dumb" ||
|
||||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
|
||||
|
||||
// Output defines the standard output of the print functions. By default
|
||||
// os.Stdout is used.
|
||||
Output = colorable.NewColorableStdout()
|
||||
|
||||
// colorsCache is used to reduce the count of created Color objects and
|
||||
// allows to reuse already created objects with required Attribute.
|
||||
colorsCache = make(map[Attribute]*Color)
|
||||
colorsCacheMu sync.Mutex // protects colorsCache
|
||||
)
|
||||
|
||||
// Color defines a custom color object which is defined by SGR parameters.
|
||||
type Color struct {
|
||||
params []Attribute
|
||||
noColor *bool
|
||||
}
|
||||
|
||||
// Attribute defines a single SGR Code
|
||||
type Attribute int
|
||||
|
||||
const escape = "\x1b"
|
||||
|
||||
// Base attributes
|
||||
const (
|
||||
Reset Attribute = iota
|
||||
Bold
|
||||
Faint
|
||||
Italic
|
||||
Underline
|
||||
BlinkSlow
|
||||
BlinkRapid
|
||||
ReverseVideo
|
||||
Concealed
|
||||
CrossedOut
|
||||
)
|
||||
|
||||
// Foreground text colors
|
||||
const (
|
||||
FgBlack Attribute = iota + 30
|
||||
FgRed
|
||||
FgGreen
|
||||
FgYellow
|
||||
FgBlue
|
||||
FgMagenta
|
||||
FgCyan
|
||||
FgWhite
|
||||
)
|
||||
|
||||
// Foreground Hi-Intensity text colors
|
||||
const (
|
||||
FgHiBlack Attribute = iota + 90
|
||||
FgHiRed
|
||||
FgHiGreen
|
||||
FgHiYellow
|
||||
FgHiBlue
|
||||
FgHiMagenta
|
||||
FgHiCyan
|
||||
FgHiWhite
|
||||
)
|
||||
|
||||
// Background text colors
|
||||
const (
|
||||
BgBlack Attribute = iota + 40
|
||||
BgRed
|
||||
BgGreen
|
||||
BgYellow
|
||||
BgBlue
|
||||
BgMagenta
|
||||
BgCyan
|
||||
BgWhite
|
||||
)
|
||||
|
||||
// Background Hi-Intensity text colors
|
||||
const (
|
||||
BgHiBlack Attribute = iota + 100
|
||||
BgHiRed
|
||||
BgHiGreen
|
||||
BgHiYellow
|
||||
BgHiBlue
|
||||
BgHiMagenta
|
||||
BgHiCyan
|
||||
BgHiWhite
|
||||
)
|
||||
|
||||
// New returns a newly created color object.
|
||||
func New(value ...Attribute) *Color {
|
||||
c := &Color{params: make([]Attribute, 0)}
|
||||
c.Add(value...)
|
||||
return c
|
||||
}
|
||||
|
||||
// Set sets the given parameters immediately. It will change the color of
|
||||
// output with the given SGR parameters until color.Unset() is called.
|
||||
func Set(p ...Attribute) *Color {
|
||||
c := New(p...)
|
||||
c.Set()
|
||||
return c
|
||||
}
|
||||
|
||||
// Unset resets all escape attributes and clears the output. Usually should
|
||||
// be called after Set().
|
||||
func Unset() {
|
||||
if NoColor {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(Output, "%s[%dm", escape, Reset)
|
||||
}
|
||||
|
||||
// Set sets the SGR sequence.
|
||||
func (c *Color) Set() *Color {
|
||||
if c.isNoColorSet() {
|
||||
return c
|
||||
}
|
||||
|
||||
fmt.Fprintf(Output, c.format())
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) unset() {
|
||||
if c.isNoColorSet() {
|
||||
return
|
||||
}
|
||||
|
||||
Unset()
|
||||
}
|
||||
|
||||
func (c *Color) setWriter(w io.Writer) *Color {
|
||||
if c.isNoColorSet() {
|
||||
return c
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, c.format())
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) unsetWriter(w io.Writer) {
|
||||
if c.isNoColorSet() {
|
||||
return
|
||||
}
|
||||
|
||||
if NoColor {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s[%dm", escape, Reset)
|
||||
}
|
||||
|
||||
// Add is used to chain SGR parameters. Use as many as parameters to combine
|
||||
// and create custom color objects. Example: Add(color.FgRed, color.Underline).
|
||||
func (c *Color) Add(value ...Attribute) *Color {
|
||||
c.params = append(c.params, value...)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) prepend(value Attribute) {
|
||||
c.params = append(c.params, 0)
|
||||
copy(c.params[1:], c.params[0:])
|
||||
c.params[0] = value
|
||||
}
|
||||
|
||||
// Fprint formats using the default formats for its operands and writes to w.
|
||||
// Spaces are added between operands when neither is a string.
|
||||
// It returns the number of bytes written and any write error encountered.
|
||||
// On Windows, users should wrap w with colorable.NewColorable() if w is of
|
||||
// type *os.File.
|
||||
func (c *Color) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
||||
c.setWriter(w)
|
||||
defer c.unsetWriter(w)
|
||||
|
||||
return fmt.Fprint(w, a...)
|
||||
}
|
||||
|
||||
// Print formats using the default formats for its operands and writes to
|
||||
// standard output. Spaces are added between operands when neither is a
|
||||
// string. It returns the number of bytes written and any write error
|
||||
// encountered. This is the standard fmt.Print() method wrapped with the given
|
||||
// color.
|
||||
func (c *Color) Print(a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer c.unset()
|
||||
|
||||
return fmt.Fprint(Output, a...)
|
||||
}
|
||||
|
||||
// Fprintf formats according to a format specifier and writes to w.
|
||||
// It returns the number of bytes written and any write error encountered.
|
||||
// On Windows, users should wrap w with colorable.NewColorable() if w is of
|
||||
// type *os.File.
|
||||
func (c *Color) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
||||
c.setWriter(w)
|
||||
defer c.unsetWriter(w)
|
||||
|
||||
return fmt.Fprintf(w, format, a...)
|
||||
}
|
||||
|
||||
// Printf formats according to a format specifier and writes to standard output.
|
||||
// It returns the number of bytes written and any write error encountered.
|
||||
// This is the standard fmt.Printf() method wrapped with the given color.
|
||||
func (c *Color) Printf(format string, a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer c.unset()
|
||||
|
||||
return fmt.Fprintf(Output, format, a...)
|
||||
}
|
||||
|
||||
// Fprintln formats using the default formats for its operands and writes to w.
|
||||
// Spaces are always added between operands and a newline is appended.
|
||||
// On Windows, users should wrap w with colorable.NewColorable() if w is of
|
||||
// type *os.File.
|
||||
func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
||||
c.setWriter(w)
|
||||
defer c.unsetWriter(w)
|
||||
|
||||
return fmt.Fprintln(w, a...)
|
||||
}
|
||||
|
||||
// Println formats using the default formats for its operands and writes to
|
||||
// standard output. Spaces are always added between operands and a newline is
|
||||
// appended. It returns the number of bytes written and any write error
|
||||
// encountered. This is the standard fmt.Print() method wrapped with the given
|
||||
// color.
|
||||
func (c *Color) Println(a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer c.unset()
|
||||
|
||||
return fmt.Fprintln(Output, a...)
|
||||
}
|
||||
|
||||
// Sprint is just like Print, but returns a string instead of printing it.
|
||||
func (c *Color) Sprint(a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Sprintln is just like Println, but returns a string instead of printing it.
|
||||
func (c *Color) Sprintln(a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprintln(a...))
|
||||
}
|
||||
|
||||
// Sprintf is just like Printf, but returns a string instead of printing it.
|
||||
func (c *Color) Sprintf(format string, a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// FprintFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Fprint().
|
||||
func (c *Color) FprintFunc() func(w io.Writer, a ...interface{}) {
|
||||
return func(w io.Writer, a ...interface{}) {
|
||||
c.Fprint(w, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Print().
|
||||
func (c *Color) PrintFunc() func(a ...interface{}) {
|
||||
return func(a ...interface{}) {
|
||||
c.Print(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// FprintfFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Fprintf().
|
||||
func (c *Color) FprintfFunc() func(w io.Writer, format string, a ...interface{}) {
|
||||
return func(w io.Writer, format string, a ...interface{}) {
|
||||
c.Fprintf(w, format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintfFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Printf().
|
||||
func (c *Color) PrintfFunc() func(format string, a ...interface{}) {
|
||||
return func(format string, a ...interface{}) {
|
||||
c.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// FprintlnFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Fprintln().
|
||||
func (c *Color) FprintlnFunc() func(w io.Writer, a ...interface{}) {
|
||||
return func(w io.Writer, a ...interface{}) {
|
||||
c.Fprintln(w, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintlnFunc returns a new function that prints the passed arguments as
|
||||
// colorized with color.Println().
|
||||
func (c *Color) PrintlnFunc() func(a ...interface{}) {
|
||||
return func(a ...interface{}) {
|
||||
c.Println(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// SprintFunc returns a new function that returns colorized strings for the
|
||||
// given arguments with fmt.Sprint(). Useful to put into or mix into other
|
||||
// string. Windows users should use this in conjunction with color.Output, example:
|
||||
//
|
||||
// put := New(FgYellow).SprintFunc()
|
||||
// fmt.Fprintf(color.Output, "This is a %s", put("warning"))
|
||||
func (c *Color) SprintFunc() func(a ...interface{}) string {
|
||||
return func(a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprint(a...))
|
||||
}
|
||||
}
|
||||
|
||||
// SprintfFunc returns a new function that returns colorized strings for the
|
||||
// given arguments with fmt.Sprintf(). Useful to put into or mix into other
|
||||
// string. Windows users should use this in conjunction with color.Output.
|
||||
func (c *Color) SprintfFunc() func(format string, a ...interface{}) string {
|
||||
return func(format string, a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprintf(format, a...))
|
||||
}
|
||||
}
|
||||
|
||||
// SprintlnFunc returns a new function that returns colorized strings for the
|
||||
// given arguments with fmt.Sprintln(). Useful to put into or mix into other
|
||||
// string. Windows users should use this in conjunction with color.Output.
|
||||
func (c *Color) SprintlnFunc() func(a ...interface{}) string {
|
||||
return func(a ...interface{}) string {
|
||||
return c.wrap(fmt.Sprintln(a...))
|
||||
}
|
||||
}
|
||||
|
||||
// sequence returns a formatted SGR sequence to be plugged into a "\x1b[...m"
|
||||
// an example output might be: "1;36" -> bold cyan
|
||||
func (c *Color) sequence() string {
|
||||
format := make([]string, len(c.params))
|
||||
for i, v := range c.params {
|
||||
format[i] = strconv.Itoa(int(v))
|
||||
}
|
||||
|
||||
return strings.Join(format, ";")
|
||||
}
|
||||
|
||||
// wrap wraps the s string with the colors attributes. The string is ready to
|
||||
// be printed.
|
||||
func (c *Color) wrap(s string) string {
|
||||
if c.isNoColorSet() {
|
||||
return s
|
||||
}
|
||||
|
||||
return c.format() + s + c.unformat()
|
||||
}
|
||||
|
||||
func (c *Color) format() string {
|
||||
return fmt.Sprintf("%s[%sm", escape, c.sequence())
|
||||
}
|
||||
|
||||
func (c *Color) unformat() string {
|
||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||
}
|
||||
|
||||
// DisableColor disables the color output. Useful to not change any existing
|
||||
// code and still being able to output. Can be used for flags like
|
||||
// "--no-color". To enable back use EnableColor() method.
|
||||
func (c *Color) DisableColor() {
|
||||
c.noColor = boolPtr(true)
|
||||
}
|
||||
|
||||
// EnableColor enables the color output. Use it in conjunction with
|
||||
// DisableColor(). Otherwise this method has no side effects.
|
||||
func (c *Color) EnableColor() {
|
||||
c.noColor = boolPtr(false)
|
||||
}
|
||||
|
||||
func (c *Color) isNoColorSet() bool {
|
||||
// check first if we have user setted action
|
||||
if c.noColor != nil {
|
||||
return *c.noColor
|
||||
}
|
||||
|
||||
// if not return the global option, which is disabled by default
|
||||
return NoColor
|
||||
}
|
||||
|
||||
// Equals returns a boolean value indicating whether two colors are equal.
|
||||
func (c *Color) Equals(c2 *Color) bool {
|
||||
if len(c.params) != len(c2.params) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, attr := range c.params {
|
||||
if !c2.attrExists(attr) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Color) attrExists(a Attribute) bool {
|
||||
for _, attr := range c.params {
|
||||
if attr == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
func getCachedColor(p Attribute) *Color {
|
||||
colorsCacheMu.Lock()
|
||||
defer colorsCacheMu.Unlock()
|
||||
|
||||
c, ok := colorsCache[p]
|
||||
if !ok {
|
||||
c = New(p)
|
||||
colorsCache[p] = c
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func colorPrint(format string, p Attribute, a ...interface{}) {
|
||||
c := getCachedColor(p)
|
||||
|
||||
if !strings.HasSuffix(format, "\n") {
|
||||
format += "\n"
|
||||
}
|
||||
|
||||
if len(a) == 0 {
|
||||
c.Print(format)
|
||||
} else {
|
||||
c.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
func colorString(format string, p Attribute, a ...interface{}) string {
|
||||
c := getCachedColor(p)
|
||||
|
||||
if len(a) == 0 {
|
||||
return c.SprintFunc()(format)
|
||||
}
|
||||
|
||||
return c.SprintfFunc()(format, a...)
|
||||
}
|
||||
|
||||
// Black is a convenient helper function to print with black foreground. A
|
||||
// newline is appended to format by default.
|
||||
func Black(format string, a ...interface{}) { colorPrint(format, FgBlack, a...) }
|
||||
|
||||
// Red is a convenient helper function to print with red foreground. A
|
||||
// newline is appended to format by default.
|
||||
func Red(format string, a ...interface{}) { colorPrint(format, FgRed, a...) }
|
||||
|
||||
// Green is a convenient helper function to print with green foreground. A
|
||||
// newline is appended to format by default.
|
||||
func Green(format string, a ...interface{}) { colorPrint(format, FgGreen, a...) }
|
||||
|
||||
// Yellow is a convenient helper function to print with yellow foreground.
|
||||
// A newline is appended to format by default.
|
||||
func Yellow(format string, a ...interface{}) { colorPrint(format, FgYellow, a...) }
|
||||
|
||||
// Blue is a convenient helper function to print with blue foreground. A
|
||||
// newline is appended to format by default.
|
||||
func Blue(format string, a ...interface{}) { colorPrint(format, FgBlue, a...) }
|
||||
|
||||
// Magenta is a convenient helper function to print with magenta foreground.
|
||||
// A newline is appended to format by default.
|
||||
func Magenta(format string, a ...interface{}) { colorPrint(format, FgMagenta, a...) }
|
||||
|
||||
// Cyan is a convenient helper function to print with cyan foreground. A
|
||||
// newline is appended to format by default.
|
||||
func Cyan(format string, a ...interface{}) { colorPrint(format, FgCyan, a...) }
|
||||
|
||||
// White is a convenient helper function to print with white foreground. A
|
||||
// newline is appended to format by default.
|
||||
func White(format string, a ...interface{}) { colorPrint(format, FgWhite, a...) }
|
||||
|
||||
// BlackString is a convenient helper function to return a string with black
|
||||
// foreground.
|
||||
func BlackString(format string, a ...interface{}) string { return colorString(format, FgBlack, a...) }
|
||||
|
||||
// RedString is a convenient helper function to return a string with red
|
||||
// foreground.
|
||||
func RedString(format string, a ...interface{}) string { return colorString(format, FgRed, a...) }
|
||||
|
||||
// GreenString is a convenient helper function to return a string with green
|
||||
// foreground.
|
||||
func GreenString(format string, a ...interface{}) string { return colorString(format, FgGreen, a...) }
|
||||
|
||||
// YellowString is a convenient helper function to return a string with yellow
|
||||
// foreground.
|
||||
func YellowString(format string, a ...interface{}) string { return colorString(format, FgYellow, a...) }
|
||||
|
||||
// BlueString is a convenient helper function to return a string with blue
|
||||
// foreground.
|
||||
func BlueString(format string, a ...interface{}) string { return colorString(format, FgBlue, a...) }
|
||||
|
||||
// MagentaString is a convenient helper function to return a string with magenta
|
||||
// foreground.
|
||||
func MagentaString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgMagenta, a...)
|
||||
}
|
||||
|
||||
// CyanString is a convenient helper function to return a string with cyan
|
||||
// foreground.
|
||||
func CyanString(format string, a ...interface{}) string { return colorString(format, FgCyan, a...) }
|
||||
|
||||
// WhiteString is a convenient helper function to return a string with white
|
||||
// foreground.
|
||||
func WhiteString(format string, a ...interface{}) string { return colorString(format, FgWhite, a...) }
|
||||
|
||||
// HiBlack is a convenient helper function to print with hi-intensity black foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiBlack(format string, a ...interface{}) { colorPrint(format, FgHiBlack, a...) }
|
||||
|
||||
// HiRed is a convenient helper function to print with hi-intensity red foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiRed(format string, a ...interface{}) { colorPrint(format, FgHiRed, a...) }
|
||||
|
||||
// HiGreen is a convenient helper function to print with hi-intensity green foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiGreen(format string, a ...interface{}) { colorPrint(format, FgHiGreen, a...) }
|
||||
|
||||
// HiYellow is a convenient helper function to print with hi-intensity yellow foreground.
|
||||
// A newline is appended to format by default.
|
||||
func HiYellow(format string, a ...interface{}) { colorPrint(format, FgHiYellow, a...) }
|
||||
|
||||
// HiBlue is a convenient helper function to print with hi-intensity blue foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiBlue(format string, a ...interface{}) { colorPrint(format, FgHiBlue, a...) }
|
||||
|
||||
// HiMagenta is a convenient helper function to print with hi-intensity magenta foreground.
|
||||
// A newline is appended to format by default.
|
||||
func HiMagenta(format string, a ...interface{}) { colorPrint(format, FgHiMagenta, a...) }
|
||||
|
||||
// HiCyan is a convenient helper function to print with hi-intensity cyan foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiCyan(format string, a ...interface{}) { colorPrint(format, FgHiCyan, a...) }
|
||||
|
||||
// HiWhite is a convenient helper function to print with hi-intensity white foreground. A
|
||||
// newline is appended to format by default.
|
||||
func HiWhite(format string, a ...interface{}) { colorPrint(format, FgHiWhite, a...) }
|
||||
|
||||
// HiBlackString is a convenient helper function to return a string with hi-intensity black
|
||||
// foreground.
|
||||
func HiBlackString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgHiBlack, a...)
|
||||
}
|
||||
|
||||
// HiRedString is a convenient helper function to return a string with hi-intensity red
|
||||
// foreground.
|
||||
func HiRedString(format string, a ...interface{}) string { return colorString(format, FgHiRed, a...) }
|
||||
|
||||
// HiGreenString is a convenient helper function to return a string with hi-intensity green
|
||||
// foreground.
|
||||
func HiGreenString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgHiGreen, a...)
|
||||
}
|
||||
|
||||
// HiYellowString is a convenient helper function to return a string with hi-intensity yellow
|
||||
// foreground.
|
||||
func HiYellowString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgHiYellow, a...)
|
||||
}
|
||||
|
||||
// HiBlueString is a convenient helper function to return a string with hi-intensity blue
|
||||
// foreground.
|
||||
func HiBlueString(format string, a ...interface{}) string { return colorString(format, FgHiBlue, a...) }
|
||||
|
||||
// HiMagentaString is a convenient helper function to return a string with hi-intensity magenta
|
||||
// foreground.
|
||||
func HiMagentaString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgHiMagenta, a...)
|
||||
}
|
||||
|
||||
// HiCyanString is a convenient helper function to return a string with hi-intensity cyan
|
||||
// foreground.
|
||||
func HiCyanString(format string, a ...interface{}) string { return colorString(format, FgHiCyan, a...) }
|
||||
|
||||
// HiWhiteString is a convenient helper function to return a string with hi-intensity white
|
||||
// foreground.
|
||||
func HiWhiteString(format string, a ...interface{}) string {
|
||||
return colorString(format, FgHiWhite, a...)
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
/*
|
||||
Package color is an ANSI color package to output colorized or SGR defined
|
||||
output to the standard output. The API can be used in several way, pick one
|
||||
that suits you.
|
||||
|
||||
Use simple and default helper functions with predefined foreground colors:
|
||||
|
||||
color.Cyan("Prints text in cyan.")
|
||||
|
||||
// a newline will be appended automatically
|
||||
color.Blue("Prints %s in blue.", "text")
|
||||
|
||||
// More default foreground colors..
|
||||
color.Red("We have red")
|
||||
color.Yellow("Yellow color too!")
|
||||
color.Magenta("And many others ..")
|
||||
|
||||
// Hi-intensity colors
|
||||
color.HiGreen("Bright green color.")
|
||||
color.HiBlack("Bright black means gray..")
|
||||
color.HiWhite("Shiny white color!")
|
||||
|
||||
However there are times where custom color mixes are required. Below are some
|
||||
examples to create custom color objects and use the print functions of each
|
||||
separate color object.
|
||||
|
||||
// Create a new color object
|
||||
c := color.New(color.FgCyan).Add(color.Underline)
|
||||
c.Println("Prints cyan text with an underline.")
|
||||
|
||||
// Or just add them to New()
|
||||
d := color.New(color.FgCyan, color.Bold)
|
||||
d.Printf("This prints bold cyan %s\n", "too!.")
|
||||
|
||||
|
||||
// Mix up foreground and background colors, create new mixes!
|
||||
red := color.New(color.FgRed)
|
||||
|
||||
boldRed := red.Add(color.Bold)
|
||||
boldRed.Println("This will print text in bold red.")
|
||||
|
||||
whiteBackground := red.Add(color.BgWhite)
|
||||
whiteBackground.Println("Red text with White background.")
|
||||
|
||||
// Use your own io.Writer output
|
||||
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
|
||||
|
||||
blue := color.New(color.FgBlue)
|
||||
blue.Fprint(myWriter, "This will print text in blue.")
|
||||
|
||||
You can create PrintXxx functions to simplify even more:
|
||||
|
||||
// Create a custom print function for convenient
|
||||
red := color.New(color.FgRed).PrintfFunc()
|
||||
red("warning")
|
||||
red("error: %s", err)
|
||||
|
||||
// Mix up multiple attributes
|
||||
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
|
||||
notice("don't forget this...")
|
||||
|
||||
You can also FprintXxx functions to pass your own io.Writer:
|
||||
|
||||
blue := color.New(FgBlue).FprintfFunc()
|
||||
blue(myWriter, "important notice: %s", stars)
|
||||
|
||||
// Mix up with multiple attributes
|
||||
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
|
||||
success(myWriter, don't forget this...")
|
||||
|
||||
|
||||
Or create SprintXxx functions to mix strings with other non-colorized strings:
|
||||
|
||||
yellow := New(FgYellow).SprintFunc()
|
||||
red := New(FgRed).SprintFunc()
|
||||
|
||||
fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))
|
||||
|
||||
info := New(FgWhite, BgGreen).SprintFunc()
|
||||
fmt.Printf("this %s rocks!\n", info("package"))
|
||||
|
||||
Windows support is enabled by default. All Print functions work as intended.
|
||||
However only for color.SprintXXX functions, user should use fmt.FprintXXX and
|
||||
set the output to color.Output:
|
||||
|
||||
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
|
||||
|
||||
info := New(FgWhite, BgGreen).SprintFunc()
|
||||
fmt.Fprintf(color.Output, "this %s rocks!\n", info("package"))
|
||||
|
||||
Using with existing code is possible. Just use the Set() method to set the
|
||||
standard output to the given parameters. That way a rewrite of an existing
|
||||
code is not required.
|
||||
|
||||
// Use handy standard colors.
|
||||
color.Set(color.FgYellow)
|
||||
|
||||
fmt.Println("Existing text will be now in Yellow")
|
||||
fmt.Printf("This one %s\n", "too")
|
||||
|
||||
color.Unset() // don't forget to unset
|
||||
|
||||
// You can mix up parameters
|
||||
color.Set(color.FgMagenta, color.Bold)
|
||||
defer color.Unset() // use it in your function
|
||||
|
||||
fmt.Println("All text will be now bold magenta.")
|
||||
|
||||
There might be a case where you want to disable color output (for example to
|
||||
pipe the standard output of your app to somewhere else). `Color` has support to
|
||||
disable colors both globally and for single color definition. For example
|
||||
suppose you have a CLI app and a `--no-color` bool flag. You can easily disable
|
||||
the color output with:
|
||||
|
||||
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
|
||||
|
||||
if *flagNoColor {
|
||||
color.NoColor = true // disables colorized output
|
||||
}
|
||||
|
||||
It also has support for single color definitions (local). You can
|
||||
disable/enable color output on the fly:
|
||||
|
||||
c := color.New(color.FgCyan)
|
||||
c.Println("Prints cyan text")
|
||||
|
||||
c.DisableColor()
|
||||
c.Println("This is printed without any color")
|
||||
|
||||
c.EnableColor()
|
||||
c.Println("This prints again cyan...")
|
||||
*/
|
||||
package color
|
@ -0,0 +1,26 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
.DS_Store
|
||||
/vendor
|
@ -0,0 +1,6 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- tip
|
||||
|
||||
script: go test -v ./
|
@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Zack Guo
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
@ -0,0 +1,151 @@
|
||||
# termui [![Build Status](https://travis-ci.org/gizak/termui.svg?branch=master)](https://travis-ci.org/gizak/termui) [![Doc Status](https://godoc.org/github.com/gizak/termui?status.png)](https://godoc.org/github.com/gizak/termui)
|
||||
|
||||
<img src="./_example/dashboard.gif" alt="demo cast under osx 10.10; Terminal.app; Menlo Regular 12pt.)" width="80%">
|
||||
|
||||
`termui` is a cross-platform, easy-to-compile, and fully-customizable terminal dashboard. It is inspired by [blessed-contrib](https://github.com/yaronn/blessed-contrib), but purely in Go.
|
||||
|
||||
Now version v2 has arrived! It brings new event system, new theme system, new `Buffer` interface and specific colour text rendering. (some docs are missing, but it will be completed soon!)
|
||||
|
||||
## Installation
|
||||
|
||||
`master` mirrors v2 branch, to install:
|
||||
|
||||
go get -u github.com/gizak/termui
|
||||
|
||||
It is recommanded to use locked deps by using [glide](https://glide.sh): move to `termui` src directory then run `glide up`.
|
||||
|
||||
For the compatible reason, you can choose to install the legacy version of `termui`:
|
||||
|
||||
go get gopkg.in/gizak/termui.v1
|
||||
|
||||
## Usage
|
||||
|
||||
### Layout
|
||||
|
||||
To use `termui`, the very first thing you may want to know is how to manage layout. `termui` offers two ways of doing this, known as absolute layout and grid layout.
|
||||
|
||||
__Absolute layout__
|
||||
|
||||
Each widget has an underlying block structure which basically is a box model. It has border, label and padding properties. A border of a widget can be chosen to hide or display (with its border label), you can pick a different front/back colour for the border as well. To display such a widget at a specific location in terminal window, you need to assign `.X`, `.Y`, `.Height`, `.Width` values for each widget before sending it to `.Render`. Let's demonstrate these by a code snippet:
|
||||
|
||||
`````go
|
||||
import ui "github.com/gizak/termui" // <- ui shortcut, optional
|
||||
|
||||
func main() {
|
||||
err := ui.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer ui.Close()
|
||||
|
||||
p := ui.NewPar(":PRESS q TO QUIT DEMO")
|
||||
p.Height = 3
|
||||
p.Width = 50
|
||||
p.TextFgColor = ui.ColorWhite
|
||||
p.BorderLabel = "Text Box"
|
||||
p.BorderFg = ui.ColorCyan
|
||||
|
||||
g := ui.NewGauge()
|
||||
g.Percent = 50
|
||||
g.Width = 50
|
||||
g.Height = 3
|
||||
g.Y = 11
|
||||
g.BorderLabel = "Gauge"
|
||||
g.BarColor = ui.ColorRed
|
||||
g.BorderFg = ui.ColorWhite
|
||||
g.BorderLabelFg = ui.ColorCyan
|
||||
|
||||
ui.Render(p, g) // feel free to call Render, it's async and non-block
|
||||
|
||||
// event handler...
|
||||
}
|
||||
`````
|
||||
|
||||
Note that components can be overlapped (I'd rather call this a feature...), `Render(rs ...Renderer)` renders its args from left to right (i.e. each component's weight is arising from left to right).
|
||||
|
||||
__Grid layout:__
|
||||
|
||||
<img src="./_example/grid.gif" alt="grid" width="60%">
|
||||
|
||||
Grid layout uses [12 columns grid system](http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp) with expressive syntax. To use `Grid`, all we need to do is build a widget tree consisting of `Row`s and `Col`s (Actually a `Col` is also a `Row` but with a widget endpoint attached).
|
||||
|
||||
```go
|
||||
import ui "github.com/gizak/termui"
|
||||
// init and create widgets...
|
||||
|
||||
// build
|
||||
ui.Body.AddRows(
|
||||
ui.NewRow(
|
||||
ui.NewCol(6, 0, widget0),
|
||||
ui.NewCol(6, 0, widget1)),
|
||||
ui.NewRow(
|
||||
ui.NewCol(3, 0, widget2),
|
||||
ui.NewCol(3, 0, widget30, widget31, widget32),
|
||||
ui.NewCol(6, 0, widget4)))
|
||||
|
||||
// calculate layout
|
||||
ui.Body.Align()
|
||||
|
||||
ui.Render(ui.Body)
|
||||
```
|
||||
|
||||
### Events
|
||||
|
||||
`termui` ships with a http-like event mux handling system. All events are channeled up from different sources (typing, click, windows resize, custom event) and then encoded as universal `Event` object. `Event.Path` indicates the event type and `Event.Data` stores the event data struct. Add a handler to a certain event is easy as below:
|
||||
|
||||
```go
|
||||
// handle key q pressing
|
||||
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
||||
// press q to quit
|
||||
ui.StopLoop()
|
||||
})
|
||||
|
||||
ui.Handle("/sys/kbd/C-x", func(ui.Event) {
|
||||
// handle Ctrl + x combination
|
||||
})
|
||||
|
||||
ui.Handle("/sys/kbd", func(ui.Event) {
|
||||
// handle all other key pressing
|
||||
})
|
||||
|
||||
// handle a 1s timer
|
||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||
t := e.Data.(ui.EvtTimer)
|
||||
// t is a EvtTimer
|
||||
if t.Count%2 ==0 {
|
||||
// do something
|
||||
}
|
||||
})
|
||||
|
||||
ui.Loop() // block until StopLoop is called
|
||||
```
|
||||
|
||||
### Widgets
|
||||
|
||||
Click image to see the corresponding demo codes.
|
||||
|
||||
[<img src="./_example/par.png" alt="par" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/par.go)
|
||||
[<img src="./_example/list.png" alt="list" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/list.go)
|
||||
[<img src="./_example/gauge.png" alt="gauge" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/gauge.go)
|
||||
[<img src="./_example/linechart.png" alt="linechart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/linechart.go)
|
||||
[<img src="./_example/barchart.png" alt="barchart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/barchart.go)
|
||||
[<img src="./_example/mbarchart.png" alt="barchart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/mbarchart.go)
|
||||
[<img src="./_example/sparklines.png" alt="sparklines" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/sparklines.go)
|
||||
[<img src="./_example/table.png" alt="table" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_example/table.go)
|
||||
|
||||
## GoDoc
|
||||
|
||||
[godoc](https://godoc.org/github.com/gizak/termui)
|
||||
|
||||
## TODO
|
||||
|
||||
- [x] Grid layout
|
||||
- [x] Event system
|
||||
- [x] Canvas widget
|
||||
- [x] Refine APIs
|
||||
- [ ] Focusable widgets
|
||||
|
||||
## Changelog
|
||||
|
||||
## License
|
||||
This library is under the [MIT License](http://opensource.org/licenses/MIT)
|
@ -0,0 +1,149 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "fmt"
|
||||
|
||||
// BarChart creates multiple bars in a widget:
|
||||
/*
|
||||
bc := termui.NewBarChart()
|
||||
data := []int{3, 2, 5, 3, 9, 5}
|
||||
bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
|
||||
bc.BorderLabel = "Bar Chart"
|
||||
bc.Data = data
|
||||
bc.Width = 26
|
||||
bc.Height = 10
|
||||
bc.DataLabels = bclabels
|
||||
bc.TextColor = termui.ColorGreen
|
||||
bc.BarColor = termui.ColorRed
|
||||
bc.NumColor = termui.ColorYellow
|
||||
*/
|
||||
type BarChart struct {
|
||||
Block
|
||||
BarColor Attribute
|
||||
TextColor Attribute
|
||||
NumColor Attribute
|
||||
Data []int
|
||||
DataLabels []string
|
||||
BarWidth int
|
||||
BarGap int
|
||||
CellChar rune
|
||||
labels [][]rune
|
||||
dataNum [][]rune
|
||||
numBar int
|
||||
scale float64
|
||||
max int
|
||||
}
|
||||
|
||||
// NewBarChart returns a new *BarChart with current theme.
|
||||
func NewBarChart() *BarChart {
|
||||
bc := &BarChart{Block: *NewBlock()}
|
||||
bc.BarColor = ThemeAttr("barchart.bar.bg")
|
||||
bc.NumColor = ThemeAttr("barchart.num.fg")
|
||||
bc.TextColor = ThemeAttr("barchart.text.fg")
|
||||
bc.BarGap = 1
|
||||
bc.BarWidth = 3
|
||||
bc.CellChar = ' '
|
||||
return bc
|
||||
}
|
||||
|
||||
func (bc *BarChart) layout() {
|
||||
bc.numBar = bc.innerArea.Dx() / (bc.BarGap + bc.BarWidth)
|
||||
bc.labels = make([][]rune, bc.numBar)
|
||||
bc.dataNum = make([][]rune, len(bc.Data))
|
||||
|
||||
for i := 0; i < bc.numBar && i < len(bc.DataLabels) && i < len(bc.Data); i++ {
|
||||
bc.labels[i] = trimStr2Runes(bc.DataLabels[i], bc.BarWidth)
|
||||
n := bc.Data[i]
|
||||
s := fmt.Sprint(n)
|
||||
bc.dataNum[i] = trimStr2Runes(s, bc.BarWidth)
|
||||
}
|
||||
|
||||
//bc.max = bc.Data[0] // what if Data is nil? Sometimes when bar graph is nill it produces panic with panic: runtime error: index out of range
|
||||
// Asign a negative value to get maxvalue auto-populates
|
||||
if bc.max == 0 {
|
||||
bc.max = -1
|
||||
}
|
||||
for i := 0; i < len(bc.Data); i++ {
|
||||
if bc.max < bc.Data[i] {
|
||||
bc.max = bc.Data[i]
|
||||
}
|
||||
}
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-1)
|
||||
}
|
||||
|
||||
func (bc *BarChart) SetMax(max int) {
|
||||
|
||||
if max > 0 {
|
||||
bc.max = max
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (bc *BarChart) Buffer() Buffer {
|
||||
buf := bc.Block.Buffer()
|
||||
bc.layout()
|
||||
|
||||
for i := 0; i < bc.numBar && i < len(bc.Data) && i < len(bc.DataLabels); i++ {
|
||||
h := int(float64(bc.Data[i]) / bc.scale)
|
||||
oftX := i * (bc.BarWidth + bc.BarGap)
|
||||
|
||||
barBg := bc.Bg
|
||||
barFg := bc.BarColor
|
||||
|
||||
if bc.CellChar == ' ' {
|
||||
barBg = bc.BarColor
|
||||
barFg = ColorDefault
|
||||
if bc.BarColor == ColorDefault { // the same as above
|
||||
barBg |= AttrReverse
|
||||
}
|
||||
}
|
||||
|
||||
// plot bar
|
||||
for j := 0; j < bc.BarWidth; j++ {
|
||||
for k := 0; k < h; k++ {
|
||||
c := Cell{
|
||||
Ch: bc.CellChar,
|
||||
Bg: barBg,
|
||||
Fg: barFg,
|
||||
}
|
||||
|
||||
x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
// plot text
|
||||
for j, k := 0, 0; j < len(bc.labels[i]); j++ {
|
||||
w := charWidth(bc.labels[i][j])
|
||||
c := Cell{
|
||||
Ch: bc.labels[i][j],
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 1
|
||||
x := bc.innerArea.Min.X + oftX + k
|
||||
buf.Set(x, y, c)
|
||||
k += w
|
||||
}
|
||||
// plot num
|
||||
for j := 0; j < len(bc.dataNum[i]); j++ {
|
||||
c := Cell{
|
||||
Ch: bc.dataNum[i][j],
|
||||
Fg: bc.NumColor,
|
||||
Bg: barBg,
|
||||
}
|
||||
|
||||
if h == 0 {
|
||||
c.Bg = bc.Bg
|
||||
}
|
||||
x := bc.innerArea.Min.X + oftX + (bc.BarWidth-len(bc.dataNum[i]))/2 + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
@ -0,0 +1,240 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "image"
|
||||
|
||||
// Hline is a horizontal line.
|
||||
type Hline struct {
|
||||
X int
|
||||
Y int
|
||||
Len int
|
||||
Fg Attribute
|
||||
Bg Attribute
|
||||
}
|
||||
|
||||
// Vline is a vertical line.
|
||||
type Vline struct {
|
||||
X int
|
||||
Y int
|
||||
Len int
|
||||
Fg Attribute
|
||||
Bg Attribute
|
||||
}
|
||||
|
||||
// Buffer draws a horizontal line.
|
||||
func (l Hline) Buffer() Buffer {
|
||||
if l.Len <= 0 {
|
||||
return NewBuffer()
|
||||
}
|
||||
return NewFilledBuffer(l.X, l.Y, l.X+l.Len, l.Y+1, HORIZONTAL_LINE, l.Fg, l.Bg)
|
||||
}
|
||||
|
||||
// Buffer draws a vertical line.
|
||||
func (l Vline) Buffer() Buffer {
|
||||
if l.Len <= 0 {
|
||||
return NewBuffer()
|
||||
}
|
||||
return NewFilledBuffer(l.X, l.Y, l.X+1, l.Y+l.Len, VERTICAL_LINE, l.Fg, l.Bg)
|
||||
}
|
||||
|
||||
// Buffer draws a box border.
|
||||
func (b Block) drawBorder(buf Buffer) {
|
||||
if !b.Border {
|
||||
return
|
||||
}
|
||||
|
||||
min := b.area.Min
|
||||
max := b.area.Max
|
||||
|
||||
x0 := min.X
|
||||
y0 := min.Y
|
||||
x1 := max.X - 1
|
||||
y1 := max.Y - 1
|
||||
|
||||
// draw lines
|
||||
if b.BorderTop {
|
||||
buf.Merge(Hline{x0, y0, x1 - x0, b.BorderFg, b.BorderBg}.Buffer())
|
||||
}
|
||||
if b.BorderBottom {
|
||||
buf.Merge(Hline{x0, y1, x1 - x0, b.BorderFg, b.BorderBg}.Buffer())
|
||||
}
|
||||
if b.BorderLeft {
|
||||
buf.Merge(Vline{x0, y0, y1 - y0, b.BorderFg, b.BorderBg}.Buffer())
|
||||
}
|
||||
if b.BorderRight {
|
||||
buf.Merge(Vline{x1, y0, y1 - y0, b.BorderFg, b.BorderBg}.Buffer())
|
||||
}
|
||||
|
||||
// draw corners
|
||||
if b.BorderTop && b.BorderLeft && b.area.Dx() > 0 && b.area.Dy() > 0 {
|
||||
buf.Set(x0, y0, Cell{TOP_LEFT, b.BorderFg, b.BorderBg})
|
||||
}
|
||||
if b.BorderTop && b.BorderRight && b.area.Dx() > 1 && b.area.Dy() > 0 {
|
||||
buf.Set(x1, y0, Cell{TOP_RIGHT, b.BorderFg, b.BorderBg})
|
||||
}
|
||||
if b.BorderBottom && b.BorderLeft && b.area.Dx() > 0 && b.area.Dy() > 1 {
|
||||
buf.Set(x0, y1, Cell{BOTTOM_LEFT, b.BorderFg, b.BorderBg})
|
||||
}
|
||||
if b.BorderBottom && b.BorderRight && b.area.Dx() > 1 && b.area.Dy() > 1 {
|
||||
buf.Set(x1, y1, Cell{BOTTOM_RIGHT, b.BorderFg, b.BorderBg})
|
||||
}
|
||||
}
|
||||
|
||||
func (b Block) drawBorderLabel(buf Buffer) {
|
||||
maxTxtW := b.area.Dx() - 2
|
||||
tx := DTrimTxCls(DefaultTxBuilder.Build(b.BorderLabel, b.BorderLabelFg, b.BorderLabelBg), maxTxtW)
|
||||
|
||||
for i, w := 0, 0; i < len(tx); i++ {
|
||||
buf.Set(b.area.Min.X+1+w, b.area.Min.Y, tx[i])
|
||||
w += tx[i].Width()
|
||||
}
|
||||
}
|
||||
|
||||
// Block is a base struct for all other upper level widgets,
|
||||
// consider it as css: display:block.
|
||||
// Normally you do not need to create it manually.
|
||||
type Block struct {
|
||||
area image.Rectangle
|
||||
innerArea image.Rectangle
|
||||
X int
|
||||
Y int
|
||||
Border bool
|
||||
BorderFg Attribute
|
||||
BorderBg Attribute
|
||||
BorderLeft bool
|
||||
BorderRight bool
|
||||
BorderTop bool
|
||||
BorderBottom bool
|
||||
BorderLabel string
|
||||
BorderLabelFg Attribute
|
||||
BorderLabelBg Attribute
|
||||
Display bool
|
||||
Bg Attribute
|
||||
Width int
|
||||
Height int
|
||||
PaddingTop int
|
||||
PaddingBottom int
|
||||
PaddingLeft int
|
||||
PaddingRight int
|
||||
id string
|
||||
Float Align
|
||||
}
|
||||
|
||||
// NewBlock returns a *Block which inherits styles from current theme.
|
||||
func NewBlock() *Block {
|
||||
b := Block{}
|
||||
b.Display = true
|
||||
b.Border = true
|
||||
b.BorderLeft = true
|
||||
b.BorderRight = true
|
||||
b.BorderTop = true
|
||||
b.BorderBottom = true
|
||||
b.BorderBg = ThemeAttr("border.bg")
|
||||
b.BorderFg = ThemeAttr("border.fg")
|
||||
b.BorderLabelBg = ThemeAttr("label.bg")
|
||||
b.BorderLabelFg = ThemeAttr("label.fg")
|
||||
b.Bg = ThemeAttr("block.bg")
|
||||
b.Width = 2
|
||||
b.Height = 2
|
||||
b.id = GenId()
|
||||
b.Float = AlignNone
|
||||
return &b
|
||||
}
|
||||
|
||||
func (b Block) Id() string {
|
||||
return b.id
|
||||
}
|
||||
|
||||
// Align computes box model
|
||||
func (b *Block) Align() {
|
||||
// outer
|
||||
b.area.Min.X = 0
|
||||
b.area.Min.Y = 0
|
||||
b.area.Max.X = b.Width
|
||||
b.area.Max.Y = b.Height
|
||||
|
||||
// float
|
||||
b.area = AlignArea(TermRect(), b.area, b.Float)
|
||||
b.area = MoveArea(b.area, b.X, b.Y)
|
||||
|
||||
// inner
|
||||
b.innerArea.Min.X = b.area.Min.X + b.PaddingLeft
|
||||
b.innerArea.Min.Y = b.area.Min.Y + b.PaddingTop
|
||||
b.innerArea.Max.X = b.area.Max.X - b.PaddingRight
|
||||
b.innerArea.Max.Y = b.area.Max.Y - b.PaddingBottom
|
||||
|
||||
if b.Border {
|
||||
if b.BorderLeft {
|
||||
b.innerArea.Min.X++
|
||||
}
|
||||
if b.BorderRight {
|
||||
b.innerArea.Max.X--
|
||||
}
|
||||
if b.BorderTop {
|
||||
b.innerArea.Min.Y++
|
||||
}
|
||||
if b.BorderBottom {
|
||||
b.innerArea.Max.Y--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InnerBounds returns the internal bounds of the block after aligning and
|
||||
// calculating the padding and border, if any.
|
||||
func (b *Block) InnerBounds() image.Rectangle {
|
||||
b.Align()
|
||||
return b.innerArea
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
// Draw background and border (if any).
|
||||
func (b *Block) Buffer() Buffer {
|
||||
b.Align()
|
||||
|
||||
buf := NewBuffer()
|
||||
buf.SetArea(b.area)
|
||||
buf.Fill(' ', ColorDefault, b.Bg)
|
||||
|
||||
b.drawBorder(buf)
|
||||
b.drawBorderLabel(buf)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
// GetHeight implements GridBufferer.
|
||||
// It returns current height of the block.
|
||||
func (b Block) GetHeight() int {
|
||||
return b.Height
|
||||
}
|
||||
|
||||
// SetX implements GridBufferer interface, which sets block's x position.
|
||||
func (b *Block) SetX(x int) {
|
||||
b.X = x
|
||||
}
|
||||
|
||||
// SetY implements GridBufferer interface, it sets y position for block.
|
||||
func (b *Block) SetY(y int) {
|
||||
b.Y = y
|
||||
}
|
||||
|
||||
// SetWidth implements GridBuffer interface, it sets block's width.
|
||||
func (b *Block) SetWidth(w int) {
|
||||
b.Width = w
|
||||
}
|
||||
|
||||
func (b Block) InnerWidth() int {
|
||||
return b.innerArea.Dx()
|
||||
}
|
||||
|
||||
func (b Block) InnerHeight() int {
|
||||
return b.innerArea.Dy()
|
||||
}
|
||||
|
||||
func (b Block) InnerX() int {
|
||||
return b.innerArea.Min.X
|
||||
}
|
||||
|
||||
func (b Block) InnerY() int { return b.innerArea.Min.Y }
|
@ -0,0 +1,20 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
// +build !windows
|
||||
|
||||
package termui
|
||||
|
||||
const TOP_RIGHT = '┐'
|
||||
const VERTICAL_LINE = '│'
|
||||
const HORIZONTAL_LINE = '─'
|
||||
const TOP_LEFT = '┌'
|
||||
const BOTTOM_RIGHT = '┘'
|
||||
const BOTTOM_LEFT = '└'
|
||||
const VERTICAL_LEFT = '┤'
|
||||
const VERTICAL_RIGHT = '├'
|
||||
const HORIZONTAL_DOWN = '┬'
|
||||
const HORIZONTAL_UP = '┴'
|
||||
const QUOTA_LEFT = '«'
|
||||
const QUOTA_RIGHT = '»'
|
@ -0,0 +1,14 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
// +build windows
|
||||
|
||||
package termui
|
||||
|
||||
const TOP_RIGHT = '+'
|
||||
const VERTICAL_LINE = '|'
|
||||
const HORIZONTAL_LINE = '-'
|
||||
const TOP_LEFT = '+'
|
||||
const BOTTOM_RIGHT = '+'
|
||||
const BOTTOM_LEFT = '+'
|
@ -0,0 +1,106 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "image"
|
||||
|
||||
// Cell is a rune with assigned Fg and Bg
|
||||
type Cell struct {
|
||||
Ch rune
|
||||
Fg Attribute
|
||||
Bg Attribute
|
||||
}
|
||||
|
||||
// Buffer is a renderable rectangle cell data container.
|
||||
type Buffer struct {
|
||||
Area image.Rectangle // selected drawing area
|
||||
CellMap map[image.Point]Cell
|
||||
}
|
||||
|
||||
// At returns the cell at (x,y).
|
||||
func (b Buffer) At(x, y int) Cell {
|
||||
return b.CellMap[image.Pt(x, y)]
|
||||
}
|
||||
|
||||
// Set assigns a char to (x,y)
|
||||
func (b Buffer) Set(x, y int, c Cell) {
|
||||
b.CellMap[image.Pt(x, y)] = c
|
||||
}
|
||||
|
||||
// Bounds returns the domain for which At can return non-zero color.
|
||||
func (b Buffer) Bounds() image.Rectangle {
|
||||
x0, y0, x1, y1 := 0, 0, 0, 0
|
||||
for p := range b.CellMap {
|
||||
if p.X > x1 {
|
||||
x1 = p.X
|
||||
}
|
||||
if p.X < x0 {
|
||||
x0 = p.X
|
||||
}
|
||||
if p.Y > y1 {
|
||||
y1 = p.Y
|
||||
}
|
||||
if p.Y < y0 {
|
||||
y0 = p.Y
|
||||
}
|
||||
}
|
||||
return image.Rect(x0, y0, x1+1, y1+1)
|
||||
}
|
||||
|
||||
// SetArea assigns a new rect area to Buffer b.
|
||||
func (b *Buffer) SetArea(r image.Rectangle) {
|
||||
b.Area.Max = r.Max
|
||||
b.Area.Min = r.Min
|
||||
}
|
||||
|
||||
// Sync sets drawing area to the buffer's bound
|
||||
func (b *Buffer) Sync() {
|
||||
b.SetArea(b.Bounds())
|
||||
}
|
||||
|
||||
// NewCell returns a new cell
|
||||
func NewCell(ch rune, fg, bg Attribute) Cell {
|
||||
return Cell{ch, fg, bg}
|
||||
}
|
||||
|
||||
// Merge merges bs Buffers onto b
|
||||
func (b *Buffer) Merge(bs ...Buffer) {
|
||||
for _, buf := range bs {
|
||||
for p, v := range buf.CellMap {
|
||||
b.Set(p.X, p.Y, v)
|
||||
}
|
||||
b.SetArea(b.Area.Union(buf.Area))
|
||||
}
|
||||
}
|
||||
|
||||
// NewBuffer returns a new Buffer
|
||||
func NewBuffer() Buffer {
|
||||
return Buffer{
|
||||
CellMap: make(map[image.Point]Cell),
|
||||
Area: image.Rectangle{}}
|
||||
}
|
||||
|
||||
// Fill fills the Buffer b with ch,fg and bg.
|
||||
func (b Buffer) Fill(ch rune, fg, bg Attribute) {
|
||||
for x := b.Area.Min.X; x < b.Area.Max.X; x++ {
|
||||
for y := b.Area.Min.Y; y < b.Area.Max.Y; y++ {
|
||||
b.Set(x, y, Cell{ch, fg, bg})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewFilledBuffer returns a new Buffer filled with ch, fb and bg.
|
||||
func NewFilledBuffer(x0, y0, x1, y1 int, ch rune, fg, bg Attribute) Buffer {
|
||||
buf := NewBuffer()
|
||||
buf.Area.Min = image.Pt(x0, y0)
|
||||
buf.Area.Max = image.Pt(x1, y1)
|
||||
|
||||
for x := buf.Area.Min.X; x < buf.Area.Max.X; x++ {
|
||||
for y := buf.Area.Min.Y; y < buf.Area.Max.Y; y++ {
|
||||
buf.Set(x, y, Cell{ch, fg, bg})
|
||||
}
|
||||
}
|
||||
return buf
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
/*
|
||||
dots:
|
||||
,___,
|
||||
|1 4|
|
||||
|2 5|
|
||||
|3 6|
|
||||
|7 8|
|
||||
`````
|
||||
*/
|
||||
|
||||
var brailleBase = '\u2800'
|
||||
|
||||
var brailleOftMap = [4][2]rune{
|
||||
{'\u0001', '\u0008'},
|
||||
{'\u0002', '\u0010'},
|
||||
{'\u0004', '\u0020'},
|
||||
{'\u0040', '\u0080'}}
|
||||
|
||||
// Canvas contains drawing map: i,j -> rune
|
||||
type Canvas map[[2]int]rune
|
||||
|
||||
// NewCanvas returns an empty Canvas
|
||||
func NewCanvas() Canvas {
|
||||
return make(map[[2]int]rune)
|
||||
}
|
||||
|
||||
func chOft(x, y int) rune {
|
||||
return brailleOftMap[y%4][x%2]
|
||||
}
|
||||
|
||||
func (c Canvas) rawCh(x, y int) rune {
|
||||
if ch, ok := c[[2]int{x, y}]; ok {
|
||||
return ch
|
||||
}
|
||||
return '\u0000' //brailleOffset
|
||||
}
|
||||
|
||||
// return coordinate in terminal
|
||||
func chPos(x, y int) (int, int) {
|
||||
return y / 4, x / 2
|
||||
}
|
||||
|
||||
// Set sets a point (x,y) in the virtual coordinate
|
||||
func (c Canvas) Set(x, y int) {
|
||||
i, j := chPos(x, y)
|
||||
ch := c.rawCh(i, j)
|
||||
ch |= chOft(x, y)
|
||||
c[[2]int{i, j}] = ch
|
||||
}
|
||||
|
||||
// Unset removes point (x,y)
|
||||
func (c Canvas) Unset(x, y int) {
|
||||
i, j := chPos(x, y)
|
||||
ch := c.rawCh(i, j)
|
||||
ch &= ^chOft(x, y)
|
||||
c[[2]int{i, j}] = ch
|
||||
}
|
||||
|
||||
// Buffer returns un-styled points
|
||||
func (c Canvas) Buffer() Buffer {
|
||||
buf := NewBuffer()
|
||||
for k, v := range c {
|
||||
buf.Set(k[0], k[1], Cell{Ch: v + brailleBase})
|
||||
}
|
||||
return buf
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import re
|
||||
import os
|
||||
import io
|
||||
|
||||
copyright = """// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
"""
|
||||
|
||||
exclude_dirs = [".git", "_docs"]
|
||||
exclude_files = []
|
||||
include_dirs = [".", "debug", "extra", "test", "_example"]
|
||||
|
||||
|
||||
def is_target(fpath):
|
||||
if os.path.splitext(fpath)[-1] == ".go":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def update_copyright(fpath):
|
||||
print("processing " + fpath)
|
||||
f = io.open(fpath, 'r', encoding='utf-8')
|
||||
fstr = f.read()
|
||||
f.close()
|
||||
|
||||
# remove old
|
||||
m = re.search('^// Copyright .+?\r?\n\r?\n', fstr, re.MULTILINE|re.DOTALL)
|
||||
if m:
|
||||
fstr = fstr[m.end():]
|
||||
|
||||
# add new
|
||||
fstr = copyright + fstr
|
||||
f = io.open(fpath, 'w',encoding='utf-8')
|
||||
f.write(fstr)
|
||||
f.close()
|
||||
|
||||
|
||||
def main():
|
||||
for d in include_dirs:
|
||||
files = [
|
||||
os.path.join(d, f) for f in os.listdir(d)
|
||||
if os.path.isfile(os.path.join(d, f))
|
||||
]
|
||||
for f in files:
|
||||
if is_target(f):
|
||||
update_copyright(f)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,29 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
/*
|
||||
Package termui is a library designed for creating command line UI. For more info, goto http://github.com/gizak/termui
|
||||
|
||||
A simplest example:
|
||||
package main
|
||||
|
||||
import ui "github.com/gizak/termui"
|
||||
|
||||
func main() {
|
||||
if err:=ui.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer ui.Close()
|
||||
|
||||
g := ui.NewGauge()
|
||||
g.Percent = 50
|
||||
g.Width = 50
|
||||
g.BorderLabel = "Gauge"
|
||||
|
||||
ui.Render(g)
|
||||
|
||||
ui.Loop()
|
||||
}
|
||||
*/
|
||||
package termui
|
@ -0,0 +1,323 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Type string
|
||||
Path string
|
||||
From string
|
||||
To string
|
||||
Data interface{}
|
||||
Time int64
|
||||
}
|
||||
|
||||
var sysEvtChs []chan Event
|
||||
|
||||
type EvtKbd struct {
|
||||
KeyStr string
|
||||
}
|
||||
|
||||
func evtKbd(e termbox.Event) EvtKbd {
|
||||
ek := EvtKbd{}
|
||||
|
||||
k := string(e.Ch)
|
||||
pre := ""
|
||||
mod := ""
|
||||
|
||||
if e.Mod == termbox.ModAlt {
|
||||
mod = "M-"
|
||||
}
|
||||
if e.Ch == 0 {
|
||||
if e.Key > 0xFFFF-12 {
|
||||
k = "<f" + strconv.Itoa(0xFFFF-int(e.Key)+1) + ">"
|
||||
} else if e.Key > 0xFFFF-25 {
|
||||
ks := []string{"<insert>", "<delete>", "<home>", "<end>", "<previous>", "<next>", "<up>", "<down>", "<left>", "<right>"}
|
||||
k = ks[0xFFFF-int(e.Key)-12]
|
||||
}
|
||||
|
||||
if e.Key <= 0x7F {
|
||||
pre = "C-"
|
||||
k = string('a' - 1 + int(e.Key))
|
||||
kmap := map[termbox.Key][2]string{
|
||||
termbox.KeyCtrlSpace: {"C-", "<space>"},
|
||||
termbox.KeyBackspace: {"", "<backspace>"},
|
||||
termbox.KeyTab: {"", "<tab>"},
|
||||
termbox.KeyEnter: {"", "<enter>"},
|
||||
termbox.KeyEsc: {"", "<escape>"},
|
||||
termbox.KeyCtrlBackslash: {"C-", "\\"},
|
||||
termbox.KeyCtrlSlash: {"C-", "/"},
|
||||
termbox.KeySpace: {"", "<space>"},
|
||||
termbox.KeyCtrl8: {"C-", "8"},
|
||||
}
|
||||
if sk, ok := kmap[e.Key]; ok {
|
||||
pre = sk[0]
|
||||
k = sk[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ek.KeyStr = pre + mod + k
|
||||
return ek
|
||||
}
|
||||
|
||||
func crtTermboxEvt(e termbox.Event) Event {
|
||||
systypemap := map[termbox.EventType]string{
|
||||
termbox.EventKey: "keyboard",
|
||||
termbox.EventResize: "window",
|
||||
termbox.EventMouse: "mouse",
|
||||
termbox.EventError: "error",
|
||||
termbox.EventInterrupt: "interrupt",
|
||||
}
|
||||
ne := Event{From: "/sys", Time: time.Now().Unix()}
|
||||
typ := e.Type
|
||||
ne.Type = systypemap[typ]
|
||||
|
||||
switch typ {
|
||||
case termbox.EventKey:
|
||||
kbd := evtKbd(e)
|
||||
ne.Path = "/sys/kbd/" + kbd.KeyStr
|
||||
ne.Data = kbd
|
||||
case termbox.EventResize:
|
||||
wnd := EvtWnd{}
|
||||
wnd.Width = e.Width
|
||||
wnd.Height = e.Height
|
||||
ne.Path = "/sys/wnd/resize"
|
||||
ne.Data = wnd
|
||||
case termbox.EventError:
|
||||
err := EvtErr(e.Err)
|
||||
ne.Path = "/sys/err"
|
||||
ne.Data = err
|
||||
case termbox.EventMouse:
|
||||
m := EvtMouse{}
|
||||
m.X = e.MouseX
|
||||
m.Y = e.MouseY
|
||||
ne.Path = "/sys/mouse"
|
||||
ne.Data = m
|
||||
}
|
||||
return ne
|
||||
}
|
||||
|
||||
type EvtWnd struct {
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type EvtMouse struct {
|
||||
X int
|
||||
Y int
|
||||
Press string
|
||||
}
|
||||
|
||||
type EvtErr error
|
||||
|
||||
func hookTermboxEvt() {
|
||||
for {
|
||||
e := termbox.PollEvent()
|
||||
|
||||
for _, c := range sysEvtChs {
|
||||
go func(ch chan Event) {
|
||||
ch <- crtTermboxEvt(e)
|
||||
}(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewSysEvtCh() chan Event {
|
||||
ec := make(chan Event)
|
||||
sysEvtChs = append(sysEvtChs, ec)
|
||||
return ec
|
||||
}
|
||||
|
||||
var DefaultEvtStream = NewEvtStream()
|
||||
|
||||
type EvtStream struct {
|
||||
sync.RWMutex
|
||||
srcMap map[string]chan Event
|
||||
stream chan Event
|
||||
wg sync.WaitGroup
|
||||
sigStopLoop chan Event
|
||||
Handlers map[string]func(Event)
|
||||
hook func(Event)
|
||||
}
|
||||
|
||||
func NewEvtStream() *EvtStream {
|
||||
return &EvtStream{
|
||||
srcMap: make(map[string]chan Event),
|
||||
stream: make(chan Event),
|
||||
Handlers: make(map[string]func(Event)),
|
||||
sigStopLoop: make(chan Event),
|
||||
}
|
||||
}
|
||||
|
||||
func (es *EvtStream) Init() {
|
||||
es.Merge("internal", es.sigStopLoop)
|
||||
go func() {
|
||||
es.wg.Wait()
|
||||
close(es.stream)
|
||||
}()
|
||||
}
|
||||
|
||||
func cleanPath(p string) string {
|
||||
if p == "" {
|
||||
return "/"
|
||||
}
|
||||
if p[0] != '/' {
|
||||
p = "/" + p
|
||||
}
|
||||
return path.Clean(p)
|
||||
}
|
||||
|
||||
func isPathMatch(pattern, path string) bool {
|
||||
if len(pattern) == 0 {
|
||||
return false
|
||||
}
|
||||
n := len(pattern)
|
||||
return len(path) >= n && path[0:n] == pattern
|
||||
}
|
||||
|
||||
func (es *EvtStream) Merge(name string, ec chan Event) {
|
||||
es.Lock()
|
||||
defer es.Unlock()
|
||||
|
||||
es.wg.Add(1)
|
||||
es.srcMap[name] = ec
|
||||
|
||||
go func(a chan Event) {
|
||||
for n := range a {
|
||||
n.From = name
|
||||
es.stream <- n
|
||||
}
|
||||
es.wg.Done()
|
||||
}(ec)
|
||||
}
|
||||
|
||||
func (es *EvtStream) Handle(path string, handler func(Event)) {
|
||||
es.Handlers[cleanPath(path)] = handler
|
||||
}
|
||||
|
||||
func findMatch(mux map[string]func(Event), path string) string {
|
||||
n := -1
|
||||
pattern := ""
|
||||
for m := range mux {
|
||||
if !isPathMatch(m, path) {
|
||||
continue
|
||||
}
|
||||
if len(m) > n {
|
||||
pattern = m
|
||||
n = len(m)
|
||||
}
|
||||
}
|
||||
return pattern
|
||||
|
||||
}
|
||||
// Remove all existing defined Handlers from the map
|
||||
func (es *EvtStream) ResetHandlers() {
|
||||
for Path, _ := range es.Handlers {
|
||||
delete(es.Handlers, Path)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (es *EvtStream) match(path string) string {
|
||||
return findMatch(es.Handlers, path)
|
||||
}
|
||||
|
||||
func (es *EvtStream) Hook(f func(Event)) {
|
||||
es.hook = f
|
||||
}
|
||||
|
||||
func (es *EvtStream) Loop() {
|
||||
for e := range es.stream {
|
||||
switch e.Path {
|
||||
case "/sig/stoploop":
|
||||
return
|
||||
}
|
||||
go func(a Event) {
|
||||
es.RLock()
|
||||
defer es.RUnlock()
|
||||
if pattern := es.match(a.Path); pattern != "" {
|
||||
es.Handlers[pattern](a)
|
||||
}
|
||||
}(e)
|
||||
if es.hook != nil {
|
||||
es.hook(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (es *EvtStream) StopLoop() {
|
||||
go func() {
|
||||
e := Event{
|
||||
Path: "/sig/stoploop",
|
||||
}
|
||||
es.sigStopLoop <- e
|
||||
}()
|
||||
}
|
||||
|
||||
func Merge(name string, ec chan Event) {
|
||||
DefaultEvtStream.Merge(name, ec)
|
||||
}
|
||||
|
||||
func Handle(path string, handler func(Event)) {
|
||||
DefaultEvtStream.Handle(path, handler)
|
||||
}
|
||||
|
||||
func Loop() {
|
||||
DefaultEvtStream.Loop()
|
||||
}
|
||||
|
||||
func StopLoop() {
|
||||
DefaultEvtStream.StopLoop()
|
||||
}
|
||||
|
||||
type EvtTimer struct {
|
||||
Duration time.Duration
|
||||
Count uint64
|
||||
}
|
||||
|
||||
func NewTimerCh(du time.Duration) chan Event {
|
||||
t := make(chan Event)
|
||||
|
||||
go func(a chan Event) {
|
||||
n := uint64(0)
|
||||
for {
|
||||
n++
|
||||
time.Sleep(du)
|
||||
e := Event{}
|
||||
e.Type = "timer"
|
||||
e.Path = "/timer/" + du.String()
|
||||
e.Time = time.Now().Unix()
|
||||
e.Data = EvtTimer{
|
||||
Duration: du,
|
||||
Count: n,
|
||||
}
|
||||
t <- e
|
||||
|
||||
}
|
||||
}(t)
|
||||
return t
|
||||
}
|
||||
|
||||
var DefualtHandler = func(e Event) {
|
||||
}
|
||||
|
||||
var usrEvtCh = make(chan Event)
|
||||
|
||||
func SendCustomEvt(path string, data interface{}) {
|
||||
e := Event{}
|
||||
e.Path = path
|
||||
e.Data = data
|
||||
e.Time = time.Now().Unix()
|
||||
usrEvtCh <- e
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Gauge is a progress bar like widget.
|
||||
// A simple example:
|
||||
/*
|
||||
g := termui.NewGauge()
|
||||
g.Percent = 40
|
||||
g.Width = 50
|
||||
g.Height = 3
|
||||
g.BorderLabel = "Slim Gauge"
|
||||
g.BarColor = termui.ColorRed
|
||||
g.PercentColor = termui.ColorBlue
|
||||
*/
|
||||
|
||||
const ColorUndef Attribute = Attribute(^uint16(0))
|
||||
|
||||
type Gauge struct {
|
||||
Block
|
||||
Percent int
|
||||
BarColor Attribute
|
||||
PercentColor Attribute
|
||||
PercentColorHighlighted Attribute
|
||||
Label string
|
||||
LabelAlign Align
|
||||
}
|
||||
|
||||
// NewGauge return a new gauge with current theme.
|
||||
func NewGauge() *Gauge {
|
||||
g := &Gauge{
|
||||
Block: *NewBlock(),
|
||||
PercentColor: ThemeAttr("gauge.percent.fg"),
|
||||
BarColor: ThemeAttr("gauge.bar.bg"),
|
||||
Label: "{{percent}}%",
|
||||
LabelAlign: AlignCenter,
|
||||
PercentColorHighlighted: ColorUndef,
|
||||
}
|
||||
|
||||
g.Width = 12
|
||||
g.Height = 5
|
||||
return g
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (g *Gauge) Buffer() Buffer {
|
||||
buf := g.Block.Buffer()
|
||||
|
||||
// plot bar
|
||||
w := g.Percent * g.innerArea.Dx() / 100
|
||||
for i := 0; i < g.innerArea.Dy(); i++ {
|
||||
for j := 0; j < w; j++ {
|
||||
c := Cell{}
|
||||
c.Ch = ' '
|
||||
c.Bg = g.BarColor
|
||||
if c.Bg == ColorDefault {
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
buf.Set(g.innerArea.Min.X+j, g.innerArea.Min.Y+i, c)
|
||||
}
|
||||
}
|
||||
|
||||
// plot percentage
|
||||
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||||
pry := g.innerArea.Min.Y + g.innerArea.Dy()/2
|
||||
rs := str2runes(s)
|
||||
var pos int
|
||||
switch g.LabelAlign {
|
||||
case AlignLeft:
|
||||
pos = 0
|
||||
|
||||
case AlignCenter:
|
||||
pos = (g.innerArea.Dx() - strWidth(s)) / 2
|
||||
|
||||
case AlignRight:
|
||||
pos = g.innerArea.Dx() - strWidth(s) - 1
|
||||
}
|
||||
pos += g.innerArea.Min.X
|
||||
|
||||
for i, v := range rs {
|
||||
c := Cell{
|
||||
Ch: v,
|
||||
Fg: g.PercentColor,
|
||||
}
|
||||
|
||||
if w+g.innerArea.Min.X > pos+i {
|
||||
c.Bg = g.BarColor
|
||||
if c.Bg == ColorDefault {
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
|
||||
if g.PercentColorHighlighted != ColorUndef {
|
||||
c.Fg = g.PercentColorHighlighted
|
||||
}
|
||||
} else {
|
||||
c.Bg = g.Block.Bg
|
||||
}
|
||||
|
||||
buf.Set(1+pos+i, pry, c)
|
||||
}
|
||||
return buf
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
hash: 7a754ba100256404a978b2fc8738aee337beb822458e4b6060399fb89ebd215c
|
||||
updated: 2016-11-03T17:39:24.323773674-04:00
|
||||
imports:
|
||||
- name: github.com/maruel/panicparse
|
||||
version: ad661195ed0e88491e0f14be6613304e3b1141d6
|
||||
subpackages:
|
||||
- stack
|
||||
- name: github.com/mattn/go-runewidth
|
||||
version: 737072b4e32b7a5018b4a7125da8d12de90e8045
|
||||
- name: github.com/mitchellh/go-wordwrap
|
||||
version: ad45545899c7b13c020ea92b2072220eefad42b8
|
||||
- name: github.com/nsf/termbox-go
|
||||
version: b6acae516ace002cb8105a89024544a1480655a5
|
||||
- name: golang.org/x/net
|
||||
version: 569280fa63be4e201b975e5411e30a92178f0118
|
||||
subpackages:
|
||||
- websocket
|
||||
testImports:
|
||||
- name: github.com/davecgh/go-spew
|
||||
version: 346938d642f2ec3594ed81d874461961cd0faa76
|
||||
subpackages:
|
||||
- spew
|
||||
- name: github.com/pmezard/go-difflib
|
||||
version: d8ed2627bdf02c080bf22230dbb337003b7aba2d
|
||||
subpackages:
|
||||
- difflib
|
||||
- name: github.com/stretchr/testify
|
||||
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
|
||||
subpackages:
|
||||
- assert
|
@ -0,0 +1,9 @@
|
||||
package: github.com/gizak/termui
|
||||
import:
|
||||
- package: github.com/mattn/go-runewidth
|
||||
- package: github.com/mitchellh/go-wordwrap
|
||||
- package: github.com/nsf/termbox-go
|
||||
- package: golang.org/x/net
|
||||
subpackages:
|
||||
- websocket
|
||||
- package: github.com/maruel/panicparse
|
@ -0,0 +1,279 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
// GridBufferer introduces a Bufferer that can be manipulated by Grid.
|
||||
type GridBufferer interface {
|
||||
Bufferer
|
||||
GetHeight() int
|
||||
SetWidth(int)
|
||||
SetX(int)
|
||||
SetY(int)
|
||||
}
|
||||
|
||||
// Row builds a layout tree
|
||||
type Row struct {
|
||||
Cols []*Row //children
|
||||
Widget GridBufferer // root
|
||||
X int
|
||||
Y int
|
||||
Width int
|
||||
Height int
|
||||
Span int
|
||||
Offset int
|
||||
}
|
||||
|
||||
// calculate and set the underlying layout tree's x, y, height and width.
|
||||
func (r *Row) calcLayout() {
|
||||
r.assignWidth(r.Width)
|
||||
r.Height = r.solveHeight()
|
||||
r.assignX(r.X)
|
||||
r.assignY(r.Y)
|
||||
}
|
||||
|
||||
// tell if the node is leaf in the tree.
|
||||
func (r *Row) isLeaf() bool {
|
||||
return r.Cols == nil || len(r.Cols) == 0
|
||||
}
|
||||
|
||||
func (r *Row) isRenderableLeaf() bool {
|
||||
return r.isLeaf() && r.Widget != nil
|
||||
}
|
||||
|
||||
// assign widgets' (and their parent rows') width recursively.
|
||||
func (r *Row) assignWidth(w int) {
|
||||
r.SetWidth(w)
|
||||
|
||||
accW := 0 // acc span and offset
|
||||
calcW := make([]int, len(r.Cols)) // calculated width
|
||||
calcOftX := make([]int, len(r.Cols)) // computated start position of x
|
||||
|
||||
for i, c := range r.Cols {
|
||||
accW += c.Span + c.Offset
|
||||
cw := int(float64(c.Span*r.Width) / 12.0)
|
||||
|
||||
if i >= 1 {
|
||||
calcOftX[i] = calcOftX[i-1] +
|
||||
calcW[i-1] +
|
||||
int(float64(r.Cols[i-1].Offset*r.Width)/12.0)
|
||||
}
|
||||
|
||||
// use up the space if it is the last col
|
||||
if i == len(r.Cols)-1 && accW == 12 {
|
||||
cw = r.Width - calcOftX[i]
|
||||
}
|
||||
calcW[i] = cw
|
||||
r.Cols[i].assignWidth(cw)
|
||||
}
|
||||
}
|
||||
|
||||
// bottom up calc and set rows' (and their widgets') height,
|
||||
// return r's total height.
|
||||
func (r *Row) solveHeight() int {
|
||||
if r.isRenderableLeaf() {
|
||||
r.Height = r.Widget.GetHeight()
|
||||
return r.Widget.GetHeight()
|
||||
}
|
||||
|
||||
maxh := 0
|
||||
if !r.isLeaf() {
|
||||
for _, c := range r.Cols {
|
||||
nh := c.solveHeight()
|
||||
// when embed rows in Cols, row widgets stack up
|
||||
if r.Widget != nil {
|
||||
nh += r.Widget.GetHeight()
|
||||
}
|
||||
if nh > maxh {
|
||||
maxh = nh
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r.Height = maxh
|
||||
return maxh
|
||||
}
|
||||
|
||||
// recursively assign x position for r tree.
|
||||
func (r *Row) assignX(x int) {
|
||||
r.SetX(x)
|
||||
|
||||
if !r.isLeaf() {
|
||||
acc := 0
|
||||
for i, c := range r.Cols {
|
||||
if c.Offset != 0 {
|
||||
acc += int(float64(c.Offset*r.Width) / 12.0)
|
||||
}
|
||||
r.Cols[i].assignX(x + acc)
|
||||
acc += c.Width
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// recursively assign y position to r.
|
||||
func (r *Row) assignY(y int) {
|
||||
r.SetY(y)
|
||||
|
||||
if r.isLeaf() {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range r.Cols {
|
||||
acc := 0
|
||||
if r.Widget != nil {
|
||||
acc = r.Widget.GetHeight()
|
||||
}
|
||||
r.Cols[i].assignY(y + acc)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// GetHeight implements GridBufferer interface.
|
||||
func (r Row) GetHeight() int {
|
||||
return r.Height
|
||||
}
|
||||
|
||||
// SetX implements GridBufferer interface.
|
||||
func (r *Row) SetX(x int) {
|
||||
r.X = x
|
||||
if r.Widget != nil {
|
||||
r.Widget.SetX(x)
|
||||
}
|
||||
}
|
||||
|
||||
// SetY implements GridBufferer interface.
|
||||
func (r *Row) SetY(y int) {
|
||||
r.Y = y
|
||||
if r.Widget != nil {
|
||||
r.Widget.SetY(y)
|
||||
}
|
||||
}
|
||||
|
||||
// SetWidth implements GridBufferer interface.
|
||||
func (r *Row) SetWidth(w int) {
|
||||
r.Width = w
|
||||
if r.Widget != nil {
|
||||
r.Widget.SetWidth(w)
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface,
|
||||
// recursively merge all widgets buffer
|
||||
func (r *Row) Buffer() Buffer {
|
||||
merged := NewBuffer()
|
||||
|
||||
if r.isRenderableLeaf() {
|
||||
return r.Widget.Buffer()
|
||||
}
|
||||
|
||||
// for those are not leaves but have a renderable widget
|
||||
if r.Widget != nil {
|
||||
merged.Merge(r.Widget.Buffer())
|
||||
}
|
||||
|
||||
// collect buffer from children
|
||||
if !r.isLeaf() {
|
||||
for _, c := range r.Cols {
|
||||
merged.Merge(c.Buffer())
|
||||
}
|
||||
}
|
||||
|
||||
return merged
|
||||
}
|
||||
|
||||
// Grid implements 12 columns system.
|
||||
// A simple example:
|
||||
/*
|
||||
import ui "github.com/gizak/termui"
|
||||
// init and create widgets...
|
||||
|
||||
// build
|
||||
ui.Body.AddRows(
|
||||
ui.NewRow(
|
||||
ui.NewCol(6, 0, widget0),
|
||||
ui.NewCol(6, 0, widget1)),
|
||||
ui.NewRow(
|
||||
ui.NewCol(3, 0, widget2),
|
||||
ui.NewCol(3, 0, widget30, widget31, widget32),
|
||||
ui.NewCol(6, 0, widget4)))
|
||||
|
||||
// calculate layout
|
||||
ui.Body.Align()
|
||||
|
||||
ui.Render(ui.Body)
|
||||
*/
|
||||
type Grid struct {
|
||||
Rows []*Row
|
||||
Width int
|
||||
X int
|
||||
Y int
|
||||
BgColor Attribute
|
||||
}
|
||||
|
||||
// NewGrid returns *Grid with given rows.
|
||||
func NewGrid(rows ...*Row) *Grid {
|
||||
return &Grid{Rows: rows}
|
||||
}
|
||||
|
||||
// AddRows appends given rows to Grid.
|
||||
func (g *Grid) AddRows(rs ...*Row) {
|
||||
g.Rows = append(g.Rows, rs...)
|
||||
}
|
||||
|
||||
// NewRow creates a new row out of given columns.
|
||||
func NewRow(cols ...*Row) *Row {
|
||||
rs := &Row{Span: 12, Cols: cols}
|
||||
return rs
|
||||
}
|
||||
|
||||
// NewCol accepts: widgets are LayoutBufferer or widgets is A NewRow.
|
||||
// Note that if multiple widgets are provided, they will stack up in the col.
|
||||
func NewCol(span, offset int, widgets ...GridBufferer) *Row {
|
||||
r := &Row{Span: span, Offset: offset}
|
||||
|
||||
if widgets != nil && len(widgets) == 1 {
|
||||
wgt := widgets[0]
|
||||
nw, isRow := wgt.(*Row)
|
||||
if isRow {
|
||||
r.Cols = nw.Cols
|
||||
} else {
|
||||
r.Widget = wgt
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
r.Cols = []*Row{}
|
||||
ir := r
|
||||
for _, w := range widgets {
|
||||
nr := &Row{Span: 12, Widget: w}
|
||||
ir.Cols = []*Row{nr}
|
||||
ir = nr
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Align calculate each rows' layout.
|
||||
func (g *Grid) Align() {
|
||||
h := 0
|
||||
for _, r := range g.Rows {
|
||||
r.SetWidth(g.Width)
|
||||
r.SetX(g.X)
|
||||
r.SetY(g.Y + h)
|
||||
r.calcLayout()
|
||||
h += r.GetHeight()
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implments Bufferer interface.
|
||||
func (g Grid) Buffer() Buffer {
|
||||
buf := NewBuffer()
|
||||
|
||||
for _, r := range g.Rows {
|
||||
buf.Merge(r.Buffer())
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
var Body *Grid
|
@ -0,0 +1,222 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
tm "github.com/nsf/termbox-go"
|
||||
)
|
||||
import rw "github.com/mattn/go-runewidth"
|
||||
|
||||
/* ---------------Port from termbox-go --------------------- */
|
||||
|
||||
// Attribute is printable cell's color and style.
|
||||
type Attribute uint16
|
||||
|
||||
// 8 basic clolrs
|
||||
const (
|
||||
ColorDefault Attribute = iota
|
||||
ColorBlack
|
||||
ColorRed
|
||||
ColorGreen
|
||||
ColorYellow
|
||||
ColorBlue
|
||||
ColorMagenta
|
||||
ColorCyan
|
||||
ColorWhite
|
||||
)
|
||||
|
||||
//Have a constant that defines number of colors
|
||||
const NumberofColors = 8
|
||||
|
||||
// Text style
|
||||
const (
|
||||
AttrBold Attribute = 1 << (iota + 9)
|
||||
AttrUnderline
|
||||
AttrReverse
|
||||
)
|
||||
|
||||
var (
|
||||
dot = "…"
|
||||
dotw = rw.StringWidth(dot)
|
||||
)
|
||||
|
||||
/* ----------------------- End ----------------------------- */
|
||||
|
||||
func toTmAttr(x Attribute) tm.Attribute {
|
||||
return tm.Attribute(x)
|
||||
}
|
||||
|
||||
func str2runes(s string) []rune {
|
||||
return []rune(s)
|
||||
}
|
||||
|
||||
// Here for backwards-compatibility.
|
||||
func trimStr2Runes(s string, w int) []rune {
|
||||
return TrimStr2Runes(s, w)
|
||||
}
|
||||
|
||||
// TrimStr2Runes trims string to w[-1 rune], appends …, and returns the runes
|
||||
// of that string if string is grather then n. If string is small then w,
|
||||
// return the runes.
|
||||
func TrimStr2Runes(s string, w int) []rune {
|
||||
if w <= 0 {
|
||||
return []rune{}
|
||||
}
|
||||
|
||||
sw := rw.StringWidth(s)
|
||||
if sw > w {
|
||||
return []rune(rw.Truncate(s, w, dot))
|
||||
}
|
||||
return str2runes(s)
|
||||
}
|
||||
|
||||
// TrimStrIfAppropriate trim string to "s[:-1] + …"
|
||||
// if string > width otherwise return string
|
||||
func TrimStrIfAppropriate(s string, w int) string {
|
||||
if w <= 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
sw := rw.StringWidth(s)
|
||||
if sw > w {
|
||||
return rw.Truncate(s, w, dot)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func strWidth(s string) int {
|
||||
return rw.StringWidth(s)
|
||||
}
|
||||
|
||||
func charWidth(ch rune) int {
|
||||
return rw.RuneWidth(ch)
|
||||
}
|
||||
|
||||
var whiteSpaceRegex = regexp.MustCompile(`\s`)
|
||||
|
||||
// StringToAttribute converts text to a termui attribute. You may specifiy more
|
||||
// then one attribute like that: "BLACK, BOLD, ...". All whitespaces
|
||||
// are ignored.
|
||||
func StringToAttribute(text string) Attribute {
|
||||
text = whiteSpaceRegex.ReplaceAllString(strings.ToLower(text), "")
|
||||
attributes := strings.Split(text, ",")
|
||||
result := Attribute(0)
|
||||
|
||||
for _, theAttribute := range attributes {
|
||||
var match Attribute
|
||||
switch theAttribute {
|
||||
case "reset", "default":
|
||||
match = ColorDefault
|
||||
|
||||
case "black":
|
||||
match = ColorBlack
|
||||
|
||||
case "red":
|
||||
match = ColorRed
|
||||
|
||||
case "green":
|
||||
match = ColorGreen
|
||||
|
||||
case "yellow":
|
||||
match = ColorYellow
|
||||
|
||||
case "blue":
|
||||
match = ColorBlue
|
||||
|
||||
case "magenta":
|
||||
match = ColorMagenta
|
||||
|
||||
case "cyan":
|
||||
match = ColorCyan
|
||||
|
||||
case "white":
|
||||
match = ColorWhite
|
||||
|
||||
case "bold":
|
||||
match = AttrBold
|
||||
|
||||
case "underline":
|
||||
match = AttrUnderline
|
||||
|
||||
case "reverse":
|
||||
match = AttrReverse
|
||||
}
|
||||
|
||||
result |= match
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// TextCells returns a coloured text cells []Cell
|
||||
func TextCells(s string, fg, bg Attribute) []Cell {
|
||||
cs := make([]Cell, 0, len(s))
|
||||
|
||||
// sequence := MarkdownTextRendererFactory{}.TextRenderer(s).Render(fg, bg)
|
||||
// runes := []rune(sequence.NormalizedText)
|
||||
runes := str2runes(s)
|
||||
|
||||
for n := range runes {
|
||||
// point, _ := sequence.PointAt(n, 0, 0)
|
||||
// cs = append(cs, Cell{point.Ch, point.Fg, point.Bg})
|
||||
cs = append(cs, Cell{runes[n], fg, bg})
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
// Width returns the actual screen space the cell takes (usually 1 or 2).
|
||||
func (c Cell) Width() int {
|
||||
return charWidth(c.Ch)
|
||||
}
|
||||
|
||||
// Copy return a copy of c
|
||||
func (c Cell) Copy() Cell {
|
||||
return c
|
||||
}
|
||||
|
||||
// TrimTxCells trims the overflowed text cells sequence.
|
||||
func TrimTxCells(cs []Cell, w int) []Cell {
|
||||
if len(cs) <= w {
|
||||
return cs
|
||||
}
|
||||
return cs[:w]
|
||||
}
|
||||
|
||||
// DTrimTxCls trims the overflowed text cells sequence and append dots at the end.
|
||||
func DTrimTxCls(cs []Cell, w int) []Cell {
|
||||
l := len(cs)
|
||||
if l <= 0 {
|
||||
return []Cell{}
|
||||
}
|
||||
|
||||
rt := make([]Cell, 0, w)
|
||||
csw := 0
|
||||
for i := 0; i < l && csw <= w; i++ {
|
||||
c := cs[i]
|
||||
cw := c.Width()
|
||||
|
||||
if cw+csw < w {
|
||||
rt = append(rt, c)
|
||||
csw += cw
|
||||
} else {
|
||||
rt = append(rt, Cell{'…', c.Fg, c.Bg})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return rt
|
||||
}
|
||||
|
||||
func CellsToStr(cs []Cell) string {
|
||||
str := ""
|
||||
for _, c := range cs {
|
||||
str += string(c.Ch)
|
||||
}
|
||||
return str
|
||||
}
|
@ -0,0 +1,331 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// only 16 possible combinations, why bother
|
||||
var braillePatterns = map[[2]int]rune{
|
||||
[2]int{0, 0}: '⣀',
|
||||
[2]int{0, 1}: '⡠',
|
||||
[2]int{0, 2}: '⡐',
|
||||
[2]int{0, 3}: '⡈',
|
||||
|
||||
[2]int{1, 0}: '⢄',
|
||||
[2]int{1, 1}: '⠤',
|
||||
[2]int{1, 2}: '⠔',
|
||||
[2]int{1, 3}: '⠌',
|
||||
|
||||
[2]int{2, 0}: '⢂',
|
||||
[2]int{2, 1}: '⠢',
|
||||
[2]int{2, 2}: '⠒',
|
||||
[2]int{2, 3}: '⠊',
|
||||
|
||||
[2]int{3, 0}: '⢁',
|
||||
[2]int{3, 1}: '⠡',
|
||||
[2]int{3, 2}: '⠑',
|
||||
[2]int{3, 3}: '⠉',
|
||||
}
|
||||
|
||||
var lSingleBraille = [4]rune{'\u2840', '⠄', '⠂', '⠁'}
|
||||
var rSingleBraille = [4]rune{'\u2880', '⠠', '⠐', '⠈'}
|
||||
|
||||
// LineChart has two modes: braille(default) and dot. Using braille gives 2x capicity as dot mode,
|
||||
// because one braille char can represent two data points.
|
||||
/*
|
||||
lc := termui.NewLineChart()
|
||||
lc.BorderLabel = "braille-mode Line Chart"
|
||||
lc.Data = [1.2, 1.3, 1.5, 1.7, 1.5, 1.6, 1.8, 2.0]
|
||||
lc.Width = 50
|
||||
lc.Height = 12
|
||||
lc.AxesColor = termui.ColorWhite
|
||||
lc.LineColor = termui.ColorGreen | termui.AttrBold
|
||||
// termui.Render(lc)...
|
||||
*/
|
||||
type LineChart struct {
|
||||
Block
|
||||
Data []float64
|
||||
DataLabels []string // if unset, the data indices will be used
|
||||
Mode string // braille | dot
|
||||
DotStyle rune
|
||||
LineColor Attribute
|
||||
scale float64 // data span per cell on y-axis
|
||||
AxesColor Attribute
|
||||
drawingX int
|
||||
drawingY int
|
||||
axisYHeight int
|
||||
axisXWidth int
|
||||
axisYLabelGap int
|
||||
axisXLabelGap int
|
||||
topValue float64
|
||||
bottomValue float64
|
||||
labelX [][]rune
|
||||
labelY [][]rune
|
||||
labelYSpace int
|
||||
maxY float64
|
||||
minY float64
|
||||
autoLabels bool
|
||||
}
|
||||
|
||||
// NewLineChart returns a new LineChart with current theme.
|
||||
func NewLineChart() *LineChart {
|
||||
lc := &LineChart{Block: *NewBlock()}
|
||||
lc.AxesColor = ThemeAttr("linechart.axes.fg")
|
||||
lc.LineColor = ThemeAttr("linechart.line.fg")
|
||||
lc.Mode = "braille"
|
||||
lc.DotStyle = '•'
|
||||
lc.axisXLabelGap = 2
|
||||
lc.axisYLabelGap = 1
|
||||
lc.bottomValue = math.Inf(1)
|
||||
lc.topValue = math.Inf(-1)
|
||||
return lc
|
||||
}
|
||||
|
||||
// one cell contains two data points
|
||||
// so the capicity is 2x as dot-mode
|
||||
func (lc *LineChart) renderBraille() Buffer {
|
||||
buf := NewBuffer()
|
||||
|
||||
// return: b -> which cell should the point be in
|
||||
// m -> in the cell, divided into 4 equal height levels, which subcell?
|
||||
getPos := func(d float64) (b, m int) {
|
||||
cnt4 := int((d-lc.bottomValue)/(lc.scale/4) + 0.5)
|
||||
b = cnt4 / 4
|
||||
m = cnt4 % 4
|
||||
return
|
||||
}
|
||||
// plot points
|
||||
for i := 0; 2*i+1 < len(lc.Data) && i < lc.axisXWidth; i++ {
|
||||
b0, m0 := getPos(lc.Data[2*i])
|
||||
b1, m1 := getPos(lc.Data[2*i+1])
|
||||
|
||||
if b0 == b1 {
|
||||
c := Cell{
|
||||
Ch: braillePatterns[[2]int{m0, m1}],
|
||||
Bg: lc.Bg,
|
||||
Fg: lc.LineColor,
|
||||
}
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b0
|
||||
x := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
buf.Set(x, y, c)
|
||||
} else {
|
||||
c0 := Cell{Ch: lSingleBraille[m0],
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.Bg}
|
||||
x0 := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y0 := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b0
|
||||
buf.Set(x0, y0, c0)
|
||||
|
||||
c1 := Cell{Ch: rSingleBraille[m1],
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.Bg}
|
||||
x1 := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y1 := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b1
|
||||
buf.Set(x1, y1, c1)
|
||||
}
|
||||
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
func (lc *LineChart) renderDot() Buffer {
|
||||
buf := NewBuffer()
|
||||
for i := 0; i < len(lc.Data) && i < lc.axisXWidth; i++ {
|
||||
c := Cell{
|
||||
Ch: lc.DotStyle,
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.Bg,
|
||||
}
|
||||
x := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - int((lc.Data[i]-lc.bottomValue)/lc.scale+0.5)
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func (lc *LineChart) calcLabelX() {
|
||||
lc.labelX = [][]rune{}
|
||||
|
||||
for i, l := 0, 0; i < len(lc.DataLabels) && l < lc.axisXWidth; i++ {
|
||||
if lc.Mode == "dot" {
|
||||
if l >= len(lc.DataLabels) {
|
||||
break
|
||||
}
|
||||
|
||||
s := str2runes(lc.DataLabels[l])
|
||||
w := strWidth(lc.DataLabels[l])
|
||||
if l+w <= lc.axisXWidth {
|
||||
lc.labelX = append(lc.labelX, s)
|
||||
}
|
||||
l += w + lc.axisXLabelGap
|
||||
} else { // braille
|
||||
if 2*l >= len(lc.DataLabels) {
|
||||
break
|
||||
}
|
||||
|
||||
s := str2runes(lc.DataLabels[2*l])
|
||||
w := strWidth(lc.DataLabels[2*l])
|
||||
if l+w <= lc.axisXWidth {
|
||||
lc.labelX = append(lc.labelX, s)
|
||||
}
|
||||
l += w + lc.axisXLabelGap
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func shortenFloatVal(x float64) string {
|
||||
s := fmt.Sprintf("%.2f", x)
|
||||
if len(s)-3 > 3 {
|
||||
s = fmt.Sprintf("%.2e", x)
|
||||
}
|
||||
|
||||
if x < 0 {
|
||||
s = fmt.Sprintf("%.2f", x)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (lc *LineChart) calcLabelY() {
|
||||
span := lc.topValue - lc.bottomValue
|
||||
lc.scale = span / float64(lc.axisYHeight)
|
||||
|
||||
n := (1 + lc.axisYHeight) / (lc.axisYLabelGap + 1)
|
||||
lc.labelY = make([][]rune, n)
|
||||
maxLen := 0
|
||||
for i := 0; i < n; i++ {
|
||||
s := str2runes(shortenFloatVal(lc.bottomValue + float64(i)*span/float64(n)))
|
||||
if len(s) > maxLen {
|
||||
maxLen = len(s)
|
||||
}
|
||||
lc.labelY[i] = s
|
||||
}
|
||||
|
||||
lc.labelYSpace = maxLen
|
||||
}
|
||||
|
||||
func (lc *LineChart) calcLayout() {
|
||||
// set datalabels if it is not provided
|
||||
if (lc.DataLabels == nil || len(lc.DataLabels) == 0) || lc.autoLabels {
|
||||
lc.autoLabels = true
|
||||
lc.DataLabels = make([]string, len(lc.Data))
|
||||
for i := range lc.Data {
|
||||
lc.DataLabels[i] = fmt.Sprint(i)
|
||||
}
|
||||
}
|
||||
|
||||
// lazy increase, to avoid y shaking frequently
|
||||
// update bound Y when drawing is gonna overflow
|
||||
lc.minY = lc.Data[0]
|
||||
lc.maxY = lc.Data[0]
|
||||
|
||||
// valid visible range
|
||||
vrange := lc.innerArea.Dx()
|
||||
if lc.Mode == "braille" {
|
||||
vrange = 2 * lc.innerArea.Dx()
|
||||
}
|
||||
if vrange > len(lc.Data) {
|
||||
vrange = len(lc.Data)
|
||||
}
|
||||
|
||||
for _, v := range lc.Data[:vrange] {
|
||||
if v > lc.maxY {
|
||||
lc.maxY = v
|
||||
}
|
||||
if v < lc.minY {
|
||||
lc.minY = v
|
||||
}
|
||||
}
|
||||
|
||||
span := lc.maxY - lc.minY
|
||||
|
||||
if lc.minY < lc.bottomValue {
|
||||
lc.bottomValue = lc.minY - 0.2*span
|
||||
}
|
||||
|
||||
if lc.maxY > lc.topValue {
|
||||
lc.topValue = lc.maxY + 0.2*span
|
||||
}
|
||||
|
||||
lc.axisYHeight = lc.innerArea.Dy() - 2
|
||||
lc.calcLabelY()
|
||||
|
||||
lc.axisXWidth = lc.innerArea.Dx() - 1 - lc.labelYSpace
|
||||
lc.calcLabelX()
|
||||
|
||||
lc.drawingX = lc.innerArea.Min.X + 1 + lc.labelYSpace
|
||||
lc.drawingY = lc.innerArea.Min.Y
|
||||
}
|
||||
|
||||
func (lc *LineChart) plotAxes() Buffer {
|
||||
buf := NewBuffer()
|
||||
|
||||
origY := lc.innerArea.Min.Y + lc.innerArea.Dy() - 2
|
||||
origX := lc.innerArea.Min.X + lc.labelYSpace
|
||||
|
||||
buf.Set(origX, origY, Cell{Ch: ORIGIN, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
|
||||
for x := origX + 1; x < origX+lc.axisXWidth; x++ {
|
||||
buf.Set(x, origY, Cell{Ch: HDASH, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
|
||||
for dy := 1; dy <= lc.axisYHeight; dy++ {
|
||||
buf.Set(origX, origY-dy, Cell{Ch: VDASH, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
|
||||
// x label
|
||||
oft := 0
|
||||
for _, rs := range lc.labelX {
|
||||
if oft+len(rs) > lc.axisXWidth {
|
||||
break
|
||||
}
|
||||
for j, r := range rs {
|
||||
c := Cell{
|
||||
Ch: r,
|
||||
Fg: lc.AxesColor,
|
||||
Bg: lc.Bg,
|
||||
}
|
||||
x := origX + oft + j
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 1
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
oft += len(rs) + lc.axisXLabelGap
|
||||
}
|
||||
|
||||
// y labels
|
||||
for i, rs := range lc.labelY {
|
||||
for j, r := range rs {
|
||||
buf.Set(
|
||||
lc.innerArea.Min.X+j,
|
||||
origY-i*(lc.axisYLabelGap+1),
|
||||
Cell{Ch: r, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (lc *LineChart) Buffer() Buffer {
|
||||
buf := lc.Block.Buffer()
|
||||
|
||||
if lc.Data == nil || len(lc.Data) == 0 {
|
||||
return buf
|
||||
}
|
||||
lc.calcLayout()
|
||||
buf.Merge(lc.plotAxes())
|
||||
|
||||
if lc.Mode == "dot" {
|
||||
buf.Merge(lc.renderDot())
|
||||
} else {
|
||||
buf.Merge(lc.renderBraille())
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
// +build !windows
|
||||
|
||||
package termui
|
||||
|
||||
const VDASH = '┊'
|
||||
const HDASH = '┈'
|
||||
const ORIGIN = '└'
|
@ -0,0 +1,11 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
// +build windows
|
||||
|
||||
package termui
|
||||
|
||||
const VDASH = '|'
|
||||
const HDASH = '-'
|
||||
const ORIGIN = '+'
|
@ -0,0 +1,89 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "strings"
|
||||
|
||||
// List displays []string as its items,
|
||||
// it has a Overflow option (default is "hidden"), when set to "hidden",
|
||||
// the item exceeding List's width is truncated, but when set to "wrap",
|
||||
// the overflowed text breaks into next line.
|
||||
/*
|
||||
strs := []string{
|
||||
"[0] github.com/gizak/termui",
|
||||
"[1] editbox.go",
|
||||
"[2] iterrupt.go",
|
||||
"[3] keyboard.go",
|
||||
"[4] output.go",
|
||||
"[5] random_out.go",
|
||||
"[6] dashboard.go",
|
||||
"[7] nsf/termbox-go"}
|
||||
|
||||
ls := termui.NewList()
|
||||
ls.Items = strs
|
||||
ls.ItemFgColor = termui.ColorYellow
|
||||
ls.BorderLabel = "List"
|
||||
ls.Height = 7
|
||||
ls.Width = 25
|
||||
ls.Y = 0
|
||||
*/
|
||||
type List struct {
|
||||
Block
|
||||
Items []string
|
||||
Overflow string
|
||||
ItemFgColor Attribute
|
||||
ItemBgColor Attribute
|
||||
}
|
||||
|
||||
// NewList returns a new *List with current theme.
|
||||
func NewList() *List {
|
||||
l := &List{Block: *NewBlock()}
|
||||
l.Overflow = "hidden"
|
||||
l.ItemFgColor = ThemeAttr("list.item.fg")
|
||||
l.ItemBgColor = ThemeAttr("list.item.bg")
|
||||
return l
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (l *List) Buffer() Buffer {
|
||||
buf := l.Block.Buffer()
|
||||
|
||||
switch l.Overflow {
|
||||
case "wrap":
|
||||
cs := DefaultTxBuilder.Build(strings.Join(l.Items, "\n"), l.ItemFgColor, l.ItemBgColor)
|
||||
i, j, k := 0, 0, 0
|
||||
for i < l.innerArea.Dy() && k < len(cs) {
|
||||
w := cs[k].Width()
|
||||
if cs[k].Ch == '\n' || j+w > l.innerArea.Dx() {
|
||||
i++
|
||||
j = 0
|
||||
if cs[k].Ch == '\n' {
|
||||
k++
|
||||
}
|
||||
continue
|
||||
}
|
||||
buf.Set(l.innerArea.Min.X+j, l.innerArea.Min.Y+i, cs[k])
|
||||
|
||||
k++
|
||||
j++
|
||||
}
|
||||
|
||||
case "hidden":
|
||||
trimItems := l.Items
|
||||
if len(trimItems) > l.innerArea.Dy() {
|
||||
trimItems = trimItems[:l.innerArea.Dy()]
|
||||
}
|
||||
for i, v := range trimItems {
|
||||
cs := DTrimTxCls(DefaultTxBuilder.Build(v, l.ItemFgColor, l.ItemBgColor), l.innerArea.Dx())
|
||||
j := 0
|
||||
for _, vv := range cs {
|
||||
w := vv.Width()
|
||||
buf.Set(l.innerArea.Min.X+j, l.innerArea.Min.Y+i, vv)
|
||||
j += w
|
||||
}
|
||||
}
|
||||
}
|
||||
return buf
|
||||
}
|
@ -0,0 +1,242 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// This is the implemetation of multi-colored or stacked bar graph. This is different from default barGraph which is implemented in bar.go
|
||||
// Multi-Colored-BarChart creates multiple bars in a widget:
|
||||
/*
|
||||
bc := termui.NewMBarChart()
|
||||
data := make([][]int, 2)
|
||||
data[0] := []int{3, 2, 5, 7, 9, 4}
|
||||
data[1] := []int{7, 8, 5, 3, 1, 6}
|
||||
bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
|
||||
bc.BorderLabel = "Bar Chart"
|
||||
bc.Data = data
|
||||
bc.Width = 26
|
||||
bc.Height = 10
|
||||
bc.DataLabels = bclabels
|
||||
bc.TextColor = termui.ColorGreen
|
||||
bc.BarColor = termui.ColorRed
|
||||
bc.NumColor = termui.ColorYellow
|
||||
*/
|
||||
type MBarChart struct {
|
||||
Block
|
||||
BarColor [NumberofColors]Attribute
|
||||
TextColor Attribute
|
||||
NumColor [NumberofColors]Attribute
|
||||
Data [NumberofColors][]int
|
||||
DataLabels []string
|
||||
BarWidth int
|
||||
BarGap int
|
||||
labels [][]rune
|
||||
dataNum [NumberofColors][][]rune
|
||||
numBar int
|
||||
scale float64
|
||||
max int
|
||||
minDataLen int
|
||||
numStack int
|
||||
ShowScale bool
|
||||
maxScale []rune
|
||||
}
|
||||
|
||||
// NewBarChart returns a new *BarChart with current theme.
|
||||
func NewMBarChart() *MBarChart {
|
||||
bc := &MBarChart{Block: *NewBlock()}
|
||||
bc.BarColor[0] = ThemeAttr("mbarchart.bar.bg")
|
||||
bc.NumColor[0] = ThemeAttr("mbarchart.num.fg")
|
||||
bc.TextColor = ThemeAttr("mbarchart.text.fg")
|
||||
bc.BarGap = 1
|
||||
bc.BarWidth = 3
|
||||
return bc
|
||||
}
|
||||
|
||||
func (bc *MBarChart) layout() {
|
||||
bc.numBar = bc.innerArea.Dx() / (bc.BarGap + bc.BarWidth)
|
||||
bc.labels = make([][]rune, bc.numBar)
|
||||
DataLen := 0
|
||||
LabelLen := len(bc.DataLabels)
|
||||
bc.minDataLen = 9999 //Set this to some very hight value so that we find the minimum one We want to know which array among data[][] has got the least length
|
||||
|
||||
// We need to know how many stack/data array data[0] , data[1] are there
|
||||
for i := 0; i < len(bc.Data); i++ {
|
||||
if bc.Data[i] == nil {
|
||||
break
|
||||
}
|
||||
DataLen++
|
||||
}
|
||||
bc.numStack = DataLen
|
||||
|
||||
//We need to know what is the mimimum size of data array data[0] could have 10 elements data[1] could have only 5, so we plot only 5 bar graphs
|
||||
|
||||
for i := 0; i < DataLen; i++ {
|
||||
if bc.minDataLen > len(bc.Data[i]) {
|
||||
bc.minDataLen = len(bc.Data[i])
|
||||
}
|
||||
}
|
||||
|
||||
if LabelLen > bc.minDataLen {
|
||||
LabelLen = bc.minDataLen
|
||||
}
|
||||
|
||||
for i := 0; i < LabelLen && i < bc.numBar; i++ {
|
||||
bc.labels[i] = trimStr2Runes(bc.DataLabels[i], bc.BarWidth)
|
||||
}
|
||||
|
||||
for i := 0; i < bc.numStack; i++ {
|
||||
bc.dataNum[i] = make([][]rune, len(bc.Data[i]))
|
||||
//For each stack of bar calcualte the rune
|
||||
for j := 0; j < LabelLen && i < bc.numBar; j++ {
|
||||
n := bc.Data[i][j]
|
||||
s := fmt.Sprint(n)
|
||||
bc.dataNum[i][j] = trimStr2Runes(s, bc.BarWidth)
|
||||
}
|
||||
//If color is not defined by default then populate a color that is different from the prevous bar
|
||||
if bc.BarColor[i] == ColorDefault && bc.NumColor[i] == ColorDefault {
|
||||
if i == 0 {
|
||||
bc.BarColor[i] = ColorBlack
|
||||
} else {
|
||||
bc.BarColor[i] = bc.BarColor[i-1] + 1
|
||||
if bc.BarColor[i] > NumberofColors {
|
||||
bc.BarColor[i] = ColorBlack
|
||||
}
|
||||
}
|
||||
bc.NumColor[i] = (NumberofColors + 1) - bc.BarColor[i] //Make NumColor opposite of barColor for visibility
|
||||
}
|
||||
}
|
||||
|
||||
//If Max value is not set then we have to populate, this time the max value will be max(sum(d1[0],d2[0],d3[0]) .... sum(d1[n], d2[n], d3[n]))
|
||||
|
||||
if bc.max == 0 {
|
||||
bc.max = -1
|
||||
}
|
||||
for i := 0; i < bc.minDataLen && i < LabelLen; i++ {
|
||||
var dsum int
|
||||
for j := 0; j < bc.numStack; j++ {
|
||||
dsum += bc.Data[j][i]
|
||||
}
|
||||
if dsum > bc.max {
|
||||
bc.max = dsum
|
||||
}
|
||||
}
|
||||
|
||||
//Finally Calculate max sale
|
||||
if bc.ShowScale {
|
||||
s := fmt.Sprintf("%d", bc.max)
|
||||
bc.maxScale = trimStr2Runes(s, len(s))
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-2)
|
||||
} else {
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (bc *MBarChart) SetMax(max int) {
|
||||
|
||||
if max > 0 {
|
||||
bc.max = max
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (bc *MBarChart) Buffer() Buffer {
|
||||
buf := bc.Block.Buffer()
|
||||
bc.layout()
|
||||
var oftX int
|
||||
|
||||
for i := 0; i < bc.numBar && i < bc.minDataLen && i < len(bc.DataLabels); i++ {
|
||||
ph := 0 //Previous Height to stack up
|
||||
oftX = i * (bc.BarWidth + bc.BarGap)
|
||||
for i1 := 0; i1 < bc.numStack; i1++ {
|
||||
h := int(float64(bc.Data[i1][i]) / bc.scale)
|
||||
// plot bars
|
||||
for j := 0; j < bc.BarWidth; j++ {
|
||||
for k := 0; k < h; k++ {
|
||||
c := Cell{
|
||||
Ch: ' ',
|
||||
Bg: bc.BarColor[i1],
|
||||
}
|
||||
if bc.BarColor[i1] == ColorDefault { // when color is default, space char treated as transparent!
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k - ph
|
||||
buf.Set(x, y, c)
|
||||
|
||||
}
|
||||
}
|
||||
ph += h
|
||||
}
|
||||
// plot text
|
||||
for j, k := 0, 0; j < len(bc.labels[i]); j++ {
|
||||
w := charWidth(bc.labels[i][j])
|
||||
c := Cell{
|
||||
Ch: bc.labels[i][j],
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 1
|
||||
x := bc.innerArea.Max.X + oftX + ((bc.BarWidth - len(bc.labels[i])) / 2) + k
|
||||
buf.Set(x, y, c)
|
||||
k += w
|
||||
}
|
||||
// plot num
|
||||
ph = 0 //re-initialize previous height
|
||||
for i1 := 0; i1 < bc.numStack; i1++ {
|
||||
h := int(float64(bc.Data[i1][i]) / bc.scale)
|
||||
for j := 0; j < len(bc.dataNum[i1][i]) && h > 0; j++ {
|
||||
c := Cell{
|
||||
Ch: bc.dataNum[i1][i][j],
|
||||
Fg: bc.NumColor[i1],
|
||||
Bg: bc.BarColor[i1],
|
||||
}
|
||||
if bc.BarColor[i1] == ColorDefault { // the same as above
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
if h == 0 {
|
||||
c.Bg = bc.Bg
|
||||
}
|
||||
x := bc.innerArea.Min.X + oftX + (bc.BarWidth-len(bc.dataNum[i1][i]))/2 + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - ph
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
ph += h
|
||||
}
|
||||
}
|
||||
|
||||
if bc.ShowScale {
|
||||
//Currently bar graph only supprts data range from 0 to MAX
|
||||
//Plot 0
|
||||
c := Cell{
|
||||
Ch: '0',
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2
|
||||
x := bc.X
|
||||
buf.Set(x, y, c)
|
||||
|
||||
//Plot the maximum sacle value
|
||||
for i := 0; i < len(bc.maxScale); i++ {
|
||||
c := Cell{
|
||||
Ch: bc.maxScale[i],
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
|
||||
y := bc.innerArea.Min.Y
|
||||
x := bc.X + i
|
||||
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
pages:
|
||||
- Home: 'index.md'
|
||||
- Quickstart: 'quickstart.md'
|
||||
- Recipes: 'recipes.md'
|
||||
- References:
|
||||
- Layouts: 'layouts.md'
|
||||
- Components: 'components.md'
|
||||
- Events: 'events.md'
|
||||
- Themes: 'themes.md'
|
||||
- Versions: 'versions.md'
|
||||
- About: 'about.md'
|
||||
|
||||
site_name: termui
|
||||
repo_url: https://github.com/gizak/termui/
|
||||
site_description: 'termui user guide'
|
||||
site_author: gizak
|
||||
|
||||
docs_dir: '_docs'
|
||||
|
||||
theme: readthedocs
|
||||
|
||||
markdown_extensions:
|
||||
- smarty
|
||||
- admonition
|
||||
- toc
|
||||
|
||||
extra:
|
||||
version: 1.0
|
@ -0,0 +1,73 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
// Par displays a paragraph.
|
||||
/*
|
||||
par := termui.NewPar("Simple Text")
|
||||
par.Height = 3
|
||||
par.Width = 17
|
||||
par.BorderLabel = "Label"
|
||||
*/
|
||||
type Par struct {
|
||||
Block
|
||||
Text string
|
||||
TextFgColor Attribute
|
||||
TextBgColor Attribute
|
||||
WrapLength int // words wrap limit. Note it may not work properly with multi-width char
|
||||
}
|
||||
|
||||
// NewPar returns a new *Par with given text as its content.
|
||||
func NewPar(s string) *Par {
|
||||
return &Par{
|
||||
Block: *NewBlock(),
|
||||
Text: s,
|
||||
TextFgColor: ThemeAttr("par.text.fg"),
|
||||
TextBgColor: ThemeAttr("par.text.bg"),
|
||||
WrapLength: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (p *Par) Buffer() Buffer {
|
||||
buf := p.Block.Buffer()
|
||||
|
||||
fg, bg := p.TextFgColor, p.TextBgColor
|
||||
cs := DefaultTxBuilder.Build(p.Text, fg, bg)
|
||||
|
||||
// wrap if WrapLength set
|
||||
if p.WrapLength < 0 {
|
||||
cs = wrapTx(cs, p.Width-2)
|
||||
} else if p.WrapLength > 0 {
|
||||
cs = wrapTx(cs, p.WrapLength)
|
||||
}
|
||||
|
||||
y, x, n := 0, 0, 0
|
||||
for y < p.innerArea.Dy() && n < len(cs) {
|
||||
w := cs[n].Width()
|
||||
if cs[n].Ch == '\n' || x+w > p.innerArea.Dx() {
|
||||
y++
|
||||
x = 0 // set x = 0
|
||||
if cs[n].Ch == '\n' {
|
||||
n++
|
||||
}
|
||||
|
||||
if y >= p.innerArea.Dy() {
|
||||
buf.Set(p.innerArea.Min.X+p.innerArea.Dx()-1,
|
||||
p.innerArea.Min.Y+p.innerArea.Dy()-1,
|
||||
Cell{Ch: '…', Fg: p.TextFgColor, Bg: p.TextBgColor})
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
buf.Set(p.innerArea.Min.X+x, p.innerArea.Min.Y+y, cs[n])
|
||||
|
||||
n++
|
||||
x += w
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "image"
|
||||
|
||||
// Align is the position of the gauge's label.
|
||||
type Align uint
|
||||
|
||||
// All supported positions.
|
||||
const (
|
||||
AlignNone Align = 0
|
||||
AlignLeft Align = 1 << iota
|
||||
AlignRight
|
||||
AlignBottom
|
||||
AlignTop
|
||||
AlignCenterVertical
|
||||
AlignCenterHorizontal
|
||||
AlignCenter = AlignCenterVertical | AlignCenterHorizontal
|
||||
)
|
||||
|
||||
func AlignArea(parent, child image.Rectangle, a Align) image.Rectangle {
|
||||
w, h := child.Dx(), child.Dy()
|
||||
|
||||
// parent center
|
||||
pcx, pcy := parent.Min.X+parent.Dx()/2, parent.Min.Y+parent.Dy()/2
|
||||
// child center
|
||||
ccx, ccy := child.Min.X+child.Dx()/2, child.Min.Y+child.Dy()/2
|
||||
|
||||
if a&AlignLeft == AlignLeft {
|
||||
child.Min.X = parent.Min.X
|
||||
child.Max.X = child.Min.X + w
|
||||
}
|
||||
|
||||
if a&AlignRight == AlignRight {
|
||||
child.Max.X = parent.Max.X
|
||||
child.Min.X = child.Max.X - w
|
||||
}
|
||||
|
||||
if a&AlignBottom == AlignBottom {
|
||||
child.Max.Y = parent.Max.Y
|
||||
child.Min.Y = child.Max.Y - h
|
||||
}
|
||||
|
||||
if a&AlignTop == AlignRight {
|
||||
child.Min.Y = parent.Min.Y
|
||||
child.Max.Y = child.Min.Y + h
|
||||
}
|
||||
|
||||
if a&AlignCenterHorizontal == AlignCenterHorizontal {
|
||||
child.Min.X += pcx - ccx
|
||||
child.Max.X = child.Min.X + w
|
||||
}
|
||||
|
||||
if a&AlignCenterVertical == AlignCenterVertical {
|
||||
child.Min.Y += pcy - ccy
|
||||
child.Max.Y = child.Min.Y + h
|
||||
}
|
||||
|
||||
return child
|
||||
}
|
||||
|
||||
func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle {
|
||||
a.Min.X += dx
|
||||
a.Max.X += dx
|
||||
a.Min.Y += dy
|
||||
a.Max.Y += dy
|
||||
return a
|
||||
}
|
||||
|
||||
var termWidth int
|
||||
var termHeight int
|
||||
|
||||
func TermRect() image.Rectangle {
|
||||
return image.Rect(0, 0, termWidth, termHeight)
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"image"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
|
||||
"os"
|
||||
|
||||
"runtime/debug"
|
||||
|
||||
"bytes"
|
||||
|
||||
"github.com/maruel/panicparse/stack"
|
||||
tm "github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
// Bufferer should be implemented by all renderable components.
|
||||
type Bufferer interface {
|
||||
Buffer() Buffer
|
||||
}
|
||||
|
||||
// Init initializes termui library. This function should be called before any others.
|
||||
// After initialization, the library must be finalized by 'Close' function.
|
||||
func Init() error {
|
||||
if err := tm.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sysEvtChs = make([]chan Event, 0)
|
||||
go hookTermboxEvt()
|
||||
|
||||
renderJobs = make(chan []Bufferer)
|
||||
//renderLock = new(sync.RWMutex)
|
||||
|
||||
Body = NewGrid()
|
||||
Body.X = 0
|
||||
Body.Y = 0
|
||||
Body.BgColor = ThemeAttr("bg")
|
||||
Body.Width = TermWidth()
|
||||
|
||||
DefaultEvtStream.Init()
|
||||
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
||||
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
||||
DefaultEvtStream.Merge("custom", usrEvtCh)
|
||||
|
||||
DefaultEvtStream.Handle("/", DefualtHandler)
|
||||
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
|
||||
w := e.Data.(EvtWnd)
|
||||
Body.Width = w.Width
|
||||
})
|
||||
|
||||
DefaultWgtMgr = NewWgtMgr()
|
||||
DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook())
|
||||
|
||||
go func() {
|
||||
for bs := range renderJobs {
|
||||
render(bs...)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close finalizes termui library,
|
||||
// should be called after successful initialization when termui's functionality isn't required anymore.
|
||||
func Close() {
|
||||
tm.Close()
|
||||
}
|
||||
|
||||
var renderLock sync.Mutex
|
||||
|
||||
func termSync() {
|
||||
renderLock.Lock()
|
||||
tm.Sync()
|
||||
termWidth, termHeight = tm.Size()
|
||||
renderLock.Unlock()
|
||||
}
|
||||
|
||||
// TermWidth returns the current terminal's width.
|
||||
func TermWidth() int {
|
||||
termSync()
|
||||
return termWidth
|
||||
}
|
||||
|
||||
// TermHeight returns the current terminal's height.
|
||||
func TermHeight() int {
|
||||
termSync()
|
||||
return termHeight
|
||||
}
|
||||
|
||||
// Render renders all Bufferer in the given order from left to right,
|
||||
// right could overlap on left ones.
|
||||
func render(bs ...Bufferer) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
Close()
|
||||
fmt.Fprintf(os.Stderr, "Captured a panic(value=%v) when rendering Bufferer. Exit termui and clean terminal...\nPrint stack trace:\n\n", e)
|
||||
//debug.PrintStack()
|
||||
gs, err := stack.ParseDump(bytes.NewReader(debug.Stack()), os.Stderr)
|
||||
if err != nil {
|
||||
debug.PrintStack()
|
||||
os.Exit(1)
|
||||
}
|
||||
p := &stack.Palette{}
|
||||
buckets := stack.SortBuckets(stack.Bucketize(gs, stack.AnyValue))
|
||||
srcLen, pkgLen := stack.CalcLengths(buckets, false)
|
||||
for _, bucket := range buckets {
|
||||
io.WriteString(os.Stdout, p.BucketHeader(&bucket, false, len(buckets) > 1))
|
||||
io.WriteString(os.Stdout, p.StackLines(&bucket.Signature, srcLen, pkgLen, false))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
for _, b := range bs {
|
||||
|
||||
buf := b.Buffer()
|
||||
// set cels in buf
|
||||
for p, c := range buf.CellMap {
|
||||
if p.In(buf.Area) {
|
||||
|
||||
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
renderLock.Lock()
|
||||
// render
|
||||
tm.Flush()
|
||||
renderLock.Unlock()
|
||||
}
|
||||
|
||||
func Clear() {
|
||||
tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg")))
|
||||
}
|
||||
|
||||
func clearArea(r image.Rectangle, bg Attribute) {
|
||||
for i := r.Min.X; i < r.Max.X; i++ {
|
||||
for j := r.Min.Y; j < r.Max.Y; j++ {
|
||||
tm.SetCell(i, j, ' ', tm.ColorDefault, toTmAttr(bg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ClearArea(r image.Rectangle, bg Attribute) {
|
||||
clearArea(r, bg)
|
||||
tm.Flush()
|
||||
}
|
||||
|
||||
var renderJobs chan []Bufferer
|
||||
|
||||
func Render(bs ...Bufferer) {
|
||||
//go func() { renderJobs <- bs }()
|
||||
renderJobs <- bs
|
||||
}
|
@ -0,0 +1,167 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.
|
||||
/*
|
||||
data := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1}
|
||||
spl := termui.NewSparkline()
|
||||
spl.Data = data
|
||||
spl.Title = "Sparkline 0"
|
||||
spl.LineColor = termui.ColorGreen
|
||||
*/
|
||||
type Sparkline struct {
|
||||
Data []int
|
||||
Height int
|
||||
Title string
|
||||
TitleColor Attribute
|
||||
LineColor Attribute
|
||||
displayHeight int
|
||||
scale float32
|
||||
max int
|
||||
}
|
||||
|
||||
// Sparklines is a renderable widget which groups together the given sparklines.
|
||||
/*
|
||||
spls := termui.NewSparklines(spl0,spl1,spl2) //...
|
||||
spls.Height = 2
|
||||
spls.Width = 20
|
||||
*/
|
||||
type Sparklines struct {
|
||||
Block
|
||||
Lines []Sparkline
|
||||
displayLines int
|
||||
displayWidth int
|
||||
}
|
||||
|
||||
var sparks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
|
||||
|
||||
// Add appends a given Sparkline to s *Sparklines.
|
||||
func (s *Sparklines) Add(sl Sparkline) {
|
||||
s.Lines = append(s.Lines, sl)
|
||||
}
|
||||
|
||||
// NewSparkline returns a unrenderable single sparkline that intended to be added into Sparklines.
|
||||
func NewSparkline() Sparkline {
|
||||
return Sparkline{
|
||||
Height: 1,
|
||||
TitleColor: ThemeAttr("sparkline.title.fg"),
|
||||
LineColor: ThemeAttr("sparkline.line.fg")}
|
||||
}
|
||||
|
||||
// NewSparklines return a new *Spaklines with given Sparkline(s), you can always add a new Sparkline later.
|
||||
func NewSparklines(ss ...Sparkline) *Sparklines {
|
||||
s := &Sparklines{Block: *NewBlock(), Lines: ss}
|
||||
return s
|
||||
}
|
||||
|
||||
func (sl *Sparklines) update() {
|
||||
for i, v := range sl.Lines {
|
||||
if v.Title == "" {
|
||||
sl.Lines[i].displayHeight = v.Height
|
||||
} else {
|
||||
sl.Lines[i].displayHeight = v.Height + 1
|
||||
}
|
||||
}
|
||||
sl.displayWidth = sl.innerArea.Dx()
|
||||
|
||||
// get how many lines gotta display
|
||||
h := 0
|
||||
sl.displayLines = 0
|
||||
for _, v := range sl.Lines {
|
||||
if h+v.displayHeight <= sl.innerArea.Dy() {
|
||||
sl.displayLines++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
h += v.displayHeight
|
||||
}
|
||||
|
||||
for i := 0; i < sl.displayLines; i++ {
|
||||
data := sl.Lines[i].Data
|
||||
|
||||
max := 0
|
||||
for _, v := range data {
|
||||
if max < v {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
sl.Lines[i].max = max
|
||||
if max != 0 {
|
||||
sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max)
|
||||
} else { // when all negative
|
||||
sl.Lines[i].scale = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (sl *Sparklines) Buffer() Buffer {
|
||||
buf := sl.Block.Buffer()
|
||||
sl.update()
|
||||
|
||||
oftY := 0
|
||||
for i := 0; i < sl.displayLines; i++ {
|
||||
l := sl.Lines[i]
|
||||
data := l.Data
|
||||
|
||||
if len(data) > sl.innerArea.Dx() {
|
||||
data = data[len(data)-sl.innerArea.Dx():]
|
||||
}
|
||||
|
||||
if l.Title != "" {
|
||||
rs := trimStr2Runes(l.Title, sl.innerArea.Dx())
|
||||
oftX := 0
|
||||
for _, v := range rs {
|
||||
w := charWidth(v)
|
||||
c := Cell{
|
||||
Ch: v,
|
||||
Fg: l.TitleColor,
|
||||
Bg: sl.Bg,
|
||||
}
|
||||
x := sl.innerArea.Min.X + oftX
|
||||
y := sl.innerArea.Min.Y + oftY
|
||||
buf.Set(x, y, c)
|
||||
oftX += w
|
||||
}
|
||||
}
|
||||
|
||||
for j, v := range data {
|
||||
// display height of the data point, zero when data is negative
|
||||
h := int(float32(v)*l.scale + 0.5)
|
||||
if v < 0 {
|
||||
h = 0
|
||||
}
|
||||
|
||||
barCnt := h / 8
|
||||
barMod := h % 8
|
||||
for jj := 0; jj < barCnt; jj++ {
|
||||
c := Cell{
|
||||
Ch: ' ', // => sparks[7]
|
||||
Bg: l.LineColor,
|
||||
}
|
||||
x := sl.innerArea.Min.X + j
|
||||
y := sl.innerArea.Min.Y + oftY + l.Height - jj
|
||||
|
||||
//p.Bg = sl.BgColor
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
if barMod != 0 {
|
||||
c := Cell{
|
||||
Ch: sparks[barMod-1],
|
||||
Fg: l.LineColor,
|
||||
Bg: sl.Bg,
|
||||
}
|
||||
x := sl.innerArea.Min.X + j
|
||||
y := sl.innerArea.Min.Y + oftY + l.Height - barCnt
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
oftY += l.displayHeight
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
@ -0,0 +1,185 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "strings"
|
||||
|
||||
/* Table is like:
|
||||
|
||||
┌Awesome Table ────────────────────────────────────────────────┐
|
||||
│ Col0 | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 |
|
||||
│──────────────────────────────────────────────────────────────│
|
||||
│ Some Item #1 | AAA | 123 | CCCCC | EEEEE | GGGGG | IIIII |
|
||||
│──────────────────────────────────────────────────────────────│
|
||||
│ Some Item #2 | BBB | 456 | DDDDD | FFFFF | HHHHH | JJJJJ |
|
||||
└──────────────────────────────────────────────────────────────┘
|
||||
|
||||
Datapoints are a two dimensional array of strings: [][]string
|
||||
|
||||
Example:
|
||||
data := [][]string{
|
||||
{"Col0", "Col1", "Col3", "Col4", "Col5", "Col6"},
|
||||
{"Some Item #1", "AAA", "123", "CCCCC", "EEEEE", "GGGGG", "IIIII"},
|
||||
{"Some Item #2", "BBB", "456", "DDDDD", "FFFFF", "HHHHH", "JJJJJ"},
|
||||
}
|
||||
|
||||
table := termui.NewTable()
|
||||
table.Rows = data // type [][]string
|
||||
table.FgColor = termui.ColorWhite
|
||||
table.BgColor = termui.ColorDefault
|
||||
table.Height = 7
|
||||
table.Width = 62
|
||||
table.Y = 0
|
||||
table.X = 0
|
||||
table.Border = true
|
||||
*/
|
||||
|
||||
// Table tracks all the attributes of a Table instance
|
||||
type Table struct {
|
||||
Block
|
||||
Rows [][]string
|
||||
CellWidth []int
|
||||
FgColor Attribute
|
||||
BgColor Attribute
|
||||
FgColors []Attribute
|
||||
BgColors []Attribute
|
||||
Separator bool
|
||||
TextAlign Align
|
||||
}
|
||||
|
||||
// NewTable returns a new Table instance
|
||||
func NewTable() *Table {
|
||||
table := &Table{Block: *NewBlock()}
|
||||
table.FgColor = ColorWhite
|
||||
table.BgColor = ColorDefault
|
||||
table.Separator = true
|
||||
return table
|
||||
}
|
||||
|
||||
// CellsWidth calculates the width of a cell array and returns an int
|
||||
func cellsWidth(cells []Cell) int {
|
||||
width := 0
|
||||
for _, c := range cells {
|
||||
width += c.Width()
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
// Analysis generates and returns an array of []Cell that represent all columns in the Table
|
||||
func (table *Table) Analysis() [][]Cell {
|
||||
var rowCells [][]Cell
|
||||
length := len(table.Rows)
|
||||
if length < 1 {
|
||||
return rowCells
|
||||
}
|
||||
|
||||
if len(table.FgColors) == 0 {
|
||||
table.FgColors = make([]Attribute, len(table.Rows))
|
||||
}
|
||||
if len(table.BgColors) == 0 {
|
||||
table.BgColors = make([]Attribute, len(table.Rows))
|
||||
}
|
||||
|
||||
cellWidths := make([]int, len(table.Rows[0]))
|
||||
|
||||
for y, row := range table.Rows {
|
||||
if table.FgColors[y] == 0 {
|
||||
table.FgColors[y] = table.FgColor
|
||||
}
|
||||
if table.BgColors[y] == 0 {
|
||||
table.BgColors[y] = table.BgColor
|
||||
}
|
||||
for x, str := range row {
|
||||
cells := DefaultTxBuilder.Build(str, table.FgColors[y], table.BgColors[y])
|
||||
cw := cellsWidth(cells)
|
||||
if cellWidths[x] < cw {
|
||||
cellWidths[x] = cw
|
||||
}
|
||||
rowCells = append(rowCells, cells)
|
||||
}
|
||||
}
|
||||
table.CellWidth = cellWidths
|
||||
return rowCells
|
||||
}
|
||||
|
||||
// SetSize calculates the table size and sets the internal value
|
||||
func (table *Table) SetSize() {
|
||||
length := len(table.Rows)
|
||||
if table.Separator {
|
||||
table.Height = length*2 + 1
|
||||
} else {
|
||||
table.Height = length + 2
|
||||
}
|
||||
table.Width = 2
|
||||
if length != 0 {
|
||||
for _, cellWidth := range table.CellWidth {
|
||||
table.Width += cellWidth + 3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CalculatePosition ...
|
||||
func (table *Table) CalculatePosition(x int, y int, coordinateX *int, coordinateY *int, cellStart *int) {
|
||||
if table.Separator {
|
||||
*coordinateY = table.innerArea.Min.Y + y*2
|
||||
} else {
|
||||
*coordinateY = table.innerArea.Min.Y + y
|
||||
}
|
||||
if x == 0 {
|
||||
*cellStart = table.innerArea.Min.X
|
||||
} else {
|
||||
*cellStart += table.CellWidth[x-1] + 3
|
||||
}
|
||||
|
||||
switch table.TextAlign {
|
||||
case AlignRight:
|
||||
*coordinateX = *cellStart + (table.CellWidth[x] - len(table.Rows[y][x])) + 2
|
||||
case AlignCenter:
|
||||
*coordinateX = *cellStart + (table.CellWidth[x]-len(table.Rows[y][x]))/2 + 2
|
||||
default:
|
||||
*coordinateX = *cellStart + 2
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer ...
|
||||
func (table *Table) Buffer() Buffer {
|
||||
buffer := table.Block.Buffer()
|
||||
rowCells := table.Analysis()
|
||||
pointerX := table.innerArea.Min.X + 2
|
||||
pointerY := table.innerArea.Min.Y
|
||||
borderPointerX := table.innerArea.Min.X
|
||||
for y, row := range table.Rows {
|
||||
for x := range row {
|
||||
table.CalculatePosition(x, y, &pointerX, &pointerY, &borderPointerX)
|
||||
background := DefaultTxBuilder.Build(strings.Repeat(" ", table.CellWidth[x]+3), table.BgColors[y], table.BgColors[y])
|
||||
cells := rowCells[y*len(row)+x]
|
||||
for i, back := range background {
|
||||
buffer.Set(borderPointerX+i, pointerY, back)
|
||||
}
|
||||
|
||||
coordinateX := pointerX
|
||||
for _, printer := range cells {
|
||||
buffer.Set(coordinateX, pointerY, printer)
|
||||
coordinateX += printer.Width()
|
||||
}
|
||||
|
||||
if x != 0 {
|
||||
dividors := DefaultTxBuilder.Build("|", table.FgColors[y], table.BgColors[y])
|
||||
for _, dividor := range dividors {
|
||||
buffer.Set(borderPointerX, pointerY, dividor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if table.Separator {
|
||||
border := DefaultTxBuilder.Build(strings.Repeat("─", table.Width-2), table.FgColor, table.BgColor)
|
||||
for i, cell := range border {
|
||||
buffer.Set(i+1, pointerY+1, cell)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buffer
|
||||
}
|
@ -0,0 +1,278 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/go-wordwrap"
|
||||
)
|
||||
|
||||
// TextBuilder is a minimal interface to produce text []Cell using specific syntax (markdown).
|
||||
type TextBuilder interface {
|
||||
Build(s string, fg, bg Attribute) []Cell
|
||||
}
|
||||
|
||||
// DefaultTxBuilder is set to be MarkdownTxBuilder.
|
||||
var DefaultTxBuilder = NewMarkdownTxBuilder()
|
||||
|
||||
// MarkdownTxBuilder implements TextBuilder interface, using markdown syntax.
|
||||
type MarkdownTxBuilder struct {
|
||||
baseFg Attribute
|
||||
baseBg Attribute
|
||||
plainTx []rune
|
||||
markers []marker
|
||||
}
|
||||
|
||||
type marker struct {
|
||||
st int
|
||||
ed int
|
||||
fg Attribute
|
||||
bg Attribute
|
||||
}
|
||||
|
||||
var colorMap = map[string]Attribute{
|
||||
"red": ColorRed,
|
||||
"blue": ColorBlue,
|
||||
"black": ColorBlack,
|
||||
"cyan": ColorCyan,
|
||||
"yellow": ColorYellow,
|
||||
"white": ColorWhite,
|
||||
"default": ColorDefault,
|
||||
"green": ColorGreen,
|
||||
"magenta": ColorMagenta,
|
||||
}
|
||||
|
||||
var attrMap = map[string]Attribute{
|
||||
"bold": AttrBold,
|
||||
"underline": AttrUnderline,
|
||||
"reverse": AttrReverse,
|
||||
}
|
||||
|
||||
func rmSpc(s string) string {
|
||||
reg := regexp.MustCompile(`\s+`)
|
||||
return reg.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
// readAttr translates strings like `fg-red,fg-bold,bg-white` to fg and bg Attribute
|
||||
func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) {
|
||||
fg := mtb.baseFg
|
||||
bg := mtb.baseBg
|
||||
|
||||
updateAttr := func(a Attribute, attrs []string) Attribute {
|
||||
for _, s := range attrs {
|
||||
// replace the color
|
||||
if c, ok := colorMap[s]; ok {
|
||||
a &= 0xFF00 // erase clr 0 ~ 8 bits
|
||||
a |= c // set clr
|
||||
}
|
||||
// add attrs
|
||||
if c, ok := attrMap[s]; ok {
|
||||
a |= c
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
ss := strings.Split(s, ",")
|
||||
fgs := []string{}
|
||||
bgs := []string{}
|
||||
for _, v := range ss {
|
||||
subs := strings.Split(v, "-")
|
||||
if len(subs) > 1 {
|
||||
if subs[0] == "fg" {
|
||||
fgs = append(fgs, subs[1])
|
||||
}
|
||||
if subs[0] == "bg" {
|
||||
bgs = append(bgs, subs[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fg = updateAttr(fg, fgs)
|
||||
bg = updateAttr(bg, bgs)
|
||||
return fg, bg
|
||||
}
|
||||
|
||||
func (mtb *MarkdownTxBuilder) reset() {
|
||||
mtb.plainTx = []rune{}
|
||||
mtb.markers = []marker{}
|
||||
}
|
||||
|
||||
// parse streams and parses text into normalized text and render sequence.
|
||||
func (mtb *MarkdownTxBuilder) parse(str string) {
|
||||
rs := str2runes(str)
|
||||
normTx := []rune{}
|
||||
square := []rune{}
|
||||
brackt := []rune{}
|
||||
accSquare := false
|
||||
accBrackt := false
|
||||
cntSquare := 0
|
||||
|
||||
reset := func() {
|
||||
square = []rune{}
|
||||
brackt = []rune{}
|
||||
accSquare = false
|
||||
accBrackt = false
|
||||
cntSquare = 0
|
||||
}
|
||||
// pipe stacks into normTx and clear
|
||||
rollback := func() {
|
||||
normTx = append(normTx, square...)
|
||||
normTx = append(normTx, brackt...)
|
||||
reset()
|
||||
}
|
||||
// chop first and last
|
||||
chop := func(s []rune) []rune {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
|
||||
for i, r := range rs {
|
||||
switch {
|
||||
// stacking brackt
|
||||
case accBrackt:
|
||||
brackt = append(brackt, r)
|
||||
if ')' == r {
|
||||
fg, bg := mtb.readAttr(string(chop(brackt)))
|
||||
st := len(normTx)
|
||||
ed := len(normTx) + len(square) - 2
|
||||
mtb.markers = append(mtb.markers, marker{st, ed, fg, bg})
|
||||
normTx = append(normTx, chop(square)...)
|
||||
reset()
|
||||
} else if i+1 == len(rs) {
|
||||
rollback()
|
||||
}
|
||||
// stacking square
|
||||
case accSquare:
|
||||
switch {
|
||||
// squares closed and followed by a '('
|
||||
case cntSquare == 0 && '(' == r:
|
||||
accBrackt = true
|
||||
brackt = append(brackt, '(')
|
||||
// squares closed but not followed by a '('
|
||||
case cntSquare == 0:
|
||||
rollback()
|
||||
if '[' == r {
|
||||
accSquare = true
|
||||
cntSquare = 1
|
||||
brackt = append(brackt, '[')
|
||||
} else {
|
||||
normTx = append(normTx, r)
|
||||
}
|
||||
// hit the end
|
||||
case i+1 == len(rs):
|
||||
square = append(square, r)
|
||||
rollback()
|
||||
case '[' == r:
|
||||
cntSquare++
|
||||
square = append(square, '[')
|
||||
case ']' == r:
|
||||
cntSquare--
|
||||
square = append(square, ']')
|
||||
// normal char
|
||||
default:
|
||||
square = append(square, r)
|
||||
}
|
||||
// stacking normTx
|
||||
default:
|
||||
if '[' == r {
|
||||
accSquare = true
|
||||
cntSquare = 1
|
||||
square = append(square, '[')
|
||||
} else {
|
||||
normTx = append(normTx, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mtb.plainTx = normTx
|
||||
}
|
||||
|
||||
func wrapTx(cs []Cell, wl int) []Cell {
|
||||
tmpCell := make([]Cell, len(cs))
|
||||
copy(tmpCell, cs)
|
||||
|
||||
// get the plaintext
|
||||
plain := CellsToStr(cs)
|
||||
|
||||
// wrap
|
||||
plainWrapped := wordwrap.WrapString(plain, uint(wl))
|
||||
|
||||
// find differences and insert
|
||||
finalCell := tmpCell // finalcell will get the inserts and is what is returned
|
||||
|
||||
plainRune := []rune(plain)
|
||||
plainWrappedRune := []rune(plainWrapped)
|
||||
trigger := "go"
|
||||
plainRuneNew := plainRune
|
||||
|
||||
for trigger != "stop" {
|
||||
plainRune = plainRuneNew
|
||||
for i := range plainRune {
|
||||
if plainRune[i] == plainWrappedRune[i] {
|
||||
trigger = "stop"
|
||||
} else if plainRune[i] != plainWrappedRune[i] && plainWrappedRune[i] == 10 {
|
||||
trigger = "go"
|
||||
cell := Cell{10, 0, 0}
|
||||
j := i - 0
|
||||
|
||||
// insert a cell into the []Cell in correct position
|
||||
tmpCell[i] = cell
|
||||
|
||||
// insert the newline into plain so we avoid indexing errors
|
||||
plainRuneNew = append(plainRune, 10)
|
||||
copy(plainRuneNew[j+1:], plainRuneNew[j:])
|
||||
plainRuneNew[j] = plainWrappedRune[j]
|
||||
|
||||
// restart the inner for loop until plain and plain wrapped are
|
||||
// the same; yeah, it's inefficient, but the text amounts
|
||||
// should be small
|
||||
break
|
||||
|
||||
} else if plainRune[i] != plainWrappedRune[i] &&
|
||||
plainWrappedRune[i-1] == 10 && // if the prior rune is a newline
|
||||
plainRune[i] == 32 { // and this rune is a space
|
||||
trigger = "go"
|
||||
// need to delete plainRune[i] because it gets rid of an extra
|
||||
// space
|
||||
plainRuneNew = append(plainRune[:i], plainRune[i+1:]...)
|
||||
break
|
||||
|
||||
} else {
|
||||
trigger = "stop" // stops the outer for loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
finalCell = tmpCell
|
||||
|
||||
return finalCell
|
||||
}
|
||||
|
||||
// Build implements TextBuilder interface.
|
||||
func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell {
|
||||
mtb.baseFg = fg
|
||||
mtb.baseBg = bg
|
||||
mtb.reset()
|
||||
mtb.parse(s)
|
||||
cs := make([]Cell, len(mtb.plainTx))
|
||||
for i := range cs {
|
||||
cs[i] = Cell{Ch: mtb.plainTx[i], Fg: fg, Bg: bg}
|
||||
}
|
||||
for _, mrk := range mtb.markers {
|
||||
for i := mrk.st; i < mrk.ed; i++ {
|
||||
cs[i].Fg = mrk.fg
|
||||
cs[i].Bg = mrk.bg
|
||||
}
|
||||
}
|
||||
|
||||
return cs
|
||||
}
|
||||
|
||||
// NewMarkdownTxBuilder returns a TextBuilder employing markdown syntax.
|
||||
func NewMarkdownTxBuilder() TextBuilder {
|
||||
return MarkdownTxBuilder{}
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import "strings"
|
||||
|
||||
/*
|
||||
// A ColorScheme represents the current look-and-feel of the dashboard.
|
||||
type ColorScheme struct {
|
||||
BodyBg Attribute
|
||||
BlockBg Attribute
|
||||
HasBorder bool
|
||||
BorderFg Attribute
|
||||
BorderBg Attribute
|
||||
BorderLabelTextFg Attribute
|
||||
BorderLabelTextBg Attribute
|
||||
ParTextFg Attribute
|
||||
ParTextBg Attribute
|
||||
SparklineLine Attribute
|
||||
SparklineTitle Attribute
|
||||
GaugeBar Attribute
|
||||
GaugePercent Attribute
|
||||
LineChartLine Attribute
|
||||
LineChartAxes Attribute
|
||||
ListItemFg Attribute
|
||||
ListItemBg Attribute
|
||||
BarChartBar Attribute
|
||||
BarChartText Attribute
|
||||
BarChartNum Attribute
|
||||
MBarChartBar Attribute
|
||||
MBarChartText Attribute
|
||||
MBarChartNum Attribute
|
||||
TabActiveBg Attribute
|
||||
}
|
||||
|
||||
// default color scheme depends on the user's terminal setting.
|
||||
var themeDefault = ColorScheme{HasBorder: true}
|
||||
|
||||
var themeHelloWorld = ColorScheme{
|
||||
BodyBg: ColorBlack,
|
||||
BlockBg: ColorBlack,
|
||||
HasBorder: true,
|
||||
BorderFg: ColorWhite,
|
||||
BorderBg: ColorBlack,
|
||||
BorderLabelTextBg: ColorBlack,
|
||||
BorderLabelTextFg: ColorGreen,
|
||||
ParTextBg: ColorBlack,
|
||||
ParTextFg: ColorWhite,
|
||||
SparklineLine: ColorMagenta,
|
||||
SparklineTitle: ColorWhite,
|
||||
GaugeBar: ColorRed,
|
||||
GaugePercent: ColorWhite,
|
||||
LineChartLine: ColorYellow | AttrBold,
|
||||
LineChartAxes: ColorWhite,
|
||||
ListItemBg: ColorBlack,
|
||||
ListItemFg: ColorYellow,
|
||||
BarChartBar: ColorRed,
|
||||
BarChartNum: ColorWhite,
|
||||
BarChartText: ColorCyan,
|
||||
MBarChartBar: ColorRed,
|
||||
MBarChartNum: ColorWhite,
|
||||
MBarChartText: ColorCyan,
|
||||
TabActiveBg: ColorMagenta,
|
||||
}
|
||||
|
||||
var theme = themeDefault // global dep
|
||||
|
||||
// Theme returns the currently used theme.
|
||||
func Theme() ColorScheme {
|
||||
return theme
|
||||
}
|
||||
|
||||
// SetTheme sets a new, custom theme.
|
||||
func SetTheme(newTheme ColorScheme) {
|
||||
theme = newTheme
|
||||
}
|
||||
|
||||
// UseTheme sets a predefined scheme. Currently available: "hello-world" and
|
||||
// "black-and-white".
|
||||
func UseTheme(th string) {
|
||||
switch th {
|
||||
case "helloworld":
|
||||
theme = themeHelloWorld
|
||||
default:
|
||||
theme = themeDefault
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
var ColorMap = map[string]Attribute{
|
||||
"fg": ColorWhite,
|
||||
"bg": ColorDefault,
|
||||
"border.fg": ColorWhite,
|
||||
"label.fg": ColorGreen,
|
||||
"par.fg": ColorYellow,
|
||||
"par.label.bg": ColorWhite,
|
||||
}
|
||||
|
||||
func ThemeAttr(name string) Attribute {
|
||||
return lookUpAttr(ColorMap, name)
|
||||
}
|
||||
|
||||
func lookUpAttr(clrmap map[string]Attribute, name string) Attribute {
|
||||
|
||||
a, ok := clrmap[name]
|
||||
if ok {
|
||||
return a
|
||||
}
|
||||
|
||||
ns := strings.Split(name, ".")
|
||||
for i := range ns {
|
||||
nn := strings.Join(ns[i:len(ns)], ".")
|
||||
a, ok = ColorMap[nn]
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// 0<=r,g,b <= 5
|
||||
func ColorRGB(r, g, b int) Attribute {
|
||||
within := func(n int) int {
|
||||
if n < 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
if n > 5 {
|
||||
return 5
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
r, b, g = within(r), within(b), within(g)
|
||||
return Attribute(0x0f + 36*r + 6*g + b)
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
package termui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// event mixins
|
||||
type WgtMgr map[string]WgtInfo
|
||||
|
||||
type WgtInfo struct {
|
||||
Handlers map[string]func(Event)
|
||||
WgtRef Widget
|
||||
Id string
|
||||
}
|
||||
|
||||
type Widget interface {
|
||||
Id() string
|
||||
}
|
||||
|
||||
func NewWgtInfo(wgt Widget) WgtInfo {
|
||||
return WgtInfo{
|
||||
Handlers: make(map[string]func(Event)),
|
||||
WgtRef: wgt,
|
||||
Id: wgt.Id(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewWgtMgr() WgtMgr {
|
||||
wm := WgtMgr(make(map[string]WgtInfo))
|
||||
return wm
|
||||
|
||||
}
|
||||
|
||||
func (wm WgtMgr) AddWgt(wgt Widget) {
|
||||
wm[wgt.Id()] = NewWgtInfo(wgt)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgt(wgt Widget) {
|
||||
wm.RmWgtById(wgt.Id())
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgtById(id string) {
|
||||
delete(wm, id)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) AddWgtHandler(id, path string, h func(Event)) {
|
||||
if w, ok := wm[id]; ok {
|
||||
w.Handlers[path] = h
|
||||
}
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgtHandler(id, path string) {
|
||||
if w, ok := wm[id]; ok {
|
||||
delete(w.Handlers, path)
|
||||
}
|
||||
}
|
||||
|
||||
var counter struct {
|
||||
sync.RWMutex
|
||||
count int
|
||||
}
|
||||
|
||||
func GenId() string {
|
||||
counter.Lock()
|
||||
defer counter.Unlock()
|
||||
|
||||
counter.count += 1
|
||||
return fmt.Sprintf("%d", counter.count)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) WgtHandlersHook() func(Event) {
|
||||
return func(e Event) {
|
||||
for _, v := range wm {
|
||||
if k := findMatch(v.Handlers, e.Path); k != "" {
|
||||
v.Handlers[k](e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var DefaultWgtMgr WgtMgr
|
||||
|
||||
func (b *Block) Handle(path string, handler func(Event)) {
|
||||
if _, ok := DefaultWgtMgr[b.Id()]; !ok {
|
||||
DefaultWgtMgr.AddWgt(b)
|
||||
}
|
||||
|
||||
DefaultWgtMgr.AddWgtHandler(b.Id(), path, handler)
|
||||
}
|
@ -0,0 +1 @@
|
||||
*.swp
|
@ -0,0 +1,30 @@
|
||||
# This is the official list of gocui authors for copyright purposes.
|
||||
|
||||
# Names should be added to this file as
|
||||
# Name or Organization <email address> contribution
|
||||
# Contribution
|
||||
# The email address is not required for organizations.
|
||||
|
||||
Roi Martin <jroi.martin@gmail.com>
|
||||
Main developer
|
||||
|
||||
Ryan Sullivan <kayoticsully@gmail.com>
|
||||
Toggleable view frames
|
||||
|
||||
Matthieu Rakotojaona <matthieu.rakotojaona@gmail.com>
|
||||
Wrapped views
|
||||
|
||||
Harry Lawrence <hazbo@gmx.com>
|
||||
Basic mouse support
|
||||
|
||||
Danny Tylman <dtylman@gmail.com>
|
||||
Masked views
|
||||
|
||||
Frederik Deweerdt <frederik.deweerdt@gmail.com>
|
||||
Colored fonts
|
||||
|
||||
Henri Koski <henri.t.koski@gmail.com>
|
||||
Custom current view color
|
||||
|
||||
Dustin Willis Webber <dustin.webber@gmail.com>
|
||||
256-colors output mode support
|
@ -0,0 +1,23 @@
|
||||
Copyright (c) 2014 The gocui Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the gocui Authors nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1,105 @@
|
||||
# GOCUI - Go Console User Interface
|
||||
|
||||
[![GoDoc](https://godoc.org/github.com/jroimartin/gocui?status.svg)](https://godoc.org/github.com/jroimartin/gocui)
|
||||
|
||||
Minimalist Go package aimed at creating Console User Interfaces.
|
||||
|
||||
## Features
|
||||
|
||||
* Minimalist API.
|
||||
* Views (the "windows" in the GUI) implement the interface io.ReadWriter.
|
||||
* Support for overlapping views.
|
||||
* The GUI can be modified at runtime (concurrent-safe).
|
||||
* Global and view-level keybindings.
|
||||
* Mouse support.
|
||||
* Colored text.
|
||||
* Customizable edition mode.
|
||||
* Easy to build reusable widgets, complex layouts...
|
||||
|
||||
## Installation
|
||||
|
||||
Execute:
|
||||
|
||||
```
|
||||
$ go get github.com/jroimartin/gocui
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Execute:
|
||||
|
||||
```
|
||||
$ go doc github.com/jroimartin/gocui
|
||||
```
|
||||
|
||||
Or visit [godoc.org](https://godoc.org/github.com/jroimartin/gocui) to read it
|
||||
online.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/jroimartin/gocui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
defer g.Close()
|
||||
|
||||
g.SetManagerFunc(layout)
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
|
||||
func layout(g *gocui.Gui) error {
|
||||
maxX, maxY := g.Size()
|
||||
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!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func quit(g *gocui.Gui, v *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
```
|
||||
|
||||
## Screenshots
|
||||
|
||||
![r2cui](https://cloud.githubusercontent.com/assets/1223476/19418932/63645052-93ce-11e6-867c-da5e97e37237.png)
|
||||
|
||||
![_examples/demo.go](https://cloud.githubusercontent.com/assets/1223476/5992750/720b84f0-aa36-11e4-88ec-296fa3247b52.png)
|
||||
|
||||
![_examples/dynamic.go](https://cloud.githubusercontent.com/assets/1223476/5992751/76ad5cc2-aa36-11e4-8204-6a90269db827.png)
|
||||
|
||||
## Projects using gocui
|
||||
|
||||
* [komanda-cli](https://github.com/mephux/komanda-cli): IRC Client For Developers.
|
||||
* [vuls](https://github.com/future-architect/vuls): Agentless vulnerability scanner for Linux/FreeBSD.
|
||||
* [wuzz](https://github.com/asciimoo/wuzz): Interactive cli tool for HTTP inspection.
|
||||
* [httplab](https://github.com/gchaincl/httplab): Interactive web server.
|
||||
* [domainr](https://github.com/MichaelThessel/domainr): Tool that checks the availability of domains based on keywords.
|
||||
* [gotime](https://github.com/nanohard/gotime): Time tracker for projects and tasks.
|
||||
* [claws](https://github.com/thehowl/claws): Interactive command line client for testing websockets.
|
||||
* [terminews](http://github.com/antavelos/terminews): Terminal based RSS reader.
|
||||
* [diagram](https://github.com/esimov/diagram): Tool to convert ascii arts into hand drawn diagrams.
|
||||
* [pody](https://github.com/JulienBreux/pody): CLI app to manage Pods in a Kubernetes cluster.
|
||||
|
||||
Note: if your project is not listed here, let us know! :)
|
@ -0,0 +1,32 @@
|
||||
// Copyright 2014 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/nsf/termbox-go"
|
||||
|
||||
// Attribute represents a terminal attribute, like color, font style, etc. They
|
||||
// can be combined using bitwise OR (|). Note that it is not possible to
|
||||
// combine multiple color attributes.
|
||||
type Attribute termbox.Attribute
|
||||
|
||||
// Color attributes.
|
||||
const (
|
||||
ColorDefault Attribute = Attribute(termbox.ColorDefault)
|
||||
ColorBlack = Attribute(termbox.ColorBlack)
|
||||
ColorRed = Attribute(termbox.ColorRed)
|
||||
ColorGreen = Attribute(termbox.ColorGreen)
|
||||
ColorYellow = Attribute(termbox.ColorYellow)
|
||||
ColorBlue = Attribute(termbox.ColorBlue)
|
||||
ColorMagenta = Attribute(termbox.ColorMagenta)
|
||||
ColorCyan = Attribute(termbox.ColorCyan)
|
||||
ColorWhite = Attribute(termbox.ColorWhite)
|
||||
)
|
||||
|
||||
// Text style attributes.
|
||||
const (
|
||||
AttrBold Attribute = Attribute(termbox.AttrBold)
|
||||
AttrUnderline = Attribute(termbox.AttrUnderline)
|
||||
AttrReverse = Attribute(termbox.AttrReverse)
|
||||
)
|
@ -0,0 +1,118 @@
|
||||
// Copyright 2014 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 allows to create console user interfaces.
|
||||
|
||||
Create a new GUI:
|
||||
|
||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
defer g.Close()
|
||||
|
||||
// Set GUI managers and key bindings
|
||||
// ...
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
// handle error
|
||||
}
|
||||
|
||||
Set GUI managers:
|
||||
|
||||
g.SetManager(mgr1, mgr2)
|
||||
|
||||
Managers are in charge of GUI's layout and can be used to build widgets. On
|
||||
each iteration of the GUI's main loop, the Layout function of each configured
|
||||
manager is executed. Managers are used to set-up and update the application's
|
||||
main views, being possible to freely change them during execution. Also, it is
|
||||
important to mention that a main loop iteration is executed on each reported
|
||||
event (key-press, mouse event, window resize, etc).
|
||||
|
||||
GUIs are composed by Views, you can think of it as buffers. Views implement the
|
||||
io.ReadWriter interface, so you can just write to them if you want to modify
|
||||
their content. The same is valid for reading.
|
||||
|
||||
Create and initialize a view with absolute coordinates:
|
||||
|
||||
if v, err := g.SetView("viewname", 2, 2, 22, 7); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
// handle error
|
||||
}
|
||||
fmt.Fprintln(v, "This is a new view")
|
||||
// ...
|
||||
}
|
||||
|
||||
Views can also be created using relative coordinates:
|
||||
|
||||
maxX, maxY := g.Size()
|
||||
if v, err := g.SetView("viewname", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil {
|
||||
// ...
|
||||
}
|
||||
|
||||
Configure keybindings:
|
||||
|
||||
if err := g.SetKeybinding("viewname", gocui.KeyEnter, gocui.ModNone, fcn); err != nil {
|
||||
// handle error
|
||||
}
|
||||
|
||||
gocui implements full mouse support that can be enabled with:
|
||||
|
||||
g.Mouse = true
|
||||
|
||||
Mouse events are handled like any other keybinding:
|
||||
|
||||
if err := g.SetKeybinding("viewname", gocui.MouseLeft, gocui.ModNone, fcn); err != nil {
|
||||
// handle error
|
||||
}
|
||||
|
||||
IMPORTANT: Views can only be created, destroyed or updated in three ways: from
|
||||
the Layout function within managers, from keybinding callbacks or via
|
||||
*Gui.Update(). The reason for this is that it allows gocui to be
|
||||
concurrent-safe. So, if you want to update your GUI from a goroutine, you must
|
||||
use *Gui.Update(). For example:
|
||||
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
v, err := g.View("viewname")
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
v.Clear()
|
||||
fmt.Fprintln(v, "Writing from different goroutines")
|
||||
return nil
|
||||
})
|
||||
|
||||
By default, gocui provides a basic edition mode. This mode can be extended
|
||||
and customized creating a new Editor and assigning it to *View.Editor:
|
||||
|
||||
type Editor interface {
|
||||
Edit(v *View, key Key, ch rune, mod Modifier)
|
||||
}
|
||||
|
||||
DefaultEditor can be taken as example to create your own custom Editor:
|
||||
|
||||
var DefaultEditor Editor = EditorFunc(simpleEditor)
|
||||
|
||||
func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
|
||||
switch {
|
||||
case ch != 0 && mod == 0:
|
||||
v.EditWrite(ch)
|
||||
case key == KeySpace:
|
||||
v.EditWrite(' ')
|
||||
case key == KeyBackspace || key == KeyBackspace2:
|
||||
v.EditDelete(true)
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
Colored text:
|
||||
|
||||
Views allow to add colored text using ANSI colors. For example:
|
||||
|
||||
fmt.Fprintln(v, "\x1b[0;31mHello world")
|
||||
|
||||
For more information, see the examples in folder "_examples/".
|
||||
*/
|
||||
package gocui
|
@ -0,0 +1,344 @@
|
||||
// Copyright 2014 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 "errors"
|
||||
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
// Editor interface must be satisfied by gocui editors.
|
||||
type Editor interface {
|
||||
Edit(v *View, key Key, ch rune, mod Modifier)
|
||||
}
|
||||
|
||||
// The EditorFunc type is an adapter to allow the use of ordinary functions as
|
||||
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
|
||||
// is an Editor object that calls f.
|
||||
type EditorFunc func(v *View, key Key, ch rune, mod Modifier)
|
||||
|
||||
// Edit calls f(v, key, ch, mod)
|
||||
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) {
|
||||
f(v, key, ch, mod)
|
||||
}
|
||||
|
||||
// DefaultEditor is the default editor.
|
||||
var DefaultEditor Editor = EditorFunc(simpleEditor)
|
||||
|
||||
// simpleEditor is used as the default gocui editor.
|
||||
func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
|
||||
switch {
|
||||
case ch != 0 && mod == 0:
|
||||
v.EditWrite(ch)
|
||||
case key == KeySpace:
|
||||
v.EditWrite(' ')
|
||||
case key == KeyBackspace || key == KeyBackspace2:
|
||||
v.EditDelete(true)
|
||||
case key == KeyDelete:
|
||||
v.EditDelete(false)
|
||||
case key == KeyInsert:
|
||||
v.Overwrite = !v.Overwrite
|
||||
case key == KeyEnter:
|
||||
v.EditNewLine()
|
||||
case key == KeyArrowDown:
|
||||
v.MoveCursor(0, 1, false)
|
||||
case key == KeyArrowUp:
|
||||
v.MoveCursor(0, -1, false)
|
||||
case key == KeyArrowLeft:
|
||||
v.MoveCursor(-1, 0, false)
|
||||
case key == KeyArrowRight:
|
||||
v.MoveCursor(1, 0, false)
|
||||
}
|
||||
}
|
||||
|
||||
// EditWrite writes a rune at the cursor position.
|
||||
func (v *View) EditWrite(ch rune) {
|
||||
v.writeRune(v.cx, v.cy, ch)
|
||||
v.MoveCursor(1, 0, true)
|
||||
}
|
||||
|
||||
// EditDelete deletes a rune at the cursor position. back determines the
|
||||
// direction.
|
||||
func (v *View) EditDelete(back bool) {
|
||||
x, y := v.ox+v.cx, v.oy+v.cy
|
||||
if y < 0 {
|
||||
return
|
||||
} else if y >= len(v.viewLines) {
|
||||
v.MoveCursor(-1, 0, true)
|
||||
return
|
||||
}
|
||||
|
||||
maxX, _ := v.Size()
|
||||
if back {
|
||||
if x == 0 { // start of the line
|
||||
if y < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
var maxPrevWidth int
|
||||
if v.Wrap {
|
||||
maxPrevWidth = maxX
|
||||
} else {
|
||||
maxPrevWidth = maxInt
|
||||
}
|
||||
|
||||
if v.viewLines[y].linesX == 0 { // regular line
|
||||
v.mergeLines(v.cy - 1)
|
||||
if len(v.viewLines[y-1].line) < maxPrevWidth {
|
||||
v.MoveCursor(-1, 0, true)
|
||||
}
|
||||
} else { // wrapped line
|
||||
v.deleteRune(len(v.viewLines[y-1].line)-1, v.cy-1)
|
||||
v.MoveCursor(-1, 0, true)
|
||||
}
|
||||
} else { // middle/end of the line
|
||||
v.deleteRune(v.cx-1, v.cy)
|
||||
v.MoveCursor(-1, 0, true)
|
||||
}
|
||||
} else {
|
||||
if x == len(v.viewLines[y].line) { // end of the line
|
||||
v.mergeLines(v.cy)
|
||||
} else { // start/middle of the line
|
||||
v.deleteRune(v.cx, v.cy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EditNewLine inserts a new line under the cursor.
|
||||
func (v *View) EditNewLine() {
|
||||
v.breakLine(v.cx, v.cy)
|
||||
v.ox = 0
|
||||
v.cx = 0
|
||||
v.MoveCursor(0, 1, true)
|
||||
}
|
||||
|
||||
// MoveCursor moves the cursor taking into account the width of the line/view,
|
||||
// displacing the origin if necessary.
|
||||
func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
||||
maxX, maxY := v.Size()
|
||||
cx, cy := v.cx+dx, v.cy+dy
|
||||
x, y := v.ox+cx, v.oy+cy
|
||||
|
||||
var curLineWidth, prevLineWidth int
|
||||
// get the width of the current line
|
||||
if writeMode {
|
||||
if v.Wrap {
|
||||
curLineWidth = maxX - 1
|
||||
} else {
|
||||
curLineWidth = maxInt
|
||||
}
|
||||
} else {
|
||||
if y >= 0 && y < len(v.viewLines) {
|
||||
curLineWidth = len(v.viewLines[y].line)
|
||||
if v.Wrap && curLineWidth >= maxX {
|
||||
curLineWidth = maxX - 1
|
||||
}
|
||||
} else {
|
||||
curLineWidth = 0
|
||||
}
|
||||
}
|
||||
// get the width of the previous line
|
||||
if y-1 >= 0 && y-1 < len(v.viewLines) {
|
||||
prevLineWidth = len(v.viewLines[y-1].line)
|
||||
} else {
|
||||
prevLineWidth = 0
|
||||
}
|
||||
|
||||
// adjust cursor's x position and view's x origin
|
||||
if x > curLineWidth { // move to next line
|
||||
if dx > 0 { // horizontal movement
|
||||
cy++
|
||||
if writeMode || v.oy+cy < len(v.viewLines) {
|
||||
if !v.Wrap {
|
||||
v.ox = 0
|
||||
}
|
||||
v.cx = 0
|
||||
}
|
||||
} else { // vertical movement
|
||||
if curLineWidth > 0 { // move cursor to the EOL
|
||||
if v.Wrap {
|
||||
v.cx = curLineWidth
|
||||
} else {
|
||||
ncx := curLineWidth - v.ox
|
||||
if ncx < 0 {
|
||||
v.ox += ncx
|
||||
if v.ox < 0 {
|
||||
v.ox = 0
|
||||
}
|
||||
v.cx = 0
|
||||
} else {
|
||||
v.cx = ncx
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if writeMode || v.oy+cy < len(v.viewLines) {
|
||||
if !v.Wrap {
|
||||
v.ox = 0
|
||||
}
|
||||
v.cx = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if cx < 0 {
|
||||
if !v.Wrap && v.ox > 0 { // move origin to the left
|
||||
v.ox += cx
|
||||
v.cx = 0
|
||||
} else { // move to previous line
|
||||
cy--
|
||||
if prevLineWidth > 0 {
|
||||
if !v.Wrap { // set origin so the EOL is visible
|
||||
nox := prevLineWidth - maxX + 1
|
||||
if nox < 0 {
|
||||
v.ox = 0
|
||||
} else {
|
||||
v.ox = nox
|
||||
}
|
||||
}
|
||||
v.cx = prevLineWidth
|
||||
} else {
|
||||
if !v.Wrap {
|
||||
v.ox = 0
|
||||
}
|
||||
v.cx = 0
|
||||
}
|
||||
}
|
||||
} else { // stay on the same line
|
||||
if v.Wrap {
|
||||
v.cx = cx
|
||||
} else {
|
||||
if cx >= maxX {
|
||||
v.ox += cx - maxX + 1
|
||||
v.cx = maxX
|
||||
} else {
|
||||
v.cx = cx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adjust cursor's y position and view's y origin
|
||||
if cy < 0 {
|
||||
if v.oy > 0 {
|
||||
v.oy--
|
||||
}
|
||||
} else if writeMode || v.oy+cy < len(v.viewLines) {
|
||||
if cy >= maxY {
|
||||
v.oy++
|
||||
} else {
|
||||
v.cy = cy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writeRune writes a rune into the view's internal buffer, at the
|
||||
// position corresponding to the point (x, y). The length of the internal
|
||||
// buffer is increased if the point is out of bounds. Overwrite mode is
|
||||
// governed by the value of View.overwrite.
|
||||
func (v *View) writeRune(x, y int, ch rune) error {
|
||||
v.tainted = true
|
||||
|
||||
x, y, err := v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if x < 0 || y < 0 {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
|
||||
if y >= len(v.lines) {
|
||||
s := make([][]cell, y-len(v.lines)+1)
|
||||
v.lines = append(v.lines, s...)
|
||||
}
|
||||
|
||||
olen := len(v.lines[y])
|
||||
|
||||
var s []cell
|
||||
if x >= len(v.lines[y]) {
|
||||
s = make([]cell, x-len(v.lines[y])+1)
|
||||
} else if !v.Overwrite {
|
||||
s = make([]cell, 1)
|
||||
}
|
||||
v.lines[y] = append(v.lines[y], s...)
|
||||
|
||||
if !v.Overwrite || (v.Overwrite && x >= olen-1) {
|
||||
copy(v.lines[y][x+1:], v.lines[y][x:])
|
||||
}
|
||||
v.lines[y][x] = cell{
|
||||
fgColor: v.FgColor,
|
||||
bgColor: v.BgColor,
|
||||
chr: ch,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteRune removes a rune from the view's internal buffer, at the
|
||||
// position corresponding to the point (x, y).
|
||||
func (v *View) deleteRune(x, y int) error {
|
||||
v.tainted = true
|
||||
|
||||
x, y, err := v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
v.lines[y] = append(v.lines[y][:x], v.lines[y][x+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// mergeLines merges the lines "y" and "y+1" if possible.
|
||||
func (v *View) mergeLines(y int) error {
|
||||
v.tainted = true
|
||||
|
||||
_, y, err := v.realPosition(0, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if y < 0 || y >= len(v.lines) {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
|
||||
if y < len(v.lines)-1 { // otherwise we don't need to merge anything
|
||||
v.lines[y] = append(v.lines[y], v.lines[y+1]...)
|
||||
v.lines = append(v.lines[:y+1], v.lines[y+2:]...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// breakLine breaks a line of the internal buffer at the position corresponding
|
||||
// to the point (x, y).
|
||||
func (v *View) breakLine(x, y int) error {
|
||||
v.tainted = true
|
||||
|
||||
x, y, err := v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if y < 0 || y >= len(v.lines) {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
|
||||
var left, right []cell
|
||||
if x < len(v.lines[y]) { // break line
|
||||
left = make([]cell, len(v.lines[y][:x]))
|
||||
copy(left, v.lines[y][:x])
|
||||
right = make([]cell, len(v.lines[y][x:]))
|
||||
copy(right, v.lines[y][x:])
|
||||
} else { // new empty line
|
||||
left = v.lines[y]
|
||||
}
|
||||
|
||||
lines := make([][]cell, len(v.lines)+1)
|
||||
lines[y] = left
|
||||
lines[y+1] = right
|
||||
copy(lines, v.lines[:y])
|
||||
copy(lines[y+2:], v.lines[y+1:])
|
||||
v.lines = lines
|
||||
return nil
|
||||
}
|
@ -0,0 +1,229 @@
|
||||
// Copyright 2014 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 (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type escapeInterpreter struct {
|
||||
state escapeState
|
||||
curch rune
|
||||
csiParam []string
|
||||
curFgColor, curBgColor Attribute
|
||||
mode OutputMode
|
||||
}
|
||||
|
||||
type escapeState int
|
||||
|
||||
const (
|
||||
stateNone escapeState = iota
|
||||
stateEscape
|
||||
stateCSI
|
||||
stateParams
|
||||
)
|
||||
|
||||
var (
|
||||
errNotCSI = errors.New("Not a CSI escape sequence")
|
||||
errCSIParseError = errors.New("CSI escape sequence parsing error")
|
||||
errCSITooLong = errors.New("CSI escape sequence is too long")
|
||||
)
|
||||
|
||||
// runes in case of error will output the non-parsed runes as a string.
|
||||
func (ei *escapeInterpreter) runes() []rune {
|
||||
switch ei.state {
|
||||
case stateNone:
|
||||
return []rune{0x1b}
|
||||
case stateEscape:
|
||||
return []rune{0x1b, ei.curch}
|
||||
case stateCSI:
|
||||
return []rune{0x1b, '[', ei.curch}
|
||||
case stateParams:
|
||||
ret := []rune{0x1b, '['}
|
||||
for _, s := range ei.csiParam {
|
||||
ret = append(ret, []rune(s)...)
|
||||
ret = append(ret, ';')
|
||||
}
|
||||
return append(ret, ei.curch)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// newEscapeInterpreter returns an escapeInterpreter that will be able to parse
|
||||
// terminal escape sequences.
|
||||
func newEscapeInterpreter(mode OutputMode) *escapeInterpreter {
|
||||
ei := &escapeInterpreter{
|
||||
state: stateNone,
|
||||
curFgColor: ColorDefault,
|
||||
curBgColor: ColorDefault,
|
||||
mode: mode,
|
||||
}
|
||||
return ei
|
||||
}
|
||||
|
||||
// reset sets the escapeInterpreter in initial state.
|
||||
func (ei *escapeInterpreter) reset() {
|
||||
ei.state = stateNone
|
||||
ei.curFgColor = ColorDefault
|
||||
ei.curBgColor = ColorDefault
|
||||
ei.csiParam = nil
|
||||
}
|
||||
|
||||
// parseOne parses a rune. If isEscape is true, it means that the rune is part
|
||||
// of an escape sequence, and as such should not be printed verbatim. Otherwise,
|
||||
// it's not an escape sequence.
|
||||
func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) {
|
||||
// Sanity checks
|
||||
if len(ei.csiParam) > 20 {
|
||||
return false, errCSITooLong
|
||||
}
|
||||
if len(ei.csiParam) > 0 && len(ei.csiParam[len(ei.csiParam)-1]) > 255 {
|
||||
return false, errCSITooLong
|
||||
}
|
||||
|
||||
ei.curch = ch
|
||||
|
||||
switch ei.state {
|
||||
case stateNone:
|
||||
if ch == 0x1b {
|
||||
ei.state = stateEscape
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
case stateEscape:
|
||||
if ch == '[' {
|
||||
ei.state = stateCSI
|
||||
return true, nil
|
||||
}
|
||||
return false, errNotCSI
|
||||
case stateCSI:
|
||||
switch {
|
||||
case ch >= '0' && ch <= '9':
|
||||
ei.csiParam = append(ei.csiParam, "")
|
||||
case ch == 'm':
|
||||
ei.csiParam = append(ei.csiParam, "0")
|
||||
default:
|
||||
return false, errCSIParseError
|
||||
}
|
||||
ei.state = stateParams
|
||||
fallthrough
|
||||
case stateParams:
|
||||
switch {
|
||||
case ch >= '0' && ch <= '9':
|
||||
ei.csiParam[len(ei.csiParam)-1] += string(ch)
|
||||
return true, nil
|
||||
case ch == ';':
|
||||
ei.csiParam = append(ei.csiParam, "")
|
||||
return true, nil
|
||||
case ch == 'm':
|
||||
var err error
|
||||
switch ei.mode {
|
||||
case OutputNormal:
|
||||
err = ei.outputNormal()
|
||||
case Output256:
|
||||
err = ei.output256()
|
||||
}
|
||||
if err != nil {
|
||||
return false, errCSIParseError
|
||||
}
|
||||
|
||||
ei.state = stateNone
|
||||
ei.csiParam = nil
|
||||
return true, nil
|
||||
default:
|
||||
return false, errCSIParseError
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// outputNormal provides 8 different colors:
|
||||
// black, red, green, yellow, blue, magenta, cyan, white
|
||||
func (ei *escapeInterpreter) outputNormal() error {
|
||||
for _, param := range ei.csiParam {
|
||||
p, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return errCSIParseError
|
||||
}
|
||||
|
||||
switch {
|
||||
case p >= 30 && p <= 37:
|
||||
ei.curFgColor = Attribute(p - 30 + 1)
|
||||
case p == 39:
|
||||
ei.curFgColor = ColorDefault
|
||||
case p >= 40 && p <= 47:
|
||||
ei.curBgColor = Attribute(p - 40 + 1)
|
||||
case p == 49:
|
||||
ei.curBgColor = ColorDefault
|
||||
case p == 1:
|
||||
ei.curFgColor |= AttrBold
|
||||
case p == 4:
|
||||
ei.curFgColor |= AttrUnderline
|
||||
case p == 7:
|
||||
ei.curFgColor |= AttrReverse
|
||||
case p == 0:
|
||||
ei.curFgColor = ColorDefault
|
||||
ei.curBgColor = ColorDefault
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// output256 allows you to leverage the 256-colors terminal mode:
|
||||
// 0x01 - 0x08: the 8 colors as in OutputNormal
|
||||
// 0x09 - 0x10: Color* | AttrBold
|
||||
// 0x11 - 0xe8: 216 different colors
|
||||
// 0xe9 - 0x1ff: 24 different shades of grey
|
||||
func (ei *escapeInterpreter) output256() error {
|
||||
if len(ei.csiParam) < 3 {
|
||||
return ei.outputNormal()
|
||||
}
|
||||
|
||||
mode, err := strconv.Atoi(ei.csiParam[1])
|
||||
if err != nil {
|
||||
return errCSIParseError
|
||||
}
|
||||
if mode != 5 {
|
||||
return ei.outputNormal()
|
||||
}
|
||||
|
||||
fgbg, err := strconv.Atoi(ei.csiParam[0])
|
||||
if err != nil {
|
||||
return errCSIParseError
|
||||
}
|
||||
color, err := strconv.Atoi(ei.csiParam[2])
|
||||
if err != nil {
|
||||
return errCSIParseError
|
||||
}
|
||||
|
||||
switch fgbg {
|
||||
case 38:
|
||||
ei.curFgColor = Attribute(color + 1)
|
||||
|
||||
for _, param := range ei.csiParam[3:] {
|
||||
p, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return errCSIParseError
|
||||
}
|
||||
|
||||
switch {
|
||||
case p == 1:
|
||||
ei.curFgColor |= AttrBold
|
||||
case p == 4:
|
||||
ei.curFgColor |= AttrUnderline
|
||||
case p == 7:
|
||||
ei.curFgColor |= AttrReverse
|
||||
}
|
||||
}
|
||||
case 48:
|
||||
ei.curBgColor = Attribute(color + 1)
|
||||
default:
|
||||
return errCSIParseError
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,636 @@
|
||||
// Copyright 2014 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 (
|
||||
"errors"
|
||||
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrQuit is used to decide if the MainLoop finished successfully.
|
||||
ErrQuit = errors.New("quit")
|
||||
|
||||
// ErrUnknownView allows to assert if a View must be initialized.
|
||||
ErrUnknownView = errors.New("unknown view")
|
||||
)
|
||||
|
||||
// OutputMode represents the terminal's output mode (8 or 256 colors).
|
||||
type OutputMode termbox.OutputMode
|
||||
|
||||
const (
|
||||
// OutputNormal provides 8-colors terminal mode.
|
||||
OutputNormal = OutputMode(termbox.OutputNormal)
|
||||
|
||||
// Output256 provides 256-colors terminal mode.
|
||||
Output256 = OutputMode(termbox.Output256)
|
||||
)
|
||||
|
||||
// Gui represents the whole User Interface, including the views, layouts
|
||||
// and keybindings.
|
||||
type Gui struct {
|
||||
tbEvents chan termbox.Event
|
||||
userEvents chan userEvent
|
||||
views []*View
|
||||
currentView *View
|
||||
managers []Manager
|
||||
keybindings []*keybinding
|
||||
maxX, maxY int
|
||||
outputMode OutputMode
|
||||
|
||||
// BgColor and FgColor allow to configure the background and foreground
|
||||
// colors of the GUI.
|
||||
BgColor, FgColor Attribute
|
||||
|
||||
// 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
|
||||
|
||||
// If Cursor is true then the cursor is enabled.
|
||||
Cursor bool
|
||||
|
||||
// If Mouse is true then mouse events will be enabled.
|
||||
Mouse bool
|
||||
|
||||
// If InputEsc is true, when ESC sequence is in the buffer and it doesn't
|
||||
// match any known sequence, ESC means KeyEsc.
|
||||
InputEsc bool
|
||||
|
||||
// If ASCII is true then use ASCII instead of unicode to draw the
|
||||
// interface. Using ASCII is more portable.
|
||||
ASCII bool
|
||||
}
|
||||
|
||||
// NewGui returns a new Gui object with a given output mode.
|
||||
func NewGui(mode OutputMode) (*Gui, error) {
|
||||
if err := termbox.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g := &Gui{}
|
||||
|
||||
g.outputMode = mode
|
||||
termbox.SetOutputMode(termbox.OutputMode(mode))
|
||||
|
||||
g.tbEvents = make(chan termbox.Event, 20)
|
||||
g.userEvents = make(chan userEvent, 20)
|
||||
|
||||
g.maxX, g.maxY = termbox.Size()
|
||||
|
||||
g.BgColor, g.FgColor = ColorDefault, ColorDefault
|
||||
g.SelBgColor, g.SelFgColor = ColorDefault, ColorDefault
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// Close finalizes the library. It should be called after a successful
|
||||
// initialization and when gocui is not needed anymore.
|
||||
func (g *Gui) Close() {
|
||||
termbox.Close()
|
||||
}
|
||||
|
||||
// Size returns the terminal's size.
|
||||
func (g *Gui) Size() (x, y int) {
|
||||
return g.maxX, g.maxY
|
||||
}
|
||||
|
||||
// SetRune writes a rune at the given point, relative to the top-left
|
||||
// corner of the terminal. It checks if the position is valid and applies
|
||||
// the given colors.
|
||||
func (g *Gui) SetRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
|
||||
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
termbox.SetCell(x, y, ch, termbox.Attribute(fgColor), termbox.Attribute(bgColor))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rune returns the rune contained in the cell at the given position.
|
||||
// It checks if the position is valid.
|
||||
func (g *Gui) Rune(x, y int) (rune, error) {
|
||||
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
|
||||
return ' ', errors.New("invalid point")
|
||||
}
|
||||
c := termbox.CellBuffer()[y*g.maxX+x]
|
||||
return c.Ch, nil
|
||||
}
|
||||
|
||||
// SetView creates a new view with its top-left corner at (x0, y0)
|
||||
// and the bottom-right one at (x1, y1). If a view with the same name
|
||||
// already exists, its dimensions are updated; otherwise, the error
|
||||
// ErrUnknownView is returned, which allows to assert if the View must
|
||||
// be initialized. It checks if the position is valid.
|
||||
func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error) {
|
||||
if x0 >= x1 || y0 >= y1 {
|
||||
return nil, errors.New("invalid dimensions")
|
||||
}
|
||||
if name == "" {
|
||||
return nil, errors.New("invalid name")
|
||||
}
|
||||
|
||||
if v, err := g.View(name); err == nil {
|
||||
v.x0 = x0
|
||||
v.y0 = y0
|
||||
v.x1 = x1
|
||||
v.y1 = y1
|
||||
v.tainted = true
|
||||
return v, nil
|
||||
}
|
||||
|
||||
v := newView(name, x0, y0, x1, y1, g.outputMode)
|
||||
v.BgColor, v.FgColor = g.BgColor, g.FgColor
|
||||
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
|
||||
g.views = append(g.views, v)
|
||||
return v, ErrUnknownView
|
||||
}
|
||||
|
||||
// SetViewOnTop sets the given view on top of the existing ones.
|
||||
func (g *Gui) SetViewOnTop(name string) (*View, error) {
|
||||
for i, v := range g.views {
|
||||
if v.name == name {
|
||||
s := append(g.views[:i], g.views[i+1:]...)
|
||||
g.views = append(s, v)
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrUnknownView
|
||||
}
|
||||
|
||||
// SetViewOnBottom sets the given view on bottom of the existing ones.
|
||||
func (g *Gui) SetViewOnBottom(name string) (*View, error) {
|
||||
for i, v := range g.views {
|
||||
if v.name == name {
|
||||
s := append(g.views[:i], g.views[i+1:]...)
|
||||
g.views = append([]*View{v}, s...)
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrUnknownView
|
||||
}
|
||||
|
||||
// Views returns all the views in the GUI.
|
||||
func (g *Gui) Views() []*View {
|
||||
return g.views
|
||||
}
|
||||
|
||||
// View returns a pointer to the view with the given name, or error
|
||||
// ErrUnknownView if a view with that name does not exist.
|
||||
func (g *Gui) View(name string) (*View, error) {
|
||||
for _, v := range g.views {
|
||||
if v.name == name {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrUnknownView
|
||||
}
|
||||
|
||||
// ViewByPosition returns a pointer to a view matching the given position, or
|
||||
// error ErrUnknownView if a view in that position does not exist.
|
||||
func (g *Gui) ViewByPosition(x, y int) (*View, error) {
|
||||
// traverse views in reverse order checking top views first
|
||||
for i := len(g.views); i > 0; i-- {
|
||||
v := g.views[i-1]
|
||||
if x > v.x0 && x < v.x1 && y > v.y0 && y < v.y1 {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrUnknownView
|
||||
}
|
||||
|
||||
// ViewPosition returns the coordinates of the view with the given name, or
|
||||
// error ErrUnknownView if a view with that name does not exist.
|
||||
func (g *Gui) ViewPosition(name string) (x0, y0, x1, y1 int, err error) {
|
||||
for _, v := range g.views {
|
||||
if v.name == name {
|
||||
return v.x0, v.y0, v.x1, v.y1, nil
|
||||
}
|
||||
}
|
||||
return 0, 0, 0, 0, ErrUnknownView
|
||||
}
|
||||
|
||||
// DeleteView deletes a view by name.
|
||||
func (g *Gui) DeleteView(name string) error {
|
||||
for i, v := range g.views {
|
||||
if v.name == name {
|
||||
g.views = append(g.views[:i], g.views[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ErrUnknownView
|
||||
}
|
||||
|
||||
// SetCurrentView gives the focus to a given view.
|
||||
func (g *Gui) SetCurrentView(name string) (*View, error) {
|
||||
for _, v := range g.views {
|
||||
if v.name == name {
|
||||
g.currentView = v
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrUnknownView
|
||||
}
|
||||
|
||||
// CurrentView returns the currently focused view, or nil if no view
|
||||
// owns the focus.
|
||||
func (g *Gui) CurrentView() *View {
|
||||
return g.currentView
|
||||
}
|
||||
|
||||
// SetKeybinding creates a new keybinding. If viewname equals to ""
|
||||
// (empty string) then the keybinding will apply to all views. key must
|
||||
// be a rune or a Key.
|
||||
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, handler func(*Gui, *View) error) error {
|
||||
var kb *keybinding
|
||||
|
||||
k, ch, err := getKey(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kb = newKeybinding(viewname, k, ch, mod, handler)
|
||||
g.keybindings = append(g.keybindings, kb)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteKeybinding deletes a keybinding.
|
||||
func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error {
|
||||
k, ch, err := getKey(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, kb := range g.keybindings {
|
||||
if kb.viewName == viewname && kb.ch == ch && kb.key == k && kb.mod == mod {
|
||||
g.keybindings = append(g.keybindings[:i], g.keybindings[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("keybinding not found")
|
||||
}
|
||||
|
||||
// DeleteKeybindings deletes all keybindings of view.
|
||||
func (g *Gui) DeleteKeybindings(viewname string) {
|
||||
var s []*keybinding
|
||||
for _, kb := range g.keybindings {
|
||||
if kb.viewName != viewname {
|
||||
s = append(s, kb)
|
||||
}
|
||||
}
|
||||
g.keybindings = s
|
||||
}
|
||||
|
||||
// getKey takes an empty interface with a key and returns the corresponding
|
||||
// typed Key or rune.
|
||||
func getKey(key interface{}) (Key, rune, error) {
|
||||
switch t := key.(type) {
|
||||
case Key:
|
||||
return t, 0, nil
|
||||
case rune:
|
||||
return 0, t, nil
|
||||
default:
|
||||
return 0, 0, errors.New("unknown type")
|
||||
}
|
||||
}
|
||||
|
||||
// userEvent represents an event triggered by the user.
|
||||
type userEvent struct {
|
||||
f func(*Gui) error
|
||||
}
|
||||
|
||||
// Update executes the passed function. This method can be called safely from a
|
||||
// goroutine in order to update the GUI. It is important to note that the
|
||||
// passed function won't be executed immediately, instead it will be added to
|
||||
// the user events queue. Given that Update spawns a goroutine, the order in
|
||||
// which the user events will be handled is not guaranteed.
|
||||
func (g *Gui) Update(f func(*Gui) error) {
|
||||
go func() { g.userEvents <- userEvent{f: f} }()
|
||||
}
|
||||
|
||||
// A Manager is in charge of GUI's layout and can be used to build widgets.
|
||||
type Manager interface {
|
||||
// Layout is called every time the GUI is redrawn, it must contain the
|
||||
// base views and its initializations.
|
||||
Layout(*Gui) error
|
||||
}
|
||||
|
||||
// The ManagerFunc type is an adapter to allow the use of ordinary functions as
|
||||
// Managers. If f is a function with the appropriate signature, ManagerFunc(f)
|
||||
// is an Manager object that calls f.
|
||||
type ManagerFunc func(*Gui) error
|
||||
|
||||
// Layout calls f(g)
|
||||
func (f ManagerFunc) Layout(g *Gui) error {
|
||||
return f(g)
|
||||
}
|
||||
|
||||
// SetManager sets the given GUI managers. It deletes all views and
|
||||
// keybindings.
|
||||
func (g *Gui) SetManager(managers ...Manager) {
|
||||
g.managers = managers
|
||||
g.currentView = nil
|
||||
g.views = nil
|
||||
g.keybindings = nil
|
||||
|
||||
go func() { g.tbEvents <- termbox.Event{Type: termbox.EventResize} }()
|
||||
}
|
||||
|
||||
// SetManagerFunc sets the given manager function. It deletes all views and
|
||||
// keybindings.
|
||||
func (g *Gui) SetManagerFunc(manager func(*Gui) error) {
|
||||
g.SetManager(ManagerFunc(manager))
|
||||
}
|
||||
|
||||
// MainLoop runs the main loop until an error is returned. A successful
|
||||
// finish should return ErrQuit.
|
||||
func (g *Gui) MainLoop() error {
|
||||
go func() {
|
||||
for {
|
||||
g.tbEvents <- termbox.PollEvent()
|
||||
}
|
||||
}()
|
||||
|
||||
inputMode := termbox.InputAlt
|
||||
if g.InputEsc {
|
||||
inputMode = termbox.InputEsc
|
||||
}
|
||||
if g.Mouse {
|
||||
inputMode |= termbox.InputMouse
|
||||
}
|
||||
termbox.SetInputMode(inputMode)
|
||||
|
||||
if err := g.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case ev := <-g.tbEvents:
|
||||
if err := g.handleEvent(&ev); err != nil {
|
||||
return err
|
||||
}
|
||||
case ev := <-g.userEvents:
|
||||
if err := ev.f(g); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := g.consumeevents(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// consumeevents handles the remaining events in the events pool.
|
||||
func (g *Gui) consumeevents() error {
|
||||
for {
|
||||
select {
|
||||
case ev := <-g.tbEvents:
|
||||
if err := g.handleEvent(&ev); err != nil {
|
||||
return err
|
||||
}
|
||||
case ev := <-g.userEvents:
|
||||
if err := ev.f(g); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleEvent handles an event, based on its type (key-press, error,
|
||||
// etc.)
|
||||
func (g *Gui) handleEvent(ev *termbox.Event) error {
|
||||
switch ev.Type {
|
||||
case termbox.EventKey, termbox.EventMouse:
|
||||
return g.onKey(ev)
|
||||
case termbox.EventError:
|
||||
return ev.Err
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// flush updates the gui, re-drawing frames and buffers.
|
||||
func (g *Gui) flush() error {
|
||||
termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
|
||||
|
||||
maxX, maxY := termbox.Size()
|
||||
// if GUI's size has changed, we need to redraw all views
|
||||
if maxX != g.maxX || maxY != g.maxY {
|
||||
for _, v := range g.views {
|
||||
v.tainted = true
|
||||
}
|
||||
}
|
||||
g.maxX, g.maxY = maxX, maxY
|
||||
|
||||
for _, m := range g.managers {
|
||||
if err := m.Layout(g); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, v := range g.views {
|
||||
if v.Frame {
|
||||
var fgColor, bgColor Attribute
|
||||
if g.Highlight && v == g.currentView {
|
||||
fgColor = g.SelFgColor
|
||||
bgColor = g.SelBgColor
|
||||
} else {
|
||||
fgColor = g.FgColor
|
||||
bgColor = g.BgColor
|
||||
}
|
||||
|
||||
if err := g.drawFrameEdges(v, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.drawFrameCorners(v, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
if v.Title != "" {
|
||||
if err := g.drawTitle(v, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := g.draw(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
termbox.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
// drawFrameEdges draws the horizontal and vertical edges of a view.
|
||||
func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
|
||||
runeH, runeV := '─', '│'
|
||||
if g.ASCII {
|
||||
runeH, runeV = '-', '|'
|
||||
}
|
||||
|
||||
for x := v.x0 + 1; x < v.x1 && x < g.maxX; x++ {
|
||||
if x < 0 {
|
||||
continue
|
||||
}
|
||||
if v.y0 > -1 && v.y0 < g.maxY {
|
||||
if err := g.SetRune(x, v.y0, runeH, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if v.y1 > -1 && v.y1 < g.maxY {
|
||||
if err := g.SetRune(x, v.y1, runeH, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for y := v.y0 + 1; y < v.y1 && y < g.maxY; y++ {
|
||||
if y < 0 {
|
||||
continue
|
||||
}
|
||||
if v.x0 > -1 && v.x0 < g.maxX {
|
||||
if err := g.SetRune(v.x0, y, runeV, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if v.x1 > -1 && v.x1 < g.maxX {
|
||||
if err := g.SetRune(v.x1, y, runeV, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// drawFrameCorners draws the corners of the view.
|
||||
func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
|
||||
runeTL, runeTR, runeBL, runeBR := '┌', '┐', '└', '┘'
|
||||
if g.ASCII {
|
||||
runeTL, runeTR, runeBL, runeBR = '+', '+', '+', '+'
|
||||
}
|
||||
|
||||
corners := []struct {
|
||||
x, y int
|
||||
ch rune
|
||||
}{{v.x0, v.y0, runeTL}, {v.x1, v.y0, runeTR}, {v.x0, v.y1, runeBL}, {v.x1, v.y1, runeBR}}
|
||||
|
||||
for _, c := range corners {
|
||||
if c.x >= 0 && c.y >= 0 && c.x < g.maxX && c.y < g.maxY {
|
||||
if err := g.SetRune(c.x, c.y, c.ch, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// drawTitle draws the title of the view.
|
||||
func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
|
||||
if v.y0 < 0 || v.y0 >= g.maxY {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i, ch := range v.Title {
|
||||
x := v.x0 + i + 2
|
||||
if x < 0 {
|
||||
continue
|
||||
} else if x > v.x1-2 || x >= g.maxX {
|
||||
break
|
||||
}
|
||||
if err := g.SetRune(x, v.y0, ch, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// draw manages the cursor and calls the draw function of a view.
|
||||
func (g *Gui) draw(v *View) error {
|
||||
if g.Cursor {
|
||||
if curview := g.currentView; curview != nil {
|
||||
vMaxX, vMaxY := curview.Size()
|
||||
if curview.cx < 0 {
|
||||
curview.cx = 0
|
||||
} else if curview.cx >= vMaxX {
|
||||
curview.cx = vMaxX - 1
|
||||
}
|
||||
if curview.cy < 0 {
|
||||
curview.cy = 0
|
||||
} else if curview.cy >= vMaxY {
|
||||
curview.cy = vMaxY - 1
|
||||
}
|
||||
|
||||
gMaxX, gMaxY := g.Size()
|
||||
cx, cy := curview.x0+curview.cx+1, curview.y0+curview.cy+1
|
||||
if cx >= 0 && cx < gMaxX && cy >= 0 && cy < gMaxY {
|
||||
termbox.SetCursor(cx, cy)
|
||||
} else {
|
||||
termbox.HideCursor()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
termbox.HideCursor()
|
||||
}
|
||||
|
||||
v.clearRunes()
|
||||
if err := v.draw(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// onKey manages key-press events. A keybinding handler is called when
|
||||
// a key-press or mouse event satisfies a configured keybinding. Furthermore,
|
||||
// currentView's internal buffer is modified if currentView.Editable is true.
|
||||
func (g *Gui) onKey(ev *termbox.Event) error {
|
||||
switch ev.Type {
|
||||
case termbox.EventKey:
|
||||
matched, err := g.execKeybindings(g.currentView, ev)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if matched {
|
||||
break
|
||||
}
|
||||
if g.currentView != nil && g.currentView.Editable && g.currentView.Editor != nil {
|
||||
g.currentView.Editor.Edit(g.currentView, Key(ev.Key), ev.Ch, Modifier(ev.Mod))
|
||||
}
|
||||
case termbox.EventMouse:
|
||||
mx, my := ev.MouseX, ev.MouseY
|
||||
v, err := g.ViewByPosition(mx, my)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if err := v.SetCursor(mx-v.x0-1, my-v.y0-1); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := g.execKeybindings(v, ev); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// execKeybindings executes the keybinding handlers that match the passed view
|
||||
// and event. The value of matched is true if there is a match and no errors.
|
||||
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error) {
|
||||
matched = false
|
||||
for _, kb := range g.keybindings {
|
||||
if kb.handler == nil {
|
||||
continue
|
||||
}
|
||||
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) {
|
||||
if err := kb.handler(g, v); err != nil {
|
||||
return false, err
|
||||
}
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
return matched, nil
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
// Copyright 2014 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/nsf/termbox-go"
|
||||
|
||||
// Keybidings are used to link a given key-press event with a handler.
|
||||
type keybinding struct {
|
||||
viewName string
|
||||
key Key
|
||||
ch rune
|
||||
mod Modifier
|
||||
handler func(*Gui, *View) error
|
||||
}
|
||||
|
||||
// newKeybinding returns a new Keybinding object.
|
||||
func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding) {
|
||||
kb = &keybinding{
|
||||
viewName: viewname,
|
||||
key: key,
|
||||
ch: ch,
|
||||
mod: mod,
|
||||
handler: handler,
|
||||
}
|
||||
return kb
|
||||
}
|
||||
|
||||
// matchKeypress returns if the keybinding matches the keypress.
|
||||
func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool {
|
||||
return kb.key == key && kb.ch == ch && kb.mod == mod
|
||||
}
|
||||
|
||||
// matchView returns if the keybinding matches the current view.
|
||||
func (kb *keybinding) matchView(v *View) bool {
|
||||
if kb.viewName == "" {
|
||||
return true
|
||||
}
|
||||
return v != nil && kb.viewName == v.name
|
||||
}
|
||||
|
||||
// Key represents special keys or keys combinations.
|
||||
type Key termbox.Key
|
||||
|
||||
// Special keys.
|
||||
const (
|
||||
KeyF1 Key = Key(termbox.KeyF1)
|
||||
KeyF2 = Key(termbox.KeyF2)
|
||||
KeyF3 = Key(termbox.KeyF3)
|
||||
KeyF4 = Key(termbox.KeyF4)
|
||||
KeyF5 = Key(termbox.KeyF5)
|
||||
KeyF6 = Key(termbox.KeyF6)
|
||||
KeyF7 = Key(termbox.KeyF7)
|
||||
KeyF8 = Key(termbox.KeyF8)
|
||||
KeyF9 = Key(termbox.KeyF9)
|
||||
KeyF10 = Key(termbox.KeyF10)
|
||||
KeyF11 = Key(termbox.KeyF11)
|
||||
KeyF12 = Key(termbox.KeyF12)
|
||||
KeyInsert = Key(termbox.KeyInsert)
|
||||
KeyDelete = Key(termbox.KeyDelete)
|
||||
KeyHome = Key(termbox.KeyHome)
|
||||
KeyEnd = Key(termbox.KeyEnd)
|
||||
KeyPgup = Key(termbox.KeyPgup)
|
||||
KeyPgdn = Key(termbox.KeyPgdn)
|
||||
KeyArrowUp = Key(termbox.KeyArrowUp)
|
||||
KeyArrowDown = Key(termbox.KeyArrowDown)
|
||||
KeyArrowLeft = Key(termbox.KeyArrowLeft)
|
||||
KeyArrowRight = Key(termbox.KeyArrowRight)
|
||||
|
||||
MouseLeft = Key(termbox.MouseLeft)
|
||||
MouseMiddle = Key(termbox.MouseMiddle)
|
||||
MouseRight = Key(termbox.MouseRight)
|
||||
MouseRelease = Key(termbox.MouseRelease)
|
||||
MouseWheelUp = Key(termbox.MouseWheelUp)
|
||||
MouseWheelDown = Key(termbox.MouseWheelDown)
|
||||
)
|
||||
|
||||
// Keys combinations.
|
||||
const (
|
||||
KeyCtrlTilde Key = Key(termbox.KeyCtrlTilde)
|
||||
KeyCtrl2 = Key(termbox.KeyCtrl2)
|
||||
KeyCtrlSpace = Key(termbox.KeyCtrlSpace)
|
||||
KeyCtrlA = Key(termbox.KeyCtrlA)
|
||||
KeyCtrlB = Key(termbox.KeyCtrlB)
|
||||
KeyCtrlC = Key(termbox.KeyCtrlC)
|
||||
KeyCtrlD = Key(termbox.KeyCtrlD)
|
||||
KeyCtrlE = Key(termbox.KeyCtrlE)
|
||||
KeyCtrlF = Key(termbox.KeyCtrlF)
|
||||
KeyCtrlG = Key(termbox.KeyCtrlG)
|
||||
KeyBackspace = Key(termbox.KeyBackspace)
|
||||
KeyCtrlH = Key(termbox.KeyCtrlH)
|
||||
KeyTab = Key(termbox.KeyTab)
|
||||
KeyCtrlI = Key(termbox.KeyCtrlI)
|
||||
KeyCtrlJ = Key(termbox.KeyCtrlJ)
|
||||
KeyCtrlK = Key(termbox.KeyCtrlK)
|
||||
KeyCtrlL = Key(termbox.KeyCtrlL)
|
||||
KeyEnter = Key(termbox.KeyEnter)
|
||||
KeyCtrlM = Key(termbox.KeyCtrlM)
|
||||
KeyCtrlN = Key(termbox.KeyCtrlN)
|
||||
KeyCtrlO = Key(termbox.KeyCtrlO)
|
||||
KeyCtrlP = Key(termbox.KeyCtrlP)
|
||||
KeyCtrlQ = Key(termbox.KeyCtrlQ)
|
||||
KeyCtrlR = Key(termbox.KeyCtrlR)
|
||||
KeyCtrlS = Key(termbox.KeyCtrlS)
|
||||
KeyCtrlT = Key(termbox.KeyCtrlT)
|
||||
KeyCtrlU = Key(termbox.KeyCtrlU)
|
||||
KeyCtrlV = Key(termbox.KeyCtrlV)
|
||||
KeyCtrlW = Key(termbox.KeyCtrlW)
|
||||
KeyCtrlX = Key(termbox.KeyCtrlX)
|
||||
KeyCtrlY = Key(termbox.KeyCtrlY)
|
||||
KeyCtrlZ = Key(termbox.KeyCtrlZ)
|
||||
KeyEsc = Key(termbox.KeyEsc)
|
||||
KeyCtrlLsqBracket = Key(termbox.KeyCtrlLsqBracket)
|
||||
KeyCtrl3 = Key(termbox.KeyCtrl3)
|
||||
KeyCtrl4 = Key(termbox.KeyCtrl4)
|
||||
KeyCtrlBackslash = Key(termbox.KeyCtrlBackslash)
|
||||
KeyCtrl5 = Key(termbox.KeyCtrl5)
|
||||
KeyCtrlRsqBracket = Key(termbox.KeyCtrlRsqBracket)
|
||||
KeyCtrl6 = Key(termbox.KeyCtrl6)
|
||||
KeyCtrl7 = Key(termbox.KeyCtrl7)
|
||||
KeyCtrlSlash = Key(termbox.KeyCtrlSlash)
|
||||
KeyCtrlUnderscore = Key(termbox.KeyCtrlUnderscore)
|
||||
KeySpace = Key(termbox.KeySpace)
|
||||
KeyBackspace2 = Key(termbox.KeyBackspace2)
|
||||
KeyCtrl8 = Key(termbox.KeyCtrl8)
|
||||
)
|
||||
|
||||
// Modifier allows to define special keys combinations. They can be used
|
||||
// in combination with Keys or Runes when a new keybinding is defined.
|
||||
type Modifier termbox.Modifier
|
||||
|
||||
// Modifiers.
|
||||
const (
|
||||
ModNone Modifier = Modifier(0)
|
||||
ModAlt = Modifier(termbox.ModAlt)
|
||||
)
|
@ -0,0 +1,479 @@
|
||||
// Copyright 2014 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 (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
// A View is a window. It maintains its own internal buffer and cursor
|
||||
// position.
|
||||
type View struct {
|
||||
name string
|
||||
x0, y0, x1, y1 int
|
||||
ox, oy int
|
||||
cx, cy int
|
||||
lines [][]cell
|
||||
readOffset int
|
||||
readCache string
|
||||
|
||||
tainted bool // marks if the viewBuffer must be updated
|
||||
viewLines []viewLine // internal representation of the view's buffer
|
||||
|
||||
ei *escapeInterpreter // used to decode ESC sequences on Write
|
||||
|
||||
// BgColor and FgColor allow to configure the background and foreground
|
||||
// colors of the View.
|
||||
BgColor, FgColor Attribute
|
||||
|
||||
// SelBgColor and SelFgColor are used to configure the background and
|
||||
// foreground colors of the selected line, when it is highlighted.
|
||||
SelBgColor, SelFgColor Attribute
|
||||
|
||||
// If Editable is true, keystrokes will be added to the view's internal
|
||||
// buffer at the cursor position.
|
||||
Editable bool
|
||||
|
||||
// Editor allows to define the editor that manages the edition mode,
|
||||
// including keybindings or cursor behaviour. DefaultEditor is used by
|
||||
// default.
|
||||
Editor Editor
|
||||
|
||||
// Overwrite enables or disables the overwrite mode of the view.
|
||||
Overwrite bool
|
||||
|
||||
// If Highlight is true, Sel{Bg,Fg}Colors will be used
|
||||
// for the line under the cursor position.
|
||||
Highlight bool
|
||||
|
||||
// If Frame is true, a border will be drawn around the view.
|
||||
Frame bool
|
||||
|
||||
// If Wrap is true, the content that is written to this View is
|
||||
// automatically wrapped when it is longer than its width. If true the
|
||||
// view's x-origin will be ignored.
|
||||
Wrap bool
|
||||
|
||||
// If Autoscroll is true, the View will automatically scroll down when the
|
||||
// text overflows. If true the view's y-origin will be ignored.
|
||||
Autoscroll bool
|
||||
|
||||
// If Frame is true, Title allows to configure a title for the view.
|
||||
Title string
|
||||
|
||||
// If Mask is true, the View will display the mask instead of the real
|
||||
// content
|
||||
Mask rune
|
||||
}
|
||||
|
||||
type viewLine struct {
|
||||
linesX, linesY int // coordinates relative to v.lines
|
||||
line []cell
|
||||
}
|
||||
|
||||
type cell struct {
|
||||
chr rune
|
||||
bgColor, fgColor Attribute
|
||||
}
|
||||
|
||||
type lineType []cell
|
||||
|
||||
// String returns a string from a given cell slice.
|
||||
func (l lineType) String() string {
|
||||
str := ""
|
||||
for _, c := range l {
|
||||
str += string(c.chr)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// newView returns a new View object.
|
||||
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
|
||||
v := &View{
|
||||
name: name,
|
||||
x0: x0,
|
||||
y0: y0,
|
||||
x1: x1,
|
||||
y1: y1,
|
||||
Frame: true,
|
||||
Editor: DefaultEditor,
|
||||
tainted: true,
|
||||
ei: newEscapeInterpreter(mode),
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Size returns the number of visible columns and rows in the View.
|
||||
func (v *View) Size() (x, y int) {
|
||||
return v.x1 - v.x0 - 1, v.y1 - v.y0 - 1
|
||||
}
|
||||
|
||||
// Name returns the name of the view.
|
||||
func (v *View) Name() string {
|
||||
return v.name
|
||||
}
|
||||
|
||||
// setRune sets a rune at the given point relative to the view. It applies the
|
||||
// specified colors, taking into account if the cell must be highlighted. Also,
|
||||
// it checks if the position is valid.
|
||||
func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
|
||||
maxX, maxY := v.Size()
|
||||
if x < 0 || x >= maxX || y < 0 || y >= maxY {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
|
||||
var (
|
||||
ry, rcy int
|
||||
err error
|
||||
)
|
||||
if v.Highlight {
|
||||
_, ry, err = v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, rcy, err = v.realPosition(v.cx, v.cy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if v.Mask != 0 {
|
||||
fgColor = v.FgColor
|
||||
bgColor = v.BgColor
|
||||
ch = v.Mask
|
||||
} else if v.Highlight && ry == rcy {
|
||||
fgColor = v.SelFgColor
|
||||
bgColor = v.SelBgColor
|
||||
}
|
||||
|
||||
termbox.SetCell(v.x0+x+1, v.y0+y+1, ch,
|
||||
termbox.Attribute(fgColor), termbox.Attribute(bgColor))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetCursor sets the cursor position of the view at the given point,
|
||||
// relative to the view. It checks if the position is valid.
|
||||
func (v *View) SetCursor(x, y int) error {
|
||||
maxX, maxY := v.Size()
|
||||
if x < 0 || x >= maxX || y < 0 || y >= maxY {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
v.cx = x
|
||||
v.cy = y
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cursor returns the cursor position of the view.
|
||||
func (v *View) Cursor() (x, y int) {
|
||||
return v.cx, v.cy
|
||||
}
|
||||
|
||||
// SetOrigin sets the origin position of the view's internal buffer,
|
||||
// so the buffer starts to be printed from this point, which means that
|
||||
// it is linked with the origin point of view. It can be used to
|
||||
// implement Horizontal and Vertical scrolling with just incrementing
|
||||
// or decrementing ox and oy.
|
||||
func (v *View) SetOrigin(x, y int) error {
|
||||
if x < 0 || y < 0 {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
v.ox = x
|
||||
v.oy = y
|
||||
return nil
|
||||
}
|
||||
|
||||
// Origin returns the origin position of the view.
|
||||
func (v *View) Origin() (x, y int) {
|
||||
return v.ox, v.oy
|
||||
}
|
||||
|
||||
// Write appends a byte slice into the view's internal buffer. Because
|
||||
// View implements the io.Writer interface, it can be passed as parameter
|
||||
// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must
|
||||
// be called to clear the view's buffer.
|
||||
func (v *View) Write(p []byte) (n int, err error) {
|
||||
v.tainted = true
|
||||
|
||||
for _, ch := range bytes.Runes(p) {
|
||||
switch ch {
|
||||
case '\n':
|
||||
v.lines = append(v.lines, nil)
|
||||
case '\r':
|
||||
nl := len(v.lines)
|
||||
if nl > 0 {
|
||||
v.lines[nl-1] = nil
|
||||
} else {
|
||||
v.lines = make([][]cell, 1)
|
||||
}
|
||||
default:
|
||||
cells := v.parseInput(ch)
|
||||
if cells == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
nl := len(v.lines)
|
||||
if nl > 0 {
|
||||
v.lines[nl-1] = append(v.lines[nl-1], cells...)
|
||||
} else {
|
||||
v.lines = append(v.lines, cells)
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// parseInput parses char by char the input written to the View. It returns nil
|
||||
// while processing ESC sequences. Otherwise, it returns a cell slice that
|
||||
// contains the processed data.
|
||||
func (v *View) parseInput(ch rune) []cell {
|
||||
cells := []cell{}
|
||||
|
||||
isEscape, err := v.ei.parseOne(ch)
|
||||
if err != nil {
|
||||
for _, r := range v.ei.runes() {
|
||||
c := cell{
|
||||
fgColor: v.FgColor,
|
||||
bgColor: v.BgColor,
|
||||
chr: r,
|
||||
}
|
||||
cells = append(cells, c)
|
||||
}
|
||||
v.ei.reset()
|
||||
} else {
|
||||
if isEscape {
|
||||
return nil
|
||||
}
|
||||
c := cell{
|
||||
fgColor: v.ei.curFgColor,
|
||||
bgColor: v.ei.curBgColor,
|
||||
chr: ch,
|
||||
}
|
||||
cells = append(cells, c)
|
||||
}
|
||||
|
||||
return cells
|
||||
}
|
||||
|
||||
// Read reads data into p. It returns the number of bytes read into p.
|
||||
// At EOF, err will be io.EOF. Calling Read() after Rewind() makes the
|
||||
// cache to be refreshed with the contents of the view.
|
||||
func (v *View) Read(p []byte) (n int, err error) {
|
||||
if v.readOffset == 0 {
|
||||
v.readCache = v.Buffer()
|
||||
}
|
||||
if v.readOffset < len(v.readCache) {
|
||||
n = copy(p, v.readCache[v.readOffset:])
|
||||
v.readOffset += n
|
||||
} else {
|
||||
err = io.EOF
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Rewind sets the offset for the next Read to 0, which also refresh the
|
||||
// read cache.
|
||||
func (v *View) Rewind() {
|
||||
v.readOffset = 0
|
||||
}
|
||||
|
||||
// draw re-draws the view's contents.
|
||||
func (v *View) draw() error {
|
||||
maxX, maxY := v.Size()
|
||||
|
||||
if v.Wrap {
|
||||
if maxX == 0 {
|
||||
return errors.New("X size of the view cannot be 0")
|
||||
}
|
||||
v.ox = 0
|
||||
}
|
||||
if v.tainted {
|
||||
v.viewLines = nil
|
||||
for i, line := range v.lines {
|
||||
if v.Wrap {
|
||||
if len(line) < maxX {
|
||||
vline := viewLine{linesX: 0, linesY: i, line: line}
|
||||
v.viewLines = append(v.viewLines, vline)
|
||||
continue
|
||||
} else {
|
||||
for n := 0; n <= len(line); n += maxX {
|
||||
if len(line[n:]) <= maxX {
|
||||
vline := viewLine{linesX: n, linesY: i, line: line[n:]}
|
||||
v.viewLines = append(v.viewLines, vline)
|
||||
} else {
|
||||
vline := viewLine{linesX: n, linesY: i, line: line[n : n+maxX]}
|
||||
v.viewLines = append(v.viewLines, vline)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vline := viewLine{linesX: 0, linesY: i, line: line}
|
||||
v.viewLines = append(v.viewLines, vline)
|
||||
}
|
||||
}
|
||||
v.tainted = false
|
||||
}
|
||||
|
||||
if v.Autoscroll && len(v.viewLines) > maxY {
|
||||
v.oy = len(v.viewLines) - maxY
|
||||
}
|
||||
y := 0
|
||||
for i, vline := range v.viewLines {
|
||||
if i < v.oy {
|
||||
continue
|
||||
}
|
||||
if y >= maxY {
|
||||
break
|
||||
}
|
||||
x := 0
|
||||
for j, c := range vline.line {
|
||||
if j < v.ox {
|
||||
continue
|
||||
}
|
||||
if x >= maxX {
|
||||
break
|
||||
}
|
||||
|
||||
fgColor := c.fgColor
|
||||
if fgColor == ColorDefault {
|
||||
fgColor = v.FgColor
|
||||
}
|
||||
bgColor := c.bgColor
|
||||
if bgColor == ColorDefault {
|
||||
bgColor = v.BgColor
|
||||
}
|
||||
|
||||
if err := v.setRune(x, y, c.chr, fgColor, bgColor); err != nil {
|
||||
return err
|
||||
}
|
||||
x++
|
||||
}
|
||||
y++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// realPosition returns the position in the internal buffer corresponding to the
|
||||
// point (x, y) of the view.
|
||||
func (v *View) realPosition(vx, vy int) (x, y int, err error) {
|
||||
vx = v.ox + vx
|
||||
vy = v.oy + vy
|
||||
|
||||
if vx < 0 || vy < 0 {
|
||||
return 0, 0, errors.New("invalid point")
|
||||
}
|
||||
|
||||
if len(v.viewLines) == 0 {
|
||||
return vx, vy, nil
|
||||
}
|
||||
|
||||
if vy < len(v.viewLines) {
|
||||
vline := v.viewLines[vy]
|
||||
x = vline.linesX + vx
|
||||
y = vline.linesY
|
||||
} else {
|
||||
vline := v.viewLines[len(v.viewLines)-1]
|
||||
x = vx
|
||||
y = vline.linesY + vy - len(v.viewLines) + 1
|
||||
}
|
||||
|
||||
return x, y, nil
|
||||
}
|
||||
|
||||
// Clear empties the view's internal buffer.
|
||||
func (v *View) Clear() {
|
||||
v.tainted = true
|
||||
|
||||
v.lines = nil
|
||||
v.viewLines = nil
|
||||
v.readOffset = 0
|
||||
v.clearRunes()
|
||||
}
|
||||
|
||||
// clearRunes erases all the cells in the view.
|
||||
func (v *View) clearRunes() {
|
||||
maxX, maxY := v.Size()
|
||||
for x := 0; x < maxX; x++ {
|
||||
for y := 0; y < maxY; y++ {
|
||||
termbox.SetCell(v.x0+x+1, v.y0+y+1, ' ',
|
||||
termbox.Attribute(v.FgColor), termbox.Attribute(v.BgColor))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer returns a string with the contents of the view's internal
|
||||
// buffer.
|
||||
func (v *View) Buffer() string {
|
||||
str := ""
|
||||
for _, l := range v.lines {
|
||||
str += lineType(l).String() + "\n"
|
||||
}
|
||||
return strings.Replace(str, "\x00", " ", -1)
|
||||
}
|
||||
|
||||
// ViewBuffer returns a string with the contents of the view's buffer that is
|
||||
// shown to the user.
|
||||
func (v *View) ViewBuffer() string {
|
||||
str := ""
|
||||
for _, l := range v.viewLines {
|
||||
str += lineType(l.line).String() + "\n"
|
||||
}
|
||||
return strings.Replace(str, "\x00", " ", -1)
|
||||
}
|
||||
|
||||
// Line returns a string with the line of the view's internal buffer
|
||||
// at the position corresponding to the point (x, y).
|
||||
func (v *View) Line(y int) (string, error) {
|
||||
_, y, err := v.realPosition(0, y)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if y < 0 || y >= len(v.lines) {
|
||||
return "", errors.New("invalid point")
|
||||
}
|
||||
|
||||
return lineType(v.lines[y]).String(), nil
|
||||
}
|
||||
|
||||
// Word returns a string with the word of the view's internal buffer
|
||||
// at the position corresponding to the point (x, y).
|
||||
func (v *View) Word(x, y int) (string, error) {
|
||||
x, y, err := v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) {
|
||||
return "", errors.New("invalid point")
|
||||
}
|
||||
|
||||
str := lineType(v.lines[y]).String()
|
||||
|
||||
nl := strings.LastIndexFunc(str[:x], indexFunc)
|
||||
if nl == -1 {
|
||||
nl = 0
|
||||
} else {
|
||||
nl = nl + 1
|
||||
}
|
||||
nr := strings.IndexFunc(str[x:], indexFunc)
|
||||
if nr == -1 {
|
||||
nr = len(str)
|
||||
} else {
|
||||
nr = nr + x
|
||||
}
|
||||
return string(str[nl:nr]), nil
|
||||
}
|
||||
|
||||
// indexFunc allows to split lines by words taking into account spaces
|
||||
// and 0.
|
||||
func indexFunc(r rune) bool {
|
||||
return r == ' ' || r == 0
|
||||
}
|
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2015 Marc-Antoine Ruel
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
@ -0,0 +1,109 @@
|
||||
// Copyright 2015 Marc-Antoine Ruel. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Similarity is the level at which two call lines arguments must match to be
|
||||
// considered similar enough to coalesce them.
|
||||
type Similarity int
|
||||
|
||||
const (
|
||||
// ExactFlags requires same bits (e.g. Locked).
|
||||
ExactFlags Similarity = iota
|
||||
// ExactLines requests the exact same arguments on the call line.
|
||||
ExactLines
|
||||
// AnyPointer considers different pointers a similar call line.
|
||||
AnyPointer
|
||||
// AnyValue accepts any value as similar call line.
|
||||
AnyValue
|
||||
)
|
||||
|
||||
// Bucketize returns the number of similar goroutines.
|
||||
func Bucketize(goroutines []Goroutine, similar Similarity) map[*Signature][]Goroutine {
|
||||
out := map[*Signature][]Goroutine{}
|
||||
// O(n²). Fix eventually.
|
||||
for _, routine := range goroutines {
|
||||
found := false
|
||||
for key := range out {
|
||||
// When a match is found, this effectively drops the other goroutine ID.
|
||||
if key.Similar(&routine.Signature, similar) {
|
||||
found = true
|
||||
if !key.Equal(&routine.Signature) {
|
||||
// Almost but not quite equal. There's different pointers passed
|
||||
// around but the same values. Zap out the different values.
|
||||
newKey := key.Merge(&routine.Signature)
|
||||
out[newKey] = append(out[key], routine)
|
||||
delete(out, key)
|
||||
} else {
|
||||
out[key] = append(out[key], routine)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
key := &Signature{}
|
||||
*key = routine.Signature
|
||||
out[key] = []Goroutine{routine}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Bucket is a stack trace signature and the list of goroutines that fits this
|
||||
// signature.
|
||||
type Bucket struct {
|
||||
Signature
|
||||
Routines []Goroutine
|
||||
}
|
||||
|
||||
// First returns true if it contains the first goroutine, e.g. the ones that
|
||||
// likely generated the panic() call, if any.
|
||||
func (b *Bucket) First() bool {
|
||||
for _, r := range b.Routines {
|
||||
if r.First {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Less does reverse sort.
|
||||
func (b *Bucket) Less(r *Bucket) bool {
|
||||
if b.First() {
|
||||
return true
|
||||
}
|
||||
if r.First() {
|
||||
return false
|
||||
}
|
||||
return b.Signature.Less(&r.Signature)
|
||||
}
|
||||
|
||||
// Buckets is a list of Bucket sorted by repeation count.
|
||||
type Buckets []Bucket
|
||||
|
||||
func (b Buckets) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b Buckets) Less(i, j int) bool {
|
||||
return b[i].Less(&b[j])
|
||||
}
|
||||
|
||||
func (b Buckets) Swap(i, j int) {
|
||||
b[j], b[i] = b[i], b[j]
|
||||
}
|
||||
|
||||
// SortBuckets creates a list of Bucket from each goroutine stack trace count.
|
||||
func SortBuckets(buckets map[*Signature][]Goroutine) Buckets {
|
||||
out := make(Buckets, 0, len(buckets))
|
||||
for signature, count := range buckets {
|
||||
out = append(out, Bucket{*signature, count})
|
||||
}
|
||||
sort.Sort(out)
|
||||
return out
|
||||
}
|
@ -0,0 +1,302 @@
|
||||
// Copyright 2015 Marc-Antoine Ruel. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// This file contains the code to process sources, to be able to deduct the
|
||||
// original types.
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cache is a cache of sources on the file system.
|
||||
type cache struct {
|
||||
files map[string][]byte
|
||||
parsed map[string]*parsedFile
|
||||
}
|
||||
|
||||
// Augment processes source files to improve calls to be more descriptive.
|
||||
//
|
||||
// It modifies goroutines in place.
|
||||
func Augment(goroutines []Goroutine) {
|
||||
c := &cache{}
|
||||
for i := range goroutines {
|
||||
c.augmentGoroutine(&goroutines[i])
|
||||
}
|
||||
}
|
||||
|
||||
// augmentGoroutine processes source files to improve call to be more
|
||||
// descriptive.
|
||||
//
|
||||
// It modifies the routine.
|
||||
func (c *cache) augmentGoroutine(goroutine *Goroutine) {
|
||||
if c.files == nil {
|
||||
c.files = map[string][]byte{}
|
||||
}
|
||||
if c.parsed == nil {
|
||||
c.parsed = map[string]*parsedFile{}
|
||||
}
|
||||
// For each call site, look at the next call and populate it. Then we can
|
||||
// walk back and reformat things.
|
||||
for i := range goroutine.Stack.Calls {
|
||||
c.load(goroutine.Stack.Calls[i].LocalSourcePath())
|
||||
}
|
||||
|
||||
// Once all loaded, we can look at the next call when available.
|
||||
for i := 0; i < len(goroutine.Stack.Calls)-1; i++ {
|
||||
// Get the AST from the previous call and process the call line with it.
|
||||
if f := c.getFuncAST(&goroutine.Stack.Calls[i]); f != nil {
|
||||
processCall(&goroutine.Stack.Calls[i], f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Private stuff.
|
||||
|
||||
// load loads a source file and parses the AST tree. Failures are ignored.
|
||||
func (c *cache) load(fileName string) {
|
||||
if _, ok := c.parsed[fileName]; ok {
|
||||
return
|
||||
}
|
||||
c.parsed[fileName] = nil
|
||||
if !strings.HasSuffix(fileName, ".go") {
|
||||
// Ignore C and assembly.
|
||||
c.files[fileName] = nil
|
||||
return
|
||||
}
|
||||
log.Printf("load(%s)", fileName)
|
||||
if _, ok := c.files[fileName]; !ok {
|
||||
var err error
|
||||
if c.files[fileName], err = ioutil.ReadFile(fileName); err != nil {
|
||||
log.Printf("Failed to read %s: %s", fileName, err)
|
||||
c.files[fileName] = nil
|
||||
return
|
||||
}
|
||||
}
|
||||
fset := token.NewFileSet()
|
||||
src := c.files[fileName]
|
||||
parsed, err := parser.ParseFile(fset, fileName, src, 0)
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse %s: %s", fileName, err)
|
||||
return
|
||||
}
|
||||
// Convert the line number into raw file offset.
|
||||
offsets := []int{0, 0}
|
||||
start := 0
|
||||
for l := 1; start < len(src); l++ {
|
||||
start += bytes.IndexByte(src[start:], '\n') + 1
|
||||
offsets = append(offsets, start)
|
||||
}
|
||||
c.parsed[fileName] = &parsedFile{offsets, parsed}
|
||||
}
|
||||
|
||||
func (c *cache) getFuncAST(call *Call) *ast.FuncDecl {
|
||||
if p := c.parsed[call.LocalSourcePath()]; p != nil {
|
||||
return p.getFuncAST(call.Func.Name(), call.Line)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type parsedFile struct {
|
||||
lineToByteOffset []int
|
||||
parsed *ast.File
|
||||
}
|
||||
|
||||
// getFuncAST gets the callee site function AST representation for the code
|
||||
// inside the function f at line l.
|
||||
func (p *parsedFile) getFuncAST(f string, l int) (d *ast.FuncDecl) {
|
||||
if len(p.lineToByteOffset) <= l {
|
||||
// The line number in the stack trace line does not exist in the file. That
|
||||
// can only mean that the sources on disk do not match the sources used to
|
||||
// build the binary.
|
||||
// TODO(maruel): This should be surfaced, so that source parsing is
|
||||
// completely ignored.
|
||||
return
|
||||
}
|
||||
|
||||
// Walk the AST to find the lineToByteOffset that fits the line number.
|
||||
var lastFunc *ast.FuncDecl
|
||||
var found ast.Node
|
||||
// Inspect() goes depth first. This means for example that a function like:
|
||||
// func a() {
|
||||
// b := func() {}
|
||||
// c()
|
||||
// }
|
||||
//
|
||||
// Were we are looking at the c() call can return confused values. It is
|
||||
// important to look at the actual ast.Node hierarchy.
|
||||
ast.Inspect(p.parsed, func(n ast.Node) bool {
|
||||
if d != nil {
|
||||
return false
|
||||
}
|
||||
if n == nil {
|
||||
return true
|
||||
}
|
||||
if found != nil {
|
||||
// We are walking up.
|
||||
}
|
||||
if int(n.Pos()) >= p.lineToByteOffset[l] {
|
||||
// We are expecting a ast.CallExpr node. It can be harder to figure out
|
||||
// when there are multiple calls on a single line, as the stack trace
|
||||
// doesn't have file byte offset information, only line based.
|
||||
// gofmt will always format to one function call per line but there can
|
||||
// be edge cases, like:
|
||||
// a = A{Foo(), Bar()}
|
||||
d = lastFunc
|
||||
//p.processNode(call, n)
|
||||
return false
|
||||
} else if f, ok := n.(*ast.FuncDecl); ok {
|
||||
lastFunc = f
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func name(n ast.Node) string {
|
||||
switch t := n.(type) {
|
||||
case *ast.InterfaceType:
|
||||
return "interface{}"
|
||||
case *ast.Ident:
|
||||
return t.Name
|
||||
case *ast.SelectorExpr:
|
||||
return t.Sel.Name
|
||||
case *ast.StarExpr:
|
||||
return "*" + name(t.X)
|
||||
default:
|
||||
return "<unknown>"
|
||||
}
|
||||
}
|
||||
|
||||
// fieldToType returns the type name and whether if it's an ellipsis.
|
||||
func fieldToType(f *ast.Field) (string, bool) {
|
||||
switch arg := f.Type.(type) {
|
||||
case *ast.ArrayType:
|
||||
return "[]" + name(arg.Elt), false
|
||||
case *ast.Ellipsis:
|
||||
return name(arg.Elt), true
|
||||
case *ast.FuncType:
|
||||
// Do not print the function signature to not overload the trace.
|
||||
return "func", false
|
||||
case *ast.Ident:
|
||||
return arg.Name, false
|
||||
case *ast.InterfaceType:
|
||||
return "interface{}", false
|
||||
case *ast.SelectorExpr:
|
||||
return arg.Sel.Name, false
|
||||
case *ast.StarExpr:
|
||||
return "*" + name(arg.X), false
|
||||
case *ast.MapType:
|
||||
return fmt.Sprintf("map[%s]%s", name(arg.Key), name(arg.Value)), false
|
||||
case *ast.ChanType:
|
||||
return fmt.Sprintf("chan %s", name(arg.Value)), false
|
||||
default:
|
||||
// TODO(maruel): Implement anything missing.
|
||||
return "<unknown>", false
|
||||
}
|
||||
}
|
||||
|
||||
// extractArgumentsType returns the name of the type of each input argument.
|
||||
func extractArgumentsType(f *ast.FuncDecl) ([]string, bool) {
|
||||
var fields []*ast.Field
|
||||
if f.Recv != nil {
|
||||
if len(f.Recv.List) != 1 {
|
||||
panic("Expect only one receiver; please fix panicparse's code")
|
||||
}
|
||||
// If it is an object receiver (vs a pointer receiver), its address is not
|
||||
// printed in the stack trace so it needs to be ignored.
|
||||
if _, ok := f.Recv.List[0].Type.(*ast.StarExpr); ok {
|
||||
fields = append(fields, f.Recv.List[0])
|
||||
}
|
||||
}
|
||||
var types []string
|
||||
extra := false
|
||||
for _, arg := range append(fields, f.Type.Params.List...) {
|
||||
// Assert that extra is only set on the last item of fields?
|
||||
var t string
|
||||
t, extra = fieldToType(arg)
|
||||
mult := len(arg.Names)
|
||||
if mult == 0 {
|
||||
mult = 1
|
||||
}
|
||||
for i := 0; i < mult; i++ {
|
||||
types = append(types, t)
|
||||
}
|
||||
}
|
||||
return types, extra
|
||||
}
|
||||
|
||||
// processCall walks the function and populate call accordingly.
|
||||
func processCall(call *Call, f *ast.FuncDecl) {
|
||||
values := make([]uint64, len(call.Args.Values))
|
||||
for i := range call.Args.Values {
|
||||
values[i] = call.Args.Values[i].Value
|
||||
}
|
||||
index := 0
|
||||
pop := func() uint64 {
|
||||
if len(values) != 0 {
|
||||
x := values[0]
|
||||
values = values[1:]
|
||||
index++
|
||||
return x
|
||||
}
|
||||
return 0
|
||||
}
|
||||
popName := func() string {
|
||||
n := call.Args.Values[index].Name
|
||||
v := pop()
|
||||
if len(n) == 0 {
|
||||
return fmt.Sprintf("0x%x", v)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
types, extra := extractArgumentsType(f)
|
||||
for i := 0; len(values) != 0; i++ {
|
||||
var t string
|
||||
if i >= len(types) {
|
||||
if !extra {
|
||||
// These are unexpected value! Print them as hex.
|
||||
call.Args.Processed = append(call.Args.Processed, popName())
|
||||
continue
|
||||
}
|
||||
t = types[len(types)-1]
|
||||
} else {
|
||||
t = types[i]
|
||||
}
|
||||
switch t {
|
||||
case "float32":
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%g", math.Float32frombits(uint32(pop()))))
|
||||
case "float64":
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%g", math.Float64frombits(pop())))
|
||||
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%d", pop()))
|
||||
case "string":
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%s(%s, len=%d)", t, popName(), pop()))
|
||||
default:
|
||||
if strings.HasPrefix(t, "*") {
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%s(%s)", t, popName()))
|
||||
} else if strings.HasPrefix(t, "[]") {
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%s(%s len=%d cap=%d)", t, popName(), pop(), pop()))
|
||||
} else {
|
||||
// Assumes it's an interface. For now, discard the object value, which
|
||||
// is probably not a good idea.
|
||||
call.Args.Processed = append(call.Args.Processed, fmt.Sprintf("%s(%s)", t, popName()))
|
||||
pop()
|
||||
}
|
||||
}
|
||||
if len(values) == 0 && call.Args.Elided {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,906 @@
|
||||
// Copyright 2015 Marc-Antoine Ruel. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package stack analyzes stack dump of Go processes and simplifies it.
|
||||
//
|
||||
// It is mostly useful on servers will large number of identical goroutines,
|
||||
// making the crash dump harder to read than strictly necessary.
|
||||
package stack
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const lockedToThread = "locked to thread"
|
||||
|
||||
var (
|
||||
// TODO(maruel): Handle corrupted stack cases:
|
||||
// - missed stack barrier
|
||||
// - found next stack barrier at 0x123; expected
|
||||
// - runtime: unexpected return pc for FUNC_NAME called from 0x123
|
||||
|
||||
reRoutineHeader = regexp.MustCompile("^goroutine (\\d+) \\[([^\\]]+)\\]\\:\r?\n$")
|
||||
reMinutes = regexp.MustCompile("^(\\d+) minutes$")
|
||||
reUnavail = regexp.MustCompile("^(?:\t| +)goroutine running on other thread; stack unavailable")
|
||||
// See gentraceback() in src/runtime/traceback.go for more information.
|
||||
// - Sometimes the source file comes up as "<autogenerated>". It is the
|
||||
// compiler than generated these, not the runtime.
|
||||
// - The tab may be replaced with spaces when a user copy-paste it, handle
|
||||
// this transparently.
|
||||
// - "runtime.gopanic" is explicitly replaced with "panic" by gentraceback().
|
||||
// - The +0x123 byte offset is printed when frame.pc > _func.entry. _func is
|
||||
// generated by the linker.
|
||||
// - The +0x123 byte offset is not included with generated code, e.g. unnamed
|
||||
// functions "func·006()" which is generally go func() { ... }()
|
||||
// statements. Since the _func is generated at runtime, it's probably why
|
||||
// _func.entry is not set.
|
||||
// - C calls may have fp=0x123 sp=0x123 appended. I think it normally happens
|
||||
// when a signal is not correctly handled. It is printed with m.throwing>0.
|
||||
// These are discarded.
|
||||
// - For cgo, the source file may be "??".
|
||||
reFile = regexp.MustCompile("^(?:\t| +)(\\?\\?|\\<autogenerated\\>|.+\\.(?:c|go|s))\\:(\\d+)(?:| \\+0x[0-9a-f]+)(?:| fp=0x[0-9a-f]+ sp=0x[0-9a-f]+)\r?\n$")
|
||||
// Sadly, it doesn't note the goroutine number so we could cascade them per
|
||||
// parenthood.
|
||||
reCreated = regexp.MustCompile("^created by (.+)\r?\n$")
|
||||
reFunc = regexp.MustCompile("^(.+)\\((.*)\\)\r?\n$")
|
||||
reElided = regexp.MustCompile("^\\.\\.\\.additional frames elided\\.\\.\\.\r?\n$")
|
||||
|
||||
// TODO(maruel): This is a global state, affected by ParseDump(). This will
|
||||
// be refactored in v2.
|
||||
|
||||
// goroot is the GOROOT as detected in the traceback, not the on the host.
|
||||
//
|
||||
// It can be empty if no root was determined, for example the traceback
|
||||
// contains only non-stdlib source references.
|
||||
goroot string
|
||||
// gopaths is the GOPATH as detected in the traceback, with the value being
|
||||
// the corresponding path mapped to the host.
|
||||
//
|
||||
// It can be empty if only stdlib code is in the traceback or if no local
|
||||
// sources were matched up. In the general case there is only one.
|
||||
gopaths map[string]string
|
||||
// Corresponding local values on the host.
|
||||
localgoroot = runtime.GOROOT()
|
||||
localgopaths = getGOPATHs()
|
||||
)
|
||||
|
||||
// Function is a function call.
|
||||
//
|
||||
// Go stack traces print a mangled function call, this wrapper unmangle the
|
||||
// string before printing and adds other filtering methods.
|
||||
type Function struct {
|
||||
Raw string
|
||||
}
|
||||
|
||||
// String is the fully qualified function name.
|
||||
//
|
||||
// Sadly Go is a bit confused when the package name doesn't match the directory
|
||||
// containing the source file and will use the directory name instead of the
|
||||
// real package name.
|
||||
func (f Function) String() string {
|
||||
s, _ := url.QueryUnescape(f.Raw)
|
||||
return s
|
||||
}
|
||||
|
||||
// Name is the naked function name.
|
||||
func (f Function) Name() string {
|
||||
parts := strings.SplitN(filepath.Base(f.Raw), ".", 2)
|
||||
if len(parts) == 1 {
|
||||
return parts[0]
|
||||
}
|
||||
return parts[1]
|
||||
}
|
||||
|
||||
// PkgName is the package name for this function reference.
|
||||
func (f Function) PkgName() string {
|
||||
parts := strings.SplitN(filepath.Base(f.Raw), ".", 2)
|
||||
if len(parts) == 1 {
|
||||
return ""
|
||||
}
|
||||
s, _ := url.QueryUnescape(parts[0])
|
||||
return s
|
||||
}
|
||||
|
||||
// PkgDotName returns "<package>.<func>" format.
|
||||
func (f Function) PkgDotName() string {
|
||||
parts := strings.SplitN(filepath.Base(f.Raw), ".", 2)
|
||||
s, _ := url.QueryUnescape(parts[0])
|
||||
if len(parts) == 1 {
|
||||
return parts[0]
|
||||
}
|
||||
if s != "" || parts[1] != "" {
|
||||
return s + "." + parts[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsExported returns true if the function is exported.
|
||||
func (f Function) IsExported() bool {
|
||||
name := f.Name()
|
||||
parts := strings.Split(name, ".")
|
||||
r, _ := utf8.DecodeRuneInString(parts[len(parts)-1])
|
||||
if unicode.ToUpper(r) == r {
|
||||
return true
|
||||
}
|
||||
return f.PkgName() == "main" && name == "main"
|
||||
}
|
||||
|
||||
// Arg is an argument on a Call.
|
||||
type Arg struct {
|
||||
Value uint64 // Value is the raw value as found in the stack trace
|
||||
Name string // Name is a pseudo name given to the argument
|
||||
}
|
||||
|
||||
// IsPtr returns true if we guess it's a pointer. It's only a guess, it can be
|
||||
// easily be confused by a bitmask.
|
||||
func (a *Arg) IsPtr() bool {
|
||||
// Assumes all pointers are above 16Mb and positive.
|
||||
return a.Value > 16*1024*1024 && a.Value < math.MaxInt64
|
||||
}
|
||||
|
||||
func (a Arg) String() string {
|
||||
if a.Name != "" {
|
||||
return a.Name
|
||||
}
|
||||
if a.Value == 0 {
|
||||
return "0"
|
||||
}
|
||||
return fmt.Sprintf("0x%x", a.Value)
|
||||
}
|
||||
|
||||
// Args is a series of function call arguments.
|
||||
type Args struct {
|
||||
Values []Arg // Values is the arguments as shown on the stack trace. They are mangled via simplification.
|
||||
Processed []string // Processed is the arguments generated from processing the source files. It can have a length lower than Values.
|
||||
Elided bool // If set, it means there was a trailing ", ..."
|
||||
}
|
||||
|
||||
func (a Args) String() string {
|
||||
var v []string
|
||||
if len(a.Processed) != 0 {
|
||||
v = make([]string, 0, len(a.Processed))
|
||||
for _, item := range a.Processed {
|
||||
v = append(v, item)
|
||||
}
|
||||
} else {
|
||||
v = make([]string, 0, len(a.Values))
|
||||
for _, item := range a.Values {
|
||||
v = append(v, item.String())
|
||||
}
|
||||
}
|
||||
if a.Elided {
|
||||
v = append(v, "...")
|
||||
}
|
||||
return strings.Join(v, ", ")
|
||||
}
|
||||
|
||||
// Equal returns true only if both arguments are exactly equal.
|
||||
func (a *Args) Equal(r *Args) bool {
|
||||
if a.Elided != r.Elided || len(a.Values) != len(r.Values) {
|
||||
return false
|
||||
}
|
||||
for i, l := range a.Values {
|
||||
if l != r.Values[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Similar returns true if the two Args are equal or almost but not quite
|
||||
// equal.
|
||||
func (a *Args) Similar(r *Args, similar Similarity) bool {
|
||||
if a.Elided != r.Elided || len(a.Values) != len(r.Values) {
|
||||
return false
|
||||
}
|
||||
if similar == AnyValue {
|
||||
return true
|
||||
}
|
||||
for i, l := range a.Values {
|
||||
switch similar {
|
||||
case ExactFlags, ExactLines:
|
||||
if l != r.Values[i] {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if l.IsPtr() != r.Values[i].IsPtr() || (!l.IsPtr() && l != r.Values[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Merge merges two similar Args, zapping out differences.
|
||||
func (a *Args) Merge(r *Args) Args {
|
||||
out := Args{
|
||||
Values: make([]Arg, len(a.Values)),
|
||||
Elided: a.Elided,
|
||||
}
|
||||
for i, l := range a.Values {
|
||||
if l != r.Values[i] {
|
||||
out.Values[i].Name = "*"
|
||||
out.Values[i].Value = l.Value
|
||||
} else {
|
||||
out.Values[i] = l
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Call is an item in the stack trace.
|
||||
type Call struct {
|
||||
SourcePath string // Full path name of the source file as seen in the trace
|
||||
Line int // Line number
|
||||
Func Function // Fully qualified function name (encoded).
|
||||
Args Args // Call arguments
|
||||
}
|
||||
|
||||
// Equal returns true only if both calls are exactly equal.
|
||||
func (c *Call) Equal(r *Call) bool {
|
||||
return c.SourcePath == r.SourcePath && c.Line == r.Line && c.Func == r.Func && c.Args.Equal(&r.Args)
|
||||
}
|
||||
|
||||
// Similar returns true if the two Call are equal or almost but not quite
|
||||
// equal.
|
||||
func (c *Call) Similar(r *Call, similar Similarity) bool {
|
||||
return c.SourcePath == r.SourcePath && c.Line == r.Line && c.Func == r.Func && c.Args.Similar(&r.Args, similar)
|
||||
}
|
||||
|
||||
// Merge merges two similar Call, zapping out differences.
|
||||
func (c *Call) Merge(r *Call) Call {
|
||||
return Call{
|
||||
SourcePath: c.SourcePath,
|
||||
Line: c.Line,
|
||||
Func: c.Func,
|
||||
Args: c.Args.Merge(&r.Args),
|
||||
}
|
||||
}
|
||||
|
||||
// SourceName returns the base file name of the source file.
|
||||
func (c *Call) SourceName() string {
|
||||
return filepath.Base(c.SourcePath)
|
||||
}
|
||||
|
||||
// SourceLine returns "source.go:line", including only the base file name.
|
||||
func (c *Call) SourceLine() string {
|
||||
return fmt.Sprintf("%s:%d", c.SourceName(), c.Line)
|
||||
}
|
||||
|
||||
// LocalSourcePath is the full path name of the source file as seen in the host.
|
||||
func (c *Call) LocalSourcePath() string {
|
||||
// TODO(maruel): Call needs members goroot and gopaths.
|
||||
if strings.HasPrefix(c.SourcePath, goroot) {
|
||||
return filepath.Join(localgoroot, c.SourcePath[len(goroot):])
|
||||
}
|
||||
for prefix, dest := range gopaths {
|
||||
if strings.HasPrefix(c.SourcePath, prefix) {
|
||||
return filepath.Join(dest, c.SourcePath[len(prefix):])
|
||||
}
|
||||
}
|
||||
return c.SourcePath
|
||||
}
|
||||
|
||||
// FullSourceLine returns "/path/to/source.go:line".
|
||||
//
|
||||
// This file path is mutated to look like the local path.
|
||||
func (c *Call) FullSourceLine() string {
|
||||
return fmt.Sprintf("%s:%d", c.SourcePath, c.Line)
|
||||
}
|
||||
|
||||
// PkgSource is one directory plus the file name of the source file.
|
||||
func (c *Call) PkgSource() string {
|
||||
return filepath.Join(filepath.Base(filepath.Dir(c.SourcePath)), c.SourceName())
|
||||
}
|
||||
|
||||
const testMainSource = "_test" + string(os.PathSeparator) + "_testmain.go"
|
||||
|
||||
// IsStdlib returns true if it is a Go standard library function. This includes
|
||||
// the 'go test' generated main executable.
|
||||
func (c *Call) IsStdlib() bool {
|
||||
// Consider _test/_testmain.go as stdlib since it's injected by "go test".
|
||||
return (goroot != "" && strings.HasPrefix(c.SourcePath, goroot)) || c.PkgSource() == testMainSource
|
||||
}
|
||||
|
||||
// IsPkgMain returns true if it is in the main package.
|
||||
func (c *Call) IsPkgMain() bool {
|
||||
return c.Func.PkgName() == "main"
|
||||
}
|
||||
|
||||
// Stack is a call stack.
|
||||
type Stack struct {
|
||||
Calls []Call // Call stack. First is original function, last is leaf function.
|
||||
Elided bool // Happens when there's >100 items in Stack, currently hardcoded in package runtime.
|
||||
}
|
||||
|
||||
// Equal returns true on if both call stacks are exactly equal.
|
||||
func (s *Stack) Equal(r *Stack) bool {
|
||||
if len(s.Calls) != len(r.Calls) || s.Elided != r.Elided {
|
||||
return false
|
||||
}
|
||||
for i := range s.Calls {
|
||||
if !s.Calls[i].Equal(&r.Calls[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Similar returns true if the two Stack are equal or almost but not quite
|
||||
// equal.
|
||||
func (s *Stack) Similar(r *Stack, similar Similarity) bool {
|
||||
if len(s.Calls) != len(r.Calls) || s.Elided != r.Elided {
|
||||
return false
|
||||
}
|
||||
for i := range s.Calls {
|
||||
if !s.Calls[i].Similar(&r.Calls[i], similar) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Merge merges two similar Stack, zapping out differences.
|
||||
func (s *Stack) Merge(r *Stack) *Stack {
|
||||
// Assumes similar stacks have the same length.
|
||||
out := &Stack{
|
||||
Calls: make([]Call, len(s.Calls)),
|
||||
Elided: s.Elided,
|
||||
}
|
||||
for i := range s.Calls {
|
||||
out.Calls[i] = s.Calls[i].Merge(&r.Calls[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Less compares two Stack, where the ones that are less are more
|
||||
// important, so they come up front. A Stack with more private functions is
|
||||
// 'less' so it is at the top. Inversely, a Stack with only public
|
||||
// functions is 'more' so it is at the bottom.
|
||||
func (s *Stack) Less(r *Stack) bool {
|
||||
lStdlib := 0
|
||||
lPrivate := 0
|
||||
for _, c := range s.Calls {
|
||||
if c.IsStdlib() {
|
||||
lStdlib++
|
||||
} else {
|
||||
lPrivate++
|
||||
}
|
||||
}
|
||||
rStdlib := 0
|
||||
rPrivate := 0
|
||||
for _, s := range r.Calls {
|
||||
if s.IsStdlib() {
|
||||
rStdlib++
|
||||
} else {
|
||||
rPrivate++
|
||||
}
|
||||
}
|
||||
if lPrivate > rPrivate {
|
||||
return true
|
||||
}
|
||||
if lPrivate < rPrivate {
|
||||
return false
|
||||
}
|
||||
if lStdlib > rStdlib {
|
||||
return false
|
||||
}
|
||||
if lStdlib < rStdlib {
|
||||
return true
|
||||
}
|
||||
|
||||
// Stack lengths are the same.
|
||||
for x := range s.Calls {
|
||||
if s.Calls[x].Func.Raw < r.Calls[x].Func.Raw {
|
||||
return true
|
||||
}
|
||||
if s.Calls[x].Func.Raw > r.Calls[x].Func.Raw {
|
||||
return true
|
||||
}
|
||||
if s.Calls[x].PkgSource() < r.Calls[x].PkgSource() {
|
||||
return true
|
||||
}
|
||||
if s.Calls[x].PkgSource() > r.Calls[x].PkgSource() {
|
||||
return true
|
||||
}
|
||||
if s.Calls[x].Line < r.Calls[x].Line {
|
||||
return true
|
||||
}
|
||||
if s.Calls[x].Line > r.Calls[x].Line {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Signature represents the signature of one or multiple goroutines.
|
||||
//
|
||||
// It is effectively the stack trace plus the goroutine internal bits, like
|
||||
// it's state, if it is thread locked, which call site created this goroutine,
|
||||
// etc.
|
||||
type Signature struct {
|
||||
// Use git grep 'gopark(|unlock)\(' to find them all plus everything listed
|
||||
// in runtime/traceback.go. Valid values includes:
|
||||
// - chan send, chan receive, select
|
||||
// - finalizer wait, mark wait (idle),
|
||||
// - Concurrent GC wait, GC sweep wait, force gc (idle)
|
||||
// - IO wait, panicwait
|
||||
// - semacquire, semarelease
|
||||
// - sleep, timer goroutine (idle)
|
||||
// - trace reader (blocked)
|
||||
// Stuck cases:
|
||||
// - chan send (nil chan), chan receive (nil chan), select (no cases)
|
||||
// Runnable states:
|
||||
// - idle, runnable, running, syscall, waiting, dead, enqueue, copystack,
|
||||
// Scan states:
|
||||
// - scan, scanrunnable, scanrunning, scansyscall, scanwaiting, scandead,
|
||||
// scanenqueue
|
||||
State string
|
||||
CreatedBy Call // Which other goroutine which created this one.
|
||||
SleepMin int // Wait time in minutes, if applicable.
|
||||
SleepMax int // Wait time in minutes, if applicable.
|
||||
Stack Stack
|
||||
Locked bool // Locked to an OS thread.
|
||||
}
|
||||
|
||||
// Equal returns true only if both signatures are exactly equal.
|
||||
func (s *Signature) Equal(r *Signature) bool {
|
||||
if s.State != r.State || !s.CreatedBy.Equal(&r.CreatedBy) || s.Locked != r.Locked || s.SleepMin != r.SleepMin || s.SleepMax != r.SleepMax {
|
||||
return false
|
||||
}
|
||||
return s.Stack.Equal(&r.Stack)
|
||||
}
|
||||
|
||||
// Similar returns true if the two Signature are equal or almost but not quite
|
||||
// equal.
|
||||
func (s *Signature) Similar(r *Signature, similar Similarity) bool {
|
||||
if s.State != r.State || !s.CreatedBy.Similar(&r.CreatedBy, similar) {
|
||||
return false
|
||||
}
|
||||
if similar == ExactFlags && s.Locked != r.Locked {
|
||||
return false
|
||||
}
|
||||
return s.Stack.Similar(&r.Stack, similar)
|
||||
}
|
||||
|
||||
// Merge merges two similar Signature, zapping out differences.
|
||||
func (s *Signature) Merge(r *Signature) *Signature {
|
||||
min := s.SleepMin
|
||||
if r.SleepMin < min {
|
||||
min = r.SleepMin
|
||||
}
|
||||
max := s.SleepMax
|
||||
if r.SleepMax > max {
|
||||
max = r.SleepMax
|
||||
}
|
||||
return &Signature{
|
||||
State: s.State, // Drop right side.
|
||||
CreatedBy: s.CreatedBy, // Drop right side.
|
||||
SleepMin: min,
|
||||
SleepMax: max,
|
||||
Stack: *s.Stack.Merge(&r.Stack),
|
||||
Locked: s.Locked || r.Locked, // TODO(maruel): This is weirdo.
|
||||
}
|
||||
}
|
||||
|
||||
// Less compares two Signature, where the ones that are less are more
|
||||
// important, so they come up front. A Signature with more private functions is
|
||||
// 'less' so it is at the top. Inversely, a Signature with only public
|
||||
// functions is 'more' so it is at the bottom.
|
||||
func (s *Signature) Less(r *Signature) bool {
|
||||
if s.Stack.Less(&r.Stack) {
|
||||
return true
|
||||
}
|
||||
if r.Stack.Less(&s.Stack) {
|
||||
return false
|
||||
}
|
||||
if s.Locked && !r.Locked {
|
||||
return true
|
||||
}
|
||||
if r.Locked && !s.Locked {
|
||||
return false
|
||||
}
|
||||
if s.State < r.State {
|
||||
return true
|
||||
}
|
||||
if s.State > r.State {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Goroutine represents the state of one goroutine, including the stack trace.
|
||||
type Goroutine struct {
|
||||
Signature // It's stack trace, internal bits, state, which call site created it, etc.
|
||||
ID int // Goroutine ID.
|
||||
First bool // First is the goroutine first printed, normally the one that crashed.
|
||||
}
|
||||
|
||||
// scanLines is similar to bufio.ScanLines except that it:
|
||||
// - doesn't drop '\n'
|
||||
// - doesn't strip '\r'
|
||||
// - returns when the data is bufio.MaxScanTokenSize bytes
|
||||
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
if atEOF && len(data) == 0 {
|
||||
return 0, nil, nil
|
||||
}
|
||||
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
||||
return i + 1, data[0 : i+1], nil
|
||||
}
|
||||
if atEOF {
|
||||
return len(data), data, nil
|
||||
}
|
||||
if len(data) >= bufio.MaxScanTokenSize {
|
||||
// Returns the line even if it is not at EOF nor has a '\n', otherwise the
|
||||
// scanner will return bufio.ErrTooLong which is definitely not what we
|
||||
// want.
|
||||
return len(data), data, nil
|
||||
}
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
// ParseDump processes the output from runtime.Stack().
|
||||
//
|
||||
// It supports piping from another command and assumes there is junk before the
|
||||
// actual stack trace. The junk is streamed to out.
|
||||
func ParseDump(r io.Reader, out io.Writer) ([]Goroutine, error) {
|
||||
goroutines := make([]Goroutine, 0, 16)
|
||||
var goroutine *Goroutine
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Split(scanLines)
|
||||
// TODO(maruel): Use a formal state machine. Patterns follows:
|
||||
// - reRoutineHeader
|
||||
// Either:
|
||||
// - reUnavail
|
||||
// - reFunc + reFile in a loop
|
||||
// - reElided
|
||||
// Optionally ends with:
|
||||
// - reCreated + reFile
|
||||
// Between each goroutine stack dump: an empty line
|
||||
created := false
|
||||
// firstLine is the first line after the reRoutineHeader header line.
|
||||
firstLine := false
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "\n" || line == "\r\n" {
|
||||
if goroutine != nil {
|
||||
goroutine = nil
|
||||
continue
|
||||
}
|
||||
} else if line[len(line)-1] == '\n' {
|
||||
if goroutine == nil {
|
||||
if match := reRoutineHeader.FindStringSubmatch(line); match != nil {
|
||||
if id, err := strconv.Atoi(match[1]); err == nil {
|
||||
// See runtime/traceback.go.
|
||||
// "<state>, \d+ minutes, locked to thread"
|
||||
items := strings.Split(match[2], ", ")
|
||||
sleep := 0
|
||||
locked := false
|
||||
for i := 1; i < len(items); i++ {
|
||||
if items[i] == lockedToThread {
|
||||
locked = true
|
||||
continue
|
||||
}
|
||||
// Look for duration, if any.
|
||||
if match2 := reMinutes.FindStringSubmatch(items[i]); match2 != nil {
|
||||
sleep, _ = strconv.Atoi(match2[1])
|
||||
}
|
||||
}
|
||||
goroutines = append(goroutines, Goroutine{
|
||||
Signature: Signature{
|
||||
State: items[0],
|
||||
SleepMin: sleep,
|
||||
SleepMax: sleep,
|
||||
Locked: locked,
|
||||
},
|
||||
ID: id,
|
||||
First: len(goroutines) == 0,
|
||||
})
|
||||
goroutine = &goroutines[len(goroutines)-1]
|
||||
firstLine = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if firstLine {
|
||||
firstLine = false
|
||||
if match := reUnavail.FindStringSubmatch(line); match != nil {
|
||||
// Generate a fake stack entry.
|
||||
goroutine.Stack.Calls = []Call{{SourcePath: "<unavailable>"}}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if match := reFile.FindStringSubmatch(line); match != nil {
|
||||
// Triggers after a reFunc or a reCreated.
|
||||
num, err := strconv.Atoi(match[2])
|
||||
if err != nil {
|
||||
return goroutines, fmt.Errorf("failed to parse int on line: \"%s\"", line)
|
||||
}
|
||||
if created {
|
||||
created = false
|
||||
goroutine.CreatedBy.SourcePath = match[1]
|
||||
goroutine.CreatedBy.Line = num
|
||||
} else {
|
||||
i := len(goroutine.Stack.Calls) - 1
|
||||
if i < 0 {
|
||||
return goroutines, errors.New("unexpected order")
|
||||
}
|
||||
goroutine.Stack.Calls[i].SourcePath = match[1]
|
||||
goroutine.Stack.Calls[i].Line = num
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if match := reCreated.FindStringSubmatch(line); match != nil {
|
||||
created = true
|
||||
goroutine.CreatedBy.Func.Raw = match[1]
|
||||
continue
|
||||
}
|
||||
|
||||
if match := reFunc.FindStringSubmatch(line); match != nil {
|
||||
args := Args{}
|
||||
for _, a := range strings.Split(match[2], ", ") {
|
||||
if a == "..." {
|
||||
args.Elided = true
|
||||
continue
|
||||
}
|
||||
if a == "" {
|
||||
// Remaining values were dropped.
|
||||
break
|
||||
}
|
||||
v, err := strconv.ParseUint(a, 0, 64)
|
||||
if err != nil {
|
||||
return goroutines, fmt.Errorf("failed to parse int on line: \"%s\"", line)
|
||||
}
|
||||
args.Values = append(args.Values, Arg{Value: v})
|
||||
}
|
||||
goroutine.Stack.Calls = append(goroutine.Stack.Calls, Call{Func: Function{match[1]}, Args: args})
|
||||
continue
|
||||
}
|
||||
|
||||
if match := reElided.FindStringSubmatch(line); match != nil {
|
||||
goroutine.Stack.Elided = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
_, _ = io.WriteString(out, line)
|
||||
goroutine = nil
|
||||
}
|
||||
nameArguments(goroutines)
|
||||
// Mutate global state.
|
||||
// TODO(maruel): Make this part of the context instead of a global.
|
||||
if goroot == "" {
|
||||
findRoots(goroutines)
|
||||
}
|
||||
return goroutines, scanner.Err()
|
||||
}
|
||||
|
||||
// NoRebase disables GOROOT and GOPATH guessing in ParseDump().
|
||||
//
|
||||
// BUG: This function will be removed in v2, as ParseDump() will accept a flag
|
||||
// explicitly.
|
||||
func NoRebase() {
|
||||
goroot = runtime.GOROOT()
|
||||
gopaths = map[string]string{}
|
||||
for _, p := range getGOPATHs() {
|
||||
gopaths[p] = p
|
||||
}
|
||||
}
|
||||
|
||||
// Private stuff.
|
||||
|
||||
func nameArguments(goroutines []Goroutine) {
|
||||
// Set a name for any pointer occurring more than once.
|
||||
type object struct {
|
||||
args []*Arg
|
||||
inPrimary bool
|
||||
id int
|
||||
}
|
||||
objects := map[uint64]object{}
|
||||
// Enumerate all the arguments.
|
||||
for i := range goroutines {
|
||||
for j := range goroutines[i].Stack.Calls {
|
||||
for k := range goroutines[i].Stack.Calls[j].Args.Values {
|
||||
arg := goroutines[i].Stack.Calls[j].Args.Values[k]
|
||||
if arg.IsPtr() {
|
||||
objects[arg.Value] = object{
|
||||
args: append(objects[arg.Value].args, &goroutines[i].Stack.Calls[j].Args.Values[k]),
|
||||
inPrimary: objects[arg.Value].inPrimary || i == 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// CreatedBy.Args is never set.
|
||||
}
|
||||
order := make(uint64Slice, 0, len(objects)/2)
|
||||
for k, obj := range objects {
|
||||
if len(obj.args) > 1 && obj.inPrimary {
|
||||
order = append(order, k)
|
||||
}
|
||||
}
|
||||
sort.Sort(order)
|
||||
nextID := 1
|
||||
for _, k := range order {
|
||||
for _, arg := range objects[k].args {
|
||||
arg.Name = fmt.Sprintf("#%d", nextID)
|
||||
}
|
||||
nextID++
|
||||
}
|
||||
|
||||
// Now do the rest. This is done so the output is deterministic.
|
||||
order = make(uint64Slice, 0, len(objects))
|
||||
for k := range objects {
|
||||
order = append(order, k)
|
||||
}
|
||||
sort.Sort(order)
|
||||
for _, k := range order {
|
||||
// Process the remaining pointers, they were not referenced by primary
|
||||
// thread so will have higher IDs.
|
||||
if objects[k].inPrimary {
|
||||
continue
|
||||
}
|
||||
for _, arg := range objects[k].args {
|
||||
arg.Name = fmt.Sprintf("#%d", nextID)
|
||||
}
|
||||
nextID++
|
||||
}
|
||||
}
|
||||
|
||||
// hasPathPrefix returns true if any of s is the prefix of p.
|
||||
func hasPathPrefix(p string, s map[string]string) bool {
|
||||
for prefix := range s {
|
||||
if strings.HasPrefix(p, prefix+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getFiles returns all the source files deduped and ordered.
|
||||
func getFiles(goroutines []Goroutine) []string {
|
||||
files := map[string]struct{}{}
|
||||
for _, g := range goroutines {
|
||||
for _, c := range g.Stack.Calls {
|
||||
files[c.SourcePath] = struct{}{}
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(files))
|
||||
for f := range files {
|
||||
out = append(out, f)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// splitPath splits a path into its components.
|
||||
//
|
||||
// The first item has its initial path separator kept.
|
||||
func splitPath(p string) []string {
|
||||
if p == "" {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
s := ""
|
||||
for _, c := range p {
|
||||
if c != '/' || (len(out) == 0 && strings.Count(s, "/") == len(s)) {
|
||||
s += string(c)
|
||||
} else if s != "" {
|
||||
out = append(out, s)
|
||||
s = ""
|
||||
}
|
||||
}
|
||||
if s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// isFile returns true if the path is a valid file.
|
||||
func isFile(p string) bool {
|
||||
// TODO(maruel): Is it faster to open the file or to stat it? Worth a perf
|
||||
// test on Windows.
|
||||
i, err := os.Stat(p)
|
||||
return err == nil && !i.IsDir()
|
||||
}
|
||||
|
||||
// isRootIn returns a root if the file split in parts is rooted in root.
|
||||
func rootedIn(root string, parts []string) string {
|
||||
//log.Printf("rootIn(%s, %v)", root, parts)
|
||||
for i := 1; i < len(parts); i++ {
|
||||
suffix := filepath.Join(parts[i:]...)
|
||||
if isFile(filepath.Join(root, suffix)) {
|
||||
return filepath.Join(parts[:i]...)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// findRoots sets global variables goroot and gopath.
|
||||
//
|
||||
// TODO(maruel): In v2, it will be a property of the new struct that will
|
||||
// contain the goroutines.
|
||||
func findRoots(goroutines []Goroutine) {
|
||||
gopaths = map[string]string{}
|
||||
for _, f := range getFiles(goroutines) {
|
||||
// TODO(maruel): Could a stack dump have mixed cases? I think it's
|
||||
// possible, need to confirm and handle.
|
||||
//log.Printf(" Analyzing %s", f)
|
||||
if goroot != "" && strings.HasPrefix(f, goroot+"/") {
|
||||
continue
|
||||
}
|
||||
if gopaths != nil && hasPathPrefix(f, gopaths) {
|
||||
continue
|
||||
}
|
||||
parts := splitPath(f)
|
||||
if goroot == "" {
|
||||
if r := rootedIn(localgoroot, parts); r != "" {
|
||||
goroot = r
|
||||
log.Printf("Found GOROOT=%s", goroot)
|
||||
continue
|
||||
}
|
||||
}
|
||||
found := false
|
||||
for _, l := range localgopaths {
|
||||
if r := rootedIn(l, parts); r != "" {
|
||||
log.Printf("Found GOPATH=%s", r)
|
||||
gopaths[r] = l
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
// If the source is not found, just too bad.
|
||||
//log.Printf("Failed to find locally: %s / %s", f, goroot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getGOPATHs() []string {
|
||||
var out []string
|
||||
for _, v := range filepath.SplitList(os.Getenv("GOPATH")) {
|
||||
// Disallow non-absolute paths?
|
||||
if v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
homeDir := ""
|
||||
u, err := user.Current()
|
||||
if err != nil {
|
||||
homeDir = os.Getenv("HOME")
|
||||
if homeDir == "" {
|
||||
panic(fmt.Sprintf("Could not get current user or $HOME: %s\n", err.Error()))
|
||||
}
|
||||
} else {
|
||||
homeDir = u.HomeDir
|
||||
}
|
||||
out = []string{homeDir + "go"}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type uint64Slice []uint64
|
||||
|
||||
func (a uint64Slice) Len() int { return len(a) }
|
||||
func (a uint64Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a uint64Slice) Less(i, j int) bool { return a[i] < a[j] }
|
@ -0,0 +1,139 @@
|
||||
// Copyright 2016 Marc-Antoine Ruel. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Palette defines the color used.
|
||||
//
|
||||
// An empty object Palette{} can be used to disable coloring.
|
||||
type Palette struct {
|
||||
EOLReset string
|
||||
|
||||
// Routine header.
|
||||
RoutineFirst string // The first routine printed.
|
||||
Routine string // Following routines.
|
||||
CreatedBy string
|
||||
|
||||
// Call line.
|
||||
Package string
|
||||
SourceFile string
|
||||
FunctionStdLib string
|
||||
FunctionStdLibExported string
|
||||
FunctionMain string
|
||||
FunctionOther string
|
||||
FunctionOtherExported string
|
||||
Arguments string
|
||||
}
|
||||
|
||||
// CalcLengths returns the maximum length of the source lines and package names.
|
||||
func CalcLengths(buckets Buckets, fullPath bool) (int, int) {
|
||||
srcLen := 0
|
||||
pkgLen := 0
|
||||
for _, bucket := range buckets {
|
||||
for _, line := range bucket.Signature.Stack.Calls {
|
||||
l := 0
|
||||
if fullPath {
|
||||
l = len(line.FullSourceLine())
|
||||
} else {
|
||||
l = len(line.SourceLine())
|
||||
}
|
||||
if l > srcLen {
|
||||
srcLen = l
|
||||
}
|
||||
l = len(line.Func.PkgName())
|
||||
if l > pkgLen {
|
||||
pkgLen = l
|
||||
}
|
||||
}
|
||||
}
|
||||
return srcLen, pkgLen
|
||||
}
|
||||
|
||||
// functionColor returns the color to be used for the function name based on
|
||||
// the type of package the function is in.
|
||||
func (p *Palette) functionColor(line *Call) string {
|
||||
if line.IsStdlib() {
|
||||
if line.Func.IsExported() {
|
||||
return p.FunctionStdLibExported
|
||||
}
|
||||
return p.FunctionStdLib
|
||||
} else if line.IsPkgMain() {
|
||||
return p.FunctionMain
|
||||
} else if line.Func.IsExported() {
|
||||
return p.FunctionOtherExported
|
||||
}
|
||||
return p.FunctionOther
|
||||
}
|
||||
|
||||
// routineColor returns the color for the header of the goroutines bucket.
|
||||
func (p *Palette) routineColor(bucket *Bucket, multipleBuckets bool) string {
|
||||
if bucket.First() && multipleBuckets {
|
||||
return p.RoutineFirst
|
||||
}
|
||||
return p.Routine
|
||||
}
|
||||
|
||||
// BucketHeader prints the header of a goroutine signature.
|
||||
func (p *Palette) BucketHeader(bucket *Bucket, fullPath, multipleBuckets bool) string {
|
||||
extra := ""
|
||||
if bucket.SleepMax != 0 {
|
||||
if bucket.SleepMin != bucket.SleepMax {
|
||||
extra += fmt.Sprintf(" [%d~%d minutes]", bucket.SleepMin, bucket.SleepMax)
|
||||
} else {
|
||||
extra += fmt.Sprintf(" [%d minutes]", bucket.SleepMax)
|
||||
}
|
||||
}
|
||||
if bucket.Locked {
|
||||
extra += " [locked]"
|
||||
}
|
||||
created := bucket.CreatedBy.Func.PkgDotName()
|
||||
if created != "" {
|
||||
created += " @ "
|
||||
if fullPath {
|
||||
created += bucket.CreatedBy.FullSourceLine()
|
||||
} else {
|
||||
created += bucket.CreatedBy.SourceLine()
|
||||
}
|
||||
extra += p.CreatedBy + " [Created by " + created + "]"
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"%s%d: %s%s%s\n",
|
||||
p.routineColor(bucket, multipleBuckets), len(bucket.Routines),
|
||||
bucket.State, extra,
|
||||
p.EOLReset)
|
||||
}
|
||||
|
||||
// callLine prints one stack line.
|
||||
func (p *Palette) callLine(line *Call, srcLen, pkgLen int, fullPath bool) string {
|
||||
src := ""
|
||||
if fullPath {
|
||||
src = line.FullSourceLine()
|
||||
} else {
|
||||
src = line.SourceLine()
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
" %s%-*s %s%-*s %s%s%s(%s)%s",
|
||||
p.Package, pkgLen, line.Func.PkgName(),
|
||||
p.SourceFile, srcLen, src,
|
||||
p.functionColor(line), line.Func.Name(),
|
||||
p.Arguments, line.Args,
|
||||
p.EOLReset)
|
||||
}
|
||||
|
||||
// StackLines prints one complete stack trace, without the header.
|
||||
func (p *Palette) StackLines(signature *Signature, srcLen, pkgLen int, fullPath bool) string {
|
||||
out := make([]string, len(signature.Stack.Calls))
|
||||
for i := range signature.Stack.Calls {
|
||||
out[i] = p.callLine(&signature.Stack.Calls[i], srcLen, pkgLen, fullPath)
|
||||
}
|
||||
if signature.Stack.Elided {
|
||||
out = append(out, " (...)")
|
||||
}
|
||||
return strings.Join(out, "\n") + "\n"
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
language: go
|
||||
go:
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get github.com/mattn/goveralls
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
script:
|
||||
- $HOME/gopath/bin/goveralls -repotoken xnXqRGwgW3SXIguzxf90ZSK1GPYZPaGrw
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Yasuhiro Matsumoto
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,48 @@
|
||||
# go-colorable
|
||||
|
||||
[![Godoc Reference](https://godoc.org/github.com/mattn/go-colorable?status.svg)](http://godoc.org/github.com/mattn/go-colorable)
|
||||
[![Build Status](https://travis-ci.org/mattn/go-colorable.svg?branch=master)](https://travis-ci.org/mattn/go-colorable)
|
||||
[![Coverage Status](https://coveralls.io/repos/github/mattn/go-colorable/badge.svg?branch=master)](https://coveralls.io/github/mattn/go-colorable?branch=master)
|
||||
[![Go Report Card](https://goreportcard.com/badge/mattn/go-colorable)](https://goreportcard.com/report/mattn/go-colorable)
|
||||
|
||||
Colorable writer for windows.
|
||||
|
||||
For example, most of logger packages doesn't show colors on windows. (I know we can do it with ansicon. But I don't want.)
|
||||
This package is possible to handle escape sequence for ansi color on windows.
|
||||
|
||||
## Too Bad!
|
||||
|
||||
![](https://raw.githubusercontent.com/mattn/go-colorable/gh-pages/bad.png)
|
||||
|
||||
|
||||
## So Good!
|
||||
|
||||
![](https://raw.githubusercontent.com/mattn/go-colorable/gh-pages/good.png)
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
||||
logrus.SetOutput(colorable.NewColorableStdout())
|
||||
|
||||
logrus.Info("succeeded")
|
||||
logrus.Warn("not correct")
|
||||
logrus.Error("something error")
|
||||
logrus.Fatal("panic")
|
||||
```
|
||||
|
||||
You can compile above code on non-windows OSs.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
$ go get github.com/mattn/go-colorable
|
||||
```
|
||||
|
||||
# License
|
||||
|
||||
MIT
|
||||
|
||||
# Author
|
||||
|
||||
Yasuhiro Matsumoto (a.k.a mattn)
|
@ -0,0 +1,29 @@
|
||||
// +build appengine
|
||||
|
||||
package colorable
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
_ "github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
// NewColorable return new instance of Writer which handle escape sequence.
|
||||
func NewColorable(file *os.File) io.Writer {
|
||||
if file == nil {
|
||||
panic("nil passed instead of *os.File to NewColorable()")
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
// NewColorableStdout return new instance of Writer which handle escape sequence for stdout.
|
||||
func NewColorableStdout() io.Writer {
|
||||
return os.Stdout
|
||||
}
|
||||
|
||||
// NewColorableStderr return new instance of Writer which handle escape sequence for stderr.
|
||||
func NewColorableStderr() io.Writer {
|
||||
return os.Stderr
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
// +build !windows
|
||||
// +build !appengine
|
||||
|
||||
package colorable
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
_ "github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
// NewColorable return new instance of Writer which handle escape sequence.
|
||||
func NewColorable(file *os.File) io.Writer {
|
||||
if file == nil {
|
||||
panic("nil passed instead of *os.File to NewColorable()")
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
// NewColorableStdout return new instance of Writer which handle escape sequence for stdout.
|
||||
func NewColorableStdout() io.Writer {
|
||||
return os.Stdout
|
||||
}
|
||||
|
||||
// NewColorableStderr return new instance of Writer which handle escape sequence for stderr.
|
||||
func NewColorableStderr() io.Writer {
|
||||
return os.Stderr
|
||||
}
|
@ -0,0 +1,884 @@
|
||||
// +build windows
|
||||
// +build !appengine
|
||||
|
||||
package colorable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
const (
|
||||
foregroundBlue = 0x1
|
||||
foregroundGreen = 0x2
|
||||
foregroundRed = 0x4
|
||||
foregroundIntensity = 0x8
|
||||
foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
|
||||
backgroundBlue = 0x10
|
||||
backgroundGreen = 0x20
|
||||
backgroundRed = 0x40
|
||||
backgroundIntensity = 0x80
|
||||
backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
|
||||
)
|
||||
|
||||
type wchar uint16
|
||||
type short int16
|
||||
type dword uint32
|
||||
type word uint16
|
||||
|
||||
type coord struct {
|
||||
x short
|
||||
y short
|
||||
}
|
||||
|
||||
type smallRect struct {
|
||||
left short
|
||||
top short
|
||||
right short
|
||||
bottom short
|
||||
}
|
||||
|
||||
type consoleScreenBufferInfo struct {
|
||||
size coord
|
||||
cursorPosition coord
|
||||
attributes word
|
||||
window smallRect
|
||||
maximumWindowSize coord
|
||||
}
|
||||
|
||||
type consoleCursorInfo struct {
|
||||
size dword
|
||||
visible int32
|
||||
}
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
|
||||
procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
|
||||
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
|
||||
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
|
||||
procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
|
||||
procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
|
||||
procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
|
||||
procSetConsoleTitle = kernel32.NewProc("SetConsoleTitleW")
|
||||
)
|
||||
|
||||
// Writer provide colorable Writer to the console
|
||||
type Writer struct {
|
||||
out io.Writer
|
||||
handle syscall.Handle
|
||||
oldattr word
|
||||
oldpos coord
|
||||
}
|
||||
|
||||
// NewColorable return new instance of Writer which handle escape sequence from File.
|
||||
func NewColorable(file *os.File) io.Writer {
|
||||
if file == nil {
|
||||
panic("nil passed instead of *os.File to NewColorable()")
|
||||
}
|
||||
|
||||
if isatty.IsTerminal(file.Fd()) {
|
||||
var csbi consoleScreenBufferInfo
|
||||
handle := syscall.Handle(file.Fd())
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
// NewColorableStdout return new instance of Writer which handle escape sequence for stdout.
|
||||
func NewColorableStdout() io.Writer {
|
||||
return NewColorable(os.Stdout)
|
||||
}
|
||||
|
||||
// NewColorableStderr return new instance of Writer which handle escape sequence for stderr.
|
||||
func NewColorableStderr() io.Writer {
|
||||
return NewColorable(os.Stderr)
|
||||
}
|
||||
|
||||
var color256 = map[int]int{
|
||||
0: 0x000000,
|
||||
1: 0x800000,
|
||||
2: 0x008000,
|
||||
3: 0x808000,
|
||||
4: 0x000080,
|
||||
5: 0x800080,
|
||||
6: 0x008080,
|
||||
7: 0xc0c0c0,
|
||||
8: 0x808080,
|
||||
9: 0xff0000,
|
||||
10: 0x00ff00,
|
||||
11: 0xffff00,
|
||||
12: 0x0000ff,
|
||||
13: 0xff00ff,
|
||||
14: 0x00ffff,
|
||||
15: 0xffffff,
|
||||
16: 0x000000,
|
||||
17: 0x00005f,
|
||||
18: 0x000087,
|
||||
19: 0x0000af,
|
||||
20: 0x0000d7,
|
||||
21: 0x0000ff,
|
||||
22: 0x005f00,
|
||||
23: 0x005f5f,
|
||||
24: 0x005f87,
|
||||
25: 0x005faf,
|
||||
26: 0x005fd7,
|
||||
27: 0x005fff,
|
||||
28: 0x008700,
|
||||
29: 0x00875f,
|
||||
30: 0x008787,
|
||||
31: 0x0087af,
|
||||
32: 0x0087d7,
|
||||
33: 0x0087ff,
|
||||
34: 0x00af00,
|
||||
35: 0x00af5f,
|
||||
36: 0x00af87,
|
||||
37: 0x00afaf,
|
||||
38: 0x00afd7,
|
||||
39: 0x00afff,
|
||||
40: 0x00d700,
|
||||
41: 0x00d75f,
|
||||
42: 0x00d787,
|
||||
43: 0x00d7af,
|
||||
44: 0x00d7d7,
|
||||
45: 0x00d7ff,
|
||||
46: 0x00ff00,
|
||||
47: 0x00ff5f,
|
||||
48: 0x00ff87,
|
||||
49: 0x00ffaf,
|
||||
50: 0x00ffd7,
|
||||
51: 0x00ffff,
|
||||
52: 0x5f0000,
|
||||
53: 0x5f005f,
|
||||
54: 0x5f0087,
|
||||
55: 0x5f00af,
|
||||
56: 0x5f00d7,
|
||||
57: 0x5f00ff,
|
||||
58: 0x5f5f00,
|
||||
59: 0x5f5f5f,
|
||||
60: 0x5f5f87,
|
||||
61: 0x5f5faf,
|
||||
62: 0x5f5fd7,
|
||||
63: 0x5f5fff,
|
||||
64: 0x5f8700,
|
||||
65: 0x5f875f,
|
||||
66: 0x5f8787,
|
||||
67: 0x5f87af,
|
||||
68: 0x5f87d7,
|
||||
69: 0x5f87ff,
|
||||
70: 0x5faf00,
|
||||
71: 0x5faf5f,
|
||||
72: 0x5faf87,
|
||||
73: 0x5fafaf,
|
||||
74: 0x5fafd7,
|
||||
75: 0x5fafff,
|
||||
76: 0x5fd700,
|
||||
77: 0x5fd75f,
|
||||
78: 0x5fd787,
|
||||
79: 0x5fd7af,
|
||||
80: 0x5fd7d7,
|
||||
81: 0x5fd7ff,
|
||||
82: 0x5fff00,
|
||||
83: 0x5fff5f,
|
||||
84: 0x5fff87,
|
||||
85: 0x5fffaf,
|
||||
86: 0x5fffd7,
|
||||
87: 0x5fffff,
|
||||
88: 0x870000,
|
||||
89: 0x87005f,
|
||||
90: 0x870087,
|
||||
91: 0x8700af,
|
||||
92: 0x8700d7,
|
||||
93: 0x8700ff,
|
||||
94: 0x875f00,
|
||||
95: 0x875f5f,
|
||||
96: 0x875f87,
|
||||
97: 0x875faf,
|
||||
98: 0x875fd7,
|
||||
99: 0x875fff,
|
||||
100: 0x878700,
|
||||
101: 0x87875f,
|
||||
102: 0x878787,
|
||||
103: 0x8787af,
|
||||
104: 0x8787d7,
|
||||
105: 0x8787ff,
|
||||
106: 0x87af00,
|
||||
107: 0x87af5f,
|
||||
108: 0x87af87,
|
||||
109: 0x87afaf,
|
||||
110: 0x87afd7,
|
||||
111: 0x87afff,
|
||||
112: 0x87d700,
|
||||
113: 0x87d75f,
|
||||
114: 0x87d787,
|
||||
115: 0x87d7af,
|
||||
116: 0x87d7d7,
|
||||
117: 0x87d7ff,
|
||||
118: 0x87ff00,
|
||||
119: 0x87ff5f,
|
||||
120: 0x87ff87,
|
||||
121: 0x87ffaf,
|
||||
122: 0x87ffd7,
|
||||
123: 0x87ffff,
|
||||
124: 0xaf0000,
|
||||
125: 0xaf005f,
|
||||
126: 0xaf0087,
|
||||
127: 0xaf00af,
|
||||
128: 0xaf00d7,
|
||||
129: 0xaf00ff,
|
||||
130: 0xaf5f00,
|
||||
131: 0xaf5f5f,
|
||||
132: 0xaf5f87,
|
||||
133: 0xaf5faf,
|
||||
134: 0xaf5fd7,
|
||||
135: 0xaf5fff,
|
||||
136: 0xaf8700,
|
||||
137: 0xaf875f,
|
||||
138: 0xaf8787,
|
||||
139: 0xaf87af,
|
||||
140: 0xaf87d7,
|
||||
141: 0xaf87ff,
|
||||
142: 0xafaf00,
|
||||
143: 0xafaf5f,
|
||||
144: 0xafaf87,
|
||||
145: 0xafafaf,
|
||||
146: 0xafafd7,
|
||||
147: 0xafafff,
|
||||
148: 0xafd700,
|
||||
149: 0xafd75f,
|
||||
150: 0xafd787,
|
||||
151: 0xafd7af,
|
||||
152: 0xafd7d7,
|
||||
153: 0xafd7ff,
|
||||
154: 0xafff00,
|
||||
155: 0xafff5f,
|
||||
156: 0xafff87,
|
||||
157: 0xafffaf,
|
||||
158: 0xafffd7,
|
||||
159: 0xafffff,
|
||||
160: 0xd70000,
|
||||
161: 0xd7005f,
|
||||
162: 0xd70087,
|
||||
163: 0xd700af,
|
||||
164: 0xd700d7,
|
||||
165: 0xd700ff,
|
||||
166: 0xd75f00,
|
||||
167: 0xd75f5f,
|
||||
168: 0xd75f87,
|
||||
169: 0xd75faf,
|
||||
170: 0xd75fd7,
|
||||
171: 0xd75fff,
|
||||
172: 0xd78700,
|
||||
173: 0xd7875f,
|
||||
174: 0xd78787,
|
||||
175: 0xd787af,
|
||||
176: 0xd787d7,
|
||||
177: 0xd787ff,
|
||||
178: 0xd7af00,
|
||||
179: 0xd7af5f,
|
||||
180: 0xd7af87,
|
||||
181: 0xd7afaf,
|
||||
182: 0xd7afd7,
|
||||
183: 0xd7afff,
|
||||
184: 0xd7d700,
|
||||
185: 0xd7d75f,
|
||||
186: 0xd7d787,
|
||||
187: 0xd7d7af,
|
||||
188: 0xd7d7d7,
|
||||
189: 0xd7d7ff,
|
||||
190: 0xd7ff00,
|
||||
191: 0xd7ff5f,
|
||||
192: 0xd7ff87,
|
||||
193: 0xd7ffaf,
|
||||
194: 0xd7ffd7,
|
||||
195: 0xd7ffff,
|
||||
196: 0xff0000,
|
||||
197: 0xff005f,
|
||||
198: 0xff0087,
|
||||
199: 0xff00af,
|
||||
200: 0xff00d7,
|
||||
201: 0xff00ff,
|
||||
202: 0xff5f00,
|
||||
203: 0xff5f5f,
|
||||
204: 0xff5f87,
|
||||
205: 0xff5faf,
|
||||
206: 0xff5fd7,
|
||||
207: 0xff5fff,
|
||||
208: 0xff8700,
|
||||
209: 0xff875f,
|
||||
210: 0xff8787,
|
||||
211: 0xff87af,
|
||||
212: 0xff87d7,
|
||||
213: 0xff87ff,
|
||||
214: 0xffaf00,
|
||||
215: 0xffaf5f,
|
||||
216: 0xffaf87,
|
||||
217: 0xffafaf,
|
||||
218: 0xffafd7,
|
||||
219: 0xffafff,
|
||||
220: 0xffd700,
|
||||
221: 0xffd75f,
|
||||
222: 0xffd787,
|
||||
223: 0xffd7af,
|
||||
224: 0xffd7d7,
|
||||
225: 0xffd7ff,
|
||||
226: 0xffff00,
|
||||
227: 0xffff5f,
|
||||
228: 0xffff87,
|
||||
229: 0xffffaf,
|
||||
230: 0xffffd7,
|
||||
231: 0xffffff,
|
||||
232: 0x080808,
|
||||
233: 0x121212,
|
||||
234: 0x1c1c1c,
|
||||
235: 0x262626,
|
||||
236: 0x303030,
|
||||
237: 0x3a3a3a,
|
||||
238: 0x444444,
|
||||
239: 0x4e4e4e,
|
||||
240: 0x585858,
|
||||
241: 0x626262,
|
||||
242: 0x6c6c6c,
|
||||
243: 0x767676,
|
||||
244: 0x808080,
|
||||
245: 0x8a8a8a,
|
||||
246: 0x949494,
|
||||
247: 0x9e9e9e,
|
||||
248: 0xa8a8a8,
|
||||
249: 0xb2b2b2,
|
||||
250: 0xbcbcbc,
|
||||
251: 0xc6c6c6,
|
||||
252: 0xd0d0d0,
|
||||
253: 0xdadada,
|
||||
254: 0xe4e4e4,
|
||||
255: 0xeeeeee,
|
||||
}
|
||||
|
||||
// `\033]0;TITLESTR\007`
|
||||
func doTitleSequence(er *bytes.Reader) error {
|
||||
var c byte
|
||||
var err error
|
||||
|
||||
c, err = er.ReadByte()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c != '0' && c != '2' {
|
||||
return nil
|
||||
}
|
||||
c, err = er.ReadByte()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c != ';' {
|
||||
return nil
|
||||
}
|
||||
title := make([]byte, 0, 80)
|
||||
for {
|
||||
c, err = er.ReadByte()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c == 0x07 || c == '\n' {
|
||||
break
|
||||
}
|
||||
title = append(title, c)
|
||||
}
|
||||
if len(title) > 0 {
|
||||
title8, err := syscall.UTF16PtrFromString(string(title))
|
||||
if err == nil {
|
||||
procSetConsoleTitle.Call(uintptr(unsafe.Pointer(title8)))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write write data on console
|
||||
func (w *Writer) Write(data []byte) (n int, err error) {
|
||||
var csbi consoleScreenBufferInfo
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
|
||||
er := bytes.NewReader(data)
|
||||
var bw [1]byte
|
||||
loop:
|
||||
for {
|
||||
c1, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
if c1 != 0x1b {
|
||||
bw[0] = c1
|
||||
w.out.Write(bw[:])
|
||||
continue
|
||||
}
|
||||
c2, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
|
||||
if c2 == ']' {
|
||||
if err := doTitleSequence(er); err != nil {
|
||||
break loop
|
||||
}
|
||||
continue
|
||||
}
|
||||
if c2 != 0x5b {
|
||||
continue
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
var m byte
|
||||
for {
|
||||
c, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
|
||||
m = c
|
||||
break
|
||||
}
|
||||
buf.Write([]byte(string(c)))
|
||||
}
|
||||
|
||||
switch m {
|
||||
case 'A':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.y -= short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'B':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.y += short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'C':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.x += short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'D':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.x -= short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'E':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.x = 0
|
||||
csbi.cursorPosition.y += short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'F':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.x = 0
|
||||
csbi.cursorPosition.y -= short(n)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'G':
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
csbi.cursorPosition.x = short(n - 1)
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'H', 'f':
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
if buf.Len() > 0 {
|
||||
token := strings.Split(buf.String(), ";")
|
||||
switch len(token) {
|
||||
case 1:
|
||||
n1, err := strconv.Atoi(token[0])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
csbi.cursorPosition.y = short(n1 - 1)
|
||||
case 2:
|
||||
n1, err := strconv.Atoi(token[0])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
n2, err := strconv.Atoi(token[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
csbi.cursorPosition.x = short(n2 - 1)
|
||||
csbi.cursorPosition.y = short(n1 - 1)
|
||||
}
|
||||
} else {
|
||||
csbi.cursorPosition.y = 0
|
||||
}
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
|
||||
case 'J':
|
||||
n := 0
|
||||
if buf.Len() > 0 {
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
var count, written dword
|
||||
var cursor coord
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
switch n {
|
||||
case 0:
|
||||
cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
|
||||
count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
|
||||
case 1:
|
||||
cursor = coord{x: csbi.window.left, y: csbi.window.top}
|
||||
count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.window.top-csbi.cursorPosition.y)*csbi.size.x)
|
||||
case 2:
|
||||
cursor = coord{x: csbi.window.left, y: csbi.window.top}
|
||||
count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
|
||||
}
|
||||
procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
|
||||
procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
|
||||
case 'K':
|
||||
n := 0
|
||||
if buf.Len() > 0 {
|
||||
n, err = strconv.Atoi(buf.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
var cursor coord
|
||||
var count, written dword
|
||||
switch n {
|
||||
case 0:
|
||||
cursor = coord{x: csbi.cursorPosition.x + 1, y: csbi.cursorPosition.y}
|
||||
count = dword(csbi.size.x - csbi.cursorPosition.x - 1)
|
||||
case 1:
|
||||
cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
|
||||
count = dword(csbi.size.x - csbi.cursorPosition.x)
|
||||
case 2:
|
||||
cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
|
||||
count = dword(csbi.size.x)
|
||||
}
|
||||
procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
|
||||
procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
|
||||
case 'm':
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
attr := csbi.attributes
|
||||
cs := buf.String()
|
||||
if cs == "" {
|
||||
procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
|
||||
continue
|
||||
}
|
||||
token := strings.Split(cs, ";")
|
||||
for i := 0; i < len(token); i++ {
|
||||
ns := token[i]
|
||||
if n, err = strconv.Atoi(ns); err == nil {
|
||||
switch {
|
||||
case n == 0 || n == 100:
|
||||
attr = w.oldattr
|
||||
case 1 <= n && n <= 5:
|
||||
attr |= foregroundIntensity
|
||||
case n == 7:
|
||||
attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
|
||||
case n == 22 || n == 25:
|
||||
attr |= foregroundIntensity
|
||||
case n == 27:
|
||||
attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
|
||||
case 30 <= n && n <= 37:
|
||||
attr &= backgroundMask
|
||||
if (n-30)&1 != 0 {
|
||||
attr |= foregroundRed
|
||||
}
|
||||
if (n-30)&2 != 0 {
|
||||
attr |= foregroundGreen
|
||||
}
|
||||
if (n-30)&4 != 0 {
|
||||
attr |= foregroundBlue
|
||||
}
|
||||
case n == 38: // set foreground color.
|
||||
if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
|
||||
if n256, err := strconv.Atoi(token[i+2]); err == nil {
|
||||
if n256foreAttr == nil {
|
||||
n256setup()
|
||||
}
|
||||
attr &= backgroundMask
|
||||
attr |= n256foreAttr[n256]
|
||||
i += 2
|
||||
}
|
||||
} else {
|
||||
attr = attr & (w.oldattr & backgroundMask)
|
||||
}
|
||||
case n == 39: // reset foreground color.
|
||||
attr &= backgroundMask
|
||||
attr |= w.oldattr & foregroundMask
|
||||
case 40 <= n && n <= 47:
|
||||
attr &= foregroundMask
|
||||
if (n-40)&1 != 0 {
|
||||
attr |= backgroundRed
|
||||
}
|
||||
if (n-40)&2 != 0 {
|
||||
attr |= backgroundGreen
|
||||
}
|
||||
if (n-40)&4 != 0 {
|
||||
attr |= backgroundBlue
|
||||
}
|
||||
case n == 48: // set background color.
|
||||
if i < len(token)-2 && token[i+1] == "5" {
|
||||
if n256, err := strconv.Atoi(token[i+2]); err == nil {
|
||||
if n256backAttr == nil {
|
||||
n256setup()
|
||||
}
|
||||
attr &= foregroundMask
|
||||
attr |= n256backAttr[n256]
|
||||
i += 2
|
||||
}
|
||||
} else {
|
||||
attr = attr & (w.oldattr & foregroundMask)
|
||||
}
|
||||
case n == 49: // reset foreground color.
|
||||
attr &= foregroundMask
|
||||
attr |= w.oldattr & backgroundMask
|
||||
case 90 <= n && n <= 97:
|
||||
attr = (attr & backgroundMask)
|
||||
attr |= foregroundIntensity
|
||||
if (n-90)&1 != 0 {
|
||||
attr |= foregroundRed
|
||||
}
|
||||
if (n-90)&2 != 0 {
|
||||
attr |= foregroundGreen
|
||||
}
|
||||
if (n-90)&4 != 0 {
|
||||
attr |= foregroundBlue
|
||||
}
|
||||
case 100 <= n && n <= 107:
|
||||
attr = (attr & foregroundMask)
|
||||
attr |= backgroundIntensity
|
||||
if (n-100)&1 != 0 {
|
||||
attr |= backgroundRed
|
||||
}
|
||||
if (n-100)&2 != 0 {
|
||||
attr |= backgroundGreen
|
||||
}
|
||||
if (n-100)&4 != 0 {
|
||||
attr |= backgroundBlue
|
||||
}
|
||||
}
|
||||
procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
|
||||
}
|
||||
}
|
||||
case 'h':
|
||||
var ci consoleCursorInfo
|
||||
cs := buf.String()
|
||||
if cs == "5>" {
|
||||
procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
ci.visible = 0
|
||||
procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
} else if cs == "?25" {
|
||||
procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
ci.visible = 1
|
||||
procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
}
|
||||
case 'l':
|
||||
var ci consoleCursorInfo
|
||||
cs := buf.String()
|
||||
if cs == "5>" {
|
||||
procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
ci.visible = 1
|
||||
procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
} else if cs == "?25" {
|
||||
procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
ci.visible = 0
|
||||
procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
|
||||
}
|
||||
case 's':
|
||||
procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
|
||||
w.oldpos = csbi.cursorPosition
|
||||
case 'u':
|
||||
procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
|
||||
}
|
||||
}
|
||||
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
type consoleColor struct {
|
||||
rgb int
|
||||
red bool
|
||||
green bool
|
||||
blue bool
|
||||
intensity bool
|
||||
}
|
||||
|
||||
func (c consoleColor) foregroundAttr() (attr word) {
|
||||
if c.red {
|
||||
attr |= foregroundRed
|
||||
}
|
||||
if c.green {
|
||||
attr |= foregroundGreen
|
||||
}
|
||||
if c.blue {
|
||||
attr |= foregroundBlue
|
||||
}
|
||||
if c.intensity {
|
||||
attr |= foregroundIntensity
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c consoleColor) backgroundAttr() (attr word) {
|
||||
if c.red {
|
||||
attr |= backgroundRed
|
||||
}
|
||||
if c.green {
|
||||
attr |= backgroundGreen
|
||||
}
|
||||
if c.blue {
|
||||
attr |= backgroundBlue
|
||||
}
|
||||
if c.intensity {
|
||||
attr |= backgroundIntensity
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var color16 = []consoleColor{
|
||||
{0x000000, false, false, false, false},
|
||||
{0x000080, false, false, true, false},
|
||||
{0x008000, false, true, false, false},
|
||||
{0x008080, false, true, true, false},
|
||||
{0x800000, true, false, false, false},
|
||||
{0x800080, true, false, true, false},
|
||||
{0x808000, true, true, false, false},
|
||||
{0xc0c0c0, true, true, true, false},
|
||||
{0x808080, false, false, false, true},
|
||||
{0x0000ff, false, false, true, true},
|
||||
{0x00ff00, false, true, false, true},
|
||||
{0x00ffff, false, true, true, true},
|
||||
{0xff0000, true, false, false, true},
|
||||
{0xff00ff, true, false, true, true},
|
||||
{0xffff00, true, true, false, true},
|
||||
{0xffffff, true, true, true, true},
|
||||
}
|
||||
|
||||
type hsv struct {
|
||||
h, s, v float32
|
||||
}
|
||||
|
||||
func (a hsv) dist(b hsv) float32 {
|
||||
dh := a.h - b.h
|
||||
switch {
|
||||
case dh > 0.5:
|
||||
dh = 1 - dh
|
||||
case dh < -0.5:
|
||||
dh = -1 - dh
|
||||
}
|
||||
ds := a.s - b.s
|
||||
dv := a.v - b.v
|
||||
return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
|
||||
}
|
||||
|
||||
func toHSV(rgb int) hsv {
|
||||
r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
|
||||
float32((rgb&0x00FF00)>>8)/256.0,
|
||||
float32(rgb&0x0000FF)/256.0
|
||||
min, max := minmax3f(r, g, b)
|
||||
h := max - min
|
||||
if h > 0 {
|
||||
if max == r {
|
||||
h = (g - b) / h
|
||||
if h < 0 {
|
||||
h += 6
|
||||
}
|
||||
} else if max == g {
|
||||
h = 2 + (b-r)/h
|
||||
} else {
|
||||
h = 4 + (r-g)/h
|
||||
}
|
||||
}
|
||||
h /= 6.0
|
||||
s := max - min
|
||||
if max != 0 {
|
||||
s /= max
|
||||
}
|
||||
v := max
|
||||
return hsv{h: h, s: s, v: v}
|
||||
}
|
||||
|
||||
type hsvTable []hsv
|
||||
|
||||
func toHSVTable(rgbTable []consoleColor) hsvTable {
|
||||
t := make(hsvTable, len(rgbTable))
|
||||
for i, c := range rgbTable {
|
||||
t[i] = toHSV(c.rgb)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (t hsvTable) find(rgb int) consoleColor {
|
||||
hsv := toHSV(rgb)
|
||||
n := 7
|
||||
l := float32(5.0)
|
||||
for i, p := range t {
|
||||
d := hsv.dist(p)
|
||||
if d < l {
|
||||
l, n = d, i
|
||||
}
|
||||
}
|
||||
return color16[n]
|
||||
}
|
||||
|
||||
func minmax3f(a, b, c float32) (min, max float32) {
|
||||
if a < b {
|
||||
if b < c {
|
||||
return a, c
|
||||
} else if a < c {
|
||||
return a, b
|
||||
} else {
|
||||
return c, b
|
||||
}
|
||||
} else {
|
||||
if a < c {
|
||||
return b, c
|
||||
} else if b < c {
|
||||
return b, a
|
||||
} else {
|
||||
return c, a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var n256foreAttr []word
|
||||
var n256backAttr []word
|
||||
|
||||
func n256setup() {
|
||||
n256foreAttr = make([]word, 256)
|
||||
n256backAttr = make([]word, 256)
|
||||
t := toHSVTable(color16)
|
||||
for i, rgb := range color256 {
|
||||
c := t.find(rgb)
|
||||
n256foreAttr[i] = c.foregroundAttr()
|
||||
n256backAttr[i] = c.backgroundAttr()
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package colorable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
// NonColorable hold writer but remove escape sequence.
|
||||
type NonColorable struct {
|
||||
out io.Writer
|
||||
}
|
||||
|
||||
// NewNonColorable return new instance of Writer which remove escape sequence from Writer.
|
||||
func NewNonColorable(w io.Writer) io.Writer {
|
||||
return &NonColorable{out: w}
|
||||
}
|
||||
|
||||
// Write write data on console
|
||||
func (w *NonColorable) Write(data []byte) (n int, err error) {
|
||||
er := bytes.NewReader(data)
|
||||
var bw [1]byte
|
||||
loop:
|
||||
for {
|
||||
c1, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
if c1 != 0x1b {
|
||||
bw[0] = c1
|
||||
w.out.Write(bw[:])
|
||||
continue
|
||||
}
|
||||
c2, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
if c2 != 0x5b {
|
||||
continue
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for {
|
||||
c, err := er.ReadByte()
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
|
||||
break
|
||||
}
|
||||
buf.Write([]byte(string(c)))
|
||||
}
|
||||
}
|
||||
|
||||
return len(data), nil
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
language: go
|
||||
go:
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get github.com/mattn/goveralls
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
script:
|
||||
- $HOME/gopath/bin/goveralls -repotoken 3gHdORO5k5ziZcWMBxnd9LrMZaJs8m9x5
|
@ -0,0 +1,9 @@
|
||||
Copyright (c) Yasuhiro MATSUMOTO <mattn.jp@gmail.com>
|
||||
|
||||
MIT License (Expat)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,50 @@
|
||||
# go-isatty
|
||||
|
||||
[![Godoc Reference](https://godoc.org/github.com/mattn/go-isatty?status.svg)](http://godoc.org/github.com/mattn/go-isatty)
|
||||
[![Build Status](https://travis-ci.org/mattn/go-isatty.svg?branch=master)](https://travis-ci.org/mattn/go-isatty)
|
||||
[![Coverage Status](https://coveralls.io/repos/github/mattn/go-isatty/badge.svg?branch=master)](https://coveralls.io/github/mattn/go-isatty?branch=master)
|
||||
[![Go Report Card](https://goreportcard.com/badge/mattn/go-isatty)](https://goreportcard.com/report/mattn/go-isatty)
|
||||
|
||||
isatty for golang
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mattn/go-isatty"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if isatty.IsTerminal(os.Stdout.Fd()) {
|
||||
fmt.Println("Is Terminal")
|
||||
} else if isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
||||
fmt.Println("Is Cygwin/MSYS2 Terminal")
|
||||
} else {
|
||||
fmt.Println("Is Not Terminal")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
$ go get github.com/mattn/go-isatty
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
## Author
|
||||
|
||||
Yasuhiro Matsumoto (a.k.a mattn)
|
||||
|
||||
## Thanks
|
||||
|
||||
* k-takata: base idea for IsCygwinTerminal
|
||||
|
||||
https://github.com/k-takata/go-iscygpty
|
@ -0,0 +1,2 @@
|
||||
// Package isatty implements interface to isatty
|
||||
package isatty
|
@ -0,0 +1,15 @@
|
||||
// +build appengine
|
||||
|
||||
package isatty
|
||||
|
||||
// IsTerminal returns true if the file descriptor is terminal which
|
||||
// is always false on on appengine classic which is a sandboxed PaaS.
|
||||
func IsTerminal(fd uintptr) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsCygwinTerminal() return true if the file descriptor is a cygwin or msys2
|
||||
// terminal. This is also always false on this environment.
|
||||
func IsCygwinTerminal(fd uintptr) bool {
|
||||
return false
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
// +build darwin freebsd openbsd netbsd dragonfly
|
||||
// +build !appengine
|
||||
|
||||
package isatty
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const ioctlReadTermios = syscall.TIOCGETA
|
||||
|
||||
// IsTerminal return true if the file descriptor is terminal.
|
||||
func IsTerminal(fd uintptr) bool {
|
||||
var termios syscall.Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
// +build linux
|
||||
// +build !appengine,!ppc64,!ppc64le
|
||||
|
||||
package isatty
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const ioctlReadTermios = syscall.TCGETS
|
||||
|
||||
// IsTerminal return true if the file descriptor is terminal.
|
||||
func IsTerminal(fd uintptr) bool {
|
||||
var termios syscall.Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue