Skip to content

Commit 4f86e55

Browse files
Add ascii title generation closes #4 (#5)
* [ImgBot] Optimize images /banner-nyan.png -- 114.47kb -> 64.04kb (44.06%) Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com> * add generated banner * example in readme * undo default banner.txt * font description * add title test Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com>
1 parent 30ba9ad commit 4f86e55

11 files changed

Lines changed: 164 additions & 55 deletions

File tree

README.md

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ I Like to add these startup banners on all my applications, I think it give pers
2626
# Usage
2727

2828
Import the package. Thats it.
29-
29+
3030
```go
3131
package main
3232

@@ -87,6 +87,7 @@ $ go run main.go -h
8787
```
8888

8989
should output
90+
9091
```
9192
Usage of main:
9293
-ansi
@@ -101,79 +102,99 @@ Usage of main:
101102

102103
You can use the following variables in the template.
103104

104-
| Variable | Value |
105-
|----------------------------------------|-----------------------------------------------|
106-
| ```{{ .GoVersion }}``` | ```runtime.Version()``` |
107-
| ```{{ .GOOS }}``` | ```runtime.GOOS``` |
108-
| ```{{ .GOARCH }}``` | ```runtime.GOARCH``` |
109-
| ```{{ .NumCPU }}``` | ```runtime.NumCPU()``` |
110-
| ```{{ .GOPATH }}``` | ```os.Getenv("GOPATH")``` |
111-
| ```{{ .GOROOT }}``` | ```runtime.GOROOT()``` |
112-
| ```{{ .Compiler }}``` | ```runtime.Compiler``` |
113-
| ```{{ .Env "GOPATH" }}``` | ```os.Getenv("GOPATH")``` |
114-
| ```{{ .Now "Monday, 2 Jan 2006" }}``` | ```time.Now().Format("Monday, 2 Jan 2006")``` |
105+
| Variable | Value |
106+
| -------------------------------------------- | ----------------------------------------- |
107+
| `{{ .Title "YourTitle" "fontname" indent }}` | `<Generated ASCII art>` |
108+
| `{{ .GoVersion }}` | `runtime.Version()` |
109+
| `{{ .GOOS }}` | `runtime.GOOS` |
110+
| `{{ .GOARCH }}` | `runtime.GOARCH` |
111+
| `{{ .NumCPU }}` | `runtime.NumCPU()` |
112+
| `{{ .GOPATH }}` | `os.Getenv("GOPATH")` |
113+
| `{{ .GOROOT }}` | `runtime.GOROOT()` |
114+
| `{{ .Compiler }}` | `runtime.Compiler` |
115+
| `{{ .Env "GOPATH" }}` | `os.Getenv("GOPATH")` |
116+
| `{{ .Now "Monday, 2 Jan 2006" }}` | `time.Now().Format("Monday, 2 Jan 2006")` |
115117

116118
Please see the layout of the function **.Now** in https://github.com/golang/go/blob/f06795d9b742cf3292a0f254646c23603fc6419b/src/time/format.go#L9-L41
117119

120+
## Title
121+
122+
Title generates ascii art for you using [go-figure](https://github.com/common-nighthawk/go-figure)
123+
Use it if you don't provide your own ascii title.
124+
125+
See [examples/title](./examples/title) for an example
126+
127+
**Note:** Must provide zero values if not using something i.e.
128+
129+
```go
130+
// .Title string string int
131+
// .Title title fontname indentation_spaces
132+
{{ .Title "Banner" "" 0 }}
133+
{{ .Title "Banner" "banner2" 0 }}
134+
{{ .Title "Banner" "" 4 }}
135+
```
136+
137+
The fonts available can be seen here [go-figure#supported-fonts](https://github.com/common-nighthawk/go-figure#supported-fonts)
138+
118139
## Colors
119140

120141
There are support for ANSI colors :)
121142

122-
| Variable |
123-
|-------------------------------------------|
124-
| ```{{ .AnsiColor.Default }}``` |
125-
| ```{{ .AnsiColor.Black }}``` |
126-
| ```{{ .AnsiColor.Red }}``` |
127-
| ```{{ .AnsiColor.Green }}``` |
128-
| ```{{ .AnsiColor.Yellow }}``` |
129-
| ```{{ .AnsiColor.Blue }}``` |
130-
| ```{{ .AnsiColor.Magenta }}``` |
131-
| ```{{ .AnsiColor.Cyan }}``` |
132-
| ```{{ .AnsiColor.White }}``` |
133-
| ```{{ .AnsiColor.BrightBlack }}``` |
134-
| ```{{ .AnsiColor.BrightRed }}``` |
135-
| ```{{ .AnsiColor.BrightGreen }}``` |
136-
| ```{{ .AnsiColor.BrightYellow }}``` |
137-
| ```{{ .AnsiColor.BrightBlue }}``` |
138-
| ```{{ .AnsiColor.BrightMagenta }}``` |
139-
| ```{{ .AnsiColor.BrightCyan }}``` |
140-
| ```{{ .AnsiColor.BrightWhite }}``` |
141-
| ```{{ .AnsiBackground.Default }}``` |
142-
| ```{{ .AnsiBackground.Black }}``` |
143-
| ```{{ .AnsiBackground.Red }}``` |
144-
| ```{{ .AnsiBackground.Green }}``` |
145-
| ```{{ .AnsiBackground.Yellow }}``` |
146-
| ```{{ .AnsiBackground.Blue }}``` |
147-
| ```{{ .AnsiBackground.Magenta }}``` |
148-
| ```{{ .AnsiBackground.Cyan }}``` |
149-
| ```{{ .AnsiBackground.White }}``` |
150-
| ```{{ .AnsiBackground.BrightBlack }}``` |
151-
| ```{{ .AnsiBackground.BrightRed }}``` |
152-
| ```{{ .AnsiBackground.BrightGreen }}``` |
153-
| ```{{ .AnsiBackground.BrightYellow }}``` |
154-
| ```{{ .AnsiBackground.BrightBlue }}``` |
155-
| ```{{ .AnsiBackground.BrightMagenta }}``` |
156-
| ```{{ .AnsiBackground.BrightCyan }}``` |
157-
| ```{{ .AnsiBackground.BrightWhite }}``` |
143+
| Variable |
144+
| ------------------------------------- |
145+
| `{{ .AnsiColor.Default }}` |
146+
| `{{ .AnsiColor.Black }}` |
147+
| `{{ .AnsiColor.Red }}` |
148+
| `{{ .AnsiColor.Green }}` |
149+
| `{{ .AnsiColor.Yellow }}` |
150+
| `{{ .AnsiColor.Blue }}` |
151+
| `{{ .AnsiColor.Magenta }}` |
152+
| `{{ .AnsiColor.Cyan }}` |
153+
| `{{ .AnsiColor.White }}` |
154+
| `{{ .AnsiColor.BrightBlack }}` |
155+
| `{{ .AnsiColor.BrightRed }}` |
156+
| `{{ .AnsiColor.BrightGreen }}` |
157+
| `{{ .AnsiColor.BrightYellow }}` |
158+
| `{{ .AnsiColor.BrightBlue }}` |
159+
| `{{ .AnsiColor.BrightMagenta }}` |
160+
| `{{ .AnsiColor.BrightCyan }}` |
161+
| `{{ .AnsiColor.BrightWhite }}` |
162+
| `{{ .AnsiBackground.Default }}` |
163+
| `{{ .AnsiBackground.Black }}` |
164+
| `{{ .AnsiBackground.Red }}` |
165+
| `{{ .AnsiBackground.Green }}` |
166+
| `{{ .AnsiBackground.Yellow }}` |
167+
| `{{ .AnsiBackground.Blue }}` |
168+
| `{{ .AnsiBackground.Magenta }}` |
169+
| `{{ .AnsiBackground.Cyan }}` |
170+
| `{{ .AnsiBackground.White }}` |
171+
| `{{ .AnsiBackground.BrightBlack }}` |
172+
| `{{ .AnsiBackground.BrightRed }}` |
173+
| `{{ .AnsiBackground.BrightGreen }}` |
174+
| `{{ .AnsiBackground.BrightYellow }}` |
175+
| `{{ .AnsiBackground.BrightBlue }}` |
176+
| `{{ .AnsiBackground.BrightMagenta }}` |
177+
| `{{ .AnsiBackground.BrightCyan }}` |
178+
| `{{ .AnsiBackground.BrightWhite }}` |
158179

159180
Want to see a nyancat?
160181

161182
```sh
162-
$ go run example/main.go -banner example/nyancat.txt
183+
$ go run examples/file/main.go -banner examples/file/nyancat.txt
163184
```
164185

165186
![NyanCat Banner](banner-nyan.png?raw=true "NyanCat Banner")
166187

167188
## Example
168189

169190
```
170-
____
171-
| _ \
172-
| |_) | __ _ _ __ _ __ ___ _ __
191+
____
192+
| _ \
193+
| |_) | __ _ _ __ _ __ ___ _ __
173194
| _ < / _` | '_ \| '_ \ / _ \ '__|
174-
| |_) | (_| | | | | | | | __/ |
175-
|____/ \__,_|_| |_|_| |_|\___|_|
176-
195+
| |_) | (_| | | | | | | | __/ |
196+
|____/ \__,_|_| |_|_| |_|\___|_|
197+
177198
GoVersion: {{ .GoVersion }}
178199
GOOS: {{ .GOOS }}
179200
GOARCH: {{ .GOARCH }}
@@ -214,6 +235,8 @@ I am using the standard golang log, but there is a function SetLog that accepts
214235

215236
Access http://patorjk.com/software/taag/#p=display&f=Big&t=Banner to generate ASCII banners.
216237

238+
Or use `{{ .Title }}` template
239+
217240
# LICENSE
218241

219242
The MIT License (MIT)

banner-nyan.png

-50.4 KB
Loading

banner.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
package banner
66

77
import (
8+
"bytes"
89
"io"
910
"io/ioutil"
1011
"log"
1112
"os"
1213
"runtime"
14+
"strings"
1315
"text/template"
1416
"time"
17+
18+
"github.com/common-nighthawk/go-figure"
1519
)
1620

1721
var logger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
@@ -45,6 +49,33 @@ func (v vars) Now(layout string) string {
4549
return time.Now().Format(layout)
4650
}
4751

52+
// Create ASCII art from title
53+
func (v vars) Title(title, font string, indentW int) string {
54+
// TODO(phanirithvij) variadic args
55+
fig := figure.NewFigure(title, font, true)
56+
text := fig.String()
57+
// TODO(phanirithvij) Use ColorString as soon as PR
58+
// https://github.com/common-nighthawk/go-figure/pull/16 lands
59+
// if color != "" {
60+
// fig = figure.NewColorFigure(title, font, color, true)
61+
// }
62+
// text := fig.ColorString()
63+
// TODO(phanirithvij) figure out indetation https://stackoverflow.com/q/46066524/8608146
64+
return indent(indentW, text)
65+
}
66+
67+
// See https://github.com/Masterminds/sprig/blob/4241ae82dd07e5d3391a83c25b79c04450eb22b0/strings.go#L109
68+
func indent(spaces int, v string) string {
69+
pad := strings.Repeat(" ", spaces)
70+
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
71+
}
72+
73+
// InitString load the banner from a string and prints it to output
74+
// All errors are ignored, the application will not print the banner in case of error.
75+
func InitString(out io.Writer, isEnabled, isColorEnabled bool, templ string) {
76+
Init(out, isEnabled, isColorEnabled, bytes.NewBufferString(templ))
77+
}
78+
4879
// Init load the banner and prints it to output
4980
// All errors are ignored, the application will not print the banner in case of error.
5081
func Init(out io.Writer, isEnabled, isColorEnabled bool, in io.Reader) {

banner_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"runtime"
1414
"testing"
1515
"time"
16+
17+
"github.com/common-nighthawk/go-figure"
1618
)
1719

1820
func Test_printBanner(t *testing.T) {
@@ -152,6 +154,18 @@ func Test_vars_Env(t *testing.T) {
152154
}
153155
}
154156

157+
func Test_vars_Title(t *testing.T) {
158+
v := vars{}
159+
ascii := v.Title("Banner", "", 0)
160+
161+
fig := figure.NewFigure("Banner", "", true)
162+
expected := fig.String()
163+
164+
if ascii != expected {
165+
t.Errorf("ascii != expected, got %s\n need\n %s\n", ascii, expected)
166+
}
167+
}
168+
155169
func Test_vars_Now(t *testing.T) {
156170
v := vars{}
157171
gopath := v.Now("Monday, 2 Jan 2006")
File renamed without changes.

examples/title/banner.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 Claudemiro Alves Feitosa Neto. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/mattn/go-colorable"
9+
10+
"github.com/dimiro1/banner"
11+
)
12+
13+
func init() {
14+
templ := `{{ .Title "Banner" "" 4 }}
15+
{{ .AnsiColor.BrightCyan }}The title will be ascii and indented 4 spaces{{ .AnsiColor.Default }}
16+
GoVersion: {{ .GoVersion }}
17+
GOOS: {{ .GOOS }}
18+
GOARCH: {{ .GOARCH }}
19+
NumCPU: {{ .NumCPU }}
20+
GOPATH: {{ .GOPATH }}
21+
GOROOT: {{ .GOROOT }}
22+
Compiler: {{ .Compiler }}
23+
ENV: {{ .Env "GOPATH" }}
24+
Now: {{ .Now "Monday, 2 Jan 2006" }}
25+
{{ .AnsiColor.BrightGreen }}This text will appear in Green
26+
{{ .AnsiColor.BrightRed }}This text will appear in Red{{ .AnsiColor.Default }}`
27+
28+
banner.InitString(colorable.NewColorableStdout(), true, true, templ)
29+
}

examples/title/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2016 Claudemiro Alves Feitosa Neto. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
// Banner init in banner.go
8+
9+
func main() {}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/dimiro1/banner
33
go 1.11
44

55
require (
6+
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2
67
github.com/mattn/go-colorable v0.1.4
78
github.com/mattn/go-isatty v0.0.10
89
)

0 commit comments

Comments
 (0)