Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func testNext(t *testing.T, c color.Color) {
t.Helper()
t.Run("donotpanicplease", func(t *testing.T) {
source := "hi"
for i := 0; i < 100; i++ {
for range 100 {
c = c.Next()
s := c.Sprint(source)
if len(s) <= len(source) {
Expand Down
1 change: 0 additions & 1 deletion pkg/completion/hl.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nohl
// +build !nohl

package completion

Expand Down
1 change: 0 additions & 1 deletion pkg/highlighter/0interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nohl
// +build !nohl

package highlighter

Expand Down
1 change: 0 additions & 1 deletion pkg/highlighter/chroma.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nohl
// +build !nohl

package highlighter

Expand Down Expand Up @@ -32,125 +31,125 @@
lexer string
}

func (h *Chroma) highLight(w io.WriteCloser, r io.ReadCloser, o Options) error {
log.Debugln(" highlighter: start chroma Highlighter")
// log.Debugln(log.Pp(o))

filename := o.FileName

// MAX_READ_SIZE Bytes max
someSourceCode, err := io.ReadAll(&io.LimitedReader{R: r, N: MaxReadSize})
if err != nil {
return err
}

additionalChar := make([]byte, 1)
if _, err := r.Read(additionalChar); err == nil {
log.Debugf("highlighter: output is too large for me (> %s), I will not highlight it", stringutils.HumanSize(MaxReadSize))
_, err = w.Write(someSourceCode)
if err != nil {
log.Printf(" highlighter: %v\n", err)
}
_, err = w.Write(additionalChar)
if err != nil {
log.Printf(" highlighter: %v\n", err)
}
_, err = io.Copy(w, r)
if err != nil {
log.Printf(" highlighter: %v\n", err)
}
return nil
}

log.Debugf(" highlighter: read %v bytes\n", len(someSourceCode))
if err := r.Close(); err != nil {
log.Printf(" highlighter: %v\n", err)
}

// log.Debugf(" highlighter: registered lexers are: %v\n", lexers.Names(true))
lexersList := lexers.Names(true)
var lexer chroma.Lexer
if checkWithFuzzy(o.LexerHint, lexersList) {
log.Debugf(" highlighter: setting the lexer to %v\n", o.LexerHint)
lexer = lexers.Get(o.LexerHint)
} else {
lexer = lexers.Match(filename)
if lexer == nil {
log.Debugf(" highlighter: filename did not help to find a lexer, analyzing content...\n")
lexer = lexers.Analyse(string(someSourceCode))
if lexer == nil {
log.Debugf(" highlighter: fallbacking the lexer\n")
lexer = lexers.Fallback
}
}
lexer = chroma.Coalesce(lexer)
}

log.Debugf(" highlighter: chosen Lexer is %v\n", lexer.Config().Name)
h.lexer = lexer.Config().Name

// log.Debugf(" highlighter: registered styles are: %v\n", styles.Names())
// registered styles are: [abap algol algol_nu arduino autumn base16-snazzy borland bw colorful doom-one doom-one2 dracula emacs friendly fruity github hr_high_contrast hrdark igor lovelace manni monokai monokailight murphy native nord onesenterprise paraiso-dark paraiso-light pastie perldoc pygments rainbow_dash rrt solarized-dark solarized-dark256 solarized-light swapoff tango trac vim vs vulcan witchhazel xcode xcode-dark]

stylesList := styles.Names()
switch {
case o.StyleHint == "random":
// rand.Seed(time.Now().UnixNano())
//#nosec G404 (weak rand)
randStyle := rand.Intn(len(stylesList))
h.style = stylesList[randStyle]
case checkWithFuzzy(o.StyleHint, stylesList):
h.style = o.StyleHint
default:
h.style = DefaultStyle
}

style := styles.Get(h.style)
if style == nil {
style = styles.Fallback
}
log.Debugf(" highlighter: style is %+v\n", style.Name)

log.Debugf(" highlighter: registered formatters are: %v\n", formatters.Names())

formattersList := formatters.Names()
if checkWithFuzzy(o.FormatterHint, formattersList) {
h.formatter = o.FormatterHint
} else {
c := term.SupportedColors()
switch {
case c >= 16_000_000:
h.formatter = "terminal16m"
case c >= 256:
h.formatter = "terminal256"
case c >= 16:
h.formatter = "terminal16"
case c >= 8:
h.formatter = "terminal8"
default:
h.formatter = "noop"
}
/* h.formatter = DEFAULT_FORMATTER */
}
formatter := formatters.Get(h.formatter)
if formatter == nil {
formatter = formatters.Fallback
}
log.Debugf(" highlighter: formatter is %v\n", h.formatter)

iterator, err := lexer.Tokenise(nil, string(someSourceCode))
if err != nil {
return err
}

err = formatter.Format(w, style, iterator)
if err != nil {
return err
}

log.Debugln(" highlighter: end chroma Highlight")
return nil
}

Check notice on line 152 in pkg/highlighter/chroma.go

View check run for this annotation

codefactor.io / CodeFactor

pkg/highlighter/chroma.go#L34-L152

Complex Method

func checkWithFuzzy(s string, list []string) bool {
if len(s) == 0 {
Expand Down
1 change: 0 additions & 1 deletion pkg/lockable/flock.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package lockable

Expand Down
8 changes: 4 additions & 4 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
Stderr = &Logger{Logger: log.New(os.Stderr, "", flags)}

DebugIsDiscard int32
continueOnFatal int32
continueOnFatal atomic.Int32
)

func init() {
Expand Down Expand Up @@ -66,14 +66,14 @@ func Println(v ...any) {

func Fatal(v ...any) {
Stderr.Output(2, fmt.Sprint(v...))
if atomic.LoadInt32(&continueOnFatal) <= 0 {
if continueOnFatal.Load() <= 0 {
os.Exit(1)
}
}

func Fatalf(format string, v ...any) {
Stderr.Output(2, fmt.Sprintf(format, v...))
if atomic.LoadInt32(&continueOnFatal) <= 0 {
if continueOnFatal.Load() <= 0 {
os.Exit(1)
}
}
Expand All @@ -91,5 +91,5 @@ func Pp(data any) string {
}

func SetContinueOnFatal() {
atomic.StoreInt32(&continueOnFatal, 1)
continueOnFatal.Store(1)
}
2 changes: 1 addition & 1 deletion pkg/miniclaude/messagesapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ stream:
if eventName == "error" {
log.Printf("error: %s", eventData)
}
var d map[string]interface{}
var d map[string]any
err = json.Unmarshal([]byte(eventData), &d)
if err != nil {
log.Debugf("error unmarshalling: %s\n%s\n", err, eventData)
Expand Down
2 changes: 1 addition & 1 deletion pkg/miniclaude/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Request struct {
type MessagesResponse struct {
ID string `json:"id"`
Type string `json:"type"` // always "message"
Error ErrorBlock `json:"error,omitempty"`
Error ErrorBlock `json:"error"`
Role string `json:"role"` // always "assistant"
Content []ContentBlock `json:"content"`
Model string `json:"model"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/mutators/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewPipeline(description string, out io.WriteCloser, in io.ReadCloser) error
globalPipeline.mu.Unlock()
return errors.New("empty pipeline requested")
}
list := strings.Split(description, mutators.StageSeparator)
for _, m := range list {
list := strings.SplitSeq(description, mutators.StageSeparator)
for m := range list {
log.Debugf("creating %v\n", m)
mutator, err := mutators.New(m)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/mutators/single/googleai.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"os"
"strings"

"github.com/batmac/ccat/pkg/log"
"github.com/google/generative-ai-go/genai"
Expand Down Expand Up @@ -55,15 +56,15 @@ func googleai(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
return 0, err
}

p := ""
var p strings.Builder
for i, c := range resp.Candidates[0].Content.Parts {
log.Debugln("part ", i, ": ", c)
if t, ok := c.(genai.Text); ok {
p += string(t)
p.WriteString(string(t))
}
}

n, err := io.WriteString(w, p)
n, err := io.WriteString(w, p.String())
if err != nil {
return 0, err
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/mutators/single/jsonIndent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ func init() {
}

func jsonIndent(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
indent := cfgInt(config)
if indent < 0 {
indent = 0
}
indent := max(cfgInt(config), 0)
j, err := io.ReadAll(r) // NOT streamable
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/mutators/single/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func jSONPath(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
return 0, err
}

var v interface{}
var v any
if err := json.Unmarshal(buf, &v); err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mutators/single/ndjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NDJSONIndent(w io.WriteCloser, r io.ReadCloser, arg any) (int64, error) {
}

// Parse and pretty-print the JSON line
var jsonObj interface{}
var jsonObj any
if err := json.Unmarshal([]byte(line), &jsonObj); err != nil {
// If it's not valid JSON, write the line as-is
if _, err := fmt.Fprintln(w, line); err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/openers/crng.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/crng_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/crng_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers_test

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/echo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/echo_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers_test

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/gemini.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/http.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
5 changes: 2 additions & 3 deletions pkg/openers/http_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers_test

Expand Down Expand Up @@ -29,7 +28,7 @@ func init() {

go func() {
// find an available port
for i := 0; i < 100; i++ {
for range 100 {
m.Lock()
portTest = 10000 + rand.Intn(55000) //#nosec G404
m.Unlock()
Expand All @@ -38,7 +37,7 @@ func init() {
}()
go func() {
// find an available port
for i := 0; i < 100; i++ {
for range 100 {
m.Lock()
insecurePortTest = 10000 + rand.Intn(55000) //#nosec G404
m.Unlock()
Expand Down
1 change: 0 additions & 1 deletion pkg/openers/mc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/nc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/prng.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/prng_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down
1 change: 0 additions & 1 deletion pkg/openers/prng_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers_test

Expand Down
5 changes: 2 additions & 3 deletions pkg/openers/sse.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down Expand Up @@ -110,8 +109,8 @@ func (s *sseReadCloser) Read(p []byte) (int, error) {
}

// Only output data lines
if strings.HasPrefix(line, "data:") {
eventData := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
if after, ok := strings.CutPrefix(line, "data:"); ok {
eventData := strings.TrimSpace(after)
s.buf.WriteString(eventData)
s.buf.WriteString("\n")
return s.buf.Read(p)
Expand Down
9 changes: 4 additions & 5 deletions pkg/openers/wormhole.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !fileonly
// +build !fileonly

package openers

Expand Down Expand Up @@ -39,10 +38,10 @@ func (f wormholeOpener) Description() string {
}

func (f wormholeOpener) Open(code string, _ bool) (io.ReadCloser, error) {
if strings.HasPrefix(code, "wormhole://") {
code = strings.TrimPrefix(code, "wormhole://")
} else if strings.HasPrefix(code, "wh://") {
code = strings.TrimPrefix(code, "wh://")
if after, ok := strings.CutPrefix(code, "wormhole://"); ok {
code = after
} else if after, ok := strings.CutPrefix(code, "wh://"); ok {
code = after
}

var c wormhole.Client
Expand Down
2 changes: 1 addition & 1 deletion pkg/pcg/pcg32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestPCG32Output(t *testing.T) {
generated := make(map[uint32]struct{})
var firstValue uint32
allSame := true
for i := 0; i < count; i++ {
for i := range count {
v := rng.Next()
generated[v] = struct{}{}
if i == 0 {
Expand Down
1 change: 0 additions & 1 deletion pkg/secretprovider/keyring_mock.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !keystore
// +build !keystore

package secretprovider

Expand Down
9 changes: 3 additions & 6 deletions pkg/stringutils/stringInSlice.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package stringutils

import "slices"

func IsStringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
return slices.Contains(list, a)
}
2 changes: 1 addition & 1 deletion pkg/utils/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "testing"
func CheckBytesRandomness(t *testing.T, data []byte) {
t.Helper()
acc := uint8(0)
for i := 0; i < len(data); i++ {
for i := range data {
acc |= data[i]
}
if acc != 0xFF {
Expand Down
Loading