writer.go

  1package lipgloss
  2
  3import (
  4	"bytes"
  5	"fmt"
  6	"io"
  7	"os"
  8
  9	"github.com/charmbracelet/colorprofile"
 10)
 11
 12// Writer is the default writer that prints to stdout, automatically
 13// downsampling colors when necessary.
 14var Writer = colorprofile.NewWriter(os.Stdout, os.Environ())
 15
 16// Println to stdout, automatically downsampling colors when necessary, ending
 17// with a trailing newline.
 18//
 19// Example:
 20//
 21//	str := NewStyle().
 22//	    Foreground(lipgloss.Color("#6a00ff")).
 23//	    Render("breakfast")
 24//
 25//	Println("Time for a", str, "sandwich!")
 26func Println(v ...any) (int, error) {
 27	return fmt.Fprintln(Writer, v...) //nolint:wrapcheck
 28}
 29
 30// Printf prints formatted text to stdout, automatically downsampling colors
 31// when necessary.
 32//
 33// Example:
 34//
 35//	str := NewStyle().
 36//	  Foreground(lipgloss.Color("#6a00ff")).
 37//	  Render("knuckle")
 38//
 39//	Printf("Time for a %s sandwich!\n", str)
 40func Printf(format string, v ...any) (int, error) {
 41	return fmt.Fprintf(Writer, format, v...) //nolint:wrapcheck
 42}
 43
 44// Print to stdout, automatically downsampling colors when necessary.
 45//
 46// Example:
 47//
 48//	str := NewStyle().
 49//	    Foreground(lipgloss.Color("#6a00ff")).
 50//	    Render("Who wants marmalade?\n")
 51//
 52//	Print(str)
 53func Print(v ...any) (int, error) {
 54	return fmt.Fprint(Writer, v...) //nolint:wrapcheck
 55}
 56
 57// Fprint pritnts to the given writer, automatically downsampling colors when
 58// necessary.
 59//
 60// Example:
 61//
 62//	str := NewStyle().
 63//	    Foreground(lipgloss.Color("#6a00ff")).
 64//	    Render("guzzle")
 65//
 66//	Fprint(os.Stderr, "I %s horchata pretty much all the time.\n", str)
 67func Fprint(w io.Writer, v ...any) (int, error) {
 68	return fmt.Fprint(colorprofile.NewWriter(w, os.Environ()), v...) //nolint:wrapcheck
 69}
 70
 71// Fprintln prints to the given writer, automatically downsampling colors when
 72// necessary, and ending with a trailing newline.
 73//
 74// Example:
 75//
 76//	str := NewStyle().
 77//	    Foreground(lipgloss.Color("#6a00ff")).
 78//	    Render("Sandwich time!")
 79//
 80//	Fprintln(os.Stderr, str)
 81func Fprintln(w io.Writer, v ...any) (int, error) {
 82	return fmt.Fprintln(colorprofile.NewWriter(w, os.Environ()), v...) //nolint:wrapcheck
 83}
 84
 85// Fprintf prints text to a writer, against the given format, automatically
 86// downsampling colors when necessary.
 87//
 88// Example:
 89//
 90//	str := NewStyle().
 91//	    Foreground(lipgloss.Color("#6a00ff")).
 92//	    Render("artichokes")
 93//
 94//	Fprintf(os.Stderr, "I really love %s!\n", food)
 95func Fprintf(w io.Writer, format string, v ...any) (int, error) {
 96	return fmt.Fprintf(colorprofile.NewWriter(w, os.Environ()), format, v...) //nolint:wrapcheck
 97}
 98
 99// Sprint returns a string for stdout, automatically downsampling colors when
100// necessary.
101//
102// Example:
103//
104//	str := NewStyle().
105//		Faint(true).
106//	    Foreground(lipgloss.Color("#6a00ff")).
107//	    Render("I love to eat")
108//
109//	str = Sprint(str)
110func Sprint(v ...any) string {
111	var buf bytes.Buffer
112	w := colorprofile.Writer{
113		Forward: &buf,
114		Profile: Writer.Profile,
115	}
116	fmt.Fprint(&w, v...) //nolint:errcheck
117	return buf.String()
118}
119
120// Sprintln returns a string for stdout, automatically downsampling colors when
121// necessary, and ending with a trailing newline.
122//
123// Example:
124//
125//	str := NewStyle().
126//		Bold(true).
127//		Foreground(lipgloss.Color("#6a00ff")).
128//		Render("Yummy!")
129//
130//	str = Sprintln(str)
131func Sprintln(v ...any) string {
132	var buf bytes.Buffer
133	w := colorprofile.Writer{
134		Forward: &buf,
135		Profile: Writer.Profile,
136	}
137	fmt.Fprintln(&w, v...) //nolint:errcheck
138	return buf.String()
139}
140
141// Sprintf returns a formatted string for stdout, automatically downsampling
142// colors when necessary.
143//
144// Example:
145//
146//	str := NewStyle().
147//		Bold(true).
148//		Foreground(lipgloss.Color("#fccaee")).
149//		Render("Cantaloupe")
150//
151//	str = Sprintf("I really love %s!", str)
152func Sprintf(format string, v ...any) string {
153	var buf bytes.Buffer
154	w := colorprofile.Writer{
155		Forward: &buf,
156		Profile: Writer.Profile,
157	}
158	fmt.Fprintf(&w, format, v...) //nolint:errcheck
159	return buf.String()
160}