Got rid of bad color implementation

This commit is contained in:
Nox Sluijtman 2022-08-22 17:44:16 +02:00
parent 4d0ab352aa
commit 3afa5fb7db
7 changed files with 78 additions and 53 deletions

View file

@ -3,30 +3,44 @@ package Coin
import (
"fmt"
"math/rand"
"strings"
"time"
"gitlab.com/EternalWanderer/dice-roller/Colors"
"github.com/fatih/color"
)
var initRandom bool
func InitRandom(initialised bool) {
if initialised {
return
}
rand.Seed(time.Now().Unix())
initRandom = true
}
func Toss(castAmount int) {
InitRandom(initRandom)
yellow := color.New(color.FgYellow).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
var (
coins []string
coin string
coinState int
)
for i := 0; i < castAmount; i++ {
coinState = rand.Intn(2)
if coinState == 0 {
coin = Colors.ColorYellow + "heads" + Colors.ColorReset
coin = yellow("heads")
} else {
coin = Colors.ColorBlue + "tails" + Colors.ColorReset
coin = blue("tails")
}
coins = append(coins, coin)
}
if castAmount > 1 {
fmt.Printf("%sTossing %d coins...%s\n", Colors.ColorYellow, castAmount, Colors.ColorReset)
color.Yellow("Tossing %d coins...\n", castAmount)
} else {
fmt.Printf("%sTossing coin...%s\n", Colors.ColorYellow, Colors.ColorReset)
color.Yellow("Tossing coin...\n", castAmount)
}
fmt.Printf("\t%s\n", strings.Trim(fmt.Sprint(coins), "[]"))
fmt.Println(fmt.Sprint(coins))
}

View file

@ -1,13 +0,0 @@
package Colors
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorMagenta = "\u001b[35m"
ColorReset = "\u001b[0m"
)

View file

@ -6,7 +6,7 @@ import (
"strings"
"time"
"gitlab.com/EternalWanderer/dice-roller/Colors"
"github.com/fatih/color"
)
var (
@ -37,22 +37,20 @@ func Cast(surfaces, diceThrows, modifier int) {
switch {
case modifier != 0:
fmt.Printf("%sRolling %dd%d + %d...\n%s", Colors.ColorYellow, diceThrows, surfaces, modifier, Colors.ColorReset)
color.Yellow("Rolling %dd%d + %d...\n", diceThrows, surfaces, modifier)
default:
fmt.Printf("%sRolling %dd%d...\n%s", Colors.ColorYellow, diceThrows, surfaces, Colors.ColorReset)
color.Yellow("Rolling %dd%d...\n", diceThrows, surfaces)
}
if diceThrows > 1 {
fmt.Println(Colors.ColorMagenta, "\tIndividual rolls:",
strings.Trim(fmt.Sprint(casts), "[]"),
Colors.ColorReset)
color.Magenta("\tIndividual rolls: %s", strings.Trim(fmt.Sprint(casts), "[]"))
}
if modifier != 0 {
fmt.Println("\tWithout modifier:", total)
fmt.Println(Colors.ColorGreen, "\tWith modifier:", total+modifier, Colors.ColorReset)
fmt.Printf("\tWithout modifier: %d\n", total)
color.Green("\tWith modifier: %d", total+modifier)
} else {
fmt.Printf("%s\t%d%s\n", Colors.ColorGreen, total, Colors.ColorReset)
color.Green("%d", total)
}
}

View file

@ -2,7 +2,7 @@ VERSION = 0.3
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man
SRC = main.go go.mod
SRC = main.go go.mod go.sum Coin Dice
all: dice-roller
@ -14,7 +14,7 @@ clean:
dist: clean
mkdir -p dice-roller-$(VERSION)
cp -R zsh.completion LICENSE Makefile README.md dice-roller.1 Colors Coin Dice $(SRC) dice-roller-$(VERSION)
cp -R zsh.completion LICENSE Makefile README.md dice-roller.1 $(SRC) dice-roller-$(VERSION)
tar -cf dice-roller-$(VERSION).tar dice-roller-$(VERSION)
gzip dice-roller-$(VERSION).tar
rm -rf dice-roller-$(VERSION)

7
go.mod
View file

@ -1,3 +1,10 @@
module gitlab.com/EternalWanderer/dice-roller
go 1.17
require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
)

