mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-19 00:49:54 +03:00
Compare commits
46 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c8f4855d20 | ||
|
43d43d14ae | ||
|
de359c53bb | ||
|
c7d69b61f0 | ||
|
11f56f9b6b | ||
|
3be2d0585b | ||
|
37a85afcd1 | ||
|
f9c79eef64 | ||
|
c303de1e85 | ||
|
7e5ef42924 | ||
|
79ca4d09d1 | ||
|
f48e53556a | ||
|
5d6d8cb0fb | ||
|
47b8d9c6a8 | ||
|
b78277e7ec | ||
|
6a768905a6 | ||
|
0f06d78b92 | ||
|
1e16eca939 | ||
|
d81e5713d4 | ||
|
b10dbcb4dd | ||
|
a2e0a2e72e | ||
|
3dea7d036e | ||
|
68e87e08c5 | ||
|
65a17d039e | ||
|
38d59517fb | ||
|
06109f45ce | ||
|
9cb8e8d90a | ||
|
e6219572e5 | ||
|
81feaacf5b | ||
|
9a25d8b8b9 | ||
|
e8e052c64b | ||
|
3d7cd43046 | ||
|
9c38e101d2 | ||
|
84f7b047bb | ||
|
9e1e435bba | ||
|
0fd072ddcc | ||
|
a07ab1d6af | ||
|
8e49ee609e | ||
|
d02641f6b5 | ||
|
9b47f26bdd | ||
|
9c780011ff | ||
|
8148e61443 | ||
|
ce7d8da084 | ||
|
d14fdf585c | ||
|
bf7719e6c1 | ||
|
1b530b293c |
20
README.md
20
README.md
@@ -1,6 +1,10 @@
|
|||||||
Bubbles
|
Bubbles
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<img src="https://stuff.charm.sh/bubbles/bubbles-github.png" width="233" alt="The Bubbles Logo">
|
||||||
|
</p>
|
||||||
|
|
||||||
[](https://github.com/charmbracelet/bubbles/releases)
|
[](https://github.com/charmbracelet/bubbles/releases)
|
||||||
[](https://pkg.go.dev/github.com/charmbracelet/bubbles)
|
[](https://pkg.go.dev/github.com/charmbracelet/bubbles)
|
||||||
[](https://github.com/charmbracelet/bubbles/actions)
|
[](https://github.com/charmbracelet/bubbles/actions)
|
||||||
@@ -36,6 +40,17 @@ the common, and many customization options.
|
|||||||
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
|
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
|
||||||
|
|
||||||
|
|
||||||
|
## Progress
|
||||||
|
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" width="800" alt="Progressbar Example">
|
||||||
|
|
||||||
|
A simple, customizable progress meter. Supports solid and gradient fills. The
|
||||||
|
empty and filled runes can be set to whatever you'd like. The percentage readout
|
||||||
|
is customizable and can also be omitted entirely.
|
||||||
|
|
||||||
|
* [Example code](https://github.com/charmbracelet/bubbletea/blob/master/examples/progress/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Paginator
|
## Paginator
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example">
|
<img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example">
|
||||||
@@ -55,7 +70,7 @@ browse SSH keys.
|
|||||||
|
|
||||||
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
|
||||||
for applications which make use of the alterate screen buffer.
|
for applications which make use of the alternate screen buffer.
|
||||||
|
|
||||||
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
||||||
|
|
||||||
@@ -74,7 +89,6 @@ indenting and text wrapping.
|
|||||||
|
|
||||||
Part of [Charm](https://charm.sh).
|
Part of [Charm](https://charm.sh).
|
||||||
|
|
||||||
<a href="https://charm.sh/"><img alt="the Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>
|
<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>
|
||||||
|
|
||||||
Charm热爱开源! / Charm loves open source!
|
Charm热爱开源! / Charm loves open source!
|
||||||
|
|
||||||
|
4
go.mod
4
go.mod
@@ -4,8 +4,10 @@ go 1.13
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.2
|
github.com/atotto/clipboard v0.1.2
|
||||||
github.com/charmbracelet/bubbletea v0.12.1
|
github.com/charmbracelet/bubbletea v0.12.2
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.0.3
|
||||||
github.com/mattn/go-runewidth v0.0.9
|
github.com/mattn/go-runewidth v0.0.9
|
||||||
|
github.com/muesli/reflow v0.2.0
|
||||||
github.com/muesli/termenv v0.7.4
|
github.com/muesli/termenv v0.7.4
|
||||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
|
||||||
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
|
||||||
|
6
go.sum
6
go.sum
@@ -1,7 +1,7 @@
|
|||||||
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
||||||
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/charmbracelet/bubbletea v0.12.1 h1:t21pkG2IDBRduPbt2J64Dx5yt8yIidAkXwhhrc11SzY=
|
github.com/charmbracelet/bubbletea v0.12.2 h1:y9Yo2Pv8tcm3mAJsWONGsmHhzrbNxJVxpVtemikxE9A=
|
||||||
github.com/charmbracelet/bubbletea v0.12.1/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg=
|
github.com/charmbracelet/bubbletea v0.12.2/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg=
|
||||||
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
|
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
|
||||||
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
|
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
|
||||||
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
||||||
@@ -12,6 +12,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
|
|||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
|
github.com/muesli/reflow v0.2.0 h1:2o0UBJPHHH4fa2GCXU4Rg4DwOtWPMekCeyc5EWbAQp0=
|
||||||
|
github.com/muesli/reflow v0.2.0/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
|
||||||
github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw=
|
github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw=
|
||||||
github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8=
|
github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8=
|
||||||
github.com/muesli/termenv v0.7.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
|
github.com/muesli/termenv v0.7.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
|
||||||
|
@@ -19,7 +19,7 @@ const (
|
|||||||
Dots
|
Dots
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model is the Tea model for this user interface.
|
// Model is the Bubble Tea model for this user interface.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Type Type
|
Type Type
|
||||||
Page int
|
Page int
|
||||||
@@ -115,7 +115,7 @@ func NewModel() Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Tea update function which binds keystrokes to pagination.
|
// Update is the Tea update function which binds keystrokes to pagination.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
if m.UsePgUpPgDownKeys {
|
if m.UsePgUpPgDownKeys {
|
||||||
@@ -164,16 +164,16 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// View renders the pagination to a string.
|
// View renders the pagination to a string.
|
||||||
func View(m Model) string {
|
func (m Model) View() string {
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case Dots:
|
case Dots:
|
||||||
return dotsView(m)
|
return m.dotsView()
|
||||||
default:
|
default:
|
||||||
return arabicView(m)
|
return m.arabicView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dotsView(m Model) string {
|
func (m Model) dotsView() string {
|
||||||
var s string
|
var s string
|
||||||
for i := 0; i < m.TotalPages; i++ {
|
for i := 0; i < m.TotalPages; i++ {
|
||||||
if i == m.Page {
|
if i == m.Page {
|
||||||
@@ -185,7 +185,7 @@ func dotsView(m Model) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func arabicView(m Model) string {
|
func (m Model) arabicView() string {
|
||||||
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
192
progress/progress.go
Normal file
192
progress/progress.go
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
package progress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/lucasb-eyer/go-colorful"
|
||||||
|
"github.com/muesli/reflow/ansi"
|
||||||
|
"github.com/muesli/termenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultWidth = 40
|
||||||
|
|
||||||
|
var color func(string) termenv.Color = termenv.ColorProfile().Color
|
||||||
|
|
||||||
|
// Option is used to set options in NewModel. For example:
|
||||||
|
//
|
||||||
|
// progress := NewModel(
|
||||||
|
// WithRamp("#ff0000", "#0000ff"),
|
||||||
|
// WithoutPercentage(),
|
||||||
|
// )
|
||||||
|
type Option func(*Model) error
|
||||||
|
|
||||||
|
// WithDefaultGradient sets a gradient fill with default colors.
|
||||||
|
func WithDefaultGradient() Option {
|
||||||
|
return WithGradient("#5A56E0", "#EE6FF8")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithGradient sets a gradient fill blending between two colors.
|
||||||
|
func WithGradient(colorA, colorB string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
return m.setRamp(colorA, colorB, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDefaultScaledGradient sets a gradient with default colors, and scales the
|
||||||
|
// gradient to fit the filled portion of the ramp.
|
||||||
|
func WithDefaultScaledGradient() Option {
|
||||||
|
return WithScaledGradient("#5A56E0", "#EE6FF8")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithScaledGradient scales the gradient to fit the width of the filled portion of
|
||||||
|
// the progress bar.
|
||||||
|
func WithScaledGradient(colorA, colorB string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
return m.setRamp(colorA, colorB, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithSolidFill sets the progress to use a solid fill with the given color.
|
||||||
|
func WithSolidFill(color string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.FullColor = color
|
||||||
|
m.useRamp = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithoutPercentage hides the numeric percentage.
|
||||||
|
func WithoutPercentage() Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.ShowPercentage = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWidth sets the initial width of the progress bar. Note that you can also
|
||||||
|
// set the width via the Width property, which can come in handy if you're
|
||||||
|
// waiting for a tea.WindowSizeMsg.
|
||||||
|
func WithWidth(w int) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.Width = w
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Model stores values we'll use when rendering the progress bar.
|
||||||
|
type Model struct {
|
||||||
|
|
||||||
|
// Total width of the progress bar, including percentage, if set.
|
||||||
|
Width int
|
||||||
|
|
||||||
|
// "Filled" sections of the progress bar
|
||||||
|
Full rune
|
||||||
|
FullColor string
|
||||||
|
|
||||||
|
// "Empty" sections of progress bar
|
||||||
|
Empty rune
|
||||||
|
EmptyColor string
|
||||||
|
|
||||||
|
// Settings for rendering the numeric percentage
|
||||||
|
ShowPercentage bool
|
||||||
|
PercentFormat string // a fmt string for a float
|
||||||
|
PercentageStyle *termenv.Style
|
||||||
|
|
||||||
|
useRamp bool
|
||||||
|
rampColorA colorful.Color
|
||||||
|
rampColorB colorful.Color
|
||||||
|
|
||||||
|
// When true, we scale the gradient to fit the width of the filled section
|
||||||
|
// of the progress bar. When false, the width of the gradient will be set
|
||||||
|
// to the full width of the progress bar.
|
||||||
|
scaleRamp bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewModel returns a model with default values.
|
||||||
|
func NewModel(opts ...Option) (*Model, error) {
|
||||||
|
m := &Model{
|
||||||
|
Width: defaultWidth,
|
||||||
|
Full: '█',
|
||||||
|
FullColor: "#7571F9",
|
||||||
|
Empty: '░',
|
||||||
|
EmptyColor: "#606060",
|
||||||
|
ShowPercentage: true,
|
||||||
|
PercentFormat: " %3.0f%%",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// View renders the progress bar as a given percentage.
|
||||||
|
func (m Model) View(percent float64) string {
|
||||||
|
b := strings.Builder{}
|
||||||
|
if m.ShowPercentage {
|
||||||
|
percentage := fmt.Sprintf(m.PercentFormat, percent*100)
|
||||||
|
if m.PercentageStyle != nil {
|
||||||
|
percentage = m.PercentageStyle.Styled(percentage)
|
||||||
|
}
|
||||||
|
m.bar(&b, percent, ansi.PrintableRuneWidth(percentage))
|
||||||
|
b.WriteString(percentage)
|
||||||
|
} else {
|
||||||
|
m.bar(&b, percent, 0)
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) bar(b *strings.Builder, percent float64, textWidth int) {
|
||||||
|
var (
|
||||||
|
tw = m.Width - textWidth // total width
|
||||||
|
fw = int(float64(tw) * percent) // filled width
|
||||||
|
p float64
|
||||||
|
)
|
||||||
|
|
||||||
|
if m.useRamp {
|
||||||
|
// Gradient fill
|
||||||
|
for i := 0; i < fw; i++ {
|
||||||
|
if m.scaleRamp {
|
||||||
|
p = float64(i) / float64(fw)
|
||||||
|
} else {
|
||||||
|
p = float64(i) / float64(tw)
|
||||||
|
}
|
||||||
|
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
|
||||||
|
b.WriteString(termenv.
|
||||||
|
String(string(m.Full)).
|
||||||
|
Foreground(color(c)).
|
||||||
|
String(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Solid fill
|
||||||
|
s := termenv.String(string(m.Full)).Foreground(color(m.FullColor)).String()
|
||||||
|
b.WriteString(strings.Repeat(s, fw))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty fill
|
||||||
|
e := termenv.String(string(m.Empty)).Foreground(color(m.EmptyColor)).String()
|
||||||
|
b.WriteString(strings.Repeat(e, tw-fw))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) setRamp(colorA, colorB string, scaled bool) error {
|
||||||
|
a, err := colorful.Hex(colorA)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := colorful.Hex(colorB)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.useRamp = true
|
||||||
|
m.scaleRamp = scaled
|
||||||
|
m.rampColorA = a
|
||||||
|
m.rampColorB = b
|
||||||
|
return nil
|
||||||
|
}
|
@@ -1,23 +1,60 @@
|
|||||||
package spinner
|
package spinner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/muesli/reflow/ansi"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const defaultFPS = time.Second / 10
|
||||||
defaultFPS = time.Second / 10
|
|
||||||
)
|
|
||||||
|
|
||||||
// Spinner is a set of frames used in animating the spinner.
|
// Spinner is a set of frames used in animating the spinner.
|
||||||
type Spinner = []string
|
type Spinner struct {
|
||||||
|
Frames []string
|
||||||
|
FPS time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Some spinners to choose from. You could also make your own.
|
// Some spinners to choose from. You could also make your own.
|
||||||
Line = Spinner([]string{"|", "/", "-", "\\"})
|
Line = Spinner{
|
||||||
Dot = Spinner([]string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "})
|
Frames: []string{"|", "/", "-", "\\"},
|
||||||
|
FPS: time.Second / 10,
|
||||||
|
}
|
||||||
|
Dot = Spinner{
|
||||||
|
Frames: []string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
||||||
|
FPS: time.Second / 10,
|
||||||
|
}
|
||||||
|
MiniDot = Spinner{
|
||||||
|
Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
|
||||||
|
FPS: time.Second / 12,
|
||||||
|
}
|
||||||
|
Jump = Spinner{
|
||||||
|
Frames: []string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
|
||||||
|
FPS: time.Second / 10,
|
||||||
|
}
|
||||||
|
Pulse = Spinner{
|
||||||
|
Frames: []string{"█", "▓", "▒", "░"},
|
||||||
|
FPS: time.Second / 8,
|
||||||
|
}
|
||||||
|
Points = Spinner{
|
||||||
|
Frames: []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●"},
|
||||||
|
FPS: time.Second / 7,
|
||||||
|
}
|
||||||
|
Globe = Spinner{
|
||||||
|
Frames: []string{"🌍", "🌎", "🌏"},
|
||||||
|
FPS: time.Second / 4,
|
||||||
|
}
|
||||||
|
Moon = Spinner{
|
||||||
|
Frames: []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"},
|
||||||
|
FPS: time.Second / 8,
|
||||||
|
}
|
||||||
|
Monkey = Spinner{
|
||||||
|
Frames: []string{"🙈", "🙉", "🙊"},
|
||||||
|
FPS: time.Second / 3,
|
||||||
|
}
|
||||||
|
|
||||||
color = termenv.ColorProfile().Color
|
color = termenv.ColorProfile().Color
|
||||||
)
|
)
|
||||||
@@ -26,22 +63,19 @@ var (
|
|||||||
// rather than using Model as a struct literal.
|
// rather than using Model as a struct literal.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
|
|
||||||
// Type is the set of frames to use. See Spinner.
|
// Spinner settings to use. See type Spinner.
|
||||||
Frames Spinner
|
Spinner Spinner
|
||||||
|
|
||||||
// FPS is the speed at which the ticker should tick.
|
// ForegroundColor sets the background color of the spinner. It can be
|
||||||
FPS time.Duration
|
// a hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
||||||
|
// support the color specified it will automatically degrade (per
|
||||||
// ForegroundColor sets the background color of the spinner. It can be a
|
// github.com/muesli/termenv).
|
||||||
// hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
|
||||||
// doesn't support the color specified it will automatically degrade
|
|
||||||
// (per github.com/muesli/termenv).
|
|
||||||
ForegroundColor string
|
ForegroundColor string
|
||||||
|
|
||||||
// BackgroundColor sets the background color of the spinner. It can be a
|
// BackgroundColor sets the background color of the spinner. It can be
|
||||||
// hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
// a hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
||||||
// doesn't support the color specified it will automatically degrade
|
// support the color specified it will automatically degrade (per
|
||||||
// (per github.com/muesli/termenv).
|
// github.com/muesli/termenv).
|
||||||
BackgroundColor string
|
BackgroundColor string
|
||||||
|
|
||||||
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
||||||
@@ -67,17 +101,49 @@ type Model struct {
|
|||||||
|
|
||||||
frame int
|
frame int
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
|
tag int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start resets resets the spinner start time. For use with MinimumLifetime and
|
// Start resets resets the spinner start time. For use with MinimumLifetime and
|
||||||
// MinimumStartTime. Optional.
|
// MinimumStartTime. Optional.
|
||||||
//
|
//
|
||||||
|
// This function is optional and generally considered for advanced use only.
|
||||||
|
// Most of the time your application logic will obviate the need for this
|
||||||
|
// method.
|
||||||
|
//
|
||||||
// This is considered experimental and may not appear in future versions of
|
// This is considered experimental and may not appear in future versions of
|
||||||
// this library.
|
// this library.
|
||||||
func (m *Model) Start() {
|
func (m *Model) Start() {
|
||||||
m.startTime = time.Now()
|
m.startTime = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finish sets the internal timer to a completed state so long as the spinner
|
||||||
|
// isn't flagged to be showing. If it is showing, finish has no effect. The
|
||||||
|
// idea here is that you call Finish if your operation has completed and, if
|
||||||
|
// the spinner isn't showing yet (by virtue of HideFor) then Visible() doesn't
|
||||||
|
// show the spinner at all.
|
||||||
|
//
|
||||||
|
// This is intended to work in conjunction with MinimumLifetime and
|
||||||
|
// MinimumStartTime, is completely optional.
|
||||||
|
//
|
||||||
|
// This function is optional and generally considered for advanced use only.
|
||||||
|
// Most of the time your application logic will obviate the need for this
|
||||||
|
// method.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
func (m *Model) Finish() {
|
||||||
|
if m.hidden() {
|
||||||
|
m.startTime = time.Time{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// advancedMode returns whether or not the user is making use of HideFor and
|
||||||
|
// MinimumLifetime properties.
|
||||||
|
func (m Model) advancedMode() bool {
|
||||||
|
return m.HideFor > 0 && m.MinimumLifetime > 0
|
||||||
|
}
|
||||||
|
|
||||||
// hidden returns whether or not Model.HideFor is in effect.
|
// hidden returns whether or not Model.HideFor is in effect.
|
||||||
func (m Model) hidden() bool {
|
func (m Model) hidden() bool {
|
||||||
if m.startTime.IsZero() {
|
if m.startTime.IsZero() {
|
||||||
@@ -89,12 +155,10 @@ func (m Model) hidden() bool {
|
|||||||
return m.startTime.Add(m.HideFor).After(time.Now())
|
return m.startTime.Add(m.HideFor).After(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// finished returns whether Model.MinimumLifetimeReached has been met.
|
// finished returns whether the minimum lifetime of this spinner has been
|
||||||
|
// exceeded.
|
||||||
func (m Model) finished() bool {
|
func (m Model) finished() bool {
|
||||||
if m.startTime.IsZero() {
|
if m.startTime.IsZero() || m.MinimumLifetime == 0 {
|
||||||
return true
|
|
||||||
}
|
|
||||||
if m.MinimumLifetime == 0 {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now())
|
return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now())
|
||||||
@@ -102,13 +166,13 @@ func (m Model) finished() bool {
|
|||||||
|
|
||||||
// Visible returns whether or not the view should be rendered. Works in
|
// Visible returns whether or not the view should be rendered. Works in
|
||||||
// conjunction with Model.HideFor and Model.MinimumLifetimeReached. You should
|
// conjunction with Model.HideFor and Model.MinimumLifetimeReached. You should
|
||||||
// use this message directly to determine whether or not to render this view in
|
// use this method directly to determine whether or not to render this view in
|
||||||
// the parent view and whether to continue sending spin messaging in the
|
// the parent view and whether to continue sending spin messaging in the
|
||||||
// parent update function.
|
// parent update function.
|
||||||
//
|
//
|
||||||
// Also note that using this function is optional and generally considered for
|
// This function is optional and generally considered for advanced use only.
|
||||||
// advanced use only. Most of the time your application logic will determine
|
// Most of the time your application logic will obviate the need for this
|
||||||
// whether or not this view should be used.
|
// method.
|
||||||
//
|
//
|
||||||
// This is considered experimental and may not appear in future versions of
|
// This is considered experimental and may not appear in future versions of
|
||||||
// this library.
|
// this library.
|
||||||
@@ -118,55 +182,78 @@ func (m Model) Visible() bool {
|
|||||||
|
|
||||||
// NewModel returns a model with default values.
|
// NewModel returns a model with default values.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{Spinner: Line}
|
||||||
Frames: Line,
|
|
||||||
FPS: defaultFPS,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TickMsg indicates that the timer has ticked and we should render a frame.
|
// TickMsg indicates that the timer has ticked and we should render a frame.
|
||||||
type TickMsg struct {
|
type TickMsg struct {
|
||||||
Time time.Time
|
Time time.Time
|
||||||
|
tag int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Tea update function. This will advance the spinner one frame
|
// Update is the Tea update function. This will advance the spinner one frame
|
||||||
// every time it's called, regardless the message passed, so be sure the logic
|
// every time it's called, regardless the message passed, so be sure the logic
|
||||||
// is setup so as not to call this Update needlessly.
|
// is setup so as not to call this Update needlessly.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
if _, ok := msg.(TickMsg); ok {
|
switch msg := msg.(type) {
|
||||||
|
case TickMsg:
|
||||||
|
|
||||||
|
// If a tag is set, and it's not the one we expect, reject the message.
|
||||||
|
// This prevents the spinner from receiving too many messages and
|
||||||
|
// this spinning too fast.
|
||||||
|
if msg.tag > 0 && msg.tag != m.tag {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
m.frame++
|
m.frame++
|
||||||
if m.frame >= len(m.Frames) {
|
if m.frame >= len(m.Spinner.Frames) {
|
||||||
m.frame = 0
|
m.frame = 0
|
||||||
}
|
}
|
||||||
return m, Tick(m)
|
|
||||||
|
m.tag++
|
||||||
|
return m, m.tick(m.tag)
|
||||||
|
default:
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the model's view.
|
// View renders the model's view.
|
||||||
func View(model Model) string {
|
func (m Model) View() string {
|
||||||
if model.frame >= len(model.Frames) {
|
if m.frame >= len(m.Spinner.Frames) {
|
||||||
return "error"
|
return "(error)"
|
||||||
}
|
}
|
||||||
|
|
||||||
frame := model.Frames[model.frame]
|
frame := m.Spinner.Frames[m.frame]
|
||||||
|
|
||||||
if model.ForegroundColor != "" || model.BackgroundColor != "" {
|
// If we're using the fine-grained hide/show spinner rules and those rules
|
||||||
|
// deem that the spinner should be hidden, draw an empty space in place of
|
||||||
|
// the spinner.
|
||||||
|
if m.advancedMode() && !m.Visible() {
|
||||||
|
frame = strings.Repeat(" ", ansi.PrintableRuneWidth(frame))
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.ForegroundColor != "" || m.BackgroundColor != "" {
|
||||||
return termenv.
|
return termenv.
|
||||||
String(frame).
|
String(frame).
|
||||||
Foreground(color(model.ForegroundColor)).
|
Foreground(color(m.ForegroundColor)).
|
||||||
Background(color(model.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
return frame
|
return frame
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick is the command used to advance the spinner one frame.
|
// Tick is the command used to advance the spinner one frame. Use this command
|
||||||
func Tick(m Model) tea.Cmd {
|
// to effectively start the spinner.
|
||||||
return tea.Tick(m.FPS, func(t time.Time) tea.Msg {
|
func Tick() tea.Msg {
|
||||||
|
return TickMsg{Time: time.Now()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) tick(tag int) tea.Cmd {
|
||||||
|
return tea.Tick(m.Spinner.FPS, func(t time.Time) tea.Msg {
|
||||||
return TickMsg{
|
return TickMsg{
|
||||||
Time: t,
|
Time: t,
|
||||||
|
tag: tag,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package textinput
|
package textinput
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -13,6 +14,20 @@ import (
|
|||||||
|
|
||||||
const defaultBlinkSpeed = time.Millisecond * 530
|
const defaultBlinkSpeed = time.Millisecond * 530
|
||||||
|
|
||||||
|
// color is a helper for returning colors.
|
||||||
|
var color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||||
|
|
||||||
|
// blinkMsg and blinkCanceled are used to manage cursor blinking.
|
||||||
|
type blinkMsg struct{}
|
||||||
|
type blinkCanceled struct{}
|
||||||
|
|
||||||
|
// Messages for clipboard events.
|
||||||
|
type pasteMsg string
|
||||||
|
type pasteErrMsg struct{ error }
|
||||||
|
|
||||||
|
// EchoMode sets the input behavior of the text input field.
|
||||||
|
type EchoMode int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// EchoNormal displays text as is. This is the default behavior.
|
// EchoNormal displays text as is. This is the default behavior.
|
||||||
EchoNormal EchoMode = iota
|
EchoNormal EchoMode = iota
|
||||||
@@ -28,31 +43,35 @@ const (
|
|||||||
// EchoOnEdit
|
// EchoOnEdit
|
||||||
)
|
)
|
||||||
|
|
||||||
// EchoMode sets the input behavior of the text input field.
|
// blinkCtx manages cursor blinking.
|
||||||
type EchoMode int
|
type blinkCtx struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
type cursorMode int
|
||||||
// color is a helper for returning colors.
|
|
||||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
const (
|
||||||
|
cursorBlink = iota
|
||||||
|
cursorStatic
|
||||||
|
cursorHide
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model is the Bubble Tea model for this text input element.
|
// Model is the Bubble Tea model for this text input element.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
Err error
|
||||||
|
|
||||||
Prompt string
|
// General settings
|
||||||
Placeholder string
|
Prompt string
|
||||||
|
Placeholder string
|
||||||
Cursor string
|
Cursor string
|
||||||
BlinkSpeed time.Duration
|
BlinkSpeed time.Duration
|
||||||
|
|
||||||
TextColor string
|
TextColor string
|
||||||
BackgroundColor string
|
BackgroundColor string
|
||||||
PlaceholderColor string
|
PlaceholderColor string
|
||||||
CursorColor string
|
CursorColor string
|
||||||
|
EchoMode EchoMode
|
||||||
EchoMode EchoMode
|
EchoCharacter rune
|
||||||
EchoCharacter rune
|
|
||||||
|
|
||||||
// CharLimit is the maximum amount of characters this input element will
|
// CharLimit is the maximum amount of characters this input element will
|
||||||
// accept. If 0 or less, there's no limit.
|
// accept. If 0 or less, there's no limit.
|
||||||
@@ -80,6 +99,36 @@ type Model struct {
|
|||||||
// overflowing.
|
// overflowing.
|
||||||
offset int
|
offset int
|
||||||
offsetRight int
|
offsetRight int
|
||||||
|
|
||||||
|
// Used to manage cursor blink
|
||||||
|
blinkCtx *blinkCtx
|
||||||
|
|
||||||
|
// cursorMode determines the behavior of the cursor
|
||||||
|
cursorMode cursorMode
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewModel creates a new model with default settings.
|
||||||
|
func NewModel() Model {
|
||||||
|
return Model{
|
||||||
|
Prompt: "> ",
|
||||||
|
Placeholder: "",
|
||||||
|
BlinkSpeed: defaultBlinkSpeed,
|
||||||
|
TextColor: "",
|
||||||
|
PlaceholderColor: "240",
|
||||||
|
CursorColor: "",
|
||||||
|
EchoCharacter: '*',
|
||||||
|
CharLimit: 0,
|
||||||
|
|
||||||
|
value: nil,
|
||||||
|
focus: false,
|
||||||
|
blink: true,
|
||||||
|
pos: 0,
|
||||||
|
cursorMode: cursorBlink,
|
||||||
|
|
||||||
|
blinkCtx: &blinkCtx{
|
||||||
|
ctx: context.Background(),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets the value of the text input.
|
// SetValue sets the value of the text input.
|
||||||
@@ -103,20 +152,28 @@ func (m Model) Value() string {
|
|||||||
|
|
||||||
// SetCursor start moves the cursor to the given position. If the position is
|
// SetCursor start moves the cursor to the given position. If the position is
|
||||||
// out of bounds the cursor will be moved to the start or end accordingly.
|
// out of bounds the cursor will be moved to the start or end accordingly.
|
||||||
func (m *Model) SetCursor(pos int) {
|
// Returns whether or nor the cursor timer should be reset.
|
||||||
|
func (m *Model) SetCursor(pos int) bool {
|
||||||
m.pos = clamp(pos, 0, len(m.value))
|
m.pos = clamp(pos, 0, len(m.value))
|
||||||
m.blink = false
|
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
|
|
||||||
|
// Show the cursor unless it's been explicitly hidden
|
||||||
|
m.blink = m.cursorMode == cursorHide
|
||||||
|
|
||||||
|
// Reset cursor blink if necessary
|
||||||
|
return m.cursorMode == cursorBlink
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorStart moves the cursor to the start of the field.
|
// CursorStart moves the cursor to the start of the field. Returns whether or
|
||||||
func (m *Model) CursorStart() {
|
// not the curosr blink should be reset.
|
||||||
m.SetCursor(0)
|
func (m *Model) CursorStart() bool {
|
||||||
|
return m.SetCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorEnd moves the cursor to the end of the field.
|
// CursorEnd moves the cursor to the end of the field. Returns whether or not
|
||||||
func (m *Model) CursorEnd() {
|
// the cursor blink should be reset.
|
||||||
m.SetCursor(len(m.value))
|
func (m *Model) CursorEnd() bool {
|
||||||
|
return m.SetCursor(len(m.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Focused returns the focus state on the model.
|
// Focused returns the focus state on the model.
|
||||||
@@ -127,7 +184,7 @@ func (m Model) Focused() bool {
|
|||||||
// Focus sets the focus state on the model.
|
// Focus sets the focus state on the model.
|
||||||
func (m *Model) Focus() {
|
func (m *Model) Focus() {
|
||||||
m.focus = true
|
m.focus = true
|
||||||
m.blink = false
|
m.blink = m.cursorMode == cursorHide // show the cursor unless we've explicitly hidden it
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blur removes the focus state on the model.
|
// Blur removes the focus state on the model.
|
||||||
@@ -136,19 +193,17 @@ func (m *Model) Blur() {
|
|||||||
m.blink = true
|
m.blink = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset sets the input to its default state with no input.
|
// Reset sets the input to its default state with no input. Returns whether
|
||||||
func (m *Model) Reset() {
|
// or not the cursor blink should reset.
|
||||||
|
func (m *Model) Reset() bool {
|
||||||
m.value = nil
|
m.value = nil
|
||||||
m.SetCursor(0)
|
return m.SetCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paste pastes the contents of the clipboard into the text area (if supported).
|
// handle a clipboard paste event, if supported. Returns whether or not the
|
||||||
func (m *Model) Paste() {
|
// cursor blink should be reset.
|
||||||
pasteString, err := clipboard.ReadAll()
|
func (m *Model) handlePaste(v string) (blink bool) {
|
||||||
if err != nil {
|
paste := []rune(v)
|
||||||
m.Err = err
|
|
||||||
}
|
|
||||||
paste := []rune(pasteString)
|
|
||||||
|
|
||||||
var availSpace int
|
var availSpace int
|
||||||
if m.CharLimit > 0 {
|
if m.CharLimit > 0 {
|
||||||
@@ -187,8 +242,8 @@ func (m *Model) Paste() {
|
|||||||
// Put it all back together
|
// Put it all back together
|
||||||
m.value = append(head, tail...)
|
m.value = append(head, tail...)
|
||||||
|
|
||||||
// Reset blink state and run overflow checks
|
// Reset blink state if necessary and run overflow checks
|
||||||
m.SetCursor(m.pos)
|
return m.SetCursor(m.pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a max width is defined, perform some logic to treat the visible area
|
// If a max width is defined, perform some logic to treat the visible area
|
||||||
@@ -256,26 +311,27 @@ func (m *Model) colorPlaceholder(s string) string {
|
|||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteWordLeft deletes the word left to the cursor.
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
||||||
func (m *Model) deleteWordLeft() {
|
// the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteWordLeft() (blink bool) {
|
||||||
if m.pos == 0 || len(m.value) == 0 {
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := m.pos
|
i := m.pos
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.SetCursor(m.pos - 1)
|
||||||
for unicode.IsSpace(m.value[m.pos]) {
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
// ignore series of whitespace before cursor
|
// ignore series of whitespace before cursor
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.SetCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for m.pos > 0 {
|
for m.pos > 0 {
|
||||||
if !unicode.IsSpace(m.value[m.pos]) {
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.SetCursor(m.pos - 1)
|
||||||
} else {
|
} else {
|
||||||
if m.pos > 0 {
|
if m.pos > 0 {
|
||||||
// keep the previous space
|
// keep the previous space
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -286,24 +342,27 @@ func (m *Model) deleteWordLeft() {
|
|||||||
} else {
|
} else {
|
||||||
m.value = append(m.value[:m.pos], m.value[i:]...)
|
m.value = append(m.value[:m.pos], m.value[i:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteWordRight deletes the word right to the cursor.
|
// deleteWordRight deletes the word right to the cursor. Returns whether or not
|
||||||
func (m *Model) deleteWordRight() {
|
// the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteWordRight() (blink bool) {
|
||||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := m.pos
|
i := m.pos
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
for unicode.IsSpace(m.value[m.pos]) {
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
// ignore series of whitespace after cursor
|
// ignore series of whitespace after cursor
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for m.pos < len(m.value) {
|
for m.pos < len(m.value) {
|
||||||
if !unicode.IsSpace(m.value[m.pos]) {
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -314,10 +373,14 @@ func (m *Model) deleteWordRight() {
|
|||||||
} else {
|
} else {
|
||||||
m.value = append(m.value[:i], m.value[m.pos:]...)
|
m.value = append(m.value[:i], m.value[m.pos:]...)
|
||||||
}
|
}
|
||||||
m.SetCursor(i)
|
blink = m.SetCursor(i)
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) wordLeft() {
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
||||||
|
// cursor blink should be reset.
|
||||||
|
func (m *Model) wordLeft() (blink bool) {
|
||||||
if m.pos == 0 || len(m.value) == 0 {
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -326,7 +389,7 @@ func (m *Model) wordLeft() {
|
|||||||
|
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if unicode.IsSpace(m.value[i]) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.SetCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -335,15 +398,19 @@ func (m *Model) wordLeft() {
|
|||||||
|
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if !unicode.IsSpace(m.value[i]) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.SetCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) wordRight() {
|
// wordRight moves the cursor one word to the right. Returns whether or not the
|
||||||
|
// cursor blink should be reset.
|
||||||
|
func (m *Model) wordRight() (blink bool) {
|
||||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -352,7 +419,7 @@ func (m *Model) wordRight() {
|
|||||||
|
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if unicode.IsSpace(m.value[i]) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -361,12 +428,14 @@ func (m *Model) wordRight() {
|
|||||||
|
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if !unicode.IsSpace(m.value[i]) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.SetCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) echoTransform(v string) string {
|
func (m Model) echoTransform(v string) string {
|
||||||
@@ -381,130 +450,119 @@ func (m Model) echoTransform(v string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlinkMsg is sent when the cursor should alternate it's blinking state.
|
// Update is the Bubble Tea update loop.
|
||||||
type BlinkMsg struct{}
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
|
|
||||||
// NewModel creates a new model with default settings.
|
|
||||||
func NewModel() Model {
|
|
||||||
return Model{
|
|
||||||
Prompt: "> ",
|
|
||||||
Placeholder: "",
|
|
||||||
|
|
||||||
BlinkSpeed: defaultBlinkSpeed,
|
|
||||||
|
|
||||||
TextColor: "",
|
|
||||||
PlaceholderColor: "240",
|
|
||||||
CursorColor: "",
|
|
||||||
|
|
||||||
EchoCharacter: '*',
|
|
||||||
|
|
||||||
CharLimit: 0,
|
|
||||||
|
|
||||||
value: nil,
|
|
||||||
focus: false,
|
|
||||||
blink: true,
|
|
||||||
pos: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update is the Tea update loop.
|
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|
||||||
if !m.focus {
|
if !m.focus {
|
||||||
m.blink = true
|
m.blink = true
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var resetBlink bool
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case tea.KeyBackspace: // delete character before cursor
|
case tea.KeyBackspace: // delete character before cursor
|
||||||
if msg.Alt {
|
if msg.Alt {
|
||||||
m.deleteWordLeft()
|
resetBlink = m.deleteWordLeft()
|
||||||
} else {
|
} else {
|
||||||
if len(m.value) > 0 {
|
if len(m.value) > 0 {
|
||||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||||
if m.pos > 0 {
|
if m.pos > 0 {
|
||||||
m.SetCursor(m.pos - 1)
|
resetBlink = m.SetCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tea.KeyLeft:
|
case tea.KeyLeft, tea.KeyCtrlB:
|
||||||
if msg.Alt { // alt+left arrow, back one word
|
if msg.Alt { // alt+left arrow, back one word
|
||||||
m.wordLeft()
|
resetBlink = m.wordLeft()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if m.pos > 0 {
|
if m.pos > 0 { // left arrow, ^F, back one character
|
||||||
m.SetCursor(m.pos - 1)
|
resetBlink = m.SetCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
case tea.KeyRight:
|
case tea.KeyRight, tea.KeyCtrlF:
|
||||||
if msg.Alt { // alt+right arrow, forward one word
|
if msg.Alt { // alt+right arrow, forward one word
|
||||||
m.wordRight()
|
resetBlink = m.wordRight()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if m.pos < len(m.value) {
|
if m.pos < len(m.value) { // right arrow, ^F, forward one word
|
||||||
m.SetCursor(m.pos + 1)
|
resetBlink = m.SetCursor(m.pos + 1)
|
||||||
}
|
}
|
||||||
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
||||||
m.deleteWordLeft()
|
resetBlink = m.deleteWordLeft()
|
||||||
case tea.KeyCtrlF: // ^F, forward one character
|
|
||||||
fallthrough
|
|
||||||
case tea.KeyCtrlB: // ^B, back one charcter
|
|
||||||
fallthrough
|
|
||||||
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
||||||
m.CursorStart()
|
resetBlink = m.CursorStart()
|
||||||
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
||||||
if len(m.value) > 0 && m.pos < len(m.value) {
|
if len(m.value) > 0 && m.pos < len(m.value) {
|
||||||
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
||||||
}
|
}
|
||||||
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
||||||
m.CursorEnd()
|
resetBlink = m.CursorEnd()
|
||||||
case tea.KeyCtrlK: // ^K, kill text after cursor
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
||||||
m.value = m.value[:m.pos]
|
m.value = m.value[:m.pos]
|
||||||
m.SetCursor(len(m.value))
|
resetBlink = m.SetCursor(len(m.value))
|
||||||
case tea.KeyCtrlU: // ^U, kill text before cursor
|
case tea.KeyCtrlU: // ^U, kill text before cursor
|
||||||
m.value = m.value[m.pos:]
|
m.value = m.value[m.pos:]
|
||||||
m.SetCursor(0)
|
resetBlink = m.SetCursor(0)
|
||||||
m.offset = 0
|
m.offset = 0
|
||||||
case tea.KeyCtrlV: // ^V paste
|
case tea.KeyCtrlV: // ^V paste
|
||||||
m.Paste()
|
return m, Paste
|
||||||
case tea.KeyRune: // input a regular character
|
case tea.KeyRunes: // input regular characters
|
||||||
if msg.Alt {
|
if msg.Alt && len(msg.Runes) == 1 {
|
||||||
if msg.Rune == 'd' { // alt+d, delete word right of cursor
|
if msg.Runes[0] == 'd' { // alt+d, delete word right of cursor
|
||||||
m.deleteWordRight()
|
resetBlink = m.deleteWordRight()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if msg.Rune == 'b' { // alt+b, back one word
|
if msg.Runes[0] == 'b' { // alt+b, back one word
|
||||||
m.wordLeft()
|
resetBlink = m.wordLeft()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if msg.Rune == 'f' { // alt+f, forward one word
|
if msg.Runes[0] == 'f' { // alt+f, forward one word
|
||||||
m.wordRight()
|
resetBlink = m.wordRight()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input a regular character
|
// Input a regular character
|
||||||
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
||||||
m.value = append(m.value[:m.pos], append([]rune{msg.Rune}, m.value[m.pos:]...)...)
|
m.value = append(m.value[:m.pos], append(msg.Runes, m.value[m.pos:]...)...)
|
||||||
m.SetCursor(m.pos + 1)
|
resetBlink = m.SetCursor(m.pos + len(msg.Runes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case BlinkMsg:
|
case blinkMsg:
|
||||||
m.blink = !m.blink
|
var cmd tea.Cmd
|
||||||
return m, Blink(m)
|
if m.cursorMode == cursorBlink {
|
||||||
|
m.blink = !m.blink
|
||||||
|
cmd = m.blinkCmd()
|
||||||
|
}
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
|
case blinkCanceled: // no-op
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case pasteMsg:
|
||||||
|
resetBlink = m.handlePaste(string(msg))
|
||||||
|
|
||||||
|
case pasteErrMsg:
|
||||||
|
m.Err = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd tea.Cmd
|
||||||
|
if resetBlink {
|
||||||
|
cmd = m.blinkCmd()
|
||||||
}
|
}
|
||||||
|
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
|
return m, cmd
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the textinput in its current state.
|
// View renders the textinput in its current state.
|
||||||
func View(m Model) string {
|
func (m Model) View() string {
|
||||||
// Placeholder text
|
// Placeholder text
|
||||||
if len(m.value) == 0 && m.Placeholder != "" {
|
if len(m.value) == 0 && m.Placeholder != "" {
|
||||||
return placeholderView(m)
|
return m.placeholderView()
|
||||||
}
|
}
|
||||||
|
|
||||||
value := m.value[m.offset:m.offsetRight]
|
value := m.value[m.offset:m.offsetRight]
|
||||||
@@ -512,10 +570,10 @@ func View(m Model) string {
|
|||||||
v := m.colorText(m.echoTransform(string(value[:pos])))
|
v := m.colorText(m.echoTransform(string(value[:pos])))
|
||||||
|
|
||||||
if pos < len(value) {
|
if pos < len(value) {
|
||||||
v += cursorView(m.echoTransform(string(value[pos])), m) // cursor and text under it
|
v += m.cursorView(m.echoTransform(string(value[pos]))) // cursor and text under it
|
||||||
v += m.colorText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
v += m.colorText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
||||||
} else {
|
} else {
|
||||||
v += cursorView(" ", m)
|
v += m.cursorView(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a max width and background color were set fill the empty spaces with
|
// If a max width and background color were set fill the empty spaces with
|
||||||
@@ -535,8 +593,8 @@ func View(m Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
// placeholderView.
|
// placeholderView returns the prompt and placeholder view, if any.
|
||||||
func placeholderView(m Model) string {
|
func (m Model) placeholderView() string {
|
||||||
var (
|
var (
|
||||||
v string
|
v string
|
||||||
p = m.Placeholder
|
p = m.Placeholder
|
||||||
@@ -544,9 +602,9 @@ func placeholderView(m Model) string {
|
|||||||
|
|
||||||
// Cursor
|
// Cursor
|
||||||
if m.blink && m.PlaceholderColor != "" {
|
if m.blink && m.PlaceholderColor != "" {
|
||||||
v += cursorView(m.colorPlaceholder(p[:1]), m)
|
v += m.cursorView(m.colorPlaceholder(p[:1]))
|
||||||
} else {
|
} else {
|
||||||
v += cursorView(p[:1], m)
|
v += m.cursorView(p[:1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// The rest of the placeholder text
|
// The rest of the placeholder text
|
||||||
@@ -556,29 +614,54 @@ func placeholderView(m Model) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cursorView styles the cursor.
|
// cursorView styles the cursor.
|
||||||
func cursorView(s string, m Model) string {
|
func (m Model) cursorView(v string) string {
|
||||||
if m.blink {
|
if m.blink {
|
||||||
if m.TextColor != "" || m.BackgroundColor != "" {
|
if m.TextColor != "" || m.BackgroundColor != "" {
|
||||||
return termenv.String(s).
|
return termenv.String(v).
|
||||||
Foreground(color(m.TextColor)).
|
Foreground(color(m.TextColor)).
|
||||||
Background(color(m.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
return s
|
return v
|
||||||
}
|
}
|
||||||
return termenv.String(s).
|
return termenv.String(v).
|
||||||
Foreground(color(m.CursorColor)).
|
Foreground(color(m.CursorColor)).
|
||||||
Background(color(m.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
Reverse().
|
Reverse().
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blink is a command used to time the cursor blinking.
|
// blinkCmd is an internal command used to manage cursor blinking.
|
||||||
func Blink(model Model) tea.Cmd {
|
func (m Model) blinkCmd() tea.Cmd {
|
||||||
return func() tea.Msg {
|
if m.blinkCtx != nil && m.blinkCtx.cancel != nil {
|
||||||
time.Sleep(model.BlinkSpeed)
|
m.blinkCtx.cancel()
|
||||||
return BlinkMsg{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(m.blinkCtx.ctx, m.BlinkSpeed)
|
||||||
|
m.blinkCtx.cancel = cancel
|
||||||
|
|
||||||
|
return func() tea.Msg {
|
||||||
|
defer cancel()
|
||||||
|
<-ctx.Done()
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
return blinkMsg{}
|
||||||
|
}
|
||||||
|
return blinkCanceled{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blink is a command used to initialize cursor blinking.
|
||||||
|
func Blink() tea.Msg {
|
||||||
|
return blinkMsg{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Paste is a command for pasting from the clipboard into the text input.
|
||||||
|
func Paste() tea.Msg {
|
||||||
|
str, err := clipboard.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return pasteErrMsg{err}
|
||||||
|
}
|
||||||
|
return pasteMsg(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(v, low, high int) int {
|
func clamp(v, low, high int) int {
|
||||||
|
@@ -7,9 +7,7 @@ import (
|
|||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const spacebar = " "
|
||||||
spacebar = " "
|
|
||||||
)
|
|
||||||
|
|
||||||
// MODEL
|
// MODEL
|
||||||
|
|
||||||
@@ -269,7 +267,7 @@ func ViewDown(m Model, lines []string) tea.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ViewUp is a high performance command the moves the viewport down by a given
|
// ViewUp is a high performance command the moves the viewport down by a given
|
||||||
// number of lines height. Use Model.ViewDown to get the lines that should be
|
// number of lines height. Use Model.ViewUp to get the lines that should be
|
||||||
// rendered.
|
// rendered.
|
||||||
func ViewUp(m Model, lines []string) tea.Cmd {
|
func ViewUp(m Model, lines []string) tea.Cmd {
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
@@ -283,7 +281,7 @@ func ViewUp(m Model, lines []string) tea.Cmd {
|
|||||||
// Update runs the update loop with default keybindings similar to popular
|
// Update runs the update loop with default keybindings similar to popular
|
||||||
// pagers. To define your own keybindings use the methods on Model (i.e.
|
// pagers. To define your own keybindings use the methods on Model (i.e.
|
||||||
// Model.LineDown()) and define your own update function.
|
// Model.LineDown()) and define your own update function.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
@@ -354,7 +352,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
// VIEW
|
// VIEW
|
||||||
|
|
||||||
// View renders the viewport into a string.
|
// View renders the viewport into a string.
|
||||||
func View(m Model) string {
|
func (m Model) View() string {
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
// Just send newlines since we're doing to be rendering the actual
|
// Just send newlines since we're doing to be rendering the actual
|
||||||
// content seprately. We still need send something that equals the
|
// content seprately. We still need send something that equals the
|
||||||
|
Reference in New Issue
Block a user