Skip to content

How to Style the Console Output

John edited this page Nov 30, 2024 · 2 revisions

One of the most boring tasks when creating console commands is to deal with the styling of the command's output, this library provide several helper for that.

package main

import (
  "github.com/DrSmithFr/go-console"
  "github.com/DrSmithFr/go-console/input"
  "github.com/DrSmithFr/go-console/output"
)

func main() {
  // create default console styler
  cmd := go_console.NewScript()

  // or create styler with custom OutputInterface
  in := input.NewArgvInput(nil)
  out := output.NewCliOutput(true, nil)

  cmd2 := go_console.NewScriptCustom(in, out, true)

  // add title
  cmd.PrintTitle("Lorem Ipsum Dolor Sit Amet")

  // you still access the OutputInterface
  cmd2.Output.Println("<info>some info</>")
}

Note:

OutputInterface and go_console.Cli implements io.Writer interface, so fmt.Fprint() can be used

package main

import (
  "fmt"
  "github.com/DrSmithFr/go-console"
)

func main() {
  cmd := go_console.NewScript().Build()
  out := cmd.Output

  // Using OutputInterface or go_console.Cli helper to display styled text
  out.Println("<info>This message has displayed by <b>Output.Println()</b>")
  cmd.PrintText("<info>This message has displayed by <b>go_console.PrintText()</b>")
  
  // Or using fmt.Fprint() with OutputInterface or go_console.Cli as io.Writer
  fmt.Fprintln(out, "<info>This message</info> using <b>Fprintln with Output</b>")
  fmt.Fprintln(cmd, "<info>This message</info> using <b>Fprintln with go_console</b>")
}

How to Color the Console Output

Whenever you output text, you can use OutputInterface to surround the text with tags to color its output. For example:

Predefined style tags

package main

import "github.com/DrSmithFr/go-console/output"

func main() {
  // creating new output
  out := output.NewCliOutput(true, nil)

  // white text on a red background
  out.Println("<error>An error</error>")

  // green text
  out.Println("<info>An information</info>")

  // yellow text
  out.Println("<comment>An comment</comment>")

  // black text on a cyan background
  out.Println("<question>A question</question>")

  // underscore text
  out.Println("<u>Some underscore text</u>")

  // bold text
  out.Println("<b>Some bold text</b>")
}

The closing tag can be replaced by </>, which revokes all formatting options established by the last opened tag.


Generic style tags

You can also set these colors and options directly inside the tag name:

package main

import "github.com/DrSmithFr/go-console/output"

func main() {
  // green text
  out := output.NewCliOutput(true, nil)

  // black text on a cyan background
  out.Println("<fg=green>foo</>")

  // green text
  out.Println("<fg=black;bg=cyan>foo</>")

  // bold text on a yellow background
  out.Println("<bg=yellow;options=bold>foo</>")

  // bold text with underscore
  out.Println("<options=bold,underscore>foo</>")
}

If you need to render a tag literally, escape it with a backslash: <info> or use the escape() method to escape all the tags included in the given string.


Custom color tags

It is possible to define your own styles using the OutputFormatterStyle

package main

import (
  "github.com/DrSmithFr/go-console/color"
  "github.com/DrSmithFr/go-console/formatter"
  "github.com/DrSmithFr/go-console/output"
)

func main() {
  // creating new output
  out := output.NewCliOutput(true, nil)

  // create new style
  s := formatter.NewOutputFormatterStyle(color.Red, color.Yellow, []string{color.Bold, color.Blink})

  // add style to formatter
  out.Formatter().SetStyle("fire", *s)

  // use the new style
  out.Println("<fire>foo</fire>")
}

Available foreground and background colors are: black, red, green, yellow, blue, magenta, cyan and white. And available options are: bold, underscore, blink, reverse (enables the "reverse video" mode where the background and foreground colors are swapped) and conceal (sets the foreground color to transparent, making the typed text invisible - although it can be selected and copied; this option is commonly used when asking the user to type sensitive information).


By using colors in the command output, you can distinguish different types of output (e.g. important messages, titles, comments, etc.).

package main

import (
  "fmt"
  "github.com/DrSmithFr/go-console/color"
  "github.com/DrSmithFr/go-console/formatter"
)

func main() {
  // create a default style
  s1 := formatter.NewOutputFormatterStyle(color.Null, color.Null, nil)
  fmt.Printf(s1.Apply("some text without coloration\n"))

  s1.SetBackground(color.Red)
  fmt.Printf(s1.Apply("some text with red background\n"))

  s1.SetForeground(color.Green)
  fmt.Printf(s1.Apply("some text with red background and green text\n"))

  s1.SetOption(color.Bold)
  fmt.Printf(s1.Apply("some bold text with red background and green text \n"))

  // override all options in one time
  s1.SetOptions([]string{color.Underscore})
  fmt.Printf(s1.Apply("some underscore text with red background and green text \n"))

  // quick declaration
  s2 := formatter.NewOutputFormatterStyle(color.Blue, color.Yellow, nil)
  fmt.Printf(s2.Apply("some text with yellow background and blue text\n"))

  // quick declaration with options
  s3 := formatter.NewOutputFormatterStyle(color.Default, color.Default, []string{color.Underscore, color.Bold})
  fmt.Printf(s3.Apply("some bold and underscore text\n"))
}

The OutputFormatterStyle is the simplest way to color output. It is not mean to be use directly, but to defined custom tags used by OutputFormatterInterface.

Clone this wiki locally