Skip to content

Commit

Permalink
feat: added option to change output writer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 13, 2024
1 parent fc21c2b commit 3a20c50
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 21 deletions.
45 changes: 24 additions & 21 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,30 @@ linters-settings:
ignore-map-index-ok: true
ignore-chan-recv-ok: true
ignore-decls:
- n int
- x int
- y int
- z int
- i int
- a int
- b int
- c int
- j int
- T any
- a any
- b any
- c any
- d any
- data any
- n any
- t time.Time
- f func()
- cb func()
- t testing.T
- b testing.B
- n int # generic number
- x int # generic number (e.g. coordinate)
- y int # generic number (e.g. coordinate)
- z int # generic number (e.g. coordinate)
- i int # generic number
- a int # generic number
- r int # generic number (e.g. red or radius)
- g int # generic number (e.g. green)
- b int # generic number (e.g. blue)
- c int # generic number (e.g. count)
- j int # generic number (e.g. index)
- T any # generic type
- a any # generic any (e.g. data)
- b any # generic any (e.g. body)
- c any # generic any
- d any # generic any (e.g. data)
- data any # generic data
- n any # generic any
- t time.Time # often used as a variable name
- f func() # often used as a callback variable name
- cb func() # often used as a callback variable name
- t testing.T # default testing.T variable name
- b testing.B # default testing.B variable name
- sb strings.Builder # often used as a variable name

issues:
exclude-rules:
Expand Down
7 changes: 7 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package color

import (
"io"
"os"
)

var Writer io.Writer = os.Stdout

type Color interface {
Sequence(background bool) string
}
47 changes: 47 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package color

import (
"fmt"
"io"
"strings"
)

Expand Down Expand Up @@ -46,6 +47,7 @@ func (s Style) Sequence() string {
return sb.String()
}

// Sprint formats using the default formats for its operands and returns the resulting string.
func (s Style) Sprint(a ...any) string {
var sb strings.Builder

Expand All @@ -55,3 +57,48 @@ func (s Style) Sprint(a ...any) string {

return sb.String()
}

// Sprintf formats according to a format specifier and returns the resulting string.
func (s Style) Sprintf(format string, a ...any) string {
return s.Sprint(fmt.Sprintf(format, a...))
}

// Fprint formats using the default formats for its operands and writes to w.
func (s Style) Fprint(w io.Writer, a ...any) (n int, err error) {
return fmt.Fprint(w, s.Sprint(a...)) //nolint:wrapcheck // stdlib error
}

// Fprintf formats according to a format specifier and writes to w.
func (s Style) Fprintf(w io.Writer, format string, a ...any) (n int, err error) {
return fmt.Fprint(w, s.Sprintf(format, a...)) //nolint:wrapcheck // stdlib error
}

// Fprintln formats using the default formats for its operands and writes to w.
func (s Style) Fprintln(w io.Writer, a ...any) (n int, err error) {
return fmt.Fprintln(w, s.Sprint(a...)) //nolint:wrapcheck // stdlib error
}

// Fprintfln formats according to a format specifier and writes to w.
func (s Style) Fprintfln(w io.Writer, format string, a ...any) (n int, err error) {
return fmt.Fprintln(w, s.Sprintf(format, a...)) //nolint:wrapcheck // stdlib error
}

// Print formats using the default formats for its operands and writes to standard output.
func (s Style) Print(a ...any) {
s.Fprint(Writer, a...)
}

// Printf formats according to a format specifier and writes to standard output.
func (s Style) Printf(format string, a ...any) {
s.Fprintf(Writer, format, a...)
}

// Println formats using the default formats for its operands and writes to standard output.
func (s Style) Println(a ...any) {
s.Fprintln(Writer, a...)
}

// Printfln formats according to a format specifier and writes to standard output.
func (s Style) Printfln(format string, a ...any) {
s.Fprintfln(Writer, format, a...)

Check failure on line 103 in style.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `s.Fprintfln` is not checked (errcheck)
}

0 comments on commit 3a20c50

Please sign in to comment.