15
go.sum Normal file
View file

@ -0,0 +1,15 @@
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

48
main.go
View file

@ -5,8 +5,8 @@ import (
"fmt"
"os"
"github.com/fatih/color"
"gitlab.com/EternalWanderer/dice-roller/Coin"
"gitlab.com/EternalWanderer/dice-roller/Colors"
"gitlab.com/EternalWanderer/dice-roller/Dice"
)
@ -40,46 +40,50 @@ func main() {
case advantage:
result, x_block, y_block := Dice.Advantage()
if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Colors.ColorGreen, modifier, Colors.ColorReset)
fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "Without modifier...", Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorGreen, result, Colors.ColorReset, "x:", x_block, "y:", y_block)
fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "With modifier...", Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorGreen, result+modifier, Colors.ColorReset, "x:", x_block+modifier, "y:", y_block+modifier)
color.Green("Rolling 1d20 + %d with advantage...\n", modifier)
color.Yellow("Without modifier...")
fmt.Printf("\tx: %d\ty: %d\n", x_block, y_block)
color.Green("\t%d", result)
color.Yellow("With modifier...")
fmt.Printf("\tx: %d\ty: %d\n", x_block+modifier, y_block+modifier)
color.Green("\t%d", result+modifier)
} else {
fmt.Printf("%sRolling 1d20 with advantage...%s\n", Colors.ColorGreen, Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorGreen, result, Colors.ColorReset, "x:", x_block, "y:", y_block)
color.Green("Rolling 1d20 with advantage...")
fmt.Printf("\tx: %d\ty: %d\n", x_block, y_block)
color.Green("\t%d", result)
}
switch result {
case 20:
fmt.Printf("\t%sNatural%s 20!%s\n", Colors.ColorMagenta, Colors.ColorGreen, Colors.ColorReset)
color.Magenta("Natural 20!")
case 1:
fmt.Printf("\t%sNatural%s 1!%s\n", Colors.ColorMagenta, Colors.ColorRed, Colors.ColorReset)
default:
color.Magenta("Natural 1!")
}
case disadvantage:
result, x_block, y_block := Dice.Disadvantage()
if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Colors.ColorRed, modifier, Colors.ColorReset)
fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "Without modifier...", Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorRed, result, Colors.ColorReset, "x:", x_block, "y:", y_block)
fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "With modifier...", Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorRed, result+modifier, Colors.ColorReset, "x:", x_block+modifier, "y:", y_block+modifier)
color.Red("Rolling 1d20 + %d with disadvantage...\n", modifier)
color.Yellow("Without modifier...")
fmt.Printf("\tx: %d\ty: %d\n", x_block, y_block)
color.Red("\t%d", result)
color.Yellow("With modifier...")
fmt.Printf("\tx: %d\ty: %d\n", x_block+modifier, y_block+modifier)
color.Red("\t%d", result+modifier)
} else {
fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Colors.ColorRed, Colors.ColorReset)
fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorRed, result, Colors.ColorReset, "x:", x_block, "y:", y_block)
color.Green("Rolling 1d20 with disadvantage...")
fmt.Printf("\tx: %d\ty: %d\n", x_block, y_block)
color.Red("\t%d", result)
}
switch result {
case 20:
fmt.Printf("\t%sNatural%s 20!%s\n", Colors.ColorMagenta, Colors.ColorGreen, Colors.ColorReset)
color.Magenta("Natural 20!")
case 1:
fmt.Printf("\t%sNatural%s 1!%s\n", Colors.ColorMagenta, Colors.ColorRed, Colors.ColorReset)
default:
color.Magenta("Natural 1!")
}
case attacks > 1:
for i := 0; i < attacks; i++ {
fmt.Printf("%sAttack %d:%s\n", Colors.ColorBlue, i+1, Colors.ColorReset)
color.Blue("Attack: %d/%d\n", i+1, attacks)
Dice.Cast(surfaces, diceThrows, modifier)
}
default: