Add Rich, Flexible Styling to Your Go Console Apps with GOStyle.
- Color
- Background Color
- Bold
- Italic
- Underline
- Strike Through
- Darken (Darker than normal)
- Lighten (Lighter than normal)
- Effortless Integration: Upgrade your console output in minutes, without major refactoring.
- True Color Support: Leverage the full range of 24-bit color for awesome styling.
- Flexible Control: Turn styling on or off. APIs have flag parameters to 'turn on' or 'turn off' styling. This provides flexibility to use the same code for both console output and logging.
- User-Friendly API: Choose your preferred syntax for a seamless coding experience.
// Enhanced console output:
// Apply Blue color to "Hello World".
fmt.Println(gostyle.ApplyTo("Hello World", true).Color(wcolor.Blue).String())GOStyle provides the following three methods to create colors.
-
GOStyle supports CSS named colors like Blue, Silver, Red, etc. You can view the full list of CSS named colors here.
clr := wcolor.Cyan
-
You can construct colors using Red, Green, and Blue components.
clr := wcolor.RGB(200, 25, 200)
-
You can use CSS3 style HEX colors.
clr1 := wcolor.HEX("#f210fe") // 6 digit hex value with # prefix. clr2 := wcolor.HEX("#fff") // 3 digit hex value with # prefix.
package main
import (
"fmt"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
color1 := wcolor.Cyan
color2 := wcolor.RGB(200, 25, 200)
color3 := wcolor.HEX("#f210fe")
// Usage 1: Apply style to a string and then print it.
styledStr1 := gostyle.ApplyTo("Hello World", true).Color(color1).Bold().String()
fmt.Println(styledStr1)
// Usage 2: Mark the start and end of the color while printing.
fmt.Printf("%sHello%s World\n", color2.Start(true), color2.End(true))
fmt.Printf("%sHello%s %sWorld%s\n", color2.Start(true), color2.End(true), color3.Start(true), color3.End(true))
// Usage 3: Define a style and apply it to a string.
style1 := gostyle.Style{
Color: wcolor.Lime.Clone(),
Background: nil,
Bold: true,
Italic: false,
Underline: true,
Strikethrough: false,
Darken: false,
Lighten: false,
}
styledStr2 := style1.ApplyTo("Hello", true).String()
fmt.Println(styledStr2)
// Usage 4: Mark the start and end of the style while printing.
fmt.Printf("%sHello%s World\n", style1.Start(true), style1.End(true))
}package main
import (
"fmt"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
// Apply Blue color to "Hello World".
styledStr1 := gostyle.ApplyTo("Hello World", true).Color(wcolor.Blue).String()
fmt.Println(styledStr1)
// You can apply other styles too. Like, Bold, Underline, etc.
styledStr2 := gostyle.ApplyTo("Hello World", true).Color(wcolor.Dodgerblue).Bold().Underline().String()
fmt.Println(styledStr2)
// You can concatinate un-styled string with styled string.
styledStr3 := gostyle.ApplyTo("Hello", true).Color(wcolor.Dodgerblue).String()
styledStr3 += " World"
// Below print statement outputs 'Hello' using Blue color. And the ' World' will be printed using the terminal's default color.
fmt.Println(styledStr3)
}package main
import (
"fmt"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
color := wcolor.Cyan
// You can mark start and end of the color while printing. Hello will be printed in Cyan.
fmt.Printf("%sHello%s World\n", color.Start(true), color.End(true))
// You can mark start and end of the color while printing. World will be printed in Red.
fmt.Printf("Hello %sWorld%s\n", wcolor.Red.Start(true), wcolor.Red.End(true))
}package main
import (
"fmt"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
// Define a style and apply it to a string.
style := gostyle.Style{
Color: wcolor.Lightblue.Clone(),
Background: nil,
Bold: true,
Italic: false,
Underline: true,
Strikethrough: false,
Darken: false,
Lighten: false,
}
styledStr := style.ApplyTo("Hello", true).String()
fmt.Println(styledStr)
// You can concatinate un-styled string with styled string.
styledStr2 := style.ApplyTo("Hello", true).String()
styledStr2 += " World"
// Below print statement outputs 'Hello' using Lime color with Bold style. And the ' World' will be printed using the terminal's default color and style.
fmt.Println(styledStr2)
}package main
import (
"fmt"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
// Define a style.
style := gostyle.Style{
Color: wcolor.Red.Clone(),
Bold: true,
}
// Mark the start and end of the style while printing. 'Hello' will be printed in Red color and bold style.
fmt.Printf("%sHello%s World\n", style.Start(true), style.End(true))
}You can use a flag variable to conditionally apply the styles. If the value of the flag is false, then GOStyle will not style the output.
package main
import (
"fmt"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
color1 := wcolor.Cyan
color2 := wcolor.RGB(200, 25, 200)
canStyle := false
// Usage 1: Apply style to a string and then print it.
styledStr1 := gostyle.ApplyTo("Hello", canStyle).Color(color1).Bold().String()
fmt.Println(styledStr1)
// Usage 2: Mark start and end color while printing.
fmt.Printf("%sHello%s World\n", color2.Start(canStyle), color2.End(canStyle))
// Usage 3: Define a style and apply it to a string.
style1 := gostyle.Style{
Color: wcolor.Lime.Clone(),
Background: nil,
Bold: true,
Italic: false,
Underline: false,
Strikethrough: false,
Darken: false,
Lighten: false,
}
styledStr2 := style1.ApplyTo("Hello", canStyle).String()
fmt.Println(styledStr2)
// Usage 4: Mark start and end style while printing.
fmt.Printf("%sHello%s World\n", style1.Start(canStyle), style1.End(canStyle))
}You can use the gostyle.CanApplyStyle() function to check whether the terminal supports styling.
package main
import (
"fmt"
"os"
"github.com/arafath-mk/gostyle"
"github.com/arafath-mk/gostyle/wcolor"
)
func main() {
color1 := wcolor.Cyan
canStyle := gostyle.CanApplyStyle(os.Stdout)
// Apply style to a string and then print it.
styledStr1 := gostyle.ApplyTo("Hello", canStyle).Color(color1).Italic().String()
fmt.Println(styledStr1)
}