Files
gh-action-readme/internal/output.go
2025-07-30 19:17:36 +03:00

105 lines
2.3 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package internal
import (
"fmt"
"os"
"github.com/fatih/color"
)
// ColoredOutput provides methods for colored terminal output.
type ColoredOutput struct {
NoColor bool
Quiet bool
}
// NewColoredOutput creates a new colored output instance.
func NewColoredOutput(quiet bool) *ColoredOutput {
return &ColoredOutput{
NoColor: color.NoColor || os.Getenv("NO_COLOR") != "",
Quiet: quiet,
}
}
// Success prints a success message in green.
func (co *ColoredOutput) Success(format string, args ...any) {
if co.Quiet {
return
}
if co.NoColor {
fmt.Printf("✅ "+format+"\n", args...)
} else {
color.Green("✅ "+format, args...)
}
}
// Error prints an error message in red to stderr.
func (co *ColoredOutput) Error(format string, args ...any) {
if co.NoColor {
fmt.Fprintf(os.Stderr, "❌ "+format+"\n", args...)
} else {
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, "❌ "+format+"\n", args...)
}
}
// Warning prints a warning message in yellow.
func (co *ColoredOutput) Warning(format string, args ...any) {
if co.Quiet {
return
}
if co.NoColor {
fmt.Printf("⚠️ "+format+"\n", args...)
} else {
color.Yellow("⚠️ "+format, args...)
}
}
// Info prints an info message in blue.
func (co *ColoredOutput) Info(format string, args ...any) {
if co.Quiet {
return
}
if co.NoColor {
fmt.Printf(" "+format+"\n", args...)
} else {
color.Blue(" "+format, args...)
}
}
// Progress prints a progress message in cyan.
func (co *ColoredOutput) Progress(format string, args ...any) {
if co.Quiet {
return
}
if co.NoColor {
fmt.Printf("🔄 "+format+"\n", args...)
} else {
color.Cyan("🔄 "+format, args...)
}
}
// Bold prints text in bold.
func (co *ColoredOutput) Bold(format string, args ...any) {
if co.Quiet {
return
}
if co.NoColor {
fmt.Printf(format+"\n", args...)
} else {
_, _ = color.New(color.Bold).Printf(format+"\n", args...)
}
}
// Printf prints without color formatting (respects quiet mode).
func (co *ColoredOutput) Printf(format string, args ...any) {
if co.Quiet {
return
}
fmt.Printf(format, args...)
}
// Fprintf prints to specified writer without color formatting.
func (co *ColoredOutput) Fprintf(w *os.File, format string, args ...any) {
_, _ = fmt.Fprintf(w, format, args...)
}