Skip to content

Commit

Permalink
refactor: replace props map with struct fields (#276)
Browse files Browse the repository at this point in the history
Use an int to store property existence and style struct fields to store
the actual values for each non-bool property.

Fixes: #139
Fixes: #141
  • Loading branch information
aymanbagabas committed May 2, 2024
2 parents 1d4cfcb + c986440 commit d2795c7
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 219 deletions.
1 change: 0 additions & 1 deletion .golangci-soft.yml
Expand Up @@ -23,7 +23,6 @@ linters:
- gomnd
- gomoddirectives
- goprintffuncname
- ifshort
# - lll
- misspell
- nakedret
Expand Down
41 changes: 13 additions & 28 deletions README.md
@@ -1,5 +1,4 @@
Lip Gloss
=========
# Lip Gloss

<p>
<a href="https://stuff.charm.sh/lipgloss/lipgloss-mascot-2k.png"><img width="340" alt="Lip Gloss title treatment" src="https://github.com/charmbracelet/lipgloss/assets/25087/147cadb1-4254-43ec-ae6b-8d6ca7b029a1"></a><br>
Expand Down Expand Up @@ -66,7 +65,6 @@ The terminal's color profile will be automatically detected, and colors outside
the gamut of the current palette will be automatically coerced to their closest
available value.


### Adaptive Colors

You can also specify color options for light and dark backgrounds:
Expand Down Expand Up @@ -117,7 +115,6 @@ var style = lipgloss.NewStyle().
Reverse(true)
```


## Block-Level Formatting

Lip Gloss also supports rules for block-level formatting:
Expand Down Expand Up @@ -156,7 +153,6 @@ lipgloss.NewStyle().Padding(1, 4, 2)
lipgloss.NewStyle().Margin(2, 4, 3, 1)
```


## Aligning Text

You can align paragraphs of text to the left, right, or center.
Expand All @@ -169,7 +165,6 @@ var style = lipgloss.NewStyle().
Align(lipgloss.Center) // just kidding, align it in the center
```


## Width and Height

Setting a minimum width and height is simple and straightforward.
Expand All @@ -182,7 +177,6 @@ var style = lipgloss.NewStyle().
Foreground(lipgloss.Color("63"))
```


## Borders

Adding borders is easy:
Expand Down Expand Up @@ -230,21 +224,19 @@ lipgloss.NewStyle().

For more on borders see [the docs][docs].


## Copying Styles

Just use `Copy()`:
Just use assignment

```go
var style = lipgloss.NewStyle().Foreground(lipgloss.Color("219"))

var wildStyle = style.Copy().Blink(true)
var wildStyle = style.Blink(true)
```

`Copy()` performs a copy on the underlying data structure ensuring that you get
a true, dereferenced copy of a style. Without copying, it's possible to mutate
styles.

Since `Style` data structures contains only primitive types, assigning a style
to another effectively creates a new copy of the style without mutating the
original.

## Inheritance

Expand All @@ -263,7 +255,6 @@ var styleB = lipgloss.NewStyle().
Inherit(styleA)
```


## Unsetting Rules

All rules can be unset:
Expand All @@ -278,7 +269,6 @@ var style = lipgloss.NewStyle().

When a rule is unset, it won't be inherited or copied.


## Enforcing Rules

Sometimes, such as when developing a component, you want to make sure style
Expand Down Expand Up @@ -355,7 +345,6 @@ For an example on using a custom renderer over SSH with [Wish][wish] see the
In addition to pure styling, Lip Gloss also ships with some utilities to help
assemble your layouts.


### Joining Paragraphs

Horizontally and vertically joining paragraphs is a cinch.
Expand All @@ -372,7 +361,6 @@ lipgloss.JoinVertical(lipgloss.Center, paragraphA, paragraphB)
lipgloss.JoinHorizontal(0.2, paragraphA, paragraphB, paragraphC)
```


### Measuring Width and Height

Sometimes you’ll want to know the width and height of text blocks when building
Expand Down Expand Up @@ -465,7 +453,7 @@ fmt.Println(t)

For more on tables see [the docs](https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc) and [examples](https://github.com/charmbracelet/lipgloss/tree/master/examples/table).

***
---

## FAQ

Expand Down Expand Up @@ -501,10 +489,11 @@ import (
lipgloss.SetColorProfile(termenv.TrueColor)
```

*Note:* this option limits the flexibility of your application and can cause
_Note:_ this option limits the flexibility of your application and can cause
ANSI escape codes to be output in cases where that might not be desired. Take
careful note of your use case and environment before choosing to force a color
profile.

</details>

## What about [Bubble Tea][tea]?
Expand All @@ -518,7 +507,6 @@ In simple terms, you can use Lip Gloss to help build your Bubble Tea views.

[tea]: https://github.com/charmbracelet/tea


## Under the Hood

Lip Gloss is built on the excellent [Termenv][termenv] and [Reflow][reflow]
Expand All @@ -528,7 +516,6 @@ For many use cases Termenv and Reflow will be sufficient for your needs.
[termenv]: https://github.com/muesli/termenv
[reflow]: https://github.com/muesli/reflow


## Rendering Markdown

For a more document-centric rendering solution with support for things like
Expand All @@ -537,28 +524,26 @@ the stylesheet-based Markdown renderer.

[glamour]: https://github.com/charmbracelet/glamour


## Feedback

We’d love to hear your thoughts on this project. Feel free to drop us a note!

* [Twitter](https://twitter.com/charmcli)
* [The Fediverse](https://mastodon.social/@charmcli)
* [Discord](https://charm.sh/chat)
- [Twitter](https://twitter.com/charmcli)
- [The Fediverse](https://mastodon.social/@charmcli)
- [Discord](https://charm.sh/chat)

## License

[MIT](https://github.com/charmbracelet/lipgloss/raw/master/LICENSE)

***
---

Part of [Charm](https://charm.sh).

<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>

Charm热爱开源 • Charm loves open source


[docs]: https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc
[wish]: https://github.com/charmbracelet/wish
[ssh-example]: examples/ssh
6 changes: 3 additions & 3 deletions examples/go.mod
Expand Up @@ -7,6 +7,7 @@ replace github.com/charmbracelet/lipgloss => ../
require (
github.com/charmbracelet/lipgloss v0.4.0
github.com/charmbracelet/wish v0.5.0
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f
github.com/gliderlabs/ssh v0.3.4
github.com/kr/pty v1.1.1
github.com/lucasb-eyer/go-colorful v1.2.0
Expand All @@ -19,11 +20,10 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/caarlos0/sshmarshal v0.1.0 // indirect
github.com/charmbracelet/keygen v0.3.0 // indirect
github.com/charmbracelet/x/exp/term v0.0.0-20240329185201-62a6965a9fad // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
)
12 changes: 7 additions & 5 deletions examples/go.sum
Expand Up @@ -17,8 +17,9 @@ github.com/charmbracelet/keygen v0.3.0/go.mod h1:1ukgO8806O25lUZ5s0IrNur+RlwTBER
github.com/charmbracelet/wish v0.5.0 h1:FkkdNBFqrLABR1ciNrAL2KCxoyWfKhXnIGZw6GfAtPg=
github.com/charmbracelet/wish v0.5.0/go.mod h1:5GAn5SrDSZ7cgKjnC+3kDmiIo7I6k4/AYiRzC4+tpCk=
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
github.com/charmbracelet/x/exp/term v0.0.0-20240329185201-62a6965a9fad h1:ixybSpyIZys0qK4JF0asuuxdr9fMXHrOQa7/G9eO+nc=
github.com/charmbracelet/x/exp/term v0.0.0-20240329185201-62a6965a9fad/go.mod h1:6GZ13FjIP6eOCqWU4lqgveGnYxQo9c3qBzHPeFu4HBE=
github.com/charmbracelet/x/exp/term v0.0.0-20240408110044-525ba71bb562/go.mod h1:yQqGHmheaQfkqiJWjklPHVAq1dKbk8uGbcoS/lcKCJ0=
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f h1:1BXkZqDueTOBECyDoFGRi0xMYgjJ6vvoPIkWyKOwzTc=
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f/go.mod h1:yQqGHmheaQfkqiJWjklPHVAq1dKbk8uGbcoS/lcKCJ0=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -54,8 +55,9 @@ github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -133,8 +135,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
22 changes: 11 additions & 11 deletions examples/layout/main.go
Expand Up @@ -68,9 +68,9 @@ var (
BorderForeground(highlight).
Padding(0, 1)

activeTab = tab.Copy().Border(activeTabBorder, true)
activeTab = tab.Border(activeTabBorder, true)

tabGap = tab.Copy().
tabGap = tab.
BorderTop(false).
BorderLeft(false).
BorderRight(false)
Expand Down Expand Up @@ -109,7 +109,7 @@ var (
Padding(0, 3).
MarginTop(1)

activeButtonStyle = buttonStyle.Copy().
activeButtonStyle = buttonStyle.
Foreground(lipgloss.Color("#FFF7DB")).
Background(lipgloss.Color("#F25D94")).
MarginRight(2).
Expand Down Expand Up @@ -173,13 +173,13 @@ var (
Padding(0, 1).
MarginRight(1)

encodingStyle = statusNugget.Copy().
encodingStyle = statusNugget.
Background(lipgloss.Color("#A550DF")).
Align(lipgloss.Right)

statusText = lipgloss.NewStyle().Inherit(statusBarStyle)

fishCakeStyle = statusNugget.Copy().Background(lipgloss.Color("#6124DF"))
fishCakeStyle = statusNugget.Background(lipgloss.Color("#6124DF"))

// Page.

Expand Down Expand Up @@ -215,7 +215,7 @@ func main() {
for i, v := range colors {
const offset = 2
c := lipgloss.Color(v[0])
fmt.Fprint(&title, titleStyle.Copy().MarginLeft(i*offset).Background(c))
fmt.Fprint(&title, titleStyle.MarginLeft(i*offset).Background(c))
if i < len(colors)-1 {
title.WriteRune('\n')
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func main() {
listItem("Pomelo"),
),
),
list.Copy().Width(columnWidth).Render(
list.Width(columnWidth).Render(
lipgloss.JoinVertical(lipgloss.Left,
listHeader("Actual Lip Gloss Vendors"),
listItem("Glossier"),
Expand All @@ -300,9 +300,9 @@ func main() {

doc.WriteString(lipgloss.JoinHorizontal(
lipgloss.Top,
historyStyle.Copy().Align(lipgloss.Right).Render(historyA),
historyStyle.Copy().Align(lipgloss.Center).Render(historyB),
historyStyle.Copy().MarginRight(0).Render(historyC),
historyStyle.Align(lipgloss.Right).Render(historyA),
historyStyle.Align(lipgloss.Center).Render(historyB),
historyStyle.MarginRight(0).Render(historyC),
))

doc.WriteString("\n\n")
Expand All @@ -315,7 +315,7 @@ func main() {
statusKey := statusStyle.Render("STATUS")
encoding := encodingStyle.Render("UTF-8")
fishCake := fishCakeStyle.Render("🍥 Fish Cake")
statusVal := statusText.Copy().
statusVal := statusText.
Width(width - w(statusKey) - w(encoding) - w(fishCake)).
Render("Ravishing")

Expand Down
2 changes: 1 addition & 1 deletion examples/ssh/main.go
Expand Up @@ -161,7 +161,7 @@ func handler(next ssh.Handler) ssh.Handler {
styles.gray,
)

fmt.Fprintf(&str, "%s %t %s\n\n", styles.bold.Copy().UnsetString().Render("Has dark background?"),
fmt.Fprintf(&str, "%s %t %s\n\n", styles.bold.UnsetString().Render("Has dark background?"),
renderer.HasDarkBackground(),
renderer.Output().BackgroundColor())

Expand Down
8 changes: 4 additions & 4 deletions examples/table/languages/main.go
Expand Up @@ -23,9 +23,9 @@ func main() {
// CellStyle is the base lipgloss style used for the table rows.
CellStyle = re.NewStyle().Padding(0, 1).Width(14)
// OddRowStyle is the lipgloss style used for odd-numbered table rows.
OddRowStyle = CellStyle.Copy().Foreground(gray)
OddRowStyle = CellStyle.Foreground(gray)
// EvenRowStyle is the lipgloss style used for even-numbered table rows.
EvenRowStyle = CellStyle.Copy().Foreground(lightGray)
EvenRowStyle = CellStyle.Foreground(lightGray)
// BorderStyle is the lipgloss style used for the table border.
BorderStyle = lipgloss.NewStyle().Foreground(purple)
)
Expand Down Expand Up @@ -55,12 +55,12 @@ func main() {

// Make the second column a little wider.
if col == 1 {
style = style.Copy().Width(22)
style = style.Width(22)
}

// Arabic is a right-to-left language, so right align the text.
if row < len(rows) && rows[row-1][0] == "Arabic" && col != 0 {
style = style.Copy().Align(lipgloss.Right)
style = style.Align(lipgloss.Right)
}

return style
Expand Down
10 changes: 5 additions & 5 deletions examples/table/pokemon/main.go
Expand Up @@ -12,8 +12,8 @@ import (
func main() {
re := lipgloss.NewRenderer(os.Stdout)
baseStyle := re.NewStyle().Padding(0, 1)
headerStyle := baseStyle.Copy().Foreground(lipgloss.Color("252")).Bold(true)
selectedStyle := baseStyle.Copy().Foreground(lipgloss.Color("#01BE85")).Background(lipgloss.Color("#00432F"))
headerStyle := baseStyle.Foreground(lipgloss.Color("252")).Bold(true)
selectedStyle := baseStyle.Foreground(lipgloss.Color("#01BE85")).Background(lipgloss.Color("#00432F"))
typeColors := map[string]lipgloss.Color{
"Bug": lipgloss.Color("#D7FF87"),
"Electric": lipgloss.Color("#FDFF90"),
Expand Down Expand Up @@ -101,13 +101,13 @@ func main() {
}

color := c[fmt.Sprint(data[row-1][col])]
return baseStyle.Copy().Foreground(color)
return baseStyle.Foreground(color)
}

if even {
return baseStyle.Copy().Foreground(lipgloss.Color("245"))
return baseStyle.Foreground(lipgloss.Color("245"))
}
return baseStyle.Copy().Foreground(lipgloss.Color("252"))
return baseStyle.Foreground(lipgloss.Color("252"))
})
fmt.Println(t)
}

0 comments on commit d2795c7

Please sign in to comment.