Skip to content

Commit 8966ea7

Browse files
authored
Merge pull request #10 from dnnrly/feature/colours
Add style formatting to output
2 parents 42fc5d0 + c8dbc79 commit 8966ea7

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ clean-deps:
4242
./bin/golangci-lint:
4343
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.17.1
4444

45-
test-deps: ./bin/bats ./bin/golangci-lint
45+
./bin/tparse: ./bin ./tmp
46+
curl -sfL -o ./tmp/tparse.tar.gz https://github.com/mfridman/tparse/releases/download/v0.7.4/tparse_0.7.4_Linux_x86_64.tar.gz
47+
tar -xf ./tmp/tparse.tar.gz -C ./bin
48+
49+
test-deps: ./bin/tparse ./bin/bats ./bin/golangci-lint
4650
$(GO_BIN) get -v ./...
4751
$(GO_BIN) mod tidy
4852

@@ -62,7 +66,7 @@ build-deps: ./bin/goreleaser
6266
deps: build-deps test-deps
6367

6468
test:
65-
$(GO_BIN) test ./...
69+
$(GO_BIN) test -json ./... | tparse -all
6670

6771
acceptance-test:
6872
bats --tap test/*.bats

cmd/root.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,26 @@ import (
66

77
"github.com/dnnrly/httpref"
88
"github.com/spf13/cobra"
9+
10+
"github.com/dnnrly/paragraphical"
911
)
1012

11-
var titles = false
13+
var (
14+
titles = false
15+
width = 100
16+
)
1217

1318
// rootCmd represents the base command when ctitlesed without any subcommands
1419
var rootCmd = &cobra.Command{
1520
Use: "httpref [filter]",
1621
Args: cobra.MaximumNArgs(1),
1722
Short: "Command line access to HTTP references",
18-
Long: `This displays useful information related to HTTP.
23+
Long: paragraphical.Format(width, `This displays useful information related to HTTP.
1924
20-
It will prefer exact matches where there are mutliple entries matching
21-
the filter (e.g. Accept and Accept-Language). If you want to match
22-
everything with the same prefix then you can use * as a wildcard suffix,
23-
for example:
25+
It will prefer exact matches where there are mutliple entries matching the filter (e.g. Accept and Accept-Language). If you want to match everything with the same prefix then you can use * as a wildcard suffix, for example:
2426
httpref 'Accept*'
2527
26-
Most of the content comes from the Mozilla developer
27-
documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP)
28-
and is copyright Mozilla and individual contributors. See
29-
https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses
30-
for details.`,
28+
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.`),
3129
RunE: root,
3230
}
3331

@@ -41,7 +39,9 @@ func Execute() {
4139
}
4240

4341
func init() {
44-
rootCmd.PersistentFlags().BoolVarP(&titles, "titles", "", titles, "List titles of the summaries available")
42+
rootCmd.PersistentFlags().BoolVarP(&titles, "titles", "t", titles, "List titles of the summaries available")
43+
rootCmd.PersistentFlags().IntVarP(&width, "width", "w", width, "Width to fit the output to")
44+
4545
rootCmd.AddCommand(subCmd("methods", "method", httpref.Methods))
4646
rootCmd.AddCommand(subCmd("statuses", "status", httpref.Statuses))
4747
rootCmd.AddCommand(subCmd("headers", "header", httpref.Headers))
@@ -82,10 +82,10 @@ func printResults(results httpref.References) {
8282
fmt.Fprintf(os.Stderr, "Filter not found any results\n")
8383
os.Exit(1)
8484
case 1:
85-
fmt.Printf("%s\n", results[0].Describe())
85+
fmt.Printf("%s\n", results[0].Describe(width))
8686
default:
8787
for _, r := range results {
88-
fmt.Printf("%s\n", r.Summarize())
88+
fmt.Printf("%s\n", r.Summarize(width))
8989
}
9090
}
9191
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.13
44

55
require (
66
github.com/dnnrly/paragraphical v0.1.1
7+
github.com/muesli/termenv v0.2.0
78
github.com/spf13/cobra v0.0.5
89
github.com/stretchr/testify v1.2.2
910
)

go.sum

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
99
github.com/dnnrly/paragraphical v0.1.1 h1:2aU1GudYxREsGr38muAO7cFdTcDnJ1o5XePQhaurKvk=
1010
github.com/dnnrly/paragraphical v0.1.1/go.mod h1:8bqT0aKyYRBRNWolMb6pYRzi0oe48Y28zCru9ZE5Jdk=
1111
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
12+
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
13+
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
1214
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1315
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1416
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
17+
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
18+
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
1519
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
1620
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1721
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
22+
github.com/muesli/termenv v0.2.0 h1:ndm3eEL62aIpsrPlwLLUIFRvM5E/4mmAhtbAI1TaHGY=
23+
github.com/muesli/termenv v0.2.0/go.mod h1:w4z90GOtP0+Cqm08fVBQ1RlXtVQ9MyHrV/ZtQ3869SM=
1824
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
1925
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2026
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -29,10 +35,10 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
2935
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
3036
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3137
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
32-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
3338
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
3439
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
3540
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
41+
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=
3642
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3743
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3844
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

httpref.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
"strings"
66

77
para "github.com/dnnrly/paragraphical"
8-
)
9-
10-
const (
11-
limit = 100
8+
"github.com/muesli/termenv"
129
)
1310

1411
type Reference struct {
@@ -20,22 +17,24 @@ type Reference struct {
2017

2118
type References []Reference
2219

23-
func (r Reference) Summarize() string {
20+
func (r Reference) Summarize(width int) string {
21+
name := termenv.String(r.Name)
22+
summary := termenv.String(r.Summary).Italic()
2423
return para.Format(
25-
limit,
26-
fmt.Sprintf("%s\n %s", r.Name, r.Summary),
24+
width,
25+
fmt.Sprintf("%s\n %s", name.Bold().Underline(), summary),
2726
)
2827
}
2928

30-
func (r Reference) Describe() string {
29+
func (r Reference) Describe(width int) string {
3130
text := fmt.Sprintf(
3231
"%s\n %s\n\n%s",
33-
r.Name,
34-
r.Summary,
32+
termenv.String(r.Name).Bold().Underline(),
33+
termenv.String(r.Summary).Italic(),
3534
r.Description,
3635
)
3736

38-
return para.Format(limit, text)
37+
return para.Format(width, text)
3938
}
4039

4140
func (r References) ByName(code string) References {

httpref_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
3838
Description: "description",
3939
}
4040

41-
s := r.Summarize()
41+
s := r.Summarize(100)
4242
assert.Contains(t, s, "name")
4343
assert.Contains(t, s, "summary")
4444
}
@@ -51,15 +51,15 @@ func TestReference_SummarizeLimitsLineLength(t *testing.T) {
5151
Description: "title description",
5252
}
5353

54-
s := r.Summarize()
54+
s := r.Summarize(100)
5555
for i, line := range strings.Split(s, "\n") {
5656
assert.True(t, len(line) < 100, "line %d is length %d - '%s'", i, len(line), line)
5757
}
5858
}
5959

6060
func TestReference_DescribeLimitsLength(t *testing.T) {
6161
r := Headers.ByName("Headers")[0]
62-
description := r.Describe()
62+
description := r.Describe(100)
6363

6464
assert.Contains(t, description, "HTTP")
6565
assert.Contains(t, description, "apply")

test/basic.bats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ BIN=./httpref
5959
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
6060
}
6161

62+
@test "Can change the width of the output" {
63+
run ${BIN} -w 70 100
64+
[ $status -eq 0 ]
65+
for l in "${lines[@]}"
66+
do
67+
[ $(echo ${l} | wc -m) -le 70 ]
68+
done
69+
}

0 commit comments

Comments
 (0)