Skip to content

Commit 15b6979

Browse files
committed
Add print commands
Bold, Success, Error
1 parent 56029f9 commit 15b6979

File tree

655 files changed

+230146
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

655 files changed

+230146
-14
lines changed

Gopkg.lock

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type Context struct {
1717
stdout io.Writer
1818
stderr io.Writer
1919
stdin io.Reader
20+
21+
successChar string
22+
errorChar string
2023
}
2124

2225
// NewContext returns a pointer to a new Context.
@@ -29,6 +32,9 @@ func NewContext() (context *Context) {
2932
context.stderr = os.Stderr
3033
context.stdin = os.Stdin
3134

35+
context.successChar = "✓"
36+
context.errorChar = "✗"
37+
3238
cwd, err := os.Getwd()
3339
if err == nil {
3440
context.SetWorkingDir(cwd)

examples/printing.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
script "github.com/jojomi/go-script"
7+
)
8+
9+
func main() {
10+
// make sure panics are printed in a human friendly way
11+
/// defer script.RecoverFunc()
12+
13+
sc := script.NewContext()
14+
15+
fmt.Println()
16+
sc.PrintlnBold("trying", "A")
17+
18+
fmt.Print("Yes,", "indeed ")
19+
sc.PrintSuccessCheck("\n")
20+
sc.PrintSuccessCheck(" oh yes!\n")
21+
sc.PrintlnSuccess("It worked")
22+
23+
fmt.Println()
24+
sc.PrintlnBold("B too?")
25+
26+
fmt.Print("No,", "no ")
27+
sc.PrintErrorCross("\n")
28+
sc.PrintErrorCross(" oh no!\n")
29+
sc.PrintlnError("It did not work")
30+
31+
fmt.Println()
32+
fmt.Println("I'm done.", "Really.")
33+
}

helpers_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package script
2+
3+
import "bytes"
4+
5+
func setOutputBuffers(sc *Context) (out, err *bytes.Buffer) {
6+
stdoutBuffer := bytes.NewBuffer(make([]byte, 0, 100))
7+
sc.stdout = stdoutBuffer
8+
stderrBuffer := bytes.NewBuffer(make([]byte, 0, 100))
9+
sc.stderr = stderrBuffer
10+
return stdoutBuffer, stderrBuffer
11+
}

print.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package script
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
8+
"github.com/fatih/color"
9+
"golang.org/x/crypto/ssh/terminal"
10+
)
11+
12+
var colorBold = color.New(color.Bold)
13+
var printBold = colorBold.FprintFunc()
14+
var printlnBold = colorBold.FprintlnFunc()
15+
16+
var colorSuccess = color.New(color.Bold, color.FgGreen)
17+
var printSuccess = colorSuccess.FprintFunc()
18+
var printlnSuccess = colorSuccess.FprintlnFunc()
19+
20+
var colorError = color.New(color.Bold, color.FgRed)
21+
var printError = colorError.FprintFunc()
22+
var printlnError = colorError.FprintlnFunc()
23+
24+
// PrintBold func
25+
func (c Context) PrintBold(input ...interface{}) {
26+
c.terminalize(c.stdout, printBold, fmt.Fprint, input...)
27+
}
28+
29+
// PrintlnBold func
30+
func (c Context) PrintlnBold(input ...interface{}) {
31+
c.terminalize(c.stdout, printlnBold, fmt.Fprintln, input...)
32+
}
33+
34+
// PrintBoldCheck func
35+
func (c Context) PrintBoldCheck(inputSuffix ...interface{}) {
36+
input := make([]interface{}, len(inputSuffix)+1)
37+
input[0] = c.successChar
38+
for index, i := range inputSuffix {
39+
input[index+1] = i
40+
}
41+
c.terminalize(c.stdout, printBold, fmt.Fprint, input...)
42+
}
43+
44+
// PrintSuccess func
45+
func (c Context) PrintSuccess(input ...interface{}) {
46+
c.terminalize(c.stdout, printSuccess, fmt.Fprint, input...)
47+
}
48+
49+
// PrintlnSuccess func
50+
func (c Context) PrintlnSuccess(input ...interface{}) {
51+
c.terminalize(c.stdout, printlnSuccess, fmt.Fprintln, input...)
52+
}
53+
54+
// PrintSuccessCheck func
55+
func (c Context) PrintSuccessCheck(inputSuffix ...interface{}) {
56+
input := make([]interface{}, len(inputSuffix)+1)
57+
input[0] = c.successChar
58+
for index, i := range inputSuffix {
59+
input[index+1] = i
60+
}
61+
c.terminalize(c.stdout, printSuccess, fmt.Fprint, input...)
62+
}
63+
64+
// PrintError func
65+
func (c Context) PrintError(input ...interface{}) {
66+
c.terminalize(c.stdout, printError, fmt.Fprint, input...)
67+
}
68+
69+
// PrintlnError func
70+
func (c Context) PrintlnError(input ...interface{}) {
71+
c.terminalize(c.stdout, printlnError, fmt.Fprintln, input...)
72+
}
73+
74+
// PrintErrorCross func
75+
func (c Context) PrintErrorCross(inputSuffix ...interface{}) {
76+
input := make([]interface{}, len(inputSuffix)+1)
77+
input[0] = c.errorChar
78+
for index, i := range inputSuffix {
79+
input[index+1] = i
80+
}
81+
c.terminalize(c.stdout, printError, fmt.Fprint, input...)
82+
}
83+
84+
// IsTerminal returns if this program is run inside an interactive terminal
85+
func (c Context) IsTerminal() bool {
86+
return terminal.IsTerminal(int(os.Stdout.Fd()))
87+
}
88+
89+
func (c Context) terminalize(w io.Writer, candy func(w io.Writer, input ...interface{}), basic func(w io.Writer, input ...interface{}) (int, error), input ...interface{}) {
90+
c.output(w, c.IsTerminal(), candy, basic, input...)
91+
}
92+
93+
func (c Context) output(w io.Writer, isTerminal bool, candy func(w io.Writer, input ...interface{}), basic func(w io.Writer, input ...interface{}) (int, error), input ...interface{}) {
94+
if !isTerminal {
95+
basic(w, input...)
96+
return
97+
}
98+
candy(w, input...)
99+
}

print_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package script
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPrintBold(t *testing.T) {
10+
sc := NewContext()
11+
stdout, stderr := setOutputBuffers(sc)
12+
13+
sc.PrintlnBold("in", "put")
14+
assert.Equal(t, "in put\n", stdout.String())
15+
assert.Equal(t, "", stderr.String())
16+
}

process_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package script
22

33
import (
4-
"bytes"
54
"strings"
65
"testing"
76

@@ -222,11 +221,3 @@ func processContext() *Context {
222221
sc.SetWorkingDir("./test/bin")
223222
return sc
224223
}
225-
226-
func setOutputBuffers(sc *Context) (out, err *bytes.Buffer) {
227-
stdoutBuffer := bytes.NewBuffer(make([]byte, 0, 100))
228-
sc.stdout = stdoutBuffer
229-
stderrBuffer := bytes.NewBuffer(make([]byte, 0, 100))
230-
sc.stderr = stderrBuffer
231-
return stdoutBuffer, stderrBuffer
232-
}

vendor/github.com/fatih/color/.travis.yml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fatih/color/LICENSE.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)