Skip to content

Commit

Permalink
fix: refactored ui package to allow (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
exu authored Jan 27, 2022
1 parent 0e6c066 commit a422c7e
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 86 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/ui/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package ui

import (
"io"
"os"
)

// Verbose adds additional info messages e.g. in case of checking errors
var Verbose = false

var Writer io.Writer = os.Stdout

// IconMedal emoji
const IconMedal = "🥇"

// IconError emoji
const IconError = "💔"
22 changes: 11 additions & 11 deletions pkg/ui/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"os"
)

func ExitOnError(item string, errors ...error) {
printAndExit(item, true, errors...)
func (ui *UI) ExitOnError(item string, errors ...error) {
ui.printAndExit(item, true, errors...)
}

func PrintOnError(item string, errors ...error) {
printAndExit(item, false, errors...)
func (ui *UI) PrintOnError(item string, errors ...error) {
ui.printAndExit(item, false, errors...)
}

func printAndExit(item string, exitOnError bool, errors ...error) {
if len(errors) > 0 && hasErrors(errors...) {
func (ui *UI) printAndExit(item string, exitOnError bool, errors ...error) {
if len(errors) > 0 && ui.hasErrors(errors...) {
for _, err := range errors {
if err != nil {
fmt.Fprintf(Writer, "%s %s (error: %s)\n\n", LightRed("⨯"), Red(item), err)
Expand All @@ -25,13 +25,13 @@ func printAndExit(item string, exitOnError bool, errors ...error) {
}
}

if Verbose {
if ui.Verbose {
fmt.Fprintf(Writer, "%s %s\n", Blue("\xE2\x9C\x94"), Green(item))
}
}

func WarnOnError(item string, errors ...error) {
if len(errors) > 0 && hasErrors(errors...) {
func (ui *UI) WarnOnError(item string, errors ...error) {
if len(errors) > 0 && ui.hasErrors(errors...) {
for _, err := range errors {
if err != nil {
fmt.Fprintf(Writer, "%s %s (error: %s)\n\n", LightYellow("⨯"), Yellow(item), err)
Expand All @@ -40,12 +40,12 @@ func WarnOnError(item string, errors ...error) {
}
}

if Verbose {
if ui.Verbose {
fmt.Fprintf(Writer, "%s %s\n", Blue("\xE2\x9C\x94"), Green(item))
}
}

func hasErrors(errors ...error) bool {
func (ui *UI) hasErrors(errors ...error) bool {
if len(errors) > 0 {
for _, err := range errors {
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/ui/logo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ var logo = `
`

func Logo() {
fmt.Fprint(Writer, Blue(logo))
fmt.Fprintln(Writer)
func (ui *UI) Logo() {
fmt.Fprint(ui.Writer, Blue(logo))
fmt.Fprintln(ui.Writer)
}

func LogoNoColor() {
fmt.Fprint(Writer, logo)
fmt.Fprintln(Writer)
func (ui *UI) LogoNoColor() {
fmt.Fprint(ui.Writer, logo)
fmt.Fprintln(ui.Writer)
}
122 changes: 56 additions & 66 deletions pkg/ui/printers.go
Original file line number Diff line number Diff line change
@@ -1,139 +1,129 @@
// simple ui - TODO use something more sophisticated :)
package ui

import (
"fmt"
"io"
"os"
"strings"
)

var Writer io.Writer = os.Stdout

// IconMedal emoji
const IconMedal = "🥇"

// IconError emoji
const IconError = "💔"

func NL() {
fmt.Fprintln(Writer)
func (ui *UI) NL() {
fmt.Fprintln(ui.Writer)
}

// Warn shows warning in terminal
func Success(message string, subMessages ...string) {
fmt.Fprintf(Writer, "%s", LightYellow(message))
func (ui *UI) Success(message string, subMessages ...string) {
fmt.Fprintf(ui.Writer, "%s", LightYellow(message))
for _, sub := range subMessages {
fmt.Fprintf(Writer, " %s", LightCyan(sub))
fmt.Fprintf(ui.Writer, " %s", LightCyan(sub))
}
fmt.Fprintf(Writer, " "+IconMedal)
fmt.Fprintln(Writer)
fmt.Fprintf(ui.Writer, " "+IconMedal)
fmt.Fprintln(ui.Writer)
}

// Warn shows warning in terminal
func Warn(message string, subMessages ...string) {
fmt.Fprintf(Writer, "%s", LightYellow(message))
func (ui *UI) Warn(message string, subMessages ...string) {
fmt.Fprintf(ui.Writer, "%s", LightYellow(message))
for _, sub := range subMessages {
fmt.Fprintf(Writer, " %s", LightCyan(sub))
fmt.Fprintf(ui.Writer, " %s", LightCyan(sub))
}
fmt.Fprintln(Writer)
fmt.Fprintln(ui.Writer)
}

func LogLine(message string) {
fmt.Fprintf(Writer, "%s\n", DarkGray(message))
func (ui *UI) LogLine(message string) {
fmt.Fprintf(ui.Writer, "%s\n", DarkGray(message))
}

func Debug(message string, subMessages ...string) {
if !Verbose {
func (ui *UI) Debug(message string, subMessages ...string) {
if !ui.Verbose {
return
}
fmt.Fprintf(Writer, "%s", DarkGray(message))
fmt.Fprintf(ui.Writer, "%s", DarkGray(message))
for _, sub := range subMessages {
fmt.Fprintf(Writer, " %s", LightGray(sub))
fmt.Fprintf(ui.Writer, " %s", LightGray(sub))
}
fmt.Fprintln(Writer)
fmt.Fprintln(ui.Writer)
}

func Info(message string, subMessages ...string) {
fmt.Fprintf(Writer, "%s", DarkGray(message))
func (ui *UI) Info(message string, subMessages ...string) {
fmt.Fprintf(ui.Writer, "%s", DarkGray(message))
for _, sub := range subMessages {
fmt.Fprintf(Writer, " %s", LightGray(sub))
fmt.Fprintf(ui.Writer, " %s", LightGray(sub))
}
fmt.Fprintln(Writer)
fmt.Fprintln(ui.Writer)
}

func Err(err error) {
fmt.Fprintf(Writer, "%s %s %s\n", LightRed("⨯"), Red(err.Error()), IconError)
func (ui *UI) Err(err error) {
fmt.Fprintf(ui.Writer, "%s %s %s\n", LightRed("⨯"), Red(err.Error()), IconError)
}

func Errf(err string, params ...interface{}) {
fmt.Fprintf(Writer, "%s %s\n", LightRed("⨯"), Red(fmt.Sprintf(err, params...)))
func (ui *UI) Errf(err string, params ...interface{}) {
fmt.Fprintf(ui.Writer, "%s %s\n", LightRed("⨯"), Red(fmt.Sprintf(err, params...)))
}

func Fail(err error) {
Err(err)
fmt.Fprintln(Writer)
func (ui *UI) Fail(err error) {
ui.Err(err)
fmt.Fprintln(ui.Writer)
os.Exit(1)
}

func Failf(err string, params ...interface{}) {
Errf(err, params...)
fmt.Fprintln(Writer)
func (ui *UI) Failf(err string, params ...interface{}) {
ui.Errf(err, params...)
fmt.Fprintln(ui.Writer)
os.Exit(1)
}

func CommandOutput(output []byte, command string, params ...string) {
func (ui *UI) CommandOutput(output []byte, command string, params ...string) {
fullCommand := fmt.Sprintf("%s %s", LightCyan(command), DarkGray(strings.Join(params, " ")))
fmt.Fprintf(Writer, "command: %s\noutput:\n%s\n", LightGray(fullCommand), DarkGray(string(output)))
fmt.Fprintf(ui.Writer, "command: %s\noutput:\n%s\n", LightGray(fullCommand), DarkGray(string(output)))
}

func Medal() {
Completed("Congratulations! - Here's your medal: " + IconMedal)
func (ui *UI) Medal() {
ui.Completed("Congratulations! - Here's your medal: " + IconMedal)
}

func Completed(main string, sub ...string) {
fmt.Fprintln(Writer)
func (ui *UI) Completed(main string, sub ...string) {
fmt.Fprintln(ui.Writer)
if len(sub) == 1 {
fmt.Fprintf(Writer, "%s: %s\n", LightGray(main), LightBlue(sub[0]))
fmt.Fprintf(ui.Writer, "%s: %s\n", LightGray(main), LightBlue(sub[0]))
} else {
fmt.Fprintln(Writer, LightGray(main), LightBlue(strings.Join(sub, " ")))
fmt.Fprintln(ui.Writer, LightGray(main), LightBlue(strings.Join(sub, " ")))
}
}

func GroupCompleted(main string, sub ...string) {
fmt.Fprintln(Writer)
line := strings.Repeat("=", calculateMessageLength(main, sub...))
fmt.Fprintln(Writer, LightBlue(line))
func (ui *UI) GroupCompleted(main string, sub ...string) {
fmt.Fprintln(ui.Writer)
line := strings.Repeat("=", ui.calculateMessageLength(main, sub...))
fmt.Fprintln(ui.Writer, LightBlue(line))
if len(sub) == 1 {
fmt.Fprintf(Writer, "%s: %s", LightGray(main), LightBlue(sub[0]))
fmt.Fprintf(ui.Writer, "%s: %s", LightGray(main), LightBlue(sub[0]))
} else {
fmt.Fprintln(Writer, LightGray(main))
fmt.Fprintln(ui.Writer, LightGray(main))
}
}

func InfoGrid(table map[string]string) {
func (ui *UI) InfoGrid(table map[string]string) {
for k, v := range table {
fmt.Fprintf(Writer, " %s: %s\n", DarkGray(k), LightBlue(v))
fmt.Fprintf(ui.Writer, " %s: %s\n", DarkGray(k), LightBlue(v))
}
fmt.Fprintln(Writer)
fmt.Fprintln(ui.Writer)
}

func Vector(table []string) {
func (ui *UI) Vector(table []string) {
for _, v := range table {
fmt.Fprintf(Writer, " %s\n", DarkGray(v))
fmt.Fprintf(ui.Writer, " %s\n", DarkGray(v))
}
}

// Warn shows warning in terminal
func ShellCommand(title string, commands ...string) {
fmt.Fprintf(Writer, "%s:\n", White(title))
func (ui *UI) ShellCommand(title string, commands ...string) {
fmt.Fprintf(ui.Writer, "%s:\n", White(title))
for _, sub := range commands {
fmt.Fprintf(Writer, "$ %s\n", LightGray(sub))
fmt.Fprintf(ui.Writer, "$ %s\n", LightGray(sub))
}
fmt.Fprintln(Writer)
fmt.Fprintln(ui.Writer)
}

func calculateMessageLength(message string, subMessages ...string) int {
func (ui *UI) calculateMessageLength(message string, subMessages ...string) int {
sum := 0
for _, sub := range subMessages {
sum += len(sub) + 1 // space
Expand Down
6 changes: 3 additions & 3 deletions pkg/ui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type TableData interface {
Table() (header []string, data [][]string)
}

func Table(tableData TableData, writer io.Writer) {
func (ui *UI) Table(tableData TableData, writer io.Writer) {
table := tablewriter.NewWriter(writer)
table.SetBorder(false)
header, data := tableData.Table()
Expand All @@ -26,12 +26,12 @@ func Table(tableData TableData, writer io.Writer) {
table.Render()
}

func JSONTable(tableData TableData, writer io.Writer) error {
func (ui *UI) JSONTable(tableData TableData, writer io.Writer) error {
_, data := tableData.Table()
return json.NewEncoder(writer).Encode(data)
}

func NewArrayTable(a [][]string) ArrayTable {
func (ui *UI) NewArrayTable(a [][]string) ArrayTable {
return ArrayTable(a)
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// simple ui - TODO use something more sophisticated :)
package ui

import (
"io"
"os"
)

var ui = NewStdoutUI(Verbose)

func NewUI(verbose bool, writer io.Writer) *UI {
return &UI{
Verbose: verbose,
Writer: writer,
}
}

func NewStdoutUI(verbose bool) *UI {
return &UI{
Verbose: verbose,
Writer: os.Stdout,
}
}

type UI struct {
Verbose bool
Writer io.Writer
}

func ExitOnError(item string, errors ...error) { ui.ExitOnError(item, errors...) }
func PrintOnError(item string, errors ...error) { ui.PrintOnError(item, errors...) }
func WarnOnError(item string, errors ...error) { ui.WarnOnError(item, errors...) }
func Logo() { ui.Logo() }
func LogoNoColor() { ui.LogoNoColor() }
func NL() { ui.NL() }
func Success(message string, subMessages ...string) { ui.Success(message, subMessages...) }
func Warn(message string, subMessages ...string) { ui.Warn(message, subMessages...) }
func LogLine(message string) { ui.LogLine(message) }
func Debug(message string, subMessages ...string) { ui.Debug(message, subMessages...) }
func Info(message string, subMessages ...string) { ui.Info(message, subMessages...) }
func Err(err error) { ui.Err(err) }
func Errf(err string, params ...interface{}) { ui.Errf(err, params...) }
func Fail(err error) { ui.Fail(err) }
func Failf(err string, params ...interface{}) { ui.Failf(err, params...) }
func CommandOutput(output []byte, command string, params ...string) {
ui.CommandOutput(output, command, params...)
}
func Medal() { ui.Medal() }
func Completed(message string, subMessages ...string) { ui.Completed(message, subMessages...) }
func GroupCompleted(main string, sub ...string) { ui.GroupCompleted(main, sub...) }
func InfoGrid(table map[string]string) { ui.InfoGrid(table) }
func Vector(table []string) { ui.Vector(table) }
func ShellCommand(title string, commands ...string) { ui.ShellCommand(title, commands...) }
func Table(tableData TableData, writer io.Writer) { ui.Table(tableData, writer) }
func JSONTable(tableData TableData, writer io.Writer) error { return ui.JSONTable(tableData, writer) }
func NewArrayTable(a [][]string) ArrayTable { return ui.NewArrayTable(a) }

0 comments on commit a422c7e

Please sign in to comment.