Skip to content

Instantly share code, notes, and snippets.

@jomoespe
Created December 12, 2019 08:42
Show Gist options
  • Save jomoespe/69bf4b591470f992d0503f1282c59938 to your computer and use it in GitHub Desktop.
Save jomoespe/69bf4b591470f992d0503f1282c59938 to your computer and use it in GitHub Desktop.
Format and colorize Go console output
module github.com/jomoespe/align-text
go 1.13
require github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
package main
import (
"fmt"
"time"
. "github.com/logrusorgru/aurora"
)
type status string
func (s status) Color() func(interface{}) Value {
switch s {
case "SUCCESS":
return Red
case "FAILED":
return Green
case "ABORTED":
return Blue
default:
return White
}
}
const dateFormat = "02/01/2006 15:04"
func main() {
// https://www.dotnetperls.com/padding-go
pipelines := map[string]struct {
status status
startedAt time.Time
}{
"short": {"SUCCESS", time.Now()},
"loooooooooooooong": {"FAILED", time.Now()},
"anohter": {"ABORTED", time.Now()},
}
for name, state := range pipelines {
fmt.Printf("%-20s\t%-10s\t%10s\n", name, state.status.Color()(state.status), state.startedAt.Format(dateFormat))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment