From 0b0cce5a74374b44ba0b1e9d0121ca32eacab534 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sat, 6 Jun 2020 04:26:07 -0700 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ .gitlab-ci.yml | 20 ++++++++ CHANGELOG | 2 + LICENSE | 21 ++++++++ README.md | 9 ++++ go.mod | 9 ++++ go.sum | 28 +++++++++++ main.go | 58 ++++++++++++++++++++++ pkg/render/card.go | 119 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 269 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 CHANGELOG create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pkg/render/card.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9619758 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +*.sh +crib diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..9dd2e78 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,20 @@ +image: golang:latest + +stages: + - validate + +fmt: + stage: validate + script: + - gofmt -l -s -e . + - exit $(gofmt -l -s -e . | wc -l) + +vet: + stage: validate + script: + - go vet -composites=false ./... + +test: + stage: validate + script: + - go test -race -v ./... diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..8da6a6f --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,2 @@ +0.1.0: +- Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7c20f89 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Trevor Slocum + +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/README.md b/README.md new file mode 100644 index 0000000..559e692 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# crib +[![CI status](https://gitlab.com/tslocum/crib/badges/master/pipeline.svg)](https://gitlab.com/tslocum/crib/commits/master) +[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space) + +[Cribbage.World](https://cribbage.world) terminal client + +## Support + +Please share issues and suggestions [here](https://gitlab.com/tslocum/crib/issues). diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..42327d8 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module gitlab.com/tslocum/crib + +go 1.14 + +require ( + github.com/gdamore/tcell v1.3.0 + gitlab.com/tslocum/cview v1.4.6 + gitlab.com/tslocum/joker v0.1.3 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b475b49 --- /dev/null +++ b/go.sum @@ -0,0 +1,28 @@ +github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= +github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM= +github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM= +github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= +github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +gitlab.com/tslocum/cbind v0.1.1 h1:JXXtxMWHgWLvoF+QkrvcNvOQ59juy7OE1RhT7hZfdt0= +gitlab.com/tslocum/cbind v0.1.1/go.mod h1:rX7vkl0pUSg/yy427MmD1FZAf99S7WwpUlxF/qTpPqk= +gitlab.com/tslocum/cview v1.4.6 h1:dEM/aOsatoaNZOZ511n8hhZABPzTunUdi0RpUw9uXjM= +gitlab.com/tslocum/cview v1.4.6/go.mod h1:PW2Ucec7oTYOfK4N+hqm/CKEN9B1PBidq5YJ3ZaeknU= +gitlab.com/tslocum/joker v0.1.3 h1:6AVQuc0Rt7LczppZKaAXfr5B5Yg/4sQky2SFaWaF9E4= +gitlab.com/tslocum/joker v0.1.3/go.mod h1:bxTQ0FFmBP465r9z76zcm97S4Ld9eCLa3q20TyVM82A= +golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 h1:YTzHMGlqJu67/uEo1lBv0n3wBXhXNeUbB1XfN2vmTm0= +golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..2faa989 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "log" + + "gitlab.com/tslocum/crib/pkg/render" + "gitlab.com/tslocum/cview" + "gitlab.com/tslocum/joker" +) + +var ( + app *cview.Application + exit = make(chan bool) +) + +func main() { + app = cview.NewApplication().EnableMouse(true) + + g := cview.NewGrid() + + mainBuf := cview.NewTextView() + mainBuf.SetDynamicColors(true) + + cards := joker.Cards{{joker.FaceAce, joker.SuitSpades}, {joker.Face2, joker.SuitSpades}, {joker.Face3, joker.SuitSpades}, {joker.Face10, joker.SuitHearts}} + mainBuf.Write(render.Cards(cards, 13, 11, 6)) + + b := cview.NewButton("test") + + b.SetSelectedFunc(func() { + panic("clicked") + }) + + g2 := cview.NewGrid() + + b2 := cview.NewButton("test2") + + b2.SetSelectedFunc(func() { + panic("clicked") + }) + + g2.AddItem(b2, 0, 0, 1, 1, 0, 0, false) + + g.AddItem(mainBuf, 0, 0, 1, 1, 0, 0, false) + g.AddItem(b, 1, 0, 1, 1, 0, 0, false) + g.AddItem(g2, 2, 0, 1, 1, 0, 0, false) + + app.SetRoot(g, true) + + go func() { + if err := app.Run(); err != nil { + log.Fatalf("failed to run application: %s", err) + } + + exit <- true + }() + + <-exit +} diff --git a/pkg/render/card.go b/pkg/render/card.go new file mode 100644 index 0000000..8a87ea0 --- /dev/null +++ b/pkg/render/card.go @@ -0,0 +1,119 @@ +package render + +import ( + "bytes" + "strconv" + + "gitlab.com/tslocum/joker" + "github.com/gdamore/tcell" +) + +// TODO Draw large symbol in center +func Cards(cards joker.Cards, w int, h int, f int) []byte { + if len(cards) == 0 { + return []byte{} + } + + var buf bytes.Buffer + var n int + + for i := range cards { + if i == 0 { + buf.WriteRune(tcell.RuneULCorner) + } else { + buf.WriteRune(tcell.RuneTTee) + } + cw := f + if i == len(cards)-1 { + cw = w - 2 + } + for i := 0; i < cw; i++ { + buf.WriteRune(tcell.RuneHLine) + } + } + buf.WriteRune(tcell.RuneURCorner) + buf.WriteRune('\n') + + for i := 0; i < h-2; i++ { + for j, card := range cards { + cw := f + if j == len(cards)-1 { + cw = w - 2 + } + + buf.WriteRune(tcell.RuneVLine) + + if i == 0 { + n, _ = buf.WriteString(cardFace(card)) + for j := 0; j < cw-n; j++ { + buf.WriteRune(' ') + } + } else if i == 1 { + buf.WriteString(card.Suit.Symbol()) + for j := 0; j < cw-1; j++ { + buf.WriteRune(' ') + } + } else if i == (h-2)/2 { + if j == len(cards)-1 { + for j := 0; j < (cw / 2); j++ { + buf.WriteRune(' ') + } + buf.WriteString(card.Suit.Symbol()) + for j := 0; j < cw-(cw/2)-1; j++ { + buf.WriteRune(' ') + } + } else { + for j := 0; j < cw-1; j++ { + buf.WriteRune(' ') + } + buf.WriteString(card.Suit.Symbol()) + } + } else if i == h-4 && j == len(cards)-1 { + for j := 0; j < cw-1; j++ { + buf.WriteRune(' ') + } + buf.WriteString(card.Suit.Symbol()) + } else if i == h-3 && j == len(cards)-1 { + n = len(cardFace(card)) + for j := 0; j < cw-n; j++ { + buf.WriteRune(' ') + } + buf.WriteString(cardFace(card)) + } else { + for j := 0; j < cw; j++ { + buf.WriteRune(' ') + } + } + } + + buf.WriteRune(tcell.RuneVLine) + buf.WriteRune('\n') + } + + for i := range cards { + if i == 0 { + buf.WriteRune(tcell.RuneLLCorner) + } else { + buf.WriteRune(tcell.RuneBTee) + } + cw := f + if i == len(cards)-1 { + cw = w - 2 + } + for i := 0; i < cw; i++ { + buf.WriteRune(tcell.RuneHLine) + } + } + buf.WriteRune(tcell.RuneLRCorner) + + return buf.Bytes() +} + +func cardFace(card joker.Card) string { + switch card.Face { + case joker.FaceAce, joker.FaceJack, joker.FaceQueen, joker.FaceKing: + return card.Face.Name()[0:1] + default: + return strconv.Itoa(int(card.Face)) + } +}