// Padding var style = lipgloss.NewStyle(). PaddingTop(2). PaddingRight(4). PaddingBottom(2). PaddingLeft(4)
// Margins var style = lipgloss.NewStyle(). MarginTop(2). MarginRight(4). MarginBottom(2). MarginLeft(4)
同样可以像 CSS 一样用复合属性:
1 2 3 4 5 6 7 8 9 10 11 12
// 2 cells on all sides lipgloss.NewStyle().Padding(2)
// 2 cells on the top and bottom, 4 cells on the left and right lipgloss.NewStyle().Margin(2, 4)
// 1 cell on the top, 4 cells on the sides, 2 cells on the bottom lipgloss.NewStyle().Padding(1, 4, 2)
// Clockwise, starting from the top: 2 cells on the top, 4 on the right, 3 on // the bottom, and 1 on the left lipgloss.NewStyle().Margin(2, 4, 3, 1)
设置文本对齐
1 2 3 4 5
var style = lipgloss.NewStyle(). Width(24). Align(lipgloss.Left). // align it left Align(lipgloss.Right). // no wait, align it right Align(lipgloss.Center) // just kidding, align it in the center
设置文本宽高
1 2 3 4 5
var style = lipgloss.NewStyle(). SetString("What’s for lunch?"). Width(24). Height(32). Foreground(lipgloss.Color("63"))
// Add a purple, rectangular border var style = lipgloss.NewStyle(). BorderStyle(lipgloss.NormalBorder()). BorderForeground(lipgloss.Color("63"))
// Set a rounded, yellow-on-purple border to the top and left var anotherStyle = lipgloss.NewStyle(). BorderStyle(lipgloss.RoundedBorder()). BorderForeground(lipgloss.Color("228")). BorderBackground(lipgloss.Color("63")). BorderTop(true). BorderLeft(true)
// Make your own border var myCuteBorder = lipgloss.Border{ Top: "._.:*:", Bottom: "._.:*:", Left: "|*", Right: "|*", TopLeft: "*", TopRight: "*", BottomLeft: "*", BottomRight: "*", }
复合形式:
1 2 3 4 5 6 7 8
// Add a thick border to the top and bottom lipgloss.NewStyle(). Border(lipgloss.ThickBorder(), true, false)
// Add a thick border to the right and bottom sides. Rules are set clockwise // from top. lipgloss.NewStyle(). Border(lipgloss.DoubleBorder(), true, false, false, true)
var style = lipgloss.NewStyle().Foreground(lipgloss.Color("219"))
var wildStyle = style.Copy().Blink(true)
继承一个样式
1 2 3 4 5 6 7 8 9
var styleA = lipgloss.NewStyle(). Foreground(lipgloss.Color("229")). Background(lipgloss.Color("63"))
// Only the background color will be inherited here, because the foreground // color will have been already set: var styleB = lipgloss.NewStyle(). Foreground(lipgloss.Color("201")). Inherit(styleA)
取消一个样式
在 rules 之前添加 Unset, 如:
1 2 3 4 5
var style = lipgloss.NewStyle(). Bold(true). // make it bold UnsetBold(). // jk don't make it bold Background(lipgloss.Color("227")). // yellow background UnsetBackground() // never mind
强制生效的一些样式
Inline, MaxWidth, 和 MaxHeight:
1 2 3 4 5 6 7 8
// Force rendering onto a single line, ignoring margins, padding, and borders. someStyle.Inline(true).Render("yadda yadda")
// Also limit rendering to five cells someStyle.Inline(true).MaxWidth(5).Render("yadda yadda")
// Limit rendering to a 5x5 cell block someStyle.MaxWidth(5).MaxHeight(5).Render("yadda yadda")
控制 Tab 的渲染
1 2 3 4
style := lipgloss.NewStyle() // tabs will render as 4 spaces, the default style = style.TabWidth(2) // render tabs as 2 spaces style = style.TabWidth(0) // remove tabs entirely style = style.TabWidth(lipgloss.NoTabConversion) // leave tabs intact
var style = lipgloss.NewStyle().SetString("你好,猫咪。").Bold(true) fmt.Println(style) // 你好,猫咪。
将文本水平或纵向排列
1 2 3 4 5 6 7 8 9
// Horizontally join three paragraphs along their bottom edges lipgloss.JoinHorizontal(lipgloss.Bottom, paragraphA, paragraphB, paragraphC)
// Vertically join two paragraphs along their center axes lipgloss.JoinVertical(lipgloss.Center, paragraphA, paragraphB)
// Horizontally join three paragraphs, with the shorter ones aligning 20% // from the top of the tallest lipgloss.JoinHorizontal(0.2, paragraphA, paragraphB, paragraphC)
得到 block 的宽高
1 2 3 4 5 6 7 8 9 10 11 12
// Render a block of text. var style = lipgloss.NewStyle(). Width(40). Padding(2) var block string = style.Render(someLongString)
// Get the actual, physical dimensions of the text block. width := lipgloss.Width(block) height := lipgloss.Height(block)
// Here's a shorthand function. w, h := lipgloss.Size(block)
指定位置放置字符串
用 Place 系列函数.
type Position
大小为 0~1 之间的数字:
1
type Position float64
预定义有:
1 2 3 4 5 6 7
const ( Top Position = 0.0 Bottom Position = 1.0 Center Position = 0.5 Left Position = 0.0 Right Position = 1.0 )