gcstyle

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 5 Imported by: 0

README

GOStyle

Add Rich, Flexible Styling to Your Go Console Apps with GOStyle.

Supported Styles

  1. Color
  2. Background Color
  3. Bold
  4. Italic
  5. Underline
  6. Strike Through
  7. Darken (Darker than normal)
  8. Lighten (Lighter than normal)

Why GOStyle?

  • 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())

Colors

GOStyle provides the following three methods to create colors.

  1. GOStyle supports CSS named colors like Blue, Silver, Red, etc. You can view the full list of CSS named colors here.

    clr := wcolor.Cyan
    
  2. You can construct colors using Red, Green, and Blue components.

    clr := wcolor.RGB(200, 25, 200)
    
  3. 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.
    

Quick Start

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))
}
Usage 1: Apply styles to string
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)
}
Usage 2: Mark the start and end of the color while printing
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))
}

Usage 3: Define style and apply it to string
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)
}
Usage 4: Mark the start and end of the style while printing
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))
}

Conditionally Apply Styles

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))
}

Check whether the terminal supports styling

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)
}

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBackgroundEnd

func AppendBackgroundEnd(dst []byte) []byte

func AppendBackgroundStart

func AppendBackgroundStart(dst []byte, c wcolor.Color) []byte

func AppendBoldEnd

func AppendBoldEnd(dst []byte) []byte

func AppendBoldStart

func AppendBoldStart(dst []byte) []byte

func AppendColorEnd

func AppendColorEnd(dst []byte) []byte

func AppendColorStart

func AppendColorStart(dst []byte, c wcolor.Color) []byte

func AppendDarkenEnd

func AppendDarkenEnd(dst []byte) []byte

func AppendDarkenStart

func AppendDarkenStart(dst []byte) []byte

func AppendItalicEnd

func AppendItalicEnd(dst []byte) []byte

func AppendItalicStart

func AppendItalicStart(dst []byte) []byte

func AppendLightenEnd

func AppendLightenEnd(dst []byte) []byte

func AppendLightenStart

func AppendLightenStart(dst []byte) []byte

func AppendStrikethroughEnd

func AppendStrikethroughEnd(dst []byte) []byte

func AppendStrikethroughStart

func AppendStrikethroughStart(dst []byte) []byte

func AppendStyleEnd

func AppendStyleEnd(dst []byte, s Style) []byte

func AppendStyleStart

func AppendStyleStart(dst []byte, s Style) []byte

func AppendUnderlineEnd

func AppendUnderlineEnd(dst []byte) []byte

func AppendUnderlineStart

func AppendUnderlineStart(dst []byte) []byte

func CanApplyStyle

func CanApplyStyle(writer io.Writer) bool

Types

type Style

type Style struct {
	Color         *wcolor.Color
	Background    *wcolor.Color
	Bold          bool
	Italic        bool
	Underline     bool
	Strikethrough bool
	Darken        bool
	Lighten       bool
}

func (Style) ApplyTo

func (s Style) ApplyTo(str string, applyStyle bool) StyledString

func (Style) End

func (s Style) End(applyStyle bool) string

func (Style) Start

func (s Style) Start(applyStyle bool) string

type StyledString

type StyledString struct {
	// contains filtered or unexported fields
}

func ApplyTo

func ApplyTo(str string, applyStyle bool) StyledString

func (StyledString) Background

func (s StyledString) Background(c wcolor.Color) StyledString

Applies background color.

func (StyledString) Bold

func (s StyledString) Bold() StyledString

Applies bold style.

func (StyledString) Color

Applies foreground color.

func (StyledString) Darken

func (s StyledString) Darken() StyledString

func (StyledString) Italic

func (s StyledString) Italic() StyledString

Applies italic style.

func (StyledString) Lighten

func (s StyledString) Lighten() StyledString

func (StyledString) Strikethrough

func (s StyledString) Strikethrough() StyledString

Applies strikethrough style.

func (StyledString) String

func (s StyledString) String() string

Returns string.

func (StyledString) Underline

func (s StyledString) Underline() StyledString

Applies underline style.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL