Skip to content

Commit

Permalink
Merge pull request #1 from asciifaceman/feature/realterm
Browse files Browse the repository at this point in the history
massive overhaul of the entire color system
  • Loading branch information
asciifaceman authored Aug 18, 2023
2 parents af7e02d + 3c38847 commit ea81106
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 94 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
# Hobocode

Simply a very small wrapper for [go-pretty](https://github.com/jedib0t/go-pretty) for different opinionated colored print calls like Info() and Error()
Hobocode is a pty-aware colorized printing library intended for use in CLI
applications for human consumption and UX rather than structured outputs.

Uses [go-pretty v6](https://github.com/jedib0t/go-pretty) for colors and Sprinting.

```go
hobocode.Header("Hobocode Example")
hobocode.Success("Welcome to hobocode")
```

![hobocode_example](static/hobocode1.png)


The [underlying functions](coloring.go) can be used if you are working with custom *os.File's or you can use the lazy opinionated helpers like

### Plain
`hobocode.Warn("Something might be going wrong")`

### Formatted
`hobocode.Debugf("Output: %v", ret.Body())`

### Indented
`hobocode.Ierror(2, "Something went terribly wrong")`

### Indented and Formatted
`hobocode.Iinfof(1, "Date: %s", time.Now().Format("2006/01/02"))`


This is meant to be a very simple utility for CLI applications relying on UX rather than structured log outputs.

# Example

See [examples](examples/)

```
import (
"github.com/asciifaceman/hobocode"
Expand Down
34 changes: 34 additions & 0 deletions coloring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hobocode

import (
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/text"
)

// Icolor prints the given string at the given indent
// to the given os.File with the given text.Color, if colorable
func Icolor(indent int, fd *os.File, color text.Color, message string) {
id := Tindent(indent)
if Colorable(fd) {
fmt.Fprint(fd, id)
Stdlin(fd, color.Sprint(message))
} else {
fmt.Fprint(fd, id)
Stdlin(fd, message)
}
}

// Icolorf prints the given format at the given indent
// to the given os.File with the given text.Color, if colorable
func Icolorf(indent int, fd *os.File, color text.Color, format string, a ...interface{}) {
id := Tindent(indent)
if Colorable(fd) {
fmt.Fprint(fd, id)
Stdlin(fd, color.Sprintf(format, a...))
} else {
fmt.Fprint(fd, id)
Stdlin(fd, fmt.Sprintf(format, a...))
}
}
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hobocode Examples

```
go run prints.go
```

39 changes: 39 additions & 0 deletions examples/prints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"time"

"github.com/asciifaceman/hobocode"
)

func main() {
hobocode.Header("Info")
hobocode.Info("This is an info message")
hobocode.Infof("This is a formatted info message: %v", time.Now().Format("2006/01/02"))
hobocode.Iinfo(1, "This is an info message indented once")
hobocode.Iinfo(2, "This is an info message indented twice")
hobocode.Iinfof(1, "This is a formatted info message indented once: %v", time.Now().Format("2006/01/02"))
hobocode.Iinfof(2, "This is a formatted info message indented twice: %v", time.Now().Format("2006/01/02"))

hobocode.Header("Warn")
hobocode.Warn("This is a warn message")
hobocode.Warnf("This is a formatted warn message: %v", time.Now().Format("2006/01/02"))

hobocode.Header("Error")
hobocode.Error("This is an error message")
hobocode.Errorf("This is a formatted error message: %v", fmt.Errorf("error occured at %v", time.Now().Format("2006/01/02")))

hobocode.Header("Success")
hobocode.Success("This is a success message!")
hobocode.Successf("This is a formatted successs message! %v", time.Now().Format("2006/01/02"))

hobocode.Header("Debug")
hobocode.Debug("This is a debug message!")
hobocode.Debugf("This is a formatted debug message! %v", time.Now().Format("2006/01/02"))

hobocode.HeaderLeft("A left justified header!?")
hobocode.HeaderLeft("Whoa!")
hobocode.HeaderLeft("That's cool")

}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ module github.com/asciifaceman/hobocode

go 1.20

require github.com/jedib0t/go-pretty/v6 v6.4.6
require (
github.com/jedib0t/go-pretty/v6 v6.4.6
golang.org/x/term v0.11.0
)

require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.11.0 // indirect
)
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
160 changes: 160 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package hobocode

import (
"os"

"github.com/jedib0t/go-pretty/v6/text"
)

var (
out = os.Stdout
err = os.Stderr
)

/*
Stdout helpers
*/

// Info prints the given message with bright cyan if Colorable
//
// Stdout
func Info(message string) {
Icolor(0, out, text.FgHiCyan, message)
}

// Infof prints the given formatted message with bright cyan if colorable
//
// Stdout
func Infof(format string, a ...interface{}) {
Icolorf(0, out, text.FgHiCyan, format, a...)
}

// Iinfo prints the given message with bright cyan if Colorable and an indent
//
// Stdout
func Iinfo(indent int, message string) {
Icolor(indent, out, text.FgHiCyan, message)
}

// Iinfof prints the given formatted message with bright cyan if Colorable and an indent
//
// Stdout
func Iinfof(indent int, format string, a ...interface{}) {
Icolorf(indent, out, text.FgHiCyan, format, a...)
}

// Success prints the given message with bright green if Colorable
//
// Stdout
func Success(message string) {
Icolor(0, out, text.FgHiGreen, message)
}

// Successf prints the given formatted message with bright green if Colorable
//
// Stdout
func Successf(format string, a ...interface{}) {
Icolorf(0, out, text.FgHiGreen, format, a...)
}

// Isuccess prints the given message with bright green if Colorable with an indent
//
// Stdout
func Isuccess(indent int, message string) {
Icolor(indent, out, text.FgHiGreen, message)
}

// Isuccessf prints the given formatted message with bright green if Colorable with an indent
//
// Stdout
func Isuccessf(indent int, format string, a ...interface{}) {
Icolorf(indent, out, text.FgHiGreen, format, a...)
}

// Debug prints the given message with bright green if Colorable
//
// Stdout
func Debug(message string) {
Icolor(0, out, text.FgHiBlue, message)
}

// Debugf prints the given formatted message with bright green if Colorable
//
// Stdout
func Debugf(format string, a ...interface{}) {
Icolorf(0, out, text.FgHiBlue, format, a...)
}

// Idebug prints the given message with bright green if Colorable with an indent
//
// Stdout
func Idebug(indent int, message string) {
Icolor(indent, out, text.FgHiBlue, message)
}

// Idebugf prints the given formatted message with bright green if Colorable with an indent
//
// Stdout
func Idebugf(indent int, format string, a ...interface{}) {
Icolorf(indent, out, text.FgHiBlue, format, a...)
}

/*
Stderr helpers
*/

// Warn prints the given message with bright yellow if Colorable
//
// Stderr
func Warn(message string) {
Icolor(0, err, text.FgHiYellow, message)
}

// Warnf prints the given formatted message with bright yellow if Colorable
//
// Stderr
func Warnf(format string, a ...interface{}) {
Icolorf(0, err, text.FgHiYellow, format, a...)
}

// Iwarn prints the given message with bright yellow if Colorable and an indent
//
// Stderr
func Iwarn(indent int, message string) {
Icolor(indent, err, text.FgHiYellow, message)
}

// Iwarnf prints the given formatted message with bright yellow if Colorable and an indent
//
// Stderr
func Iwarnf(indent int, format string, a ...interface{}) {
Icolorf(indent, err, text.FgHiYellow, format, a...)
}

// Error prints the given message with bright red if Colorable
//
// Stderr
func Error(message string) {
Icolor(0, err, text.FgHiRed, message)
}

// Errorf prints the given formatted message with bright red if Colorable
//
// Stderr
func Errorf(format string, a ...interface{}) {
Icolorf(0, err, text.FgHiRed, format, a...)
}

// Ierror prints the given message with bright red if Colorable and an indent
//
// Stderr
func Ierror(indent int, message string) {
Icolor(indent, err, text.FgHiRed, message)
}

// Ierrorf prints the given formatted message with bright red if Colorable and an indent
//
// Stderr
func Ierrorf(indent int, format string, a ...interface{}) {
Icolorf(indent, err, text.FgHiRed, format, a...)
}
Loading

0 comments on commit ea81106

Please sign in to comment.