diff options
43 files changed, 802 insertions, 502 deletions
@@ -5,14 +5,14 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/distatus/battery v0.9.0 github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 - github.com/gizak/termui v0.0.0-20190124041613-958a28575d74 + github.com/gizak/termui/v3 v3.0.0 github.com/go-ole/go-ole v1.2.1 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mitchellh/go-wordwrap v1.0.0 // indirect - github.com/nsf/termbox-go v0.0.0-20190104133558-0938b5187e61 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shirou/gopsutil v2.18.11+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/stretchr/testify v1.2.2 // indirect + golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect howett.net/plist v0.0.0-20181124034731-591f970eefbb // indirect ) @@ -1,19 +1,17 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM= +github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distatus/battery v0.9.0 h1:8NS5o00/j3Oh2xgocA6pQROTp5guoR+s8CZlWzHC4QM= github.com/distatus/battery v0.9.0/go.mod h1:gGO7GxHTi1zlRT+cAj8uGG0/8HFiqAeH0TJvoipnuPs= github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 h1:HMAfwOa33y82IaQEKQDfUCiwNlxtM1iw7HLM9ru0RNc= github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:l7JNRynTRuqe45tpIyItHNqZWTxywYjp87MWTOnU5cg= -github.com/gizak/termui v0.0.0-20190124041613-958a28575d74 h1:gQbT+IgWIflxp7EQaxWGgGoHAxEFzvP36DyxiBGEV9g= -github.com/gizak/termui v0.0.0-20190124041613-958a28575d74/go.mod h1:hJmkzz29zwvMdxina9wLc5fWN7bZuougH5YR93VrJtA= +github.com/gizak/termui/v3 v3.0.0 h1:NYTUG6ig/sJK05O5FyhWemwlVPO8ilNpvS/PgRtrKAE= +github.com/gizak/termui/v3 v3.0.0/go.mod h1:uinu2dMdtMI+FTIdEFUJQT5y+KShnhQRshvPblXq3lY= github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E= github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= -github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 h1:beB+Da4k9B1zmgag78k3k1Bx4L/fdWr5FwNa0f8RxmY= -github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c= -github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -26,10 +24,8 @@ github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzC github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= -github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb h1:YahEjAGkJtCrkqgVHhX6n8ZX+CZ3hDRL9fjLYugLfSs= -github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= -github.com/nsf/termbox-go v0.0.0-20190104133558-0938b5187e61 h1:pEzZYac/uQ4cgaN1Q/UYZg+ZtCSWz2HQ3rvl8MeN9MA= -github.com/nsf/termbox-go v0.0.0-20190104133558-0938b5187e61/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= +github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840= +github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shirou/gopsutil v2.18.11+incompatible h1:PMFTKnFTr/YTRW5rbLK4vWALV3a+IGXse5nvhSjztmg= @@ -40,8 +36,6 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 h1:Pn8fQdvx+z1avAi7fdM2kRYWQNxGlavNDSyzrQg2SsU= golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= -golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M= -golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/sys v0.0.0-20190116161447-11f53e031339 h1:g/Jesu8+QLnA0CPzF3E1pURg0Byr7i6jLoX5sqjcAh0= golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -15,7 +15,7 @@ import ( "time" docopt "github.com/docopt/docopt.go" - ui "github.com/gizak/termui" + ui "github.com/gizak/termui/v3" "github.com/cjbassi/gotop/colorschemes" "github.com/cjbassi/gotop/src/logging" diff --git a/src/termui/linegraph.go b/src/termui/linegraph.go index 2d0c8e0..fb98cca 100644 --- a/src/termui/linegraph.go +++ b/src/termui/linegraph.go @@ -5,7 +5,7 @@ import ( "sort" drawille "github.com/cjbassi/gotop/src/termui/drawille-go" - . "github.com/gizak/termui" + . "github.com/gizak/termui/v3" ) // LineGraph implements a line graph of data points. diff --git a/src/termui/sparkline.go b/src/termui/sparkline.go index 85ca919..ef09efd 100644 --- a/src/termui/sparkline.go +++ b/src/termui/sparkline.go @@ -4,7 +4,7 @@ import ( "image" "log" - . "github.com/gizak/termui" + . "github.com/gizak/termui/v3" ) // Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers. diff --git a/src/termui/table.go b/src/termui/table.go index 57cd9e2..a6839a1 100644 --- a/src/termui/table.go +++ b/src/termui/table.go @@ -5,7 +5,7 @@ import ( "log" "strings" - . "github.com/gizak/termui" + . "github.com/gizak/termui/v3" ) type Table struct { diff --git a/src/widgets/help.go b/src/widgets/help.go index 8900603..eaa6f59 100644 --- a/src/widgets/help.go +++ b/src/widgets/help.go @@ -4,7 +4,7 @@ import ( "image" "strings" - ui "github.com/gizak/termui" + ui "github.com/gizak/termui/v3" ) const KEYBINDS = ` diff --git a/src/widgets/statusbar.go b/src/widgets/statusbar.go index 27ba548..f610081 100644 --- a/src/widgets/statusbar.go +++ b/src/widgets/statusbar.go @@ -6,7 +6,7 @@ import ( "os" "time" - ui "github.com/gizak/termui" + ui "github.com/gizak/termui/v3" ) type StatusBar struct { diff --git a/src/widgets/temp.go b/src/widgets/temp.go index 1162c84..047a639 100644 --- a/src/widgets/temp.go +++ b/src/widgets/temp.go @@ -7,7 +7,7 @@ import ( "sync" "time" - ui "github.com/gizak/termui" + ui "github.com/gizak/termui/v3" "github.com/cjbassi/gotop/src/utils" ) diff --git a/vendor/github.com/cjbassi/drawille-go/LICENSE b/vendor/github.com/cjbassi/drawille-go/LICENSE new file mode 100644 index 0000000..b71d02e --- /dev/null +++ b/vendor/github.com/cjbassi/drawille-go/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Caleb Bassi + +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. diff --git a/vendor/github.com/cjbassi/drawille-go/README.md b/vendor/github.com/cjbassi/drawille-go/README.md new file mode 100644 index 0000000..3b76afa --- /dev/null +++ b/vendor/github.com/cjbassi/drawille-go/README.md @@ -0,0 +1,5 @@ +# drawille-go + +A [drawille](https://github.com/asciimoo/drawille) implementation in Go with a more permissive license. + +Used in [termui](https://github.com/gizak/termui). diff --git a/vendor/github.com/cjbassi/drawille-go/drawille.go b/vendor/github.com/cjbassi/drawille-go/drawille.go new file mode 100644 index 0000000..11401a3 --- /dev/null +++ b/vendor/github.com/cjbassi/drawille-go/drawille.go @@ -0,0 +1,90 @@ +package drawille + +import ( + "image" +) + +const BRAILLE_OFFSET = '\u2800' + +var BRAILLE = [4][2]rune{ + {'\u0001', '\u0008'}, + {'\u0002', '\u0010'}, + {'\u0004', '\u0020'}, + {'\u0040', '\u0080'}, +} + +type Color int + +type Cell struct { + Rune rune + Color Color +} + +type Canvas struct { + CellMap map[image.Point]Cell +} + +func NewCanvas() *Canvas { + return &Canvas{ + CellMap: make(map[image.Point]Cell), + } +} + +func (self *Canvas) SetPoint(p image.Point, color Color) { + point := image.Pt(p.X/2, p.Y/4) + self.CellMap[point] = Cell{ + self.CellMap[point].Rune | BRAILLE[p.Y%4][p.X%2], + color, + } +} + +func (self *Canvas) SetLine(p0, p1 image.Point, color Color) { + for _, p := range line(p0, p1) { + self.SetPoint(p, color) + } +} + +func (self *Canvas) GetCells() map[image.Point]Cell { + cellMap := make(map[image.Point]Cell) + for point, cell := range self.CellMap { + cellMap[point] = Cell{cell.Rune + BRAILLE_OFFSET, cell.Color} + } + return cellMap +} + +func line(p0, p1 image.Point) []image.Point { + points := []image.Point{} + + leftPoint, rightPoint := p0, p1 + if leftPoint.X > rightPoint.X { + leftPoint, rightPoint = rightPoint, leftPoint + } + + xDistance := absInt(leftPoint.X - rightPoint.X) + yDistance := absInt(leftPoint.Y - rightPoint.Y) + slope := float64(yDistance) / float64(xDistance) + slopeSign := 1 + if rightPoint.Y < leftPoint.Y { + slopeSign = -1 + } + + targetYCoordinate := float64(leftPoint.Y) + currentYCoordinate := leftPoint.Y + for i := leftPoint.X; i < rightPoint.X; i++ { + points = append(points, image.Pt(i, currentYCoordinate)) + targetYCoordinate += (slope * float64(slopeSign)) + for currentYCoordinate != int(targetYCoordinate) { + points = append(points, image.Pt(i, currentYCoordinate)) + currentYCoordinate += slopeSign + } + } + + return points +} + +func absInt(x int) int { + if x >= 0 { + return x + } + return -x +} diff --git a/vendor/github.com/gizak/termui/CHANGELOG.md b/vendor/github.com/gizak/termui/CHANGELOG.md deleted file mode 100644 index 655403b..0000000 --- a/vendor/github.com/gizak/termui/CHANGELOG.md +++ /dev/null @@ -1,41 +0,0 @@ -Feel free to search/open an issue if something is missing or confusing from the changelog, since many things have been in flux. - -## TODO - -- moved widgets to `github.com/gizak/termui/widgets` -- rewrote widgets (check examples and code) -- rewrote grid - - grids are instantiated locally instead of through `termui.Body` - - grids can be nested - - changed grid layout mechanism - - columns and rows can be arbitrarily nested - - column and row size is now specified as a ratio of the available space -- `Cell`s now contain a `Style` which holds a `Fg`, `Bg`, and `Modifier` -- Change `Bufferer` interface to `Drawable` - - Add `GetRect` and `SetRect` methods to control widget sizing - - Change `Buffer` method to `Draw` - - `Draw` takes a `Buffer` and draws to it instead of returning a new `Buffer` -- Refactored `Theme` - - `Theme` is now a large struct which holds the default `Styles` of everything -- Combined `TermWidth` and `TermHeight` functions into `TerminalDimensions` -- Added `Canvas` which allows for drawing braille lines to a `Buffer` -- Refactored `Block` -- Refactored `Buffer` methods -- Set `termbox-go` backend to 256 colors by default -- Decremented color numbers by 1 to match xterm colors -- Changed text parsing - - style items changed from `fg-color` to `fg:color` - - added mod item like `mod:reverse` - -## 18/11/29 - -- Move Tabpane from termui/extra to termui and rename it to TabPane -- Rename PollEvent to PollEvents - -## 18/11/28 - -- Migrated from Dep to vgo -- Overhauled the event system - - check the wiki/examples for details -- Renamed Par widget to Paragraph -- Renamed MBarChart widget to StackedBarChart diff --git a/vendor/github.com/gizak/termui/README.md b/vendor/github.com/gizak/termui/README.md deleted file mode 100644 index 8144e49..0000000 --- a/vendor/github.com/gizak/termui/README.md +++ /dev/null @@ -1,86 +0,0 @@ -# termui - -<img src="./_assets/dashboard1.gif" alt="demo cast under osx 10.10; Terminal.app; Menlo Regular 12pt.)" width="100%"> - -termui is a cross-platform and fully-customizable terminal dashboard and widget library built on top of [termbox-go](https://github.com/nsf/termbox-go). It is inspired by [blessed-contrib](https://github.com/yaronn/blessed-contrib) and written purely in Go. - -## Installation - -Installing from the master branch is recommended: - -```bash -go get -u github.com/gizak/termui@master -``` - -**Note**: termui is currently undergoing API changes so make sure to check the changelog when upgrading. -If you upgrade and notice something is missing or don't like a change, revert the upgrade and open an issue. - -## Usage - -### Hello World - -```go -package main - -import ( - "log" - - ui "github.com/gizak/termui" - "github.com/gizak/termui/widgets" -) - -func main() { - if err := ui.Init(); err != nil { - log.Fatalf("failed to initialize termui: %v", err) - } - defer ui.Close() - - p := widgets.NewParagraph() - p.Text = "Hello World!" - p.SetRect(0, 0, 25, 5) - - ui.Render(p) - - for e := range ui.PollEvents() { - if e.Type == ui.KeyboardEvent { - break - } - } -} -``` - -### Widgets - -Click image to see the corresponding demo codes. - -[<img src="./_assets/barchart.png" alt="barchart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/barchart.go) -[<img src="./_assets/gauge.png" alt="gauge" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/gauge.go) -[<img src="./_assets/linechart.png" alt="linechart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/linechart.go) -[<img src="./_assets/list.png" alt="list" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/list.go) -[<img src="./_assets/paragraph.png" alt="paragraph" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/paragraph.go) -[<img src="./_assets/sparkline.png" alt="sparkline" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/sparkline.go) -[<img src="./_assets/stacked_barchart.png" alt="stacked_barchart" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/stacked_barchart.go) -[<img src="./_assets/table.png" alt="table" type="image/png" width="45%">](https://github.com/gizak/termui/blob/master/_examples/table.go) - -### Examples - -Examples can be found in [\_examples](./_examples). Run an example with `go run _examples/{example}.go` or run all of them consecutively with `make run-examples`. - -### Documentation - -- [wiki](https://github.com/gizak/termui/wiki) - -## Uses - -- [cjbassi/gotop](https://github.com/cjbassi/gotop) -- [go-ethereum/monitorcmd](https://github.com/ethereum/go-ethereum/blob/96116758d22ddbff4dbef2050d6b63a7b74502d8/cmd/geth/monitorcmd.go) - -## Related Works - -- [blessed-contrib](https://github.com/yaronn/blessed-contrib) -- [tui-rs](https://github.com/fdehau/tui-rs) -- [gocui](https://github.com/jroimartin/gocui) - -## License - -[MIT](http://opensource.org/licenses/MIT) diff --git a/vendor/github.com/gizak/termui/canvas.go b/vendor/github.com/gizak/termui/canvas.go deleted file mode 100644 index 94733f9..0000000 --- a/vendor/github.com/gizak/termui/canvas.go +++ /dev/null @@ -1,63 +0,0 @@ -package termui - -import ( - "image" -) - -type Canvas struct { - CellMap map[image.Point]Cell - Block -} - -func NewCanvas() *Canvas { - return &Canvas{ - Block: *NewBlock(), - CellMap: make(map[image.Point]Cell), - } -} - -// points given as arguments correspond to dots within a braille character -// and therefore have 2x4 times the resolution of a normal cell -func (self *Canvas) Line(p0, p1 image.Point, color Color) { - leftPoint, rightPoint := p0, p1 - if leftPoint.X > rightPoint.X { - leftPoint, rightPoint = rightPoint, leftPoint - } - - xDistance := AbsInt(leftPoint.X - rightPoint.X) - yDistance := AbsInt(leftPoint.Y - rightPoint.Y) - slope := float64(yDistance) / float64(xDistance) - slopeDirection := 1 - if rightPoint.Y < leftPoint.Y { - slopeDirection = -1 - } - - targetYCoordinate := float64(leftPoint.Y) - currentYCoordinate := leftPoint.Y - for i := leftPoint.X; i < rightPoint.X; i++ { - targetYCoordinate += (slope * float64(slopeDirection)) - if currentYCoordinate == int(targetYCoordinate) { - point := image.Pt(i/2, currentYCoordinate/4) - self.CellMap[point] = Cell{ - self.CellMap[point].Rune | BRAILLE[currentYCoordinate%4][i%2], - NewStyle(color), - } - } - for currentYCoordinate != int(targetYCoordinate) { - point := image.Pt(i/2, currentYCoordinate/4) - self.CellMap[point] = Cell{ - self.CellMap[point].Rune | BRAILLE[currentYCoordinate%4][i%2], - NewStyle(color), - } - currentYCoordinate += slopeDirection - } - } -} - -func (self *Canvas) Draw(buf *Buffer) { - for point, cell := range self.CellMap { - if point.In(self.Rectangle) { - buf.SetCell(Cell{cell.Rune + BRAILLE_OFFSET, cell.Style}, point) - } - } -} diff --git a/vendor/github.com/gizak/termui/events.go b/vendor/github.com/gizak/termui/events.go deleted file mode 100644 index 1337416..0000000 --- a/vendor/github.com/gizak/termui/events.go +++ /dev/null @@ -1,175 +0,0 @@ -// 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" - - tb "github.com/nsf/termbox-go" -) - -/* -List of events: - mouse events: - <MouseLeft> <MouseRight> <MouseMiddle> - <MouseWheelUp> <MouseWheelDown> - keyboard events: - any uppercase or lowercase letter like j or J - <C-d> etc - <M-d> etc - <Up> <Down> <Left> <Right> - <Insert> <Delete> <Home> <End> <Previous> <Next> - <Backspace> <Tab> <Enter> <Escape> <Space> - <C-<Space>> etc - terminal events: - <Resize> -*/ - -type EventType int - -const ( - KeyboardEvent EventType = iota - MouseEvent - ResizeEvent -) - -type Event struct { - Type EventType - ID string - Payload interface{} -} - -// Mouse payload. -type Mouse struct { - Drag bool - X int - Y int -} - -// Resize payload. -type Resize struct { - Width int - Height int -} - -// PollEvents gets events from termbox, converts them, then sends them to each of its channels. -func PollEvents() <-chan Event { - ch := make(chan Event) - go func() { - for { - ch <- convertTermboxEvent(tb.PollEvent()) - } - }() - return ch -} - -// convertTermboxKeyboardEvent converts a termbox keyboard event to a more friendly string format. -// Combines modifiers into the string instead of having them as additional fields in an event. -func convertTermboxKeyboardEvent(e tb.Event) Event { - k := string(e.Ch) - pre := "" - mod := "" - - if e.Mod == tb.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[tb.Key][2]string{ - tb.KeyCtrlSpace: {"C-", "<Space>"}, - tb.KeyBackspace: {"", "<Backspace>"}, - tb.KeyTab: {"", "<Tab>"}, - tb.KeyEnter: {"", "<Enter>"}, - tb.KeyEsc: {"", "<Escape>"}, - tb.KeyCtrlBackslash: {"C-", "\\"}, - tb.KeyCtrlSlash: {"C-", "/"}, - tb.KeySpace: {"", "<Space>"}, - tb.KeyCtrl8: {"C-", "8"}, - } - if sk, ok := kmap[e.Key]; ok { - pre = sk[0] - k = sk[1] - } - } - } - - if pre != "" { - k += ">" - } - - id := pre + mod + k - - return Event{ - Type: KeyboardEvent, - ID: id, - } -} - -func convertTermboxMouseEvent(e tb.Event) Event { - mouseButtonMap := map[tb.Key]string{ - tb.MouseLeft: "<MouseLeft>", - tb.MouseMiddle: "<MouseMiddle>", - tb.MouseRight: "<MouseRight>", - tb.MouseRelease: "<MouseRelease>", - tb.MouseWheelUp: "<MouseWheelUp>", - tb.MouseWheelDown: "<MouseWheelDown>", - } - - converted, ok := mouseButtonMap[e.Key] - if !ok { - converted = "Unknown_Mouse_Button" - } - - Drag := false - if e.Mod == tb.ModMotion { - Drag = true - } - - return Event{ - Type: MouseEvent, - ID: converted, - Payload: Mouse{ - X: e.MouseX, - Y: e.MouseY, - Drag: Drag, - }, - } -} - -// convertTermboxEvent turns a termbox event into a termui event. -func convertTermboxEvent(e tb.Event) Event { - if e.Type == tb.EventError { - panic(e.Err) - } - - var event Event - - switch e.Type { - case tb.EventKey: - event = convertTermboxKeyboardEvent(e) - case tb.EventMouse: - event = convertTermboxMouseEvent(e) - case tb.EventResize: - event = Event{ - Type: ResizeEvent, - ID: "<Resize>", - Payload: Resize{ - Width: e.Width, - Height: e.Height, - }, - } - } - - return event -} |