From c7bdc01309977be9963c9d07902d978a968bdb20 Mon Sep 17 00:00:00 2001 From: Daulet Zhanguzin Date: Mon, 2 Sep 2024 15:44:59 -0700 Subject: [PATCH] feat: highlight code block output errors --- cmd/color.go | 17 +++++++++++++++++ cmd/main.go | 11 ++++++++--- go.mod | 8 +++++++- go.sum | 11 +++++++++++ provider/cache.go | 4 +++- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 cmd/color.go diff --git a/cmd/color.go b/cmd/color.go new file mode 100644 index 0000000..be2ed6d --- /dev/null +++ b/cmd/color.go @@ -0,0 +1,17 @@ +package main + +import ( + "io" + + "github.com/fatih/color" +) + +type colorWriter struct { + io.Writer + *color.Color +} + +func (c *colorWriter) Write(p []byte) (n int, err error) { + c.Color.Fprint(c.Writer, string(p)) + return len(p), nil +} diff --git a/cmd/main.go b/cmd/main.go index 76253e1..8d006ed 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,6 +16,8 @@ import ( "github.com/daulet/cmd/config" "github.com/daulet/cmd/parser" "github.com/daulet/cmd/provider" + + "github.com/fatih/color" ) const ( @@ -148,7 +150,10 @@ func runBlock(block *parser.CodeBlock) error { func runCmd(prog string, args ...string) error { cmd := exec.Command(prog, args...) cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stderr = &colorWriter{ + Writer: os.Stderr, + Color: color.New(color.FgHiRed), + } return cmd.Run() } @@ -254,7 +259,7 @@ func cmd(ctx context.Context) error { go func() { defer close(done) for block := range blockCh { - runBlock(block) + _ = runBlock(block) } }() // no output to the user, we just execute the code @@ -376,7 +381,7 @@ func main() { } if err := cmd(ctx); err != nil { - fmt.Println(err) + color.Yellow("error: %v", err) os.Exit(1) } } diff --git a/go.mod b/go.mod index 72fbddf..03e4087 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,13 @@ go 1.23.0 require ( github.com/cohere-ai/cohere-go/v2 v2.7.0 + github.com/fatih/color v1.17.0 github.com/sashabaranov/go-openai v1.29.0 ) -require github.com/google/uuid v1.4.0 // indirect +require ( + github.com/google/uuid v1.4.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.18.0 // indirect +) diff --git a/go.sum b/go.sum index d90c668..4ecf21b 100644 --- a/go.sum +++ b/go.sum @@ -2,13 +2,24 @@ github.com/cohere-ai/cohere-go/v2 v2.7.0 h1:O9XYYTtMvDdREbw3UzLgDXt+pWhdwHOTLyPt github.com/cohere-ai/cohere-go/v2 v2.7.0/go.mod h1:dlDCT66i8BqZDuuskFvYzsrc+O0M4l5J9Ibckoflvt4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= +github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sashabaranov/go-openai v1.29.0 h1:eBH6LSjtX4md5ImDCX8hNhHQvaRf22zujiERoQpsvLo= github.com/sashabaranov/go-openai v1.29.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +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.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/provider/cache.go b/provider/cache.go index dbd6fc5..5c164a1 100644 --- a/provider/cache.go +++ b/provider/cache.go @@ -27,7 +27,9 @@ func NewCacheProvider(p Provider, cachePath string) (ProviderCloser, error) { } return nil, err } - cache := &cache{} + cache := &cache{ + AudioSegments: make(map[string][]*AudioSegment), + } if err := json.Unmarshal(data, cache); err != nil { return nil, err }