banner

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package banner 提供启动横幅功能,用于 enhance 框架。

该包定义了启动横幅的接口和多种实现,支持控制台输出、日志输出和关闭模式。 参考 Spring Boot 的 Banner 设计,支持 ASCII 艺术、文本模板等多种横幅格式。

架构设计

  • Banner: 启动横幅接口,定义横幅打印和模式查询方法
  • BannerMode: 横幅模式枚举,控制横幅的输出方式
  • TextBanner: 文本横幅实现,支持模板和属性替换
  • ASCIIArtBanner: ASCII 艺术横幅实现,支持自定义艺术文本
  • LegacyBanner: 旧版横幅实现,兼容 boot 包中的横幅格式

核心功能

  • 启动横幅显示: 在应用启动时打印横幅信息
  • 多种输出模式: 支持控制台输出、日志输出和关闭模式
  • 模板渲染: 支持文本模板和属性替换
  • ASCII 艺术: 支持自定义 ASCII 艺术文本

使用方式

使用文本横幅:

banner := banner.NewTextBanner("Hello {{name}} v{{version}}", map[string]any{
    "name": "my-app",
    "version": "1.0.0",
})
banner.Print("1.0.0")

使用 ASCII 艺术横幅:

banner := banner.NewASCIIArtBanner("  ___  \n /   \\ \n|     |", "")
banner.Print("1.0.0")

使用旧版横幅:

b := banner.NewLegacyBanner(
    banner.WithLines([]string{"Line 1", "Line 2"}),
    banner.WithAppName("my-app"),
    banner.WithProfiles([]string{"dev"}),
)
b.Print("1.0.0")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ASCIIArtBanner

type ASCIIArtBanner struct {
	// contains filtered or unexported fields
}

ASCIIArtBanner ASCII 艺术横幅实现。

使用自定义的 ASCII 艺术文本作为启动横幅。 支持可选的颜色设置(预留功能)。

示例:

banner := NewASCIIArtBanner("  ___  \n /   \\ \n|     |", "")
banner.Print("1.0.0")

func (*ASCIIArtBanner) Mode

func (b *ASCIIArtBanner) Mode() BannerMode

Mode 返回当前横幅的输出模式。

func (*ASCIIArtBanner) Print

func (b *ASCIIArtBanner) Print(version string) error

Print 打印 ASCII 艺术横幅到标准输出。

输出 ASCII 艺术文本和版本信息到 os.Stdout。 如果 art 为空,则输出简单的版本信息。

参数 version 为应用版本号,会显示在横幅底部。

func (*ASCIIArtBanner) SetMode

func (b *ASCIIArtBanner) SetMode(mode BannerMode)

SetMode 设置横幅的输出模式。

type Banner interface {
	// Print 打印横幅到标准输出。
	//
	// 参数 version 为应用版本号,用于在横幅中显示版本信息。
	// 返回值 error 表示打印过程中是否发生错误。
	Print(version string) error

	// Mode 返回当前横幅的输出模式。
	//
	// 返回值 BannerMode 枚举,指示横幅将输出到控制台、日志还是关闭。
	Mode() BannerMode
}

Banner 启动横幅接口。

参考 Spring Boot 的 Banner,支持多种格式的启动横幅显示。 每个横幅实现都必须实现此接口,以便与 enhance 的启动流程集成。

func NewASCIIArtBanner

func NewASCIIArtBanner(art string, color string) Banner

NewASCIIArtBanner 创建 ASCII 艺术横幅实例。

参数:

  • art: ASCII 艺术文本,支持多行文本
  • color: 显示颜色(预留,当前未使用)

返回值:

  • Banner: ASCII 艺术横幅实例,已设置默认控制台输出模式

func NewLegacyBanner

func NewLegacyBanner(opts ...LegacyOption) Banner

NewLegacyBanner 创建旧版横幅实例。

