Skip to content

Commit

Permalink
Support clear
Browse files Browse the repository at this point in the history
Also improve the doc.
  • Loading branch information
knz committed Sep 3, 2022
1 parent fab06d1 commit f085409
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Import(dst Style, input string) (Style, error)
func Export(s Style) string
```

## Exporting styles to text

For example:

Expand Down Expand Up @@ -62,18 +63,22 @@ padding-top: 2;
width: 22;
```

Then using the `Import()` function on the result will recover the original `lipgloss.Style`.
## Importing styles from text

See the [lipgloss
The `Import` function applies the text directives specified in its input
argument to the style also provided as argument. Other properties already
in the style remain unchanged.

Which properties are supported? See the [lipgloss
documentation](https://pkg.go.dev/github.com/charmbracelet/lipgloss)
for details. This library automatically supports all the lipgloss
for details. `Import` automatically supports all the lipgloss
properties, as follows:

- `Foreground` in lipgloss becomes `foreground` in the textual syntax.
- `UnderlineSpaces` becomes `underline-spaces`.
- etc.

It also supports the following special values:
`Import` also supports the following special cases:

- For colors:

Expand Down Expand Up @@ -109,3 +114,6 @@ It also supports the following special values:
border-style: normal true false;
border-style: normal true false false true;
```

- Resetting a style with `clear`: this erases all the properties
in the style, to start with a fresh style.
6 changes: 6 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func Import(dst S, input string) (S, error) {
continue
}

if a == "clear" {
// Special keyword: reset style.
dst = lipgloss.NewStyle()
continue
}

pair := strings.SplitN(a, ":", 2)
if len(pair) != 2 {
return dst, fmt.Errorf("invalid syntax: %q", a)
Expand Down
2 changes: 2 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestImport(t *testing.T) {
{emptyStyle, `align: right`, `align: 1;`, ``},
{emptyStyle, `align: 1.0`, `align: 1;`, ``},
{emptyStyle.Foreground(lipgloss.Color("11")), `foreground: none`, ``, ``},
{emptyStyle.Foreground(lipgloss.Color("11")), `clear`, ``, ``},
{emptyStyle.Foreground(lipgloss.Color("11")), `background: 12; clear`, ``, ``},
{emptyStyle, `foreground: 11`, `foreground: 11;`, ``},
{emptyStyle, `foreground: #123`, `foreground: #123;`, ``},
{emptyStyle, `foreground: #123456`, `foreground: #123456;`, ``},
Expand Down

0 comments on commit f085409

Please sign in to comment.