[introduction]: Go terminal output style library based on TUI.
brief introduction
Lip gloss is an exquisite terminal output style library, which is built based on TUI. You can DIY your own Go terminal style and define text style, background color matching, etc. Lip Gloss adopts an expressive and declarative method for terminal rendering. If you are familiar with CSS, lip gloss is very simple for you.
import "github.com/charmbracelet/lipgloss" var style = lipgloss.NewStyle(). Bold(true). Foreground(lipgloss.Color("#FAFAFA")). Background(lipgloss.Color("#7D56F4")). PaddingTop(2). PaddingLeft(4). Width(22) fmt.Println(style.Render("Hello, kitty."))
The project address is:
https://github.com/charmbracelet/lipgloss
color setting
Lip Gloss supports the following color profiles:
- ANSI 16 Colors (4-bit)
lipgloss.Color("5") // magenta lipgloss.Color("9") // red lipgloss.Color("12") // light blue
- ANSI 256 Colors (8-bit)
lipgloss.Color("86") // aqua lipgloss.Color("201") // hot pink lipgloss.Color("202") // orange
- True Color (24-bit)
lipgloss.Color("#0000FF") // good ol' 100% blue lipgloss.Color("#04B575") // a green lipgloss.Color("#3C3C3C") // a dark gray
These configuration files will be automatically detected, and the color outside the color palette of the palette will be automatically coerced into its nearest available value.
- Adaptive Colors can also specify color options for light and dark backgrounds:
lipgloss.AdaptiveColor{Light: "236", Dark: "248"}
inline formatting
Lip Gloss supports the usual ANSI text format options:
var style = lipgloss.NewStyle(). Bold(true). Italic(true). Faint(true). Blink(true). Strikethrough(true). Underline(true). Reverse(true)
Block level formatting
Lip Gloss also supports block level formatting rules:
// Padding var style = lipgloss.NewStyle(). PaddingTop(2). PaddingRight(4). PaddingBottom(2). PaddingLeft(4) // Margins var style = lipgloss.NewStyle(). MarginTop(2). RightMarginRight(4). MarginBottom(2). MarginLeft(4)
There is also a shorthand syntax for margins and padding, which is the same as CSS:
lipgloss.NewStyle().Padding(2) lipgloss.NewStyle().Margin(2, 4) lipgloss.NewStyle().Padding(1, 4, 2) lipgloss.NewStyle().Margin(2, 4, 3, 1)
align text
You can set left alignment, right alignment and center display of text:
var style = lipgloss.NewStyle(). Width(24). Align(lipgloss.Left). //Align left Align(lipgloss.Right). //Right align Align(lipgloss.Center) //Center
Width and height
Setting the width and height is simple:
var str = lipgloss.NewStyle(). Width(24). Height(32). Foreground(lipgloss.Color("63")). Render("What's for lunch?")
Copy style
You can copy styles, copy and add new effects on the basis of existing styles:
var style = lipgloss.NewStyle().Foreground(lipgloss.Color("219")) var wildStyle = style.Copy().Blink(true)
Inherit style
You can inherit from other styles:
var styleA = lipgloss.NewStyle(). Foreground(lipgloss.Color("229")). Background(lipgloss.Color("63")) //Only the background is inherited because the foreground is reset var styleB = lipgloss.NewStyle(). Foreground(lipgloss.Color("201")). Inherit(styleA)
Cancel style
You can set a cancellation style. After cancellation, the style will not be used by copying and inheritance:
var style = lipgloss.NewStyle(). Bold(true). //Bold UnsetBold(). //Cancel bold Background(lipgloss.Color("227")). //Background color UnsetBackground() //Cancel background color
Expected style
Sometimes when developing components, you want to ensure that the style definition complies with the intended purpose in the UI. You can force the style to be set, and no one is allowed to modify the relevant style:
//Force rendering, ignoring margins, padding, borders someStyle.Inline(true).Render("yadda yadda") //Add limit, inline (true), maximum width (5) someStyle.Inline(true).MaxWidth(5).Render("yadda yadda") //Add limit, max width (5), Max height (5) someStyle.MaxWidth(5).MaxHeight(5).Render("yadda yadda")
Render
Use the Render method:
fmt.Println(lipgloss.NewStyle().Bold(true).Render("Hello, kitty."))
Or use the Stringer interface:
var style = lipgloss.NewStyle().String("Hello, cat.").Bold(true) fmt.Printf("%s\n", style)
Paragraph connection
Connect text paragraphs horizontally or vertically:
//Horizontal merge lipgloss.HorizontalJoin(lipgloss.Bottom, paragraphA, paragraphB, paragraphC) //Vertical merge lipgloss.VerticalJoin(lipgloss.Center, paragraphA, paragraphB)
Place text in spaces
//Center the paragraph horizontally in a space with a width of 80 block := lipgloss.PlaceHorizontal(80, lipgloss.Center, fancyStyledParagraph) //Place a paragraph in a space with a height of 30 block := lipgloss.PlaceVertical(30, lipgloss.Bottom, fancyStyledParagraph) //Place the paragraph in the lower right corner of 30x80 cell space. block := lipgloss.Place(30, 80, lipgloss.Right, lipgloss.Bottom, fancyStyledParagraph)
Open source outposts share popular, interesting and practical open source projects on a daily basis. Participate in maintaining the open source technology resource library of 100000 + Star, including Python, Java, C/C + +, Go, JS, CSS and node js,PHP,. NET, etc.