参数:

  • opts: 可选配置项,如 WithLines, WithAppName, WithProfiles

返回值:

  • Banner: 旧版横幅实例,已设置默认控制台输出模式

func NewTextBanner

func NewTextBanner(template string, properties map[string]any) Banner

NewTextBanner 创建文本横幅实例。

参数:

  • template: 横幅文本模板,支持 {{key}} 格式的属性替换
  • properties: 模板属性键值对,用于替换模板中的占位符

返回值:

  • Banner: 文本横幅实例,已设置默认控制台输出模式

type BannerMode

type BannerMode int

BannerMode 横幅模式枚举。

控制横幅的输出方式,支持控制台输出、日志输出和关闭模式。

const (
	// BannerModeConsole 控制台模式。
	//
	// 横幅直接输出到标准输出(os.Stdout)。
	BannerModeConsole BannerMode = iota

	// BannerModeLog 日志模式。
	//
	// 横幅通过日志框架输出,不直接打印到控制台。
	BannerModeLog

	// BannerModeOff 关闭模式。
	//
	// 横幅不输出任何内容,完全禁用横幅显示。
	BannerModeOff
)

func (BannerMode) String

func (m BannerMode) String() string

String 返回横幅模式的字符串表示。

type LegacyBanner

type LegacyBanner struct {
	// contains filtered or unexported fields
}

LegacyBanner 旧版横幅实现。

兼容 boot 包中的 LegacyBanner 格式,使用预定义的 ASCII 艺术行列表 和应用名称、版本号、Profile 信息渲染启动横幅。

示例:

banner := NewLegacyBanner(
    WithLines([]string{"Line 1", "Line 2"}),
    WithAppName("my-app"),
    WithProfiles([]string{"dev"}),
)
banner.Print("1.0.0")

func (*LegacyBanner) Mode

func (b *LegacyBanner) Mode() BannerMode

Mode 返回当前横幅的输出模式。

func (*LegacyBanner) Print

func (b *LegacyBanner) Print(version string) error

Print 打印旧版横幅到标准输出。

输出 ASCII 艺术行、应用名称、版本号和 Profile 信息到 os.Stdout。 格式为::: appName :: vversion :: profiles(p1, p2)

参数 version 为应用版本号,会显示在横幅信息行中。

func (*LegacyBanner) SetMode

func (b *LegacyBanner) SetMode(mode BannerMode)

SetMode 设置横幅的输出模式。

type LegacyOption

type LegacyOption func(*LegacyBanner)

LegacyOption LegacyBanner 的选项函数。

func WithAppName

func WithAppName(name string) LegacyOption

WithAppName 设置应用名称。

func WithLines

func WithLines(lines []string) LegacyOption

WithLines 设置 ASCII 艺术行列表。

func WithProfiles

func WithProfiles(profiles []string) LegacyOption

WithProfiles 设置激活的 Profile 列表。

type TextBanner

type TextBanner struct {
	// contains filtered or unexported fields
}

TextBanner 文本横幅实现。

使用模板和属性渲染启动横幅,支持简单的属性替换。 模板中的 {{key}} 会被替换为对应的属性值。

示例:

banner := NewTextBanner("Hello {{name}} v{{version}}", map[string]any{
    "name": "my-app",
    "version": "1.0.0",
})
banner.Print("1.0.0")

func (*TextBanner) Mode

func (b *TextBanner) Mode() BannerMode

Mode 返回当前横幅的输出模式。

func (*TextBanner) Print

func (b *TextBanner) Print(version string) error

Print 打印文本横幅到标准输出。

渲染模板并输出到 os.Stdout,模板中的 {{key}} 会被替换为属性值。 如果模板为空,则输出简单的版本信息。

参数 version 为应用版本号,会自动添加到 properties 中。

func (*TextBanner) SetMode

func (b *TextBanner) SetMode(mode BannerMode)

SetMode 设置横幅的输出模式。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL