golang为Go应用添加精美横幅的插件库banner的使用
Golang为Go应用添加精美横幅的插件库banner的使用
简介
Banner是一个可以为Go应用程序添加精美启动横幅的库,它能让你的应用更具个性。
快速开始
自动加载方式
最简单的使用方式是导入自动加载包:
package main
import _ "github.com/dimiro1/banner/autoload"
func main() {}
默认情况下,它会查找当前目录下的banner.txt
文件。
手动初始化方式
如果你不想使用自动加载包,可以使用API手动初始化:
package main
import (
"bytes"
"os"
"github.com/dimiro1/banner"
)
func main() {
isEnabled := true
isColorEnabled := true
banner.Init(os.Stdout, isEnabled, isColorEnabled, bytes.NewBufferString("My Custom Banner"))
}
Windows平台支持
在Windows平台上,建议使用go-colorable
以确保颜色正常显示:
package main
import (
"bytes"
"os"
"github.com/dimiro1/banner"
"github.com/mattn/go-colorable"
)
func main() {
isEnabled := true
isColorEnabled := true
banner.Init(colorable.NewColorableStdout(), isEnabled, isColorEnabled, bytes.NewBufferString("My Custom Banner"))
}
命令行参数
你可以通过命令行参数自定义banner的行为:
Usage of main:
-ansi
ansi colors enabled? (default true)
-banner string
banner.txt file (default "banner.txt")
-show-banner
print the banner? (default true)
模板功能
你可以在banner模板中使用以下变量:
变量 | 值 |
---|---|
{{ .Title "YourTitle" "fontname" indent }} |
生成的ASCII艺术字 |
{{ .GoVersion }} |
runtime.Version() |
{{ .GOOS }} |
runtime.GOOS |
{{ .GOARCH }} |
runtime.GOARCH |
{{ .NumCPU }} |
runtime.NumCPU() |
{{ .GOPATH }} |
os.Getenv("GOPATH") |
{{ .GOROOT }} |
runtime.GOROOT() |
{{ .Compiler }} |
runtime.Compiler |
{{ .Env "GOPATH" }} |
os.Getenv("GOPATH") |
{{ .Now "Monday, 2 Jan 2006" }} |
time.Now().Format("Monday, 2 Jan 2006") |
标题生成
.Title
函数可以为你生成ASCII艺术字标题:
// .Title string string int
// .Title title fontname indentation_spaces
{{ .Title "Banner" "" 0 }}
{{ .Title "Banner" "banner2" 0 }}
{{ .Title "Banner" "" 4 }}
颜色支持
Banner支持ANSI颜色:
{{ .AnsiColor.Default }}
{{ .AnsiColor.Black }}
{{ .AnsiColor.Red }}
{{ .AnsiColor.Green }}
{{ .AnsiColor.Yellow }}
{{ .AnsiColor.Blue }}
{{ .AnsiColor.Magenta }}
{{ .AnsiColor.Cyan }}
{{ .AnsiColor.White }}
// 还有更多颜色选项...
完整示例
下面是一个完整的banner.txt示例:
____
| _ \
| |_) | __ _ _ __ _ __ ___ _ __
| _ < / _` | '_ \| '_ \ / _ \ '__|
| |_) | (_| | | | | | | | __/ |
|____/ \__,_|_| |_|_| |_|\___|_|
GoVersion: {{ .GoVersion }}
GOOS: {{ .GOOS }}
GOARCH: {{ .GOARCH }}
NumCPU: {{ .NumCPU }}
GOPATH: {{ .GOPATH }}
GOROOT: {{ .GOROOT }}
Compiler: {{ .Compiler }}
ENV: {{ .Env "GOPATH" }}
Now: {{ .Now "Monday, 2 Jan 2006" }}
运行后会输出类似这样的内容:
____
| _ \
| |_) | __ _ _ __ _ __ ___ _ __
| _ < / _` | '_ \| '_ \ / _ \ '__|
| |_) | (_| | | | | | | | __/ |
|____/ \__,_|_| |_|_| |_|\___|_|
GoVersion: go1.6
GOOS: darwin
GOARCH: amd64
NumCPU: 4
GOPATH: /Users/claudemiro/go
GOROOT: /usr/local/Cellar/go/1.6/libexec
Compiler: gc
ENV: /Users/claudemiro/go
Now: Friday, 26 Mar 2016
ASCII横幅生成
你可以使用在线工具生成ASCII横幅,或者直接使用.Title
模板函数自动生成。
许可证
Banner使用MIT许可证。
更多关于golang为Go应用添加精美横幅的插件库banner的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复