Skip to content

Commit

Permalink
Merge branch 'master' of github.com:goccy/go-yaml into fix-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 9, 2024
2 parents e764083 + e1bab38 commit cb2678a
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
make ycat/build
- name: run
run: |
./ycat .github/workflows/go.yml
./bin/ycat .github/workflows/go.yml
coverage:
name: Coverage
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/
cover.out
30 changes: 19 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
TESTMOD := testdata/go_test.mod

$(LOCALBIN):
mkdir -p $(LOCALBIN)

.PHONY: testmod
cp go.mod testdata/go_test.mod
go mod tidy -modfile=$(TESTMOD)

.PHONY: test
test:
test: testmod
go test -v -race ./...
go test -v -race ./testdata -modfile=$(TESTMOD)

.PHONY: simple-test
simple-test:
simple-test: testmod
go test -v ./...
go test -v ./testdata -modfile=$(TESTMOD)

.PHONY: cover
cover:
go test -coverprofile=cover.out ./...
cover: testmod
go test -coverpkg=.,./ast,./lexer,./parser,./printer,./scanner,./token -coverprofile=cover.out -modfile=$(TESTMOD) ./... ./testdata

.PHONY: cover-html
cover-html: cover
go tool cover -html=cover.out

.PHONY: ycat/build
ycat/build:
go build -o ycat ./cmd/ycat
ycat/build: $(LOCALBIN)
cd ./cmd/ycat && go build -o $(LOCALBIN)/ycat .

.PHONY: lint
lint: golangci-lint ## Run golangci-lint
Expand All @@ -28,11 +41,6 @@ fmt: golangci-lint ## Ensure consistent code style
@go fmt ./...
@$(GOLANGCI_LINT) run --fix

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,16 @@ print yaml file with color
### Installation

```sh
go install github.com/goccy/go-yaml/cmd/ycat@latest
git clone https://github.com/goccy/go-yaml.git
cd ./cmd/ycat && go insatll .
```


# For Developers

> [!NOTE]
> In this project, we manage such test code under the `testdata` directory to avoid adding dependencies on libraries that are only needed for testing to the top `go.mod` file. Therefore, if you want to add test cases that use 3rd party libraries, please add the test code to the `testdata` directory.
# Looking for Sponsors

I'm looking for sponsors this library. This library is being developed as a personal project in my spare time. If you want a quick response or problem resolution when using this library in your project, please register as a [sponsor](https://github.com/sponsors/goccy). I will cooperate as much as possible. Of course, this library is developed as an MIT license, so you can use it freely for free.
Expand Down
16 changes: 16 additions & 0 deletions cmd/ycat/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/goccy/go-yaml/cmd/ycat

go 1.21.0

require (
github.com/fatih/color v1.18.0
github.com/goccy/go-yaml v0.0.0-00010101000000-000000000000
github.com/mattn/go-colorable v0.1.13
)

require (
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.26.0 // indirect
)

replace github.com/goccy/go-yaml => ../../
29 changes: 29 additions & 0 deletions cmd/ycat/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
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.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
6 changes: 6 additions & 0 deletions cmd/ycat/ycat.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func _main(args []string) error {
Suffix: format(color.Reset),
}
}
p.Comment = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiBlack),
Suffix: format(color.Reset),
}
}
writer := colorable.NewColorableStdout()
_, _ = writer.Write([]byte(p.PrintTokens(tokens) + "\n"))
return nil
Expand Down
21 changes: 1 addition & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
module github.com/goccy/go-yaml

go 1.19

require (
github.com/fatih/color v1.18.0
github.com/go-playground/validator/v10 v10.22.1
github.com/google/go-cmp v0.6.0
github.com/mattn/go-colorable v0.1.13
)

require (
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
)
go 1.21.0
34 changes: 0 additions & 34 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
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.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
83 changes: 83 additions & 0 deletions printer/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This source inspired by https://github.com/fatih/color.
package printer

import (
"fmt"
"strings"
)

type ColorAttribute int

const (
ColorReset ColorAttribute = iota
ColorBold
ColorFaint
ColorItalic
ColorUnderline
ColorBlinkSlow
ColorBlinkRapid
ColorReverseVideo
ColorConcealed
ColorCrossedOut
)

