mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-19 00:49:54 +03:00
Compare commits
1 Commits
vim
...
go-bump-11
Author | SHA1 | Date | |
---|---|---|---|
|
8d709dc384 |
24
README.md
24
README.md
@@ -56,11 +56,7 @@ options.
|
|||||||
|
|
||||||
## Progress
|
## Progress
|
||||||
|
|
||||||
<picture>
|
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" width="800" alt="Progressbar Example">
|
||||||
<source media="(max-width: 800px)" srcset="https://stuff.charm.sh/bubbles-examples/progress.gif">
|
|
||||||
<source media="(min-width: 800px)" width="800" srcset="https://stuff.charm.sh/bubbles-examples/progress.gif">
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" alt="Progressbar Example">
|
|
||||||
</picture>
|
|
||||||
|
|
||||||
A simple, customizable progress meter, with optional animation via
|
A simple, customizable progress meter, with optional animation via
|
||||||
[Harmonica][harmonica]. Supports solid and gradient fills. The empty and filled
|
[Harmonica][harmonica]. Supports solid and gradient fills. The empty and filled
|
||||||
@@ -87,11 +83,7 @@ logic and visualize pagination however you like.
|
|||||||
|
|
||||||
## Viewport
|
## Viewport
|
||||||
|
|
||||||
<picture>
|
<img src="https://stuff.charm.sh/bubbles-examples/viewport.gif?0" width="600" alt="Viewport Example">
|
||||||
<source media="(max-width: 600px)" srcset="https://stuff.charm.sh/bubbles-examples/viewport.gif?0">
|
|
||||||
<source media="(min-width: 600px)" width="600" srcset="https://stuff.charm.sh/bubbles-examples/viewport.gif?0">
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/viewport.gif?0" alt="Viewport Example">
|
|
||||||
</picture>
|
|
||||||
|
|
||||||
A viewport for vertically scrolling content. Optionally includes standard
|
A viewport for vertically scrolling content. Optionally includes standard
|
||||||
pager keybindings and mouse wheel support. A high performance mode is available
|
pager keybindings and mouse wheel support. A high performance mode is available
|
||||||
@@ -107,11 +99,7 @@ indenting and text wrapping.
|
|||||||
|
|
||||||
## List
|
## List
|
||||||
|
|
||||||
<picture>
|
<img src="https://stuff.charm.sh/bubbles-examples/list.gif" width="600" alt="List Example">
|
||||||
<source media="(max-width: 600px)" srcset="https://stuff.charm.sh/bubbles-examples/list.gif">
|
|
||||||
<source media="(min-width: 600px)" width="600" srcset="https://stuff.charm.sh/bubbles-examples/list.gif">
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/list.gif" alt="List Example">
|
|
||||||
</picture>
|
|
||||||
|
|
||||||
A customizable, batteries-included component for browsing a set of items.
|
A customizable, batteries-included component for browsing a set of items.
|
||||||
Features pagination, fuzzy filtering, auto-generated help, an activity spinner,
|
Features pagination, fuzzy filtering, auto-generated help, an activity spinner,
|
||||||
@@ -145,11 +133,7 @@ can be customized as you see fit.
|
|||||||
|
|
||||||
## Help
|
## Help
|
||||||
|
|
||||||
<picture>
|
<img src="https://stuff.charm.sh/bubbles-examples/help.gif" width="500" alt="Help Example">
|
||||||
<source media="(max-width: 500px)" srcset="https://stuff.charm.sh/bubbles-examples/help.gif">
|
|
||||||
<source media="(min-width: 500px)" width="500" srcset="https://stuff.charm.sh/bubbles-examples/help.gif">
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/help.gif" alt="Help Example">
|
|
||||||
</picture>
|
|
||||||
|
|
||||||
A customizable horizontal mini help view that automatically generates itself
|
A customizable horizontal mini help view that automatically generates itself
|
||||||
from your keybindings. It features single and multi-line modes, which the user
|
from your keybindings. It features single and multi-line modes, which the user
|
||||||
|
18
go.mod
18
go.mod
@@ -1,16 +1,26 @@
|
|||||||
module github.com/charmbracelet/bubbles
|
module github.com/charmbracelet/bubbles
|
||||||
|
|
||||||
go 1.13
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.4
|
github.com/atotto/clipboard v0.1.4
|
||||||
github.com/charmbracelet/bubbletea v0.21.0
|
github.com/charmbracelet/bubbletea v0.22.0
|
||||||
github.com/charmbracelet/harmonica v0.2.0
|
github.com/charmbracelet/harmonica v0.2.0
|
||||||
github.com/charmbracelet/lipgloss v0.5.0
|
github.com/charmbracelet/lipgloss v0.5.0
|
||||||
github.com/kylelemons/godebug v1.1.0 // indirect
|
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0
|
github.com/lucasb-eyer/go-colorful v1.2.0
|
||||||
github.com/mattn/go-runewidth v0.0.13
|
github.com/mattn/go-runewidth v0.0.13
|
||||||
github.com/muesli/reflow v0.3.0
|
github.com/muesli/reflow v0.3.0
|
||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739
|
github.com/muesli/termenv v0.12.0
|
||||||
github.com/sahilm/fuzzy v0.1.0
|
github.com/sahilm/fuzzy v0.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/containerd/console v1.0.3 // indirect
|
||||||
|
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
|
||||||
|
github.com/muesli/cancelreader v0.2.1 // indirect
|
||||||
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||||
|
)
|
||||||
|
14
go.sum
14
go.sum
@@ -1,7 +1,7 @@
|
|||||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/charmbracelet/bubbletea v0.21.0 h1:f3y+kanzgev5PA916qxmDybSHU3N804uOnKnhRPXTcI=
|
github.com/charmbracelet/bubbletea v0.22.0 h1:E1BTNSE3iIrq0G0X6TjGAmrQ32cGCbFDPcIuImikrUc=
|
||||||
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
|
github.com/charmbracelet/bubbletea v0.22.0/go.mod h1:aoVIwlNlr5wbCB26KhxfrqAn0bMp4YpJcoOelbxApjs=
|
||||||
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
|
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
|
||||||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||||
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
|
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
|
||||||
@@ -20,19 +20,17 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
|
|||||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
|
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
|
||||||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
||||||
github.com/muesli/cancelreader v0.2.0 h1:SOpr+CfyVNce341kKqvbhhzQhBPyJRXQaCtn03Pae1Q=
|
github.com/muesli/cancelreader v0.2.1 h1:Xzd1B4U5bWQOuSKuN398MyynIGTNT89dxzpEDsalXZs=
|
||||||
github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
github.com/muesli/cancelreader v0.2.1/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
||||||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||||
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
|
|
||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc=
|
||||||
|
github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A=
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
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 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
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.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
||||||
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
||||||
|
@@ -106,7 +106,7 @@ func (b Binding) Help() Help {
|
|||||||
// keybindings won't be activated and won't show up in help. Keybindings are
|
// keybindings won't be activated and won't show up in help. Keybindings are
|
||||||
// enabled by default.
|
// enabled by default.
|
||||||
func (b Binding) Enabled() bool {
|
func (b Binding) Enabled() bool {
|
||||||
return !b.disabled && b.keys != nil
|
return !b.disabled || b.keys == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEnabled enables or disables the keybinding.
|
// SetEnabled enables or disables the keybinding.
|
||||||
@@ -130,10 +130,9 @@ type Help struct {
|
|||||||
|
|
||||||
// Matches checks if the given KeyMsg matches the given bindings.
|
// Matches checks if the given KeyMsg matches the given bindings.
|
||||||
func Matches(k tea.KeyMsg, b ...Binding) bool {
|
func Matches(k tea.KeyMsg, b ...Binding) bool {
|
||||||
keys := k.String()
|
|
||||||
for _, binding := range b {
|
for _, binding := range b {
|
||||||
for _, v := range binding.keys {
|
for _, v := range binding.keys {
|
||||||
if keys == v && binding.Enabled() {
|
if k.String() == v && binding.Enabled() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,26 +0,0 @@
|
|||||||
package key
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBinding_Enabled(t *testing.T) {
|
|
||||||
binding := NewBinding(
|
|
||||||
WithKeys("k", "up"),
|
|
||||||
WithHelp("↑/k", "move up"),
|
|
||||||
)
|
|
||||||
if !binding.Enabled() {
|
|
||||||
t.Errorf("expected key to be Enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
binding.SetEnabled(false)
|
|
||||||
if binding.Enabled() {
|
|
||||||
t.Errorf("expected key not to be Enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
binding.SetEnabled(true)
|
|
||||||
binding.Unbind()
|
|
||||||
if binding.Enabled() {
|
|
||||||
t.Errorf("expected key not to be Enabled")
|
|
||||||
}
|
|
||||||
}
|
|
387
table/table.go
387
table/table.go
@@ -1,387 +0,0 @@
|
|||||||
package table
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/key"
|
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
|
||||||
"github.com/mattn/go-runewidth"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Model defines a state for the table widget.
|
|
||||||
type Model struct {
|
|
||||||
KeyMap KeyMap
|
|
||||||
|
|
||||||
cols []Column
|
|
||||||
rows []Row
|
|
||||||
cursor int
|
|
||||||
focus bool
|
|
||||||
styles Styles
|
|
||||||
|
|
||||||
viewport viewport.Model
|
|
||||||
}
|
|
||||||
|
|
||||||
// Row represents one line in the table.
|
|
||||||
type Row []string
|
|
||||||
|
|
||||||
// Column defines the table structure.
|
|
||||||
type Column struct {
|
|
||||||
Title string
|
|
||||||
Width int
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which
|
|
||||||
// is used to render the menu menu.
|
|
||||||
type KeyMap struct {
|
|
||||||
LineUp key.Binding
|
|
||||||
LineDown key.Binding
|
|
||||||
PageUp key.Binding
|
|
||||||
PageDown key.Binding
|
|
||||||
HalfPageUp key.Binding
|
|
||||||
HalfPageDown key.Binding
|
|
||||||
GotoTop key.Binding
|
|
||||||
GotoBottom key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultKeyMap returns a default set of keybindings.
|
|
||||||
func DefaultKeyMap() KeyMap {
|
|
||||||
const spacebar = " "
|
|
||||||
return KeyMap{
|
|
||||||
LineUp: key.NewBinding(
|
|
||||||
key.WithKeys("up", "k"),
|
|
||||||
key.WithHelp("↑/k", "up"),
|
|
||||||
),
|
|
||||||
LineDown: key.NewBinding(
|
|
||||||
key.WithKeys("down", "j"),
|
|
||||||
key.WithHelp("↓/j", "down"),
|
|
||||||
),
|
|
||||||
PageUp: key.NewBinding(
|
|
||||||
key.WithKeys("b", "pgup"),
|
|
||||||
key.WithHelp("b", "page up"),
|
|
||||||
),
|
|
||||||
PageDown: key.NewBinding(
|
|
||||||
key.WithKeys("f", "pgup", spacebar),
|
|
||||||
key.WithHelp("f", "page down"),
|
|
||||||
),
|
|
||||||
HalfPageUp: key.NewBinding(
|
|
||||||
key.WithKeys("u", "ctrl+u"),
|
|
||||||
key.WithHelp("u", "½ page up"),
|
|
||||||
),
|
|
||||||
HalfPageDown: key.NewBinding(
|
|
||||||
key.WithKeys("d", "ctrl+d"),
|
|
||||||
key.WithHelp("d", "½ page down"),
|
|
||||||
),
|
|
||||||
GotoTop: key.NewBinding(
|
|
||||||
key.WithKeys("home", "g"),
|
|
||||||
key.WithHelp("g/home", "go to start"),
|
|
||||||
),
|
|
||||||
GotoBottom: key.NewBinding(
|
|
||||||
key.WithKeys("end", "G"),
|
|
||||||
key.WithHelp("G/end", "go to end"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Styles contains style definitions for this list component. By default, these
|
|
||||||
// values are generated by DefaultStyles.
|
|
||||||
type Styles struct {
|
|
||||||
Header lipgloss.Style
|
|
||||||
Cell lipgloss.Style
|
|
||||||
Selected lipgloss.Style
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultStyles returns a set of default style definitions for this table.
|
|
||||||
func DefaultStyles() Styles {
|
|
||||||
return Styles{
|
|
||||||
Selected: lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212")),
|
|
||||||
Header: lipgloss.NewStyle().Bold(true).Padding(0, 1),
|
|
||||||
Cell: lipgloss.NewStyle().Padding(0, 1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetStyles sets the table styles.
|
|
||||||
func (m *Model) SetStyles(s Styles) {
|
|
||||||
m.styles = s
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option is used to set options in New. For example:
|
|
||||||
//
|
|
||||||
// table := New(WithColumns([]Column{{Title: "ID", Width: 10}}))
|
|
||||||
//
|
|
||||||
type Option func(*Model)
|
|
||||||
|
|
||||||
// New creates a new model for the table widget.
|
|
||||||
func New(opts ...Option) Model {
|
|
||||||
m := Model{
|
|
||||||
cursor: 0,
|
|
||||||
viewport: viewport.New(0, 20),
|
|
||||||
|
|
||||||
KeyMap: DefaultKeyMap(),
|
|
||||||
styles: DefaultStyles(),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(&m)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.UpdateViewport()
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithColumns sets the table columns (headers).
|
|
||||||
func WithColumns(cols []Column) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.cols = cols
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithRows sets the table rows (data).
|
|
||||||
func WithRows(rows []Row) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.rows = rows
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithHeight sets the height of the table.
|
|
||||||
func WithHeight(h int) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.viewport.Height = h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithWidth sets the width of the table.
|
|
||||||
func WithWidth(w int) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.viewport.Width = w
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithFocused sets the focus state of the table.
|
|
||||||
func WithFocused(f bool) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.focus = f
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithStyles sets the table styles.
|
|
||||||
func WithStyles(s Styles) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.styles = s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithKeyMap sets the key map.
|
|
||||||
func WithKeyMap(km KeyMap) Option {
|
|
||||||
return func(m *Model) {
|
|
||||||
m.KeyMap = km
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update is the Bubble Tea update loop.
|
|
||||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|
||||||
if !m.focus {
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmds []tea.Cmd
|
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case tea.KeyMsg:
|
|
||||||
switch {
|
|
||||||
case key.Matches(msg, m.KeyMap.LineUp):
|
|
||||||
m.MoveUp(1)
|
|
||||||
case key.Matches(msg, m.KeyMap.LineDown):
|
|
||||||
m.MoveDown(1)
|
|
||||||
case key.Matches(msg, m.KeyMap.PageUp):
|
|
||||||
m.MoveUp(m.viewport.Height)
|
|
||||||
case key.Matches(msg, m.KeyMap.PageDown):
|
|
||||||
m.MoveDown(m.viewport.Height)
|
|
||||||
case key.Matches(msg, m.KeyMap.HalfPageUp):
|
|
||||||
m.MoveUp(m.viewport.Height / 2)
|
|
||||||
case key.Matches(msg, m.KeyMap.HalfPageDown):
|
|
||||||
m.MoveDown(m.viewport.Height / 2)
|
|
||||||
case key.Matches(msg, m.KeyMap.LineDown):
|
|
||||||
m.MoveDown(1)
|
|
||||||
case key.Matches(msg, m.KeyMap.GotoTop):
|
|
||||||
m.GotoTop()
|
|
||||||
case key.Matches(msg, m.KeyMap.GotoBottom):
|
|
||||||
m.GotoBottom()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, tea.Batch(cmds...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Focused returns the focus state of the table.
|
|
||||||
func (m Model) Focused() bool {
|
|
||||||
return m.focus
|
|
||||||
}
|
|
||||||
|
|
||||||
// Focus focusses the table, allowing the user to move around the rows and
|
|
||||||
// interact.
|
|
||||||
func (m *Model) Focus() {
|
|
||||||
m.focus = true
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blur blurs the table, preventing selection or movement.
|
|
||||||
func (m *Model) Blur() {
|
|
||||||
m.focus = false
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// View renders the component.
|
|
||||||
func (m Model) View() string {
|
|
||||||
return m.headersView() + "\n" + m.viewport.View()
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateViewport updates the list content based on the previously defined
|
|
||||||
// columns and rows.
|
|
||||||
func (m *Model) UpdateViewport() {
|
|
||||||
renderedRows := make([]string, 0, len(m.rows))
|
|
||||||
for i := range m.rows {
|
|
||||||
renderedRows = append(renderedRows, m.renderRow(i))
|
|
||||||
}
|
|
||||||
|
|
||||||
m.viewport.SetContent(
|
|
||||||
lipgloss.JoinVertical(lipgloss.Left, renderedRows...),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectedRow returns the selected row.
|
|
||||||
// You can cast it to your own implementation.
|
|
||||||
func (m Model) SelectedRow() Row {
|
|
||||||
return m.rows[m.cursor]
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetRows set a new rows state.
|
|
||||||
func (m *Model) SetRows(r []Row) {
|
|
||||||
m.rows = r
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWidth sets the width of the viewport of the table.
|
|
||||||
func (m *Model) SetWidth(w int) {
|
|
||||||
m.viewport.Width = w
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetHeight sets the height of the viewport of the table.
|
|
||||||
func (m *Model) SetHeight(h int) {
|
|
||||||
m.viewport.Height = h
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Height returns the viewport height of the table.
|
|
||||||
func (m Model) Height() int {
|
|
||||||
return m.viewport.Height
|
|
||||||
}
|
|
||||||
|
|
||||||
// Width returns the viewport width of the table.
|
|
||||||
func (m Model) Width() int {
|
|
||||||
return m.viewport.Width
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cursor returns the index of the selected row.
|
|
||||||
func (m Model) Cursor() int {
|
|
||||||
return m.cursor
|
|
||||||
}
|
|
||||||
|
|
||||||
// MoveUp moves the selection up by any number of row.
|
|
||||||
// It can not go above the first row.
|
|
||||||
func (m *Model) MoveUp(n int) {
|
|
||||||
m.cursor = clamp(m.cursor-n, 0, len(m.rows)-1)
|
|
||||||
m.UpdateViewport()
|
|
||||||
|
|
||||||
if m.cursor < m.viewport.YOffset {
|
|
||||||
m.viewport.SetYOffset(m.cursor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MoveDown moves the selection down by any number of row.
|
|
||||||
// It can not go below the last row.
|
|
||||||
func (m *Model) MoveDown(n int) {
|
|
||||||
m.cursor = clamp(m.cursor+n, 0, len(m.rows)-1)
|
|
||||||
m.UpdateViewport()
|
|
||||||
|
|
||||||
if m.cursor > (m.viewport.YOffset + (m.viewport.Height - 1)) {
|
|
||||||
m.viewport.SetYOffset(m.cursor - (m.viewport.Height - 1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GotoTop moves the selection to the first row.
|
|
||||||
func (m *Model) GotoTop() {
|
|
||||||
m.MoveUp(m.cursor)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GotoBottom moves the selection to the last row.
|
|
||||||
func (m *Model) GotoBottom() {
|
|
||||||
m.MoveDown(len(m.rows))
|
|
||||||
}
|
|
||||||
|
|
||||||
// FromValues create the table rows from a simple string. It uses `\n` by
|
|
||||||
// default for getting all the rows and the given separator for the fields on
|
|
||||||
// each row.
|
|
||||||
func (m *Model) FromValues(value, separator string) {
|
|
||||||
rows := []Row{}
|
|
||||||
for _, line := range strings.Split(value, "\n") {
|
|
||||||
r := Row{}
|
|
||||||
for _, field := range strings.Split(line, separator) {
|
|
||||||
r = append(r, field)
|
|
||||||
}
|
|
||||||
rows = append(rows, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.SetRows(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m Model) headersView() string {
|
|
||||||
var s = make([]string, len(m.cols))
|
|
||||||
for _, col := range m.cols {
|
|
||||||
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
|
|
||||||
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
|
|
||||||
s = append(s, m.styles.Header.Render(renderedCell))
|
|
||||||
}
|
|
||||||
return lipgloss.JoinHorizontal(lipgloss.Left, s...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) renderRow(rowID int) string {
|
|
||||||
var s = make([]string, len(m.cols))
|
|
||||||
for i, value := range m.rows[rowID] {
|
|
||||||
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
|
|
||||||
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
|
|
||||||
s = append(s, renderedCell)
|
|
||||||
}
|
|
||||||
|
|
||||||
row := lipgloss.JoinHorizontal(lipgloss.Left, s...)
|
|
||||||
|
|
||||||
if rowID == m.cursor {
|
|
||||||
return m.styles.Selected.Render(row)
|
|
||||||
}
|
|
||||||
|
|
||||||
return row
|
|
||||||
}
|
|
||||||
|
|
||||||
func max(a, b int) int {
|
|
||||||
if a > b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func clamp(v, low, high int) int {
|
|
||||||
return min(max(v, low), high)
|
|
||||||
}
|
|
@@ -1,54 +0,0 @@
|
|||||||
package table
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestFromValues(t *testing.T) {
|
|
||||||
input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3"
|
|
||||||
table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}}))
|
|
||||||
table.FromValues(input, ",")
|
|
||||||
|
|
||||||
if len(table.rows) != 3 {
|
|
||||||
t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows))
|
|
||||||
}
|
|
||||||
|
|
||||||
expect := []Row{
|
|
||||||
{"foo1", "bar1"},
|
|
||||||
{"foo2", "bar2"},
|
|
||||||
{"foo3", "bar3"},
|
|
||||||
}
|
|
||||||
if !deepEqual(table.rows, expect) {
|
|
||||||
t.Fatal("table rows is not equals to the input")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFromValuesWithTabSeparator(t *testing.T) {
|
|
||||||
input := "foo1.\tbar1\nfoo,bar,baz\tbar,2"
|
|
||||||
table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}}))
|
|
||||||
table.FromValues(input, "\t")
|
|
||||||
|
|
||||||
if len(table.rows) != 2 {
|
|
||||||
t.Fatalf("expect table to have 2 rows but it has %d", len(table.rows))
|
|
||||||
}
|
|
||||||
|
|
||||||
expect := []Row{
|
|
||||||
{"foo1.", "bar1"},
|
|
||||||
{"foo,bar,baz", "bar,2"},
|
|
||||||
}
|
|
||||||
if !deepEqual(table.rows, expect) {
|
|
||||||
t.Fatal("table rows is not equals to the input")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func deepEqual(a, b []Row) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i, r := range a {
|
|
||||||
for j, f := range r {
|
|
||||||
if f != b[i][j] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
@@ -1,791 +0,0 @@
|
|||||||
package textarea
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/cursor"
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
|
||||||
rw "github.com/mattn/go-runewidth"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Mode is the possible modes of the textarea for modal editing.
|
|
||||||
type Mode string
|
|
||||||
|
|
||||||
const (
|
|
||||||
// ModeNormal is the normal mode for navigating around text.
|
|
||||||
ModeNormal Mode = "normal"
|
|
||||||
// ModeInsert is the insert mode for inserting text.
|
|
||||||
ModeInsert Mode = "insert"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SetMode sets the mode of the textarea.
|
|
||||||
func (m *Model) SetMode(mode Mode) tea.Cmd {
|
|
||||||
switch mode {
|
|
||||||
case ModeInsert:
|
|
||||||
m.mode = ModeInsert
|
|
||||||
return m.Cursor.SetCursorMode(cursor.CursorBlink)
|
|
||||||
case ModeNormal:
|
|
||||||
m.mode = ModeNormal
|
|
||||||
m.col = clamp(m.col-1, 0, len(m.value[m.row]))
|
|
||||||
return m.Cursor.SetCursorMode(cursor.CursorStatic)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Action is the type of action that will be performed when the user completes
|
|
||||||
// a key combination.
|
|
||||||
type Action int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// ActionMove moves the cursor.
|
|
||||||
ActionMove Action = iota
|
|
||||||
// ActionReplace replaces text.
|
|
||||||
ActionReplace
|
|
||||||
// ActionDelete deletes text.
|
|
||||||
ActionDelete
|
|
||||||
// ActionYank yanks text.
|
|
||||||
ActionYank
|
|
||||||
// ActionChange deletes text and enters insert mode.
|
|
||||||
ActionChange
|
|
||||||
)
|
|
||||||
|
|
||||||
// Position is a (row, column) pair representing a position of the cursor or
|
|
||||||
// any character.
|
|
||||||
type Position struct {
|
|
||||||
Row int
|
|
||||||
Col int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Range is a range of characters in the text area.
|
|
||||||
type Range struct {
|
|
||||||
Start Position
|
|
||||||
End Position
|
|
||||||
}
|
|
||||||
|
|
||||||
// NormalCommand is a helper for keeping track of the various relevant information
|
|
||||||
// when performing vim motions in the textarea.
|
|
||||||
type NormalCommand struct {
|
|
||||||
// Buffer is the buffer of keys that have been press for the current
|
|
||||||
// command.
|
|
||||||
Buffer string
|
|
||||||
// Count is the number of times to replay the action. This is usually
|
|
||||||
// optional and defaults to 1.
|
|
||||||
Count int
|
|
||||||
// Action is the action to be performed.
|
|
||||||
Action Action
|
|
||||||
// Range is the range of characters to perform the action on.
|
|
||||||
Range Range
|
|
||||||
// Seeking is the type of seeking that is in progress.
|
|
||||||
Seeking SeekType
|
|
||||||
}
|
|
||||||
|
|
||||||
// SeekType are the possible types of seeking that can be in progress.
|
|
||||||
type SeekType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// SeekNone is the default seeking action.
|
|
||||||
SeekNone SeekType = iota
|
|
||||||
// SeekForwardTo is the seeking action for f.
|
|
||||||
SeekForwardTo // f
|
|
||||||
// SeekBackwardTo is the seeking action for F.
|
|
||||||
SeekBackwardTo // F
|
|
||||||
// SeekForwardUntil is the seeking action for t.
|
|
||||||
SeekForwardUntil // t
|
|
||||||
// SeekBackwardUntil is the seeking action for T.
|
|
||||||
SeekBackwardUntil // T
|
|
||||||
// SeekAround is the seeking action for a.
|
|
||||||
SeekAround // a
|
|
||||||
// SeekInner is the seeking action for i.
|
|
||||||
SeekInner // i
|
|
||||||
)
|
|
||||||
|
|
||||||
// IsSeeking returns whether the command is in the middle of seeking a range.
|
|
||||||
func (n *NormalCommand) IsSeeking() bool {
|
|
||||||
return n.Seeking != SeekNone
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsSeekingForward returns whether the seeking action is in the forward direction (f, t).
|
|
||||||
func (n *NormalCommand) IsSeekingForward() bool {
|
|
||||||
return n.Seeking == SeekForwardTo || n.Seeking == SeekForwardUntil
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsSeekingBackward returns whether the seeking action is in the backward direction (F, T).
|
|
||||||
func (n *NormalCommand) IsSeekingBackward() bool {
|
|
||||||
return n.Seeking == SeekBackwardTo || n.Seeking == SeekBackwardUntil
|
|
||||||
}
|
|
||||||
|
|
||||||
// executeMsg executes a command.
|
|
||||||
type executeMsg NormalCommand
|
|
||||||
|
|
||||||
// executeCmd implements tea.Cmd for an executeMsg.
|
|
||||||
func executeCmd(n NormalCommand) tea.Cmd {
|
|
||||||
return func() tea.Msg {
|
|
||||||
return executeMsg(n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) insertUpdate(msg tea.Msg) tea.Cmd {
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case tea.KeyMsg:
|
|
||||||
switch msg.String() {
|
|
||||||
case "esc":
|
|
||||||
return m.SetMode(ModeNormal)
|
|
||||||
case "ctrl+k":
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteAfterCursor()
|
|
||||||
case "ctrl+u":
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteBeforeCursor()
|
|
||||||
case "backspace", "ctrl+h":
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if len(m.value[m.row]) > 0 {
|
|
||||||
m.value[m.row] = append(m.value[m.row][:max(0, m.col-1)], m.value[m.row][m.col:]...)
|
|
||||||
if m.col > 0 {
|
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "delete", "ctrl+d":
|
|
||||||
if len(m.value[m.row]) > 0 && m.col < len(m.value[m.row]) {
|
|
||||||
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][m.col+1:]...)
|
|
||||||
}
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case "alt+backspace", "ctrl+w":
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteWordLeft()
|
|
||||||
case "alt+delete", "alt+d":
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteWordRight()
|
|
||||||
case "enter", "ctrl+m":
|
|
||||||
if len(m.value) >= maxHeight {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
m.splitLine(m.row, m.col)
|
|
||||||
case "end", "ctrl+e":
|
|
||||||
m.CursorEnd()
|
|
||||||
case "home", "ctrl+a":
|
|
||||||
m.CursorStart()
|
|
||||||
case "right", "ctrl+f":
|
|
||||||
if m.col < len(m.value[m.row]) {
|
|
||||||
m.SetCursor(m.col + 1)
|
|
||||||
} else {
|
|
||||||
if m.row < len(m.value)-1 {
|
|
||||||
m.row++
|
|
||||||
m.CursorStart()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "down", "ctrl+n":
|
|
||||||
m.CursorDown()
|
|
||||||
case "alt+right", "alt+f":
|
|
||||||
m.wordRight()
|
|
||||||
case "ctrl+v":
|
|
||||||
return Paste
|
|
||||||
case "left", "ctrl+b":
|
|
||||||
if m.col == 0 && m.row != 0 {
|
|
||||||
m.row--
|
|
||||||
m.CursorEnd()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if m.col > 0 {
|
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
|
||||||
case "up", "ctrl+p":
|
|
||||||
m.CursorUp()
|
|
||||||
case "alt+left", "alt+b":
|
|
||||||
m.wordLeft()
|
|
||||||
default:
|
|
||||||
if m.CharLimit > 0 && rw.StringWidth(m.Value()) >= m.CharLimit {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
m.col = min(m.col, len(m.value[m.row]))
|
|
||||||
m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...)
|
|
||||||
m.SetCursor(m.col + len(msg.Runes))
|
|
||||||
}
|
|
||||||
|
|
||||||
case pasteMsg:
|
|
||||||
m.handlePaste(string(msg))
|
|
||||||
|
|
||||||
case pasteErrMsg:
|
|
||||||
m.Err = msg
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case tea.KeyMsg:
|
|
||||||
if m.command.IsSeeking() {
|
|
||||||
return m.seekingUpdate(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.command.Action == ActionReplace {
|
|
||||||
for i := m.col; i < m.col+max(m.command.Count, 1); i++ {
|
|
||||||
if i >= len(m.value[m.row]) || len(msg.Runes) <= 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.value[m.row][i] = msg.Runes[0]
|
|
||||||
}
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.ContainsAny(msg.String(), "jk") {
|
|
||||||
m.lastCharOffset = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
switch msg.String() {
|
|
||||||
case "esc":
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return m.SetMode(ModeNormal)
|
|
||||||
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
|
|
||||||
if m.command.Count == 0 && msg.String() == "0" {
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: 0},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
}
|
|
||||||
|
|
||||||
v := m.command.Buffer + msg.String()
|
|
||||||
count, err := strconv.Atoi(v)
|
|
||||||
if err != nil {
|
|
||||||
count, _ = strconv.Atoi(msg.String())
|
|
||||||
m.command.Buffer = msg.String()
|
|
||||||
m.command.Count = count
|
|
||||||
} else {
|
|
||||||
m.command.Buffer = v
|
|
||||||
m.command.Count = count
|
|
||||||
}
|
|
||||||
case "o":
|
|
||||||
m.CursorEnd()
|
|
||||||
m.splitLine(m.row, m.col)
|
|
||||||
return m.SetMode(ModeInsert)
|
|
||||||
case "O":
|
|
||||||
m.CursorStart()
|
|
||||||
m.splitLine(m.row, m.col)
|
|
||||||
m.CursorUp()
|
|
||||||
return m.SetMode(ModeInsert)
|
|
||||||
case "G":
|
|
||||||
var row int
|
|
||||||
if m.command.Count > 0 {
|
|
||||||
row = m.command.Count - 1
|
|
||||||
} else {
|
|
||||||
row = len(m.value) - 1
|
|
||||||
}
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: row, Col: m.col},
|
|
||||||
End: Position{Row: clamp(row, 0, len(m.value)-1), Col: m.col},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "g":
|
|
||||||
if m.command.Buffer == "g" {
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: 0, Col: m.col},
|
|
||||||
End: Position{Row: 0, Col: clamp(m.col, 0, len(m.value[0]))},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
}
|
|
||||||
m.command.Buffer = "g"
|
|
||||||
case "x":
|
|
||||||
m.command.Action = ActionDelete
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: m.col + max(m.command.Count, 1)},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "X":
|
|
||||||
m.command.Action = ActionDelete
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: m.col - max(m.command.Count, 1)},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "c":
|
|
||||||
if m.command.Action == ActionChange {
|
|
||||||
m.CursorStart()
|
|
||||||
m.deleteAfterCursor()
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return m.SetMode(ModeInsert)
|
|
||||||
}
|
|
||||||
m.command.Action = ActionChange
|
|
||||||
case "d":
|
|
||||||
if m.command.Action == ActionDelete {
|
|
||||||
for i := 0; i < max(m.command.Count, 1); i++ {
|
|
||||||
m.value[m.row] = []rune{}
|
|
||||||
if m.row < len(m.value)-1 {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
} else {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
m.command.Action = ActionDelete
|
|
||||||
case "y":
|
|
||||||
m.command.Action = ActionYank
|
|
||||||
case "t", "T", "f", "F":
|
|
||||||
m.command.Buffer = msg.String()
|
|
||||||
switch msg.String() {
|
|
||||||
case "t":
|
|
||||||
m.command.Seeking = SeekForwardUntil
|
|
||||||
case "T":
|
|
||||||
m.command.Seeking = SeekBackwardUntil
|
|
||||||
case "f":
|
|
||||||
m.command.Seeking = SeekForwardTo
|
|
||||||
case "F":
|
|
||||||
m.command.Seeking = SeekBackwardTo
|
|
||||||
}
|
|
||||||
case "r":
|
|
||||||
m.command.Action = ActionReplace
|
|
||||||
case "i":
|
|
||||||
if m.command.Action != ActionMove {
|
|
||||||
m.command.Buffer = "i"
|
|
||||||
m.command.Seeking = SeekInner
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: m.col},
|
|
||||||
}
|
|
||||||
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
|
||||||
case "I":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: 0},
|
|
||||||
}
|
|
||||||
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
|
||||||
case "a":
|
|
||||||
if m.command.Action != ActionMove {
|
|
||||||
m.command.Buffer = "a"
|
|
||||||
m.command.Seeking = SeekAround
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: m.col + 1},
|
|
||||||
}
|
|
||||||
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
|
||||||
case "A":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{Row: m.row, Col: m.col},
|
|
||||||
End: Position{Row: m.row, Col: len(m.value[m.row]) + 1},
|
|
||||||
}
|
|
||||||
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
|
||||||
case "^":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: Position{m.row, 0},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "$":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: Position{m.row, len(m.value[m.row])},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "e", "E":
|
|
||||||
end := m.findWordEndRight(max(m.command.Count, 1), msg.String() == "E")
|
|
||||||
if m.command.Action == ActionDelete || m.command.Action == ActionChange {
|
|
||||||
end.Col = min(end.Col+1, len(m.value[end.Row]))
|
|
||||||
}
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: end,
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "w", "W":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: m.findWordStartRight(max(m.command.Count, 1), msg.String() == "W"),
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "b", "B":
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: m.findWordLeft(max(m.command.Count, 1), msg.String() == "B"),
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "h", "l":
|
|
||||||
direction := 1
|
|
||||||
if msg.String() == "h" {
|
|
||||||
direction = -1
|
|
||||||
}
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: Position{m.row, clamp(m.col+(direction*max(m.command.Count, 1)), 0, len(m.value[m.row]))},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "j", "k":
|
|
||||||
direction := 1
|
|
||||||
if msg.String() == "k" {
|
|
||||||
direction = -1
|
|
||||||
}
|
|
||||||
row := clamp(m.row+(direction*max(m.command.Count, 1)), 0, len(m.value)-1)
|
|
||||||
li := m.LineInfo()
|
|
||||||
charOffset := max(m.lastCharOffset, li.CharOffset)
|
|
||||||
m.lastCharOffset = charOffset
|
|
||||||
|
|
||||||
rowContent := m.value[row]
|
|
||||||
charWidth := rw.StringWidth(string(rowContent))
|
|
||||||
|
|
||||||
col := 0
|
|
||||||
offset := 0
|
|
||||||
|
|
||||||
for offset < charOffset {
|
|
||||||
if col > len(m.value[row]) || offset >= charWidth-1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
offset += rw.RuneWidth(m.value[row][col])
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: Position{row, col},
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "C":
|
|
||||||
m.deleteAfterCursor()
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return m.SetMode(ModeInsert)
|
|
||||||
case "D":
|
|
||||||
m.deleteAfterCursor()
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return nil
|
|
||||||
case "J":
|
|
||||||
m.CursorEnd()
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return nil
|
|
||||||
case "p":
|
|
||||||
return Paste
|
|
||||||
}
|
|
||||||
case executeMsg:
|
|
||||||
switch m.command.Action {
|
|
||||||
case ActionDelete:
|
|
||||||
m.deleteRange(m.command.Range)
|
|
||||||
case ActionChange:
|
|
||||||
m.deleteRange(m.command.Range)
|
|
||||||
return m.SetMode(ModeInsert)
|
|
||||||
case ActionMove:
|
|
||||||
row := clamp(m.command.Range.End.Row, 0, len(m.value)-1)
|
|
||||||
m.row = row
|
|
||||||
m.col = clamp(m.command.Range.End.Col, 0, len(m.value[row]))
|
|
||||||
}
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
|
|
||||||
case pasteMsg:
|
|
||||||
m.handlePaste(string(msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) seekingUpdate(msg tea.KeyMsg) tea.Cmd {
|
|
||||||
if len(msg.Runes) <= 0 {
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
}
|
|
||||||
if msg.Runes[0] == 'w' || msg.Runes[0] == 'W' {
|
|
||||||
m.command.Range = m.findPairRange(msg.Runes[0])
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
}
|
|
||||||
switch m.command.Buffer {
|
|
||||||
case "a":
|
|
||||||
m.command.Range = m.findPairRange(msg.Runes[0])
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "i":
|
|
||||||
pr := m.findPairRange(msg.Runes[0])
|
|
||||||
|
|
||||||
pr.Start.Col++
|
|
||||||
pr.End.Col--
|
|
||||||
|
|
||||||
m.command.Range = pr
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
case "f":
|
|
||||||
end := m.findCharRight(msg.Runes[0])
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: end,
|
|
||||||
}
|
|
||||||
case "F":
|
|
||||||
start := m.findCharLeft(msg.Runes[0])
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: start,
|
|
||||||
End: Position{m.row, m.col},
|
|
||||||
}
|
|
||||||
case "t":
|
|
||||||
end := m.findCharRight(msg.Runes[0])
|
|
||||||
end.Col--
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: end,
|
|
||||||
}
|
|
||||||
case "T":
|
|
||||||
start := m.findCharLeft(msg.Runes[0])
|
|
||||||
start.Col++
|
|
||||||
m.command.Range = Range{
|
|
||||||
Start: start,
|
|
||||||
End: Position{m.row, m.col},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return executeCmd(*m.command)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) findCharLeft(r rune) Position {
|
|
||||||
col := m.col
|
|
||||||
|
|
||||||
for col > 0 {
|
|
||||||
col--
|
|
||||||
if m.value[m.row][col] == r {
|
|
||||||
return Position{m.row, col}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Position{m.row, m.col}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) findCharRight(r rune) Position {
|
|
||||||
col := m.col
|
|
||||||
|
|
||||||
for col < len(m.value[m.row]) {
|
|
||||||
if m.value[m.row][col] == r {
|
|
||||||
return Position{m.row, col}
|
|
||||||
}
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
return Position{m.row, m.col}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) findPairRange(r rune) Range {
|
|
||||||
var startRune, endRune rune
|
|
||||||
|
|
||||||
switch r {
|
|
||||||
case 'w', 'W':
|
|
||||||
return Range{
|
|
||||||
Start: m.findWordLeft(0, r == 'W'),
|
|
||||||
End: m.findWordEndRight(1, true),
|
|
||||||
}
|
|
||||||
case '(', ')':
|
|
||||||
startRune, endRune = '(', ')'
|
|
||||||
case '{', '}':
|
|
||||||
startRune, endRune = '{', '}'
|
|
||||||
case '[', ']':
|
|
||||||
startRune, endRune = '[', ']'
|
|
||||||
case '<', '>':
|
|
||||||
startRune, endRune = '<', '>'
|
|
||||||
case '"', '\'':
|
|
||||||
startRune, endRune = r, r
|
|
||||||
default:
|
|
||||||
m.command = &NormalCommand{}
|
|
||||||
return Range{
|
|
||||||
Start: Position{m.row, m.col},
|
|
||||||
End: Position{m.row, m.col},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Range{
|
|
||||||
Start: m.findCharLeft(startRune),
|
|
||||||
End: m.findCharRight(endRune),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// findWordLeft locates the start of the word on the left of the current word.
|
|
||||||
// It takes whether or not to break words on spaces or any non-alpha-numeric
|
|
||||||
// character as an argument.
|
|
||||||
func (m *Model) findWordLeft(count int, ignorePunctuation bool) Position {
|
|
||||||
wordBreak := isSoftWordBreak
|
|
||||||
if ignorePunctuation {
|
|
||||||
wordBreak = isWordBreak
|
|
||||||
}
|
|
||||||
|
|
||||||
if count == 0 {
|
|
||||||
if wordBreak(m.value[m.row][m.col-1]) {
|
|
||||||
return Position{m.row, m.col}
|
|
||||||
}
|
|
||||||
count = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
row, col := m.row, m.col
|
|
||||||
|
|
||||||
for count > 0 {
|
|
||||||
if col <= 0 && row > 0 {
|
|
||||||
row--
|
|
||||||
col = len(m.value[row]) - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip all spaces (and punctuation) to the left of the cursor.
|
|
||||||
for col > 0 && wordBreak(m.value[row][col-1]) {
|
|
||||||
col--
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then skip all non-spaces to the left of the cursor.
|
|
||||||
for col > 0 && !wordBreak(m.value[row][col-1]) {
|
|
||||||
col--
|
|
||||||
}
|
|
||||||
|
|
||||||
count--
|
|
||||||
|
|
||||||
if row <= 0 && col <= 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Position{Row: row, Col: col}
|
|
||||||
}
|
|
||||||
|
|
||||||
// findWordStartRight locates the start of the next word. It takes whether or not to
|
|
||||||
// break words on spaces or any non-alpha-numeric character as an argument.
|
|
||||||
func (m *Model) findWordStartRight(count int, ignorePunctuation bool) Position {
|
|
||||||
wordBreak := isSoftWordBreak
|
|
||||||
if ignorePunctuation {
|
|
||||||
wordBreak = isWordBreak
|
|
||||||
}
|
|
||||||
|
|
||||||
row, col := m.row, m.col
|
|
||||||
|
|
||||||
for count > 0 {
|
|
||||||
if col >= len(m.value[row])-1 && row < len(m.value)-1 {
|
|
||||||
row++
|
|
||||||
col = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip until the start of a word is found.
|
|
||||||
for col < len(m.value[row]) && !wordBreak(m.value[row][col]) {
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
// Skip all spaces to the right of the cursor.
|
|
||||||
for col < len(m.value[row])-1 && wordBreak(m.value[row][col]) {
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
count--
|
|
||||||
|
|
||||||
if row >= len(m.value)-1 && col >= len(m.value[row])-1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Position{Row: row, Col: col}
|
|
||||||
}
|
|
||||||
|
|
||||||
// findWordEndRight locates the start of the next word. It takes whether or not to
|
|
||||||
// break words on spaces or any non-alpha-numeric character as an argument.
|
|
||||||
func (m *Model) findWordEndRight(count int, ignorePunctuation bool) Position {
|
|
||||||
wordBreak := isSoftWordBreak
|
|
||||||
if ignorePunctuation {
|
|
||||||
wordBreak = isWordBreak
|
|
||||||
}
|
|
||||||
|
|
||||||
row, col := m.row, m.col
|
|
||||||
|
|
||||||
for count > 0 {
|
|
||||||
if col > len(m.value[row]) && row < len(m.value)-1 {
|
|
||||||
row++
|
|
||||||
col = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip all spaces to the right of the cursor.
|
|
||||||
for col < len(m.value[row])-1 && wordBreak(m.value[row][col+1]) {
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then skip all non-spaces to the right of the cursor.
|
|
||||||
for col < len(m.value[row])-1 && !wordBreak(m.value[row][col+1]) {
|
|
||||||
col++
|
|
||||||
}
|
|
||||||
|
|
||||||
count--
|
|
||||||
|
|
||||||
if row <= 0 && col <= 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Position{Row: row, Col: col}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isWordBreak(char rune) bool {
|
|
||||||
return unicode.IsSpace(char)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isSoftWordBreak(char rune) bool {
|
|
||||||
return unicode.IsSpace(char) || unicode.IsPunct(char)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) deleteRange(r Range) {
|
|
||||||
if r.Start.Row == r.End.Row && r.Start.Col == r.End.Col {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
minCol, maxCol := min(r.Start.Col, r.End.Col), max(r.Start.Col, r.End.Col)
|
|
||||||
|
|
||||||
minCol = clamp(minCol, 0, len(m.value[r.Start.Row]))
|
|
||||||
maxCol = clamp(maxCol, 0, len(m.value[r.Start.Row]))
|
|
||||||
|
|
||||||
if r.Start.Row == r.End.Row {
|
|
||||||
// If the action is delete and from a seek action, we need to delete
|
|
||||||
// the range inclusively.
|
|
||||||
if m.command.IsSeeking() {
|
|
||||||
if m.command.IsSeekingForward() {
|
|
||||||
maxCol = min(maxCol+1, len(m.value[r.Start.Row]))
|
|
||||||
} else if m.command.IsSeekingBackward() {
|
|
||||||
minCol = max(minCol-1, 0)
|
|
||||||
} else if m.command.Seeking == SeekAround || m.command.Seeking == SeekInner {
|
|
||||||
maxCol = min(maxCol+1, len(m.value[r.Start.Row]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.value[r.Start.Row] = append(m.value[r.Start.Row][:minCol], m.value[r.Start.Row][maxCol:]...)
|
|
||||||
m.SetCursor(minCol)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
minRow, maxRow := min(r.Start.Row, r.End.Row), max(r.Start.Row, r.End.Row)
|
|
||||||
|
|
||||||
if maxRow-minRow == 1 {
|
|
||||||
if r.Start.Row < r.End.Row {
|
|
||||||
m.value[r.Start.Row] = append([]rune{}, m.value[r.Start.Row][:r.Start.Col]...)
|
|
||||||
m.value[r.End.Row] = append([]rune{}, m.value[r.End.Row][r.End.Col:]...)
|
|
||||||
m.mergeLineBelow(minRow)
|
|
||||||
} else {
|
|
||||||
m.value[r.Start.Row] = append([]rune{}, m.value[r.Start.Row][r.Start.Col:]...)
|
|
||||||
m.value[r.End.Row] = append([]rune{}, m.value[r.End.Row][:r.End.Col]...)
|
|
||||||
m.mergeLineAbove(maxRow)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := max(minRow, 0); i <= min(maxRow, len(m.value)-1); i++ {
|
|
||||||
m.value[i] = []rune{}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.value = append(m.value[:minRow], m.value[maxRow:]...)
|
|
||||||
|
|
||||||
m.row = clamp(0, minRow, len(m.value))
|
|
||||||
}
|
|
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/atotto/clipboard"
|
"github.com/atotto/clipboard"
|
||||||
"github.com/charmbracelet/bubbles/cursor"
|
"github.com/charmbracelet/bubbles/cursor"
|
||||||
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
@@ -27,6 +28,47 @@ const (
|
|||||||
type pasteMsg string
|
type pasteMsg string
|
||||||
type pasteErrMsg struct{ error }
|
type pasteErrMsg struct{ error }
|
||||||
|
|
||||||
|
// KeyMap is the key bindings for different actions within the textarea.
|
||||||
|
type KeyMap struct {
|
||||||
|
CharacterBackward key.Binding
|
||||||
|
CharacterForward key.Binding
|
||||||
|
DeleteAfterCursor key.Binding
|
||||||
|
DeleteBeforeCursor key.Binding
|
||||||
|
DeleteCharacterBackward key.Binding
|
||||||
|
DeleteCharacterForward key.Binding
|
||||||
|
DeleteWordBackward key.Binding
|
||||||
|
DeleteWordForward key.Binding
|
||||||
|
InsertNewline key.Binding
|
||||||
|
LineEnd key.Binding
|
||||||
|
LineNext key.Binding
|
||||||
|
LinePrevious key.Binding
|
||||||
|
LineStart key.Binding
|
||||||
|
Paste key.Binding
|
||||||
|
WordBackward key.Binding
|
||||||
|
WordForward key.Binding
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultKeyMap is the default set of key bindings for navigating and acting
|
||||||
|
// upon the textarea.
|
||||||
|
var DefaultKeyMap = KeyMap{
|
||||||
|
CharacterForward: key.NewBinding(key.WithKeys("right", "ctrl+f")),
|
||||||
|
CharacterBackward: key.NewBinding(key.WithKeys("left", "ctrl+b")),
|
||||||
|
WordForward: key.NewBinding(key.WithKeys("alt+right", "alt+f")),
|
||||||
|
WordBackward: key.NewBinding(key.WithKeys("alt+left", "alt+b")),
|
||||||
|
LineNext: key.NewBinding(key.WithKeys("down", "ctrl+n")),
|
||||||
|
LinePrevious: key.NewBinding(key.WithKeys("up", "ctrl+p")),
|
||||||
|
DeleteWordBackward: key.NewBinding(key.WithKeys("alt+backspace", "ctrl+w")),
|
||||||
|
DeleteWordForward: key.NewBinding(key.WithKeys("alt+delete", "alt+d")),
|
||||||
|
DeleteAfterCursor: key.NewBinding(key.WithKeys("ctrl+k")),
|
||||||
|
DeleteBeforeCursor: key.NewBinding(key.WithKeys("ctrl+u")),
|
||||||
|
InsertNewline: key.NewBinding(key.WithKeys("enter", "ctrl+m")),
|
||||||
|
DeleteCharacterBackward: key.NewBinding(key.WithKeys("backspace", "ctrl+h")),
|
||||||
|
DeleteCharacterForward: key.NewBinding(key.WithKeys("delete", "ctrl+d")),
|
||||||
|
LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")),
|
||||||
|
LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")),
|
||||||
|
Paste: key.NewBinding(key.WithKeys("ctrl+v")),
|
||||||
|
}
|
||||||
|
|
||||||
// LineInfo is a helper for keeping track of line information regarding
|
// LineInfo is a helper for keeping track of line information regarding
|
||||||
// soft-wrapped lines.
|
// soft-wrapped lines.
|
||||||
type LineInfo struct {
|
type LineInfo struct {
|
||||||
@@ -79,6 +121,7 @@ type Model struct {
|
|||||||
Placeholder string
|
Placeholder string
|
||||||
ShowLineNumbers bool
|
ShowLineNumbers bool
|
||||||
EndOfBufferCharacter rune
|
EndOfBufferCharacter rune
|
||||||
|
KeyMap KeyMap
|
||||||
|
|
||||||
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
|
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
|
||||||
// focused and blurred states.
|
// focused and blurred states.
|
||||||
@@ -119,12 +162,6 @@ type Model struct {
|
|||||||
// Cursor row.
|
// Cursor row.
|
||||||
row int
|
row int
|
||||||
|
|
||||||
// mode is the current mode of the textarea.
|
|
||||||
mode Mode
|
|
||||||
|
|
||||||
// command is the normal command of the textarea.
|
|
||||||
command *NormalCommand
|
|
||||||
|
|
||||||
// Last character offset, used to maintain state when the cursor is moved
|
// Last character offset, used to maintain state when the cursor is moved
|
||||||
// vertically such that we can maintain the same navigating position.
|
// vertically such that we can maintain the same navigating position.
|
||||||
lastCharOffset int
|
lastCharOffset int
|
||||||
@@ -154,14 +191,13 @@ func New() Model {
|
|||||||
EndOfBufferCharacter: '~',
|
EndOfBufferCharacter: '~',
|
||||||
ShowLineNumbers: true,
|
ShowLineNumbers: true,
|
||||||
Cursor: cur,
|
Cursor: cur,
|
||||||
|
KeyMap: DefaultKeyMap,
|
||||||
|
|
||||||
value: make([][]rune, minHeight, maxHeight),
|
value: make([][]rune, minHeight, maxWidth),
|
||||||
mode: ModeInsert,
|
|
||||||
focus: false,
|
focus: false,
|
||||||
col: 0,
|
col: 0,
|
||||||
row: 0,
|
row: 0,
|
||||||
lineNumberFormat: "%2v ",
|
lineNumberFormat: "%2v ",
|
||||||
command: &NormalCommand{},
|
|
||||||
|
|
||||||
viewport: &vp,
|
viewport: &vp,
|
||||||
}
|
}
|
||||||
@@ -181,7 +217,7 @@ func DefaultStyles() (Style, Style) {
|
|||||||
CursorLineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "240"}),
|
CursorLineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "240"}),
|
||||||
EndOfBuffer: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "254", Dark: "0"}),
|
EndOfBuffer: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "254", Dark: "0"}),
|
||||||
LineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
LineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
||||||
Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
||||||
Prompt: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
Prompt: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
||||||
Text: lipgloss.NewStyle(),
|
Text: lipgloss.NewStyle(),
|
||||||
}
|
}
|
||||||
@@ -191,7 +227,7 @@ func DefaultStyles() (Style, Style) {
|
|||||||
CursorLineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
CursorLineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
||||||
EndOfBuffer: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "254", Dark: "0"}),
|
EndOfBuffer: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "254", Dark: "0"}),
|
||||||
LineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
LineNumber: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "249", Dark: "7"}),
|
||||||
Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
||||||
Prompt: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
Prompt: lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
|
||||||
Text: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "245", Dark: "7"}),
|
Text: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "245", Dark: "7"}),
|
||||||
}
|
}
|
||||||
@@ -235,13 +271,13 @@ func (m Model) Value() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var v strings.Builder
|
var v string
|
||||||
for _, l := range m.value {
|
for _, l := range m.value {
|
||||||
v.WriteString(string(l))
|
v += string(l)
|
||||||
v.WriteByte('\n')
|
v += "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSuffix(v.String(), "\n")
|
return strings.TrimSuffix(v, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Length returns the number of characters currently in the text input.
|
// Length returns the number of characters currently in the text input.
|
||||||
@@ -250,12 +286,7 @@ func (m *Model) Length() int {
|
|||||||
for _, row := range m.value {
|
for _, row := range m.value {
|
||||||
l += rw.StringWidth(string(row))
|
l += rw.StringWidth(string(row))
|
||||||
}
|
}
|
||||||
return l + len(m.value) - 1
|
return l
|
||||||
}
|
|
||||||
|
|
||||||
// LineCount returns the number of lines that are currently in the text input.
|
|
||||||
func (m *Model) LineCount() int {
|
|
||||||
return len(m.value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Line returns the line position.
|
// Line returns the line position.
|
||||||
@@ -263,11 +294,6 @@ func (m Model) Line() int {
|
|||||||
return m.row
|
return m.row
|
||||||
}
|
}
|
||||||
|
|
||||||
// NormalCommand returns the normal command.
|
|
||||||
func (m *Model) NormalCommand() *NormalCommand {
|
|
||||||
return m.command
|
|
||||||
}
|
|
||||||
|
|
||||||
// CursorDown moves the cursor down by one line.
|
// CursorDown moves the cursor down by one line.
|
||||||
// Returns whether or not the cursor blink should be reset.
|
// Returns whether or not the cursor blink should be reset.
|
||||||
func (m *Model) CursorDown() {
|
func (m *Model) CursorDown() {
|
||||||
@@ -378,7 +404,7 @@ func (m *Model) Blur() {
|
|||||||
|
|
||||||
// Reset sets the input to its default state with no input.
|
// Reset sets the input to its default state with no input.
|
||||||
func (m *Model) Reset() {
|
func (m *Model) Reset() {
|
||||||
m.value = make([][]rune, minHeight, maxHeight)
|
m.value = make([][]rune, minHeight, maxWidth)
|
||||||
m.col = 0
|
m.col = 0
|
||||||
m.row = 0
|
m.row = 0
|
||||||
m.viewport.GotoTop()
|
m.viewport.GotoTop()
|
||||||
@@ -682,21 +708,109 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
m.value[m.row] = make([]rune, 0)
|
m.value[m.row] = make([]rune, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmd tea.Cmd
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch {
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteAfterCursor):
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteAfterCursor()
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteBeforeCursor):
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteBeforeCursor()
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteCharacterBackward):
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if len(m.value[m.row]) > 0 {
|
||||||
|
m.value[m.row] = append(m.value[m.row][:max(0, m.col-1)], m.value[m.row][m.col:]...)
|
||||||
|
if m.col > 0 {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteCharacterForward):
|
||||||
|
if len(m.value[m.row]) > 0 && m.col < len(m.value[m.row]) {
|
||||||
|
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][m.col+1:]...)
|
||||||
|
}
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteWordBackward):
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteWordLeft()
|
||||||
|
case key.Matches(msg, m.KeyMap.DeleteWordForward):
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteWordRight()
|
||||||
|
case key.Matches(msg, m.KeyMap.InsertNewline):
|
||||||
|
if len(m.value) >= maxHeight {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
m.splitLine(m.row, m.col)
|
||||||
|
case key.Matches(msg, m.KeyMap.LineEnd):
|
||||||
|
m.CursorEnd()
|
||||||
|
case key.Matches(msg, m.KeyMap.LineStart):
|
||||||
|
m.CursorStart()
|
||||||
|
case key.Matches(msg, m.KeyMap.CharacterForward):
|
||||||
|
if m.col < len(m.value[m.row]) {
|
||||||
|
m.SetCursor(m.col + 1)
|
||||||
|
} else {
|
||||||
|
if m.row < len(m.value)-1 {
|
||||||
|
m.row++
|
||||||
|
m.CursorStart()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case key.Matches(msg, m.KeyMap.LineNext):
|
||||||
|
m.CursorDown()
|
||||||
|
case key.Matches(msg, m.KeyMap.WordForward):
|
||||||
|
m.wordRight()
|
||||||
|
case key.Matches(msg, m.KeyMap.Paste):
|
||||||
|
return m, Paste
|
||||||
|
case key.Matches(msg, m.KeyMap.CharacterBackward):
|
||||||
|
if m.col == 0 && m.row != 0 {
|
||||||
|
m.row--
|
||||||
|
m.CursorEnd()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if m.col > 0 {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
}
|
||||||
|
case key.Matches(msg, m.KeyMap.LinePrevious):
|
||||||
|
m.CursorUp()
|
||||||
|
case key.Matches(msg, m.KeyMap.WordBackward):
|
||||||
|
m.wordLeft()
|
||||||
|
default:
|
||||||
|
if rw.StringWidth(m.Value()) >= m.CharLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
switch m.mode {
|
m.col = min(m.col, len(m.value[m.row]))
|
||||||
case ModeInsert:
|
m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...)
|
||||||
cmd = m.insertUpdate(msg)
|
m.SetCursor(m.col + len(msg.Runes))
|
||||||
cmds = append(cmds, cmd)
|
|
||||||
// Sometimes, we need to enter insert mode after executing a command.
|
|
||||||
switch msg.(type) {
|
|
||||||
case executeMsg:
|
|
||||||
cmd = m.normalUpdate(msg)
|
|
||||||
cmds = append(cmds, cmd)
|
|
||||||
}
|
}
|
||||||
case ModeNormal:
|
|
||||||
cmd = m.normalUpdate(msg)
|
case pasteMsg:
|
||||||
cmds = append(cmds, cmd)
|
m.handlePaste(string(msg))
|
||||||
|
|
||||||
|
case pasteErrMsg:
|
||||||
|
m.Err = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
vp, cmd := m.viewport.Update(msg)
|
vp, cmd := m.viewport.Update(msg)
|
||||||
@@ -705,7 +819,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
|
|
||||||
newRow, newCol := m.cursorLineNumber(), m.col
|
newRow, newCol := m.cursorLineNumber(), m.col
|
||||||
m.Cursor, cmd = m.Cursor.Update(msg)
|
m.Cursor, cmd = m.Cursor.Update(msg)
|
||||||
if m.mode == ModeInsert && (newRow != oldRow || newCol != oldCol) {
|
if newRow != oldRow || newCol != oldCol {
|
||||||
m.Cursor.Blink = false
|
m.Cursor.Blink = false
|
||||||
cmd = m.Cursor.BlinkCmd()
|
cmd = m.Cursor.BlinkCmd()
|
||||||
}
|
}
|
||||||
|
@@ -611,7 +611,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case tea.KeyBackspace, tea.KeyCtrlH: // delete character before cursor
|
case tea.KeyBackspace: // delete character before cursor
|
||||||
m.Err = nil
|
m.Err = nil
|
||||||
|
|
||||||
if msg.Alt {
|
if msg.Alt {
|
||||||
|
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// New returns a new model with the given width and height as well as default
|
// New returns a new model with the given width and height as well as default
|
||||||
// key mappings.
|
// keymappings.
|
||||||
func New(width, height int) (m Model) {
|
func New(width, height int) (m Model) {
|
||||||
m.Width = width
|
m.Width = width
|
||||||
m.Height = height
|
m.Height = height
|
||||||
@@ -237,7 +237,7 @@ func Sync(m Model) tea.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ViewDown is a high performance command that moves the viewport up by a given
|
// ViewDown is a high performance command that moves the viewport up by a given
|
||||||
// number of lines. Use Model.ViewDown to get the lines that should be rendered.
|
// numer of lines. Use Model.ViewDown to get the lines that should be rendered.
|
||||||
// For example:
|
// For example:
|
||||||
//
|
//
|
||||||
// lines := model.ViewDown(1)
|
// lines := model.ViewDown(1)
|
||||||
@@ -344,10 +344,10 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
func (m Model) View() string {
|
func (m Model) View() string {
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
// Just send newlines since we're going to be rendering the actual
|
// Just send newlines since we're going to be rendering the actual
|
||||||
// content separately. We still need to send something that equals the
|
// content seprately. We still need to send something that equals the
|
||||||
// height of this view so that the Bubble Tea standard renderer can
|
// height of this view so that the Bubble Tea standard renderer can
|
||||||
// position anything below this view properly.
|
// position anything below this view properly.
|
||||||
return strings.Repeat("\n", max(0, m.Height-1))
|
return strings.Repeat("\n", m.Height-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := m.visibleLines()
|
lines := m.visibleLines()
|
||||||
|
Reference in New Issue
Block a user