Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
coldfgirl committed Oct 10, 2024
0 parents commit a5647c8
Show file tree
Hide file tree
Showing 17 changed files with 677 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @coldfgirl
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/instyle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 robin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# InStyle

![demo.gif](demo.gif)

InStyle is a small library for efficiently decorating strings with ANSI escape codes.

## Syntax

The tags follow the following format:

```
[!style]text to be styled[/]
```

Style can be a named style, or a raw value to be used in an ANSI escape code.
For example, both of these will turn the text red:

```
[!red]this text will show up as red[/]
[!31]this text will show up as red[/]
```

The ending sequence of `[/]` can be fully omitted for minor performance gains like so:

```
[!italic]ending tags need not be included
```

### Multiple Styles

Multiple styles can be added by using the `+` character between each style desired.

```
[!magenta+bold]this text has two styles[/]
```

### Nesting & Sequential Tags

Up to 5 tags can be nested.
All unclosed tags are terminated at the end of a string.

```
[!cyan]i never said [!bold]you[/] did that[/]... [!italic]somebody else[/] did
```

### Named Styles

<details>

<summary>Complete list of default styles.</summary>

#### Text Styling

- `plain`
- `reset`
- `bold`
- `faint`
- `italic`
- `underline`
- `blink`
- `strike`

#### Basic Colors

- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
- `default`

#### Basic Backgrounds

- `bg-black`
- `bg-red`
- `bg-green`
- `bg-yellow`
- `bg-blue`
- `bg-magenta`
- `bg-cyan`
- `bg-white`
- `bg-default`

#### Light Colors

- `light-black`
- `light-red`
- `light-green`
- `light-yellow`
- `light-blue`
- `light-magenta`
- `light-cyan`
- `light-white`

#### Light Backgrounds

- `bg-light-black`
- `bg-light-red`
- `bg-light-green`
- `bg-light-yellow`
- `bg-light-blue`
- `bg-light-magenta`
- `bg-light-cyan`
- `bg-light-white`

</details>

Aside from the named styles, additional styles can be added to a `Styler` instance by using the `Register` method.
This can be used to associated more than one ANSI escape code to a name.

```go
s := instyle.NewStyler()
s.Register("error", "1;31") // Bold and red
```

A style name can only be a maximum of 15 characters long.

## Performance

While applying a set of styles, this code runs ~2-3x slower than an unbuffered copy of an array of runes:

```go
// ideal performance goal:
var dst []rune
for _, r := range []rune("...") {
dst = append(dst, r)
}
```

However, when compared a regex solution or using [Lip Gloss](https://github.com/charmbracelet/lipgloss) directly this will perform about 5-10x faster.

Running on a M2 2022 MacBook Air, the truncated / formatted benchmark results look like:

```
BenchmarkBaseline/BestCase-8 15542974 70 ns/op
BenchmarkBaseline/PerformanceGoal-8 4497812 266 ns/op
BenchmarkApply/NoStyle-8 4112559 291 ns/op
BenchmarkApply/WithStyle-8 1775058 677 ns/op
BenchmarkApply/WithStyleToFromString-8 747465 1581 ns/op
BenchmarkSimilarLipGloss-8 129320 9212 ns/op
```
7 changes: 7 additions & 0 deletions apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package instyle

import "fmt"

func Apply(format string, args ...any) string {
return fmt.Sprintf(string(NewStyler().Apply([]rune(format))), args...)
}
18 changes: 18 additions & 0 deletions apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package instyle_test

import (
"instyle"
"testing"
)

func TestApply(t *testing.T) {
in := "[!bold]%s[/]"
inParam := "testing [!faint]string[/]"
out := "\033[0m\033[1mtesting [!faint]string[/]\033[0m"

if result := instyle.Apply(in, inParam); result != out {
t.Logf("Want: %+v", []rune(out))
t.Logf("Got: %+v", []rune(result))
t.FailNow()
}
}
17 changes: 17 additions & 0 deletions cmd/instyle/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/coldfgirl/instyle"
)

func main() {
if len(os.Args) > 1 {
fmt.Println(instyle.Apply(strings.Join(os.Args[1:], " ")))
} else {
_, _ = fmt.Fprintln(os.Stderr, instyle.Apply("[!bold+red]no command line arguments provided"))
}
}
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions demo.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Output demo.gif

Require echo

Set Theme "Catppuccin Frappe"

Set Shell "bash"
Set FontSize 18
Set Width 1200
Set Height 200

Type@45ms "go run ./cmd/... '[!italic]you can [!cyan]style[/] text with [!bold+magenta]InStyle[/]!!!'" Sleep 100ms Enter
Sleep 10s
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/coldfgirl/instyle

go 1.21.4

require github.com/charmbracelet/lipgloss v0.13.0

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.19.0 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
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.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Loading

0 comments on commit a5647c8

Please sign in to comment.