const (
ColorFgHiBlack ColorAttribute = iota + 90
ColorFgHiRed
ColorFgHiGreen
ColorFgHiYellow
ColorFgHiBlue
ColorFgHiMagenta
ColorFgHiCyan
ColorFgHiWhite
)

const (
ColorResetBold ColorAttribute = iota + 22
ColorResetItalic
ColorResetUnderline
ColorResetBlinking

ColorResetReversed
ColorResetConcealed
ColorResetCrossedOut
)

const escape = "\x1b"

var colorResetMap = map[ColorAttribute]ColorAttribute{
ColorBold: ColorResetBold,
ColorFaint: ColorResetBold,
ColorItalic: ColorResetItalic,
ColorUnderline: ColorResetUnderline,
ColorBlinkSlow: ColorResetBlinking,
ColorBlinkRapid: ColorResetBlinking,
ColorReverseVideo: ColorResetReversed,
ColorConcealed: ColorResetConcealed,
ColorCrossedOut: ColorResetCrossedOut,
}

func format(attrs ...ColorAttribute) string {
format := make([]string, 0, len(attrs))
for _, attr := range attrs {
format = append(format, fmt.Sprint(attr))
}
return fmt.Sprintf("%s[%sm", escape, strings.Join(format, ";"))
}

func unformat(attrs ...ColorAttribute) string {
format := make([]string, len(attrs))
for _, attr := range attrs {
v := fmt.Sprint(ColorReset)
reset, exists := colorResetMap[attr]
if exists {
v = fmt.Sprint(reset)
}
format = append(format, v)
}
return fmt.Sprintf("%s[%sm", escape, strings.Join(format, ";"))
}

func colorize(msg string, attrs ...ColorAttribute) string {
return format(attrs...) + msg + unformat(attrs...)
}
45 changes: 21 additions & 24 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"math"
"strings"

"github.com/fatih/color"

"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/token"
)
Expand Down Expand Up @@ -151,57 +149,57 @@ func (p *Printer) PrintNode(node ast.Node) []byte {
return []byte(fmt.Sprintf("%+v\n", node))
}

const escape = "\x1b"

func format(attr color.Attribute) string {
return fmt.Sprintf("%s[%dm", escape, attr)
}

func (p *Printer) setDefaultColorSet() {
p.Bool = func() *Property {
return &Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiMagenta),
Suffix: format(ColorReset),
}
}
p.Number = func() *Property {
return &Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiMagenta),
Suffix: format(ColorReset),
}
}
p.MapKey = func() *Property {
return &Property{
Prefix: format(color.FgHiCyan),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiCyan),
Suffix: format(ColorReset),
}
}
p.Anchor = func() *Property {
return &Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiYellow),
Suffix: format(ColorReset),
}
}
p.Alias = func() *Property {
return &Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiYellow),
Suffix: format(ColorReset),
}
}
p.String = func() *Property {
return &Property{
Prefix: format(color.FgHiGreen),
Suffix: format(color.Reset),
Prefix: format(ColorFgHiGreen),
Suffix: format(ColorReset),
}
}
p.Comment = func() *Property {
return &Property{
Prefix: format(ColorFgHiBlack),
Suffix: format(ColorReset),
}
}
}

func (p *Printer) PrintErrorMessage(msg string, isColored bool) string {
if isColored {
return fmt.Sprintf("%s%s%s",
format(color.FgHiRed),
format(ColorFgHiRed),
msg,
format(color.Reset),
format(ColorReset),
)
}
return msg
Expand Down Expand Up @@ -324,8 +322,7 @@ func (p *Printer) setupErrorTokenFormat(annotateLine int, isColored bool) {
p.LineNumber = true
p.LineNumberFormat = func(num int) string {
if isColored {
fn := color.New(color.Bold, color.FgHiWhite).SprintFunc()
return fn(prefix(annotateLine, num))
return colorize(prefix(annotateLine, num), ColorBold, ColorFgHiWhite)
}
return prefix(annotateLine, num)
}
Expand Down
Loading

0 comments on commit cb2678a

Please sign in to comment.