perf(textarea): implement wrapping memoization

* Improve textarea performance by caching the wrap results

* Add memoization utility

* Fix failing github jobs due to requirement for 1.18

* Address linting issues

* Fix redundant docstrings

* fix: minor changes to memoization

* fix: soft-wrapped hidden line numbers

* perf: switch to `uniseg.StringWidth`

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
Manuel Odendahl
2024-01-22 13:30:34 -05:00
committed by GitHub
co-authored by Maas Lalani
parent ec883029c8
commit 6fe92f94b5
9 changed files with 426 additions and 52 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [~1.17, ^1]
go-version: [~1.18, ^1]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
+1 -1
View File
@@ -5,7 +5,7 @@ jobs:
coverage:
strategy:
matrix:
go-version: [^1]
go-version: [^1.18]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
env:
+1 -1
View File
@@ -16,7 +16,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ^1
go-version: ^1.18
- uses: actions/checkout@v4
- name: golangci-lint
+1 -1
View File
@@ -16,7 +16,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ^1
go-version: ^1.18
- uses: actions/checkout@v4
- name: golangci-lint
+2 -2
View File
@@ -1,6 +1,6 @@
module github.com/charmbracelet/bubbles
go 1.17
go 1.18
require (
github.com/atotto/clipboard v0.1.4
@@ -12,6 +12,7 @@ require (
github.com/mattn/go-runewidth v0.0.15
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.15.2
github.com/rivo/uniseg v0.4.4
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f
)
@@ -23,7 +24,6 @@ require (
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.6.0 // indirect
+2 -28
View File
@@ -21,8 +21,6 @@ github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
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=
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/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
@@ -34,42 +32,18 @@ github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKt
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.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+123
View File
@@ -0,0 +1,123 @@
package memoization
import (
"container/list"
"crypto/sha256"
"fmt"
"sync"
)
// Hasher is an interface that requires a Hash method. The Hash method is
// expected to return a string representation of the hash of the object.
type Hasher interface {
Hash() string
}
// entry is a struct that holds a key-value pair. It is used as an element
// in the evictionList of the MemoCache.
type entry[T any] struct {
key string
value T
}
// MemoCache is a struct that represents a cache with a set capacity. It
// uses an LRU (Least Recently Used) eviction policy. It is safe for
// concurrent use.
type MemoCache[H Hasher, T any] struct {
capacity int
mutex sync.Mutex
cache map[string]*list.Element // The cache holding the results
evictionList *list.List // A list to keep track of the order for LRU
hashableItems map[string]T // This map keeps track of the original hashable items (optional)
}
// NewMemoCache is a function that creates a new MemoCache with a given
// capacity. It returns a pointer to the created MemoCache.
func NewMemoCache[H Hasher, T any](capacity int) *MemoCache[H, T] {
return &MemoCache[H, T]{
capacity: capacity,
cache: make(map[string]*list.Element),
evictionList: list.New(),
hashableItems: make(map[string]T),
}
}
// Capacity is a method that returns the capacity of the MemoCache.
func (m *MemoCache[H, T]) Capacity() int {
return m.capacity
}
// Size is a method that returns the current size of the MemoCache. It is
// the number of items currently stored in the cache.
func (m *MemoCache[H, T]) Size() int {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.evictionList.Len()
}
// Get is a method that returns the value associated with the given
// hashable item in the MemoCache. If there is no corresponding value, the
// method returns nil.
func (m *MemoCache[H, T]) Get(h H) (T, bool) {
m.mutex.Lock()
defer m.mutex.Unlock()
hashedKey := h.Hash()
if element, found := m.cache[hashedKey]; found {
m.evictionList.MoveToFront(element)
return element.Value.(*entry[T]).value, true
}
var result T
return result, false
}
// Set is a method that sets the value for the given hashable item in the
// MemoCache. If the cache is at capacity, it evicts the least recently
// used item before adding the new item.
func (m *MemoCache[H, T]) Set(h H, value T) {
m.mutex.Lock()
defer m.mutex.Unlock()
hashedKey := h.Hash()
if element, found := m.cache[hashedKey]; found {
m.evictionList.MoveToFront(element)
element.Value.(*entry[T]).value = value
return
}
// Check if the cache is at capacity
if m.evictionList.Len() >= m.capacity {
// Evict the least recently used item from the cache
toEvict := m.evictionList.Back()
if toEvict != nil {
evictedEntry := m.evictionList.Remove(toEvict).(*entry[T])
delete(m.cache, evictedEntry.key)
delete(m.hashableItems, evictedEntry.key) // if you're keeping track of original items
}
}
// Add the value to the cache and the evictionList
newEntry := &entry[T]{
key: hashedKey,
value: value,
}
element := m.evictionList.PushFront(newEntry)
m.cache[hashedKey] = element
m.hashableItems[hashedKey] = value // if you're keeping track of original items
}
// HString is a type that implements the Hasher interface for strings.
type HString string
// Hash is a method that returns the hash of the string.
func (h HString) Hash() string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(h)))
}
// HInt is a type that implements the Hasher interface for integers.
type HInt int
// Hash is a method that returns the hash of the integer.
func (h HInt) Hash() string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%d", h))))
}
+241
View File
@@ -0,0 +1,241 @@
package memoization
import (
"encoding/binary"
"fmt"
"os"
"testing"
)
type actionType int
const (
set actionType = iota
get
)
type cacheAction struct {
actionType actionType
key HString
value interface{}
expectedValue interface{}
}
type testCase struct {
name string
capacity int
actions []cacheAction
}
func TestCache(t *testing.T) {
tests := []testCase{
{
name: "TestNewMemoCache",
capacity: 5,
actions: []cacheAction{
{actionType: get, expectedValue: nil},
},
},
{
name: "TestSetAndGet",
capacity: 10,
actions: []cacheAction{
{actionType: set, key: "key1", value: "value1"},
{actionType: get, key: "key1", expectedValue: "value1"},
{actionType: set, key: "key1", value: "newValue1"},
{actionType: get, key: "key1", expectedValue: "newValue1"},
{actionType: get, key: "nonExistentKey", expectedValue: nil},
{actionType: set, key: "nilKey", value: ""},
{actionType: get, key: "nilKey", expectedValue: ""},
{actionType: set, key: "keyA", value: "valueA"},
{actionType: set, key: "keyB", value: "valueB"},
{actionType: get, key: "keyA", expectedValue: "valueA"},
{actionType: get, key: "keyB", expectedValue: "valueB"},
},
},
{
name: "TestSetNilValue",
capacity: 10,
actions: []cacheAction{
{actionType: set, key: HString("nilKey"), value: nil},
{actionType: get, key: HString("nilKey"), expectedValue: nil},
},
},
{
name: "TestGetAfterEviction",
capacity: 2,
actions: []cacheAction{
{actionType: set, key: HString("1"), value: 1},
{actionType: set, key: HString("2"), value: 2},
{actionType: set, key: HString("3"), value: 3},
{actionType: get, key: HString("1"), expectedValue: nil},
{actionType: get, key: HString("2"), expectedValue: 2},
},
},
{
name: "TestGetAfterLRU",
capacity: 2,
actions: []cacheAction{
{actionType: set, key: HString("1"), value: 1},
{actionType: set, key: HString("2"), value: 2},
{actionType: get, key: HString("1"), expectedValue: 1},
{actionType: set, key: HString("3"), value: 3},
{actionType: get, key: HString("1"), expectedValue: 1},
{actionType: get, key: HString("3"), expectedValue: 3},
{actionType: get, key: HString("2"), expectedValue: nil},
},
},
{
name: "TestLRU_Capacity3",
capacity: 3,
actions: []cacheAction{
{actionType: set, key: HString("1"), value: 1},
{actionType: set, key: HString("2"), value: 2},
{actionType: set, key: HString("3"), value: 3},
{actionType: get, key: HString("1"), expectedValue: 1}, // Accessing key "1"
{actionType: set, key: HString("4"), value: 4}, // Should evict key "2" since "1" was recently accessed
{actionType: get, key: HString("2"), expectedValue: nil},
{actionType: get, key: HString("1"), expectedValue: 1},
{actionType: get, key: HString("3"), expectedValue: 3},
{actionType: get, key: HString("4"), expectedValue: 4},
},
},
// Test LRU behavior with varying accesses
{
name: "TestLRU_VaryingAccesses",
capacity: 3,
actions: []cacheAction{
{actionType: set, key: HString("1"), value: 1},
{actionType: set, key: HString("2"), value: 2},
{actionType: set, key: HString("3"), value: 3},
{actionType: get, key: HString("1"), expectedValue: 1}, // Accessing key "1"
{actionType: get, key: HString("2"), expectedValue: 2}, // Accessing key "2"
{actionType: set, key: HString("4"), value: 4}, // Should evict key "3"
{actionType: get, key: HString("3"), expectedValue: nil},
{actionType: get, key: HString("1"), expectedValue: 1},
{actionType: get, key: HString("2"), expectedValue: 2},
{actionType: get, key: HString("4"), expectedValue: 4},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cache := NewMemoCache[HString, interface{}](tt.capacity)
for _, action := range tt.actions {
switch action.actionType {
case set:
cache.Set(action.key, action.value)
case get:
if got, _ := cache.Get(action.key); got != action.expectedValue {
t.Errorf("Get() = %v, want %v", got, action.expectedValue)
}
}
}
})
}
}
func FuzzCache(f *testing.F) {
// Define some seed values for initial scenarios
for _, seed := range [][]byte{
[]byte("7\x010\x0000000020"),
{0, 0, 0, 0}, // Set key 0 to 0
{1, 0, 0, 1}, // Set key 0 to 1
{2, 0}, // Get key 0
} {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, in []byte) {
if len(in) < 1 {
t.Skip() // Skip the test if the input is less than 1 byte
}
cache := NewMemoCache[HInt, int](10) // Initialize a cache with the initial size
expectedValues := make(map[HInt]int) // Map to store expected key-value pairs
accessOrder := make([]HInt, 0) // Slice to store the order of keys accessed
for i := 0; i < len(in); {
opCode := in[i] % 4 // Determine the operation: Set, Get, or Reset (added case for Reset)
i++
switch opCode {
case 0, 1: // Set operation
if i+3 > len(in) {
t.Skip() // Not enough input to continue, so skip
}
key := HInt(binary.BigEndian.Uint16(in[i : i+2]))
value := int(in[i+2])
i += 3
// If the key is already in accessOrder, we remove it and append it again later
for index, accessedKey := range accessOrder {
if accessedKey == key {
accessOrder = append(accessOrder[:index], accessOrder[index+1:]...)
break
}
}
cache.Set(key, value) // Set the value in the cache
expectedValues[key] = value
accessOrder = append(accessOrder, key) // Add the key to the access order slice
// If we exceeded the cache size, we need to evict the least recently used item
if len(accessOrder) > cache.Capacity() {
evictedKey := accessOrder[0]
accessOrder = accessOrder[1:]
delete(expectedValues, evictedKey) // Remove the evicted key from expected values
}
case 2: // Get operation
if i >= len(in) {
t.Skip() // Not enough input to continue, so skip
}
key := HInt(in[i])
i++
expectedValue, ok := expectedValues[key]
if !ok {
// If the key is not found, it means it was either evicted or never added
expectedValue = 0 // The zero value, depends on your cache implementation
} else {
// If the key was accessed, move it to the end of the accessOrder to represent recent use
for index, accessedKey := range accessOrder {
if accessedKey == key {
accessOrder = append(accessOrder[:index], accessOrder[index+1:]...)
accessOrder = append(accessOrder, key)
break
}
}
}
if got, _ := cache.Get(key); got != expectedValue {
fmt.Fprintf(os.Stderr, "cache: capacity: %d, hashable: %v, cache: %v\n", cache.capacity, cache.hashableItems, cache.cache)
t.Fatalf("Get(%v) = %v, want %v", key, got, expectedValue) // The values do not match
}
case 3: // Reset operation
if i >= len(in) {
t.Skip() // Not enough input to continue, so skip
}
newCacheSize := int(in[i]) // Read the new cache size from the input
i++
if newCacheSize == 0 {
t.Skip() // If the size is zero, we skip this test
}
// Create a new cache with the specified size
cache = NewMemoCache[HInt, int](newCacheSize)
// clear and reinitialize the expected values
expectedValues = make(map[HInt]int)
accessOrder = make([]HInt, 0)
}
}
})
}
+54 -18
View File
@@ -1,6 +1,7 @@
package textarea
import (
"crypto/sha256"
"fmt"
"strings"
"unicode"
@@ -9,10 +10,12 @@ import (
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/runeutil"
"github.com/charmbracelet/bubbles/textarea/memoization"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
rw "github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
)
const (
@@ -129,11 +132,25 @@ type Style struct {
Text lipgloss.Style
}
// line is the input to the text wrapping function. This is stored in a struct
// so that it can be hashed and memoized.
type line struct {
runes []rune
width int
}
// Hash returns a hash of the line.
func (w line) Hash() string {
v := fmt.Sprintf("%s:%d", string(w.runes), w.width)
return fmt.Sprintf("%x", sha256.Sum256([]byte(v)))
}
// Model is the Bubble Tea model for this text area element.
type Model struct {
Err error
// General settings.
cache *memoization.MemoCache[line, [][]rune]
// Prompt is printed at the beginning of each line.
//
@@ -242,6 +259,7 @@ func New() Model {
style: &blurredStyle,
FocusedStyle: focusedStyle,
BlurredStyle: blurredStyle,
cache: memoization.NewMemoCache[line, [][]rune](defaultMaxHeight),
EndOfBufferCharacter: '~',
ShowLineNumbers: true,
Cursor: cur,
@@ -251,7 +269,7 @@ func New() Model {
focus: false,
col: 0,
row: 0,
lineNumberFormat: "%2v ",
lineNumberFormat: "%3v ",
viewport: &vp,
}
@@ -414,7 +432,7 @@ func (m Model) Value() string {
func (m *Model) Length() int {
var l int
for _, row := range m.value {
l += rw.StringWidth(string(row))
l += uniseg.StringWidth(string(row))
}
// We add len(m.value) to include the newline characters.
return l + len(m.value) - 1
@@ -456,7 +474,7 @@ func (m *Model) CursorDown() {
offset := 0
for offset < charOffset {
if m.col > len(m.value[m.row]) || offset >= nli.CharWidth-1 {
if m.row >= len(m.value) || m.col >= len(m.value[m.row]) || offset >= nli.CharWidth-1 {
break
}
offset += rw.RuneWidth(m.value[m.row][m.col])
@@ -768,7 +786,7 @@ func (m *Model) capitalizeRight() {
// LineInfo returns the number of characters from the start of the
// (soft-wrapped) line and the (soft-wrapped) line width.
func (m Model) LineInfo() LineInfo {
grid := wrap(m.value[m.row], m.width)
grid := m.memoizedWrap(m.value[m.row], m.width)
// Find out which line we are currently on. This can be determined by the
// m.col and counting the number of runes that we need to skip.
@@ -785,19 +803,19 @@ func (m Model) LineInfo() LineInfo {
RowOffset: i + 1,
StartColumn: m.col,
Width: len(grid[i+1]),
CharWidth: rw.StringWidth(string(line)),
CharWidth: uniseg.StringWidth(string(line)),
}
}
if counter+len(line) >= m.col {
return LineInfo{
CharOffset: rw.StringWidth(string(line[:max(0, m.col-counter)])),
CharOffset: uniseg.StringWidth(string(line[:max(0, m.col-counter)])),
ColumnOffset: m.col - counter,
Height: len(grid),
RowOffset: i,
StartColumn: counter,
Width: len(line),
CharWidth: rw.StringWidth(string(line)),
CharWidth: uniseg.StringWidth(string(line)),
}
}
@@ -854,14 +872,14 @@ func (m *Model) SetWidth(w int) {
// prompt and line numbers, we need to calculate it by subtracting.
inputWidth := w
if m.ShowLineNumbers {
inputWidth -= rw.StringWidth(fmt.Sprintf(m.lineNumberFormat, 0))
inputWidth -= uniseg.StringWidth(fmt.Sprintf(m.lineNumberFormat, 0))
}
// Account for base style borders and padding.
inputWidth -= m.style.Base.GetHorizontalFrameSize()
if m.promptFunc == nil {
m.promptWidth = rw.StringWidth(m.Prompt)
m.promptWidth = uniseg.StringWidth(m.Prompt)
}
inputWidth -= m.promptWidth
@@ -916,6 +934,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.value[m.row] = make([]rune, 0)
}
if m.MaxHeight > 0 && m.MaxHeight != m.cache.Capacity() {
m.cache = memoization.NewMemoCache[line, [][]rune](m.MaxHeight)
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
@@ -1046,7 +1068,7 @@ func (m Model) View() string {
displayLine := 0
for l, line := range m.value {
wrappedLines := wrap(line, m.width)
wrappedLines := m.memoizedWrap(line, m.width)
if m.row == l {
style = m.style.CursorLine
@@ -1068,11 +1090,15 @@ func (m Model) View() string {
s.WriteString(style.Render(m.style.LineNumber.Render(fmt.Sprintf(m.lineNumberFormat, l+1))))
}
} else {
s.WriteString(m.style.LineNumber.Render(style.Render(" ")))
if m.row == l {
s.WriteString(style.Render(m.style.CursorLineNumber.Render(fmt.Sprintf(m.lineNumberFormat, " "))))
} else {
s.WriteString(style.Render(m.style.LineNumber.Render(fmt.Sprintf(m.lineNumberFormat, " "))))
}
}
}
strwidth := rw.StringWidth(string(wrappedLine))
strwidth := uniseg.StringWidth(string(wrappedLine))
padding := m.width - strwidth
// If the trailing space causes the line to be wider than the
// width, we should not draw it to the screen since it will result
@@ -1129,7 +1155,7 @@ func (m Model) getPromptString(displayLine int) (prompt string) {
return prompt
}
prompt = m.promptFunc(displayLine)
pl := rw.StringWidth(prompt)
pl := uniseg.StringWidth(prompt)
if pl < m.promptWidth {
prompt = fmt.Sprintf("%*s%s", m.promptWidth-pl, "", prompt)
}
@@ -1157,7 +1183,7 @@ func (m Model) placeholderView() string {
s.WriteString(m.style.CursorLine.Render(m.Cursor.View()))
// The rest of the placeholder text
s.WriteString(m.style.CursorLine.Render(style.Render(p[1:] + strings.Repeat(" ", max(0, m.width-rw.StringWidth(p))))))
s.WriteString(m.style.CursorLine.Render(style.Render(p[1:] + strings.Repeat(" ", max(0, m.width-uniseg.StringWidth(p))))))
// The rest of the new lines
for i := 1; i < m.height; i++ {
@@ -1181,6 +1207,16 @@ func Blink() tea.Msg {
return cursor.Blink()
}
func (m Model) memoizedWrap(runes []rune, width int) [][]rune {
input := line{runes: runes, width: width}
if v, ok := m.cache.Get(input); ok {
return v
}
v := wrap(runes, width)
m.cache.Set(input, v)
return v
}
// cursorLineNumber returns the line number that the cursor is on.
// This accounts for soft wrapped lines.
func (m Model) cursorLineNumber() int {
@@ -1188,7 +1224,7 @@ func (m Model) cursorLineNumber() int {
for i := 0; i < m.row; i++ {
// Calculate the number of lines that the current line will be split
// into.
line += len(wrap(m.value[i], m.width))
line += len(m.memoizedWrap(m.value[i], m.width))
}
line += m.LineInfo().RowOffset
return line
@@ -1280,7 +1316,7 @@ func wrap(runes []rune, width int) [][]rune {
}
if spaces > 0 {
if rw.StringWidth(string(lines[row]))+rw.StringWidth(string(word))+spaces > width {
if uniseg.StringWidth(string(lines[row]))+uniseg.StringWidth(string(word))+spaces > width {
row++
lines = append(lines, []rune{})
lines[row] = append(lines[row], word...)
@@ -1297,7 +1333,7 @@ func wrap(runes []rune, width int) [][]rune {
// If the last character is a double-width rune, then we may not be able to add it to this line
// as it might cause us to go past the width.
lastCharLen := rw.RuneWidth(word[len(word)-1])
if rw.StringWidth(string(word))+lastCharLen > width {
if uniseg.StringWidth(string(word))+lastCharLen > width {
// If the current line has any content, let's move to the next
// line because the current word fills up the entire line.
if len(lines[row]) > 0 {
@@ -1310,7 +1346,7 @@ func wrap(runes []rune, width int) [][]rune {
}
}
if rw.StringWidth(string(lines[row]))+rw.StringWidth(string(word))+spaces >= width {
if uniseg.StringWidth(string(lines[row]))+uniseg.StringWidth(string(word))+spaces >= width {
lines = append(lines, []rune{})
lines[row+1] = append(lines[row+1], word...)
// We add an extra space at the end of the line to account for the