mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-17 16:15:58 +03:00
Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9449cc7e41 | ||
|
58a177394e | ||
|
f016c31d83 | ||
|
74436326b9 | ||
|
158097df66 | ||
|
0d5d7e5acd | ||
|
ddd48d9ab7 | ||
|
7242bbe8dc | ||
|
9e324205c2 | ||
|
b2f42066a2 | ||
|
a0d7cb77a1 | ||
|
046b9ca129 | ||
|
da9a4049de | ||
|
490a599c05 | ||
|
f719cc8cb1 |
@@ -14,6 +14,7 @@ linters:
|
||||
- godot
|
||||
- godox
|
||||
- goimports
|
||||
- golint
|
||||
- gomnd
|
||||
- goprintffuncname
|
||||
- gosec
|
||||
|
12
go.mod
12
go.mod
@@ -4,11 +4,11 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/atotto/clipboard v0.1.2
|
||||
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/muesli/reflow v0.2.0
|
||||
github.com/muesli/termenv v0.7.4
|
||||
github.com/charmbracelet/bubbletea v0.13.1
|
||||
github.com/charmbracelet/lipgloss v0.1.2
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0
|
||||
github.com/mattn/go-runewidth v0.0.12
|
||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68
|
||||
github.com/muesli/termenv v0.8.1
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
|
||||
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
|
||||
)
|
||||
|
32
go.sum
32
go.sum
@@ -1,27 +1,32 @@
|
||||
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
||||
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
github.com/charmbracelet/bubbletea v0.12.2 h1:y9Yo2Pv8tcm3mAJsWONGsmHhzrbNxJVxpVtemikxE9A=
|
||||
github.com/charmbracelet/bubbletea v0.12.2/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg=
|
||||
github.com/charmbracelet/bubbletea v0.13.1 h1:huvX8mPaeMZ8DLulT50iEWRF+iitY5FNEDqDVLu69nM=
|
||||
github.com/charmbracelet/bubbletea v0.13.1/go.mod h1:tp9tr9Dadh0PLhgiwchE5zZJXm5543JYjHG9oY+5qSg=
|
||||
github.com/charmbracelet/lipgloss v0.1.2 h1:D+LUMg34W7n2pkuMrevKVxT7HXqnoRHm7IoomkX3/ZU=
|
||||
github.com/charmbracelet/lipgloss v0.1.2/go.mod h1:5D8zradw52m7QmxRF6QgwbwJi9je84g8MkWiGN07uKg=
|
||||
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
|
||||
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/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
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/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/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 h1:y1p/ycavWjGT9FnmSjdbWUlLGvcxrY0Rw3ATltrxOhk=
|
||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
||||
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/go.mod h1:pZ7qY9l3F7e5xsAOS0zCew2tME+p7bWeBkotCEcIIcc=
|
||||
github.com/muesli/termenv v0.8.1 h1:9q230czSP3DHVpkaPDXGp0TOfAwyjyYwXlUCQxQSaBk=
|
||||
github.com/muesli/termenv v0.8.1/go.mod h1:kzt/D/4a88RoheZmwfqorY3A+tnsSMA9HJC/fQSFKo0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
|
||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@@ -30,8 +35,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
|
||||
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
|
||||
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// package paginator provides a Bubble Tea package for calulating pagination
|
||||
// Package paginator provides a Bubble Tea package for calulating pagination
|
||||
// and rendering pagination info. Note that this package does not render actual
|
||||
// pages: it's purely for handling keystrokes related to pagination, and
|
||||
// rendering pagination status.
|
||||
@@ -91,7 +91,7 @@ func (m *Model) NextPage() {
|
||||
}
|
||||
}
|
||||
|
||||
// LastPage returns whether or not we're on the last page.
|
||||
// OnLastPage returns whether or not we're on the last page.
|
||||
func (m Model) OnLastPage() bool {
|
||||
return m.Page == m.TotalPages-1
|
||||
}
|
||||
|
@@ -128,7 +128,7 @@ func NewModel(opts ...Option) (*Model, error) {
|
||||
func (m Model) View(percent float64) string {
|
||||
b := strings.Builder{}
|
||||
if m.ShowPercentage {
|
||||
percentage := fmt.Sprintf(m.PercentFormat, percent*100)
|
||||
percentage := fmt.Sprintf(m.PercentFormat, percent*100) //nolint:gomnd
|
||||
if m.PercentageStyle != nil {
|
||||
percentage = m.PercentageStyle.Styled(percentage)
|
||||
}
|
||||
|
@@ -5,58 +5,54 @@ import (
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/reflow/ansi"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
const defaultFPS = time.Second / 10
|
||||
|
||||
// Spinner is a set of frames used in animating the spinner.
|
||||
type Spinner struct {
|
||||
Frames []string
|
||||
FPS time.Duration
|
||||
}
|
||||
|
||||
// Some spinners to choose from. You could also make your own.
|
||||
var (
|
||||
// Some spinners to choose from. You could also make your own.
|
||||
Line = Spinner{
|
||||
Frames: []string{"|", "/", "-", "\\"},
|
||||
FPS: time.Second / 10,
|
||||
FPS: time.Second / 10, //nolint:gomnd
|
||||
}
|
||||
Dot = Spinner{
|
||||
Frames: []string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
||||
FPS: time.Second / 10,
|
||||
FPS: time.Second / 10, //nolint:gomnd
|
||||
}
|
||||
MiniDot = Spinner{
|
||||
Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
|
||||
FPS: time.Second / 12,
|
||||
FPS: time.Second / 12, //nolint:gomnd
|
||||
}
|
||||
Jump = Spinner{
|
||||
Frames: []string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
|
||||
FPS: time.Second / 10,
|
||||
FPS: time.Second / 10, //nolint:gomnd
|
||||
}
|
||||
Pulse = Spinner{
|
||||
Frames: []string{"█", "▓", "▒", "░"},
|
||||
FPS: time.Second / 8,
|
||||
FPS: time.Second / 8, //nolint:gomnd
|
||||
}
|
||||
Points = Spinner{
|
||||
Frames: []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●"},
|
||||
FPS: time.Second / 7,
|
||||
FPS: time.Second / 7, //nolint:gomnd
|
||||
}
|
||||
Globe = Spinner{
|
||||
Frames: []string{"🌍", "🌎", "🌏"},
|
||||
FPS: time.Second / 4,
|
||||
FPS: time.Second / 4, //nolint:gomnd
|
||||
}
|
||||
Moon = Spinner{
|
||||
Frames: []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"},
|
||||
FPS: time.Second / 8,
|
||||
FPS: time.Second / 8, //nolint:gomnd
|
||||
}
|
||||
Monkey = Spinner{
|
||||
Frames: []string{"🙈", "🙉", "🙊"},
|
||||
FPS: time.Second / 3,
|
||||
FPS: time.Second / 3, //nolint:gomnd
|
||||
}
|
||||
|
||||
color = termenv.ColorProfile().Color
|
||||
)
|
||||
|
||||
// Model contains the state for the spinner. Use NewModel to create new models
|
||||
@@ -66,17 +62,12 @@ type Model struct {
|
||||
// Spinner settings to use. See type Spinner.
|
||||
Spinner Spinner
|
||||
|
||||
// ForegroundColor sets the background color of the spinner. It can be
|
||||
// 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
|
||||
// github.com/muesli/termenv).
|
||||
ForegroundColor string
|
||||
|
||||
// BackgroundColor sets the background color of the spinner. It can be
|
||||
// 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
|
||||
// github.com/muesli/termenv).
|
||||
BackgroundColor string
|
||||
// Style sets the styling for the spinner. Most of the time you'll just
|
||||
// want foreground and background coloring, and potentially some padding.
|
||||
//
|
||||
// For an introduction to styling with Lip Gloss see:
|
||||
// https://github.com/charmbracelet/lipgloss
|
||||
Style lipgloss.Style
|
||||
|
||||
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
||||
// logic around this can be implemented in view that implements this
|
||||
@@ -232,15 +223,7 @@ func (m Model) View() string {
|
||||
frame = strings.Repeat(" ", ansi.PrintableRuneWidth(frame))
|
||||
}
|
||||
|
||||
if m.ForegroundColor != "" || m.BackgroundColor != "" {
|
||||
return termenv.
|
||||
String(frame).
|
||||
Foreground(color(m.ForegroundColor)).
|
||||
Background(color(m.BackgroundColor)).
|
||||
String()
|
||||
}
|
||||
|
||||
return frame
|
||||
return m.Style.Render(frame)
|
||||
}
|
||||
|
||||
// Tick is the command used to advance the spinner one frame. Use this command
|
||||
|
@@ -8,15 +8,12 @@ import (
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
rw "github.com/mattn/go-runewidth"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
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{}
|
||||
@@ -61,17 +58,22 @@ const (
|
||||
type Model struct {
|
||||
Err error
|
||||
|
||||
// General settings
|
||||
Prompt string
|
||||
Placeholder string
|
||||
Cursor string
|
||||
BlinkSpeed time.Duration
|
||||
TextColor string
|
||||
BackgroundColor string
|
||||
PlaceholderColor string
|
||||
CursorColor string
|
||||
EchoMode EchoMode
|
||||
EchoCharacter rune
|
||||
// General settings.
|
||||
Prompt string
|
||||
Placeholder string
|
||||
BlinkSpeed time.Duration
|
||||
EchoMode EchoMode
|
||||
EchoCharacter rune
|
||||
|
||||
// Styles. These will be applied as inline styles.
|
||||
//
|
||||
// For an introduction to styling with Lip Gloss see:
|
||||
// https://github.com/charmbracelet/lipgloss
|
||||
PromptStyle lipgloss.Style
|
||||
TextStyle lipgloss.Style
|
||||
BackgroundStyle lipgloss.Style
|
||||
PlaceholderStyle lipgloss.Style
|
||||
CursorStyle lipgloss.Style
|
||||
|
||||
// CharLimit is the maximum amount of characters this input element will
|
||||
// accept. If 0 or less, there's no limit.
|
||||
@@ -111,13 +113,10 @@ type Model struct {
|
||||
func NewModel() Model {
|
||||
return Model{
|
||||
Prompt: "> ",
|
||||
Placeholder: "",
|
||||
BlinkSpeed: defaultBlinkSpeed,
|
||||
TextColor: "",
|
||||
PlaceholderColor: "240",
|
||||
CursorColor: "",
|
||||
EchoCharacter: '*',
|
||||
CharLimit: 0,
|
||||
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
||||
|
||||
value: nil,
|
||||
focus: false,
|
||||
@@ -139,8 +138,8 @@ func (m *Model) SetValue(s string) {
|
||||
} else {
|
||||
m.value = runes
|
||||
}
|
||||
if m.pos > len(m.value) {
|
||||
m.SetCursor(len(m.value))
|
||||
if m.pos == 0 || m.pos > len(m.value) {
|
||||
m.setCursor(len(m.value))
|
||||
}
|
||||
m.handleOverflow()
|
||||
}
|
||||
@@ -150,10 +149,21 @@ func (m Model) Value() string {
|
||||
return string(m.value)
|
||||
}
|
||||
|
||||
// SetCursor start moves the cursor to the given position. If the position is
|
||||
// Cursor returns the cursor position.
|
||||
func (m Model) Cursor() int {
|
||||
return m.pos
|
||||
}
|
||||
|
||||
// SetCursor 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.
|
||||
// Returns whether or nor the cursor timer should be reset.
|
||||
func (m *Model) SetCursor(pos int) bool {
|
||||
func (m *Model) SetCursor(pos int) {
|
||||
m.setCursor(pos)
|
||||
}
|
||||
|
||||
// setCursor moves the cursor to the given position and returns whether or not
|
||||
// the cursor blink should be reset. If the position is out of bounds the
|
||||
// cursor will be moved to the start or end accordingly.
|
||||
func (m *Model) setCursor(pos int) bool {
|
||||
m.pos = clamp(pos, 0, len(m.value))
|
||||
m.handleOverflow()
|
||||
|
||||
@@ -164,16 +174,26 @@ func (m *Model) SetCursor(pos int) bool {
|
||||
return m.cursorMode == cursorBlink
|
||||
}
|
||||
|
||||
// CursorStart moves the cursor to the start of the field. Returns whether or
|
||||
// not the curosr blink should be reset.
|
||||
func (m *Model) CursorStart() bool {
|
||||
return m.SetCursor(0)
|
||||
// CursorStart moves the cursor to the start of the input field.
|
||||
func (m *Model) CursorStart() {
|
||||
m.cursorStart()
|
||||
}
|
||||
|
||||
// CursorEnd moves the cursor to the end of the field. Returns whether or not
|
||||
// the cursor blink should be reset.
|
||||
func (m *Model) CursorEnd() bool {
|
||||
return m.SetCursor(len(m.value))
|
||||
// cursorStart moves the cursor to the start of the input field and returns
|
||||
// whether or not the curosr blink should be reset.
|
||||
func (m *Model) cursorStart() bool {
|
||||
return m.setCursor(0)
|
||||
}
|
||||
|
||||
// CursorEnd moves the cursor to the end of the input field
|
||||
func (m *Model) CursorEnd() {
|
||||
m.cursorEnd()
|
||||
}
|
||||
|
||||
// cursorEnd moves the cursor to the end of the input field and returns whether
|
||||
// or not
|
||||
func (m *Model) cursorEnd() bool {
|
||||
return m.setCursor(len(m.value))
|
||||
}
|
||||
|
||||
// Focused returns the focus state on the model.
|
||||
@@ -197,12 +217,12 @@ func (m *Model) Blur() {
|
||||
// or not the cursor blink should reset.
|
||||
func (m *Model) Reset() bool {
|
||||
m.value = nil
|
||||
return m.SetCursor(0)
|
||||
return m.setCursor(0)
|
||||
}
|
||||
|
||||
// handle a clipboard paste event, if supported. Returns whether or not the
|
||||
// cursor blink should be reset.
|
||||
func (m *Model) handlePaste(v string) (blink bool) {
|
||||
func (m *Model) handlePaste(v string) bool {
|
||||
paste := []rune(v)
|
||||
|
||||
var availSpace int
|
||||
@@ -212,7 +232,7 @@ func (m *Model) handlePaste(v string) (blink bool) {
|
||||
|
||||
// If the char limit's been reached cancel
|
||||
if m.CharLimit > 0 && availSpace <= 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
// If there's not enough space to paste the whole thing cut the pasted
|
||||
@@ -243,7 +263,7 @@ func (m *Model) handlePaste(v string) (blink bool) {
|
||||
m.value = append(head, tail...)
|
||||
|
||||
// Reset blink state if necessary and run overflow checks
|
||||
return m.SetCursor(m.pos)
|
||||
return m.setCursor(m.pos)
|
||||
}
|
||||
|
||||
// If a max width is defined, perform some logic to treat the visible area
|
||||
@@ -291,47 +311,47 @@ func (m *Model) handleOverflow() {
|
||||
}
|
||||
}
|
||||
|
||||
// colorText colorizes a given string according to the TextColor value of the
|
||||
// model.
|
||||
func (m *Model) colorText(s string) string {
|
||||
return termenv.
|
||||
String(s).
|
||||
Foreground(color(m.TextColor)).
|
||||
Background(color(m.BackgroundColor)).
|
||||
String()
|
||||
// deleteBeforeCursor deletes all text before the cursor. Returns whether or
|
||||
// not the cursor blink should be reset.
|
||||
func (m *Model) deleteBeforeCursor() bool {
|
||||
m.value = m.value[m.pos:]
|
||||
m.offset = 0
|
||||
return m.setCursor(0)
|
||||
}
|
||||
|
||||
// colorPlaceholder colorizes a given string according to the TextColor value
|
||||
// of the model.
|
||||
func (m *Model) colorPlaceholder(s string) string {
|
||||
return termenv.
|
||||
String(s).
|
||||
Foreground(color(m.PlaceholderColor)).
|
||||
Background(color(m.BackgroundColor)).
|
||||
String()
|
||||
// deleteAfterCursor deletes all text after the cursor. Returns whether or not
|
||||
// the cursor blink should be reset. If input is masked delete everything after
|
||||
// the cursor so as not to reveal word breaks in the masked input.
|
||||
func (m *Model) deleteAfterCursor() bool {
|
||||
m.value = m.value[:m.pos]
|
||||
return m.setCursor(len(m.value))
|
||||
}
|
||||
|
||||
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
||||
// the cursor blink should be reset.
|
||||
func (m *Model) deleteWordLeft() (blink bool) {
|
||||
func (m *Model) deleteWordLeft() bool {
|
||||
if m.pos == 0 || len(m.value) == 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
if m.EchoMode != EchoNormal {
|
||||
return m.deleteBeforeCursor()
|
||||
}
|
||||
|
||||
i := m.pos
|
||||
blink = m.SetCursor(m.pos - 1)
|
||||
blink := m.setCursor(m.pos - 1)
|
||||
for unicode.IsSpace(m.value[m.pos]) {
|
||||
// ignore series of whitespace before cursor
|
||||
blink = m.SetCursor(m.pos - 1)
|
||||
blink = m.setCursor(m.pos - 1)
|
||||
}
|
||||
|
||||
for m.pos > 0 {
|
||||
if !unicode.IsSpace(m.value[m.pos]) {
|
||||
blink = m.SetCursor(m.pos - 1)
|
||||
blink = m.setCursor(m.pos - 1)
|
||||
} else {
|
||||
if m.pos > 0 {
|
||||
// keep the previous space
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
blink = m.setCursor(m.pos + 1)
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -343,26 +363,31 @@ func (m *Model) deleteWordLeft() (blink bool) {
|
||||
m.value = append(m.value[:m.pos], m.value[i:]...)
|
||||
}
|
||||
|
||||
return
|
||||
return blink
|
||||
}
|
||||
|
||||
// deleteWordRight deletes the word right to the cursor. Returns whether or not
|
||||
// the cursor blink should be reset.
|
||||
func (m *Model) deleteWordRight() (blink bool) {
|
||||
// the cursor blink should be reset. If input is masked delete everything after
|
||||
// the cursor so as not to reveal word breaks in the masked input.
|
||||
func (m *Model) deleteWordRight() bool {
|
||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
if m.EchoMode != EchoNormal {
|
||||
return m.deleteAfterCursor()
|
||||
}
|
||||
|
||||
i := m.pos
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
m.setCursor(m.pos + 1)
|
||||
for unicode.IsSpace(m.value[m.pos]) {
|
||||
// ignore series of whitespace after cursor
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
m.setCursor(m.pos + 1)
|
||||
}
|
||||
|
||||
for m.pos < len(m.value) {
|
||||
if !unicode.IsSpace(m.value[m.pos]) {
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
m.setCursor(m.pos + 1)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
@@ -373,23 +398,27 @@ func (m *Model) deleteWordRight() (blink bool) {
|
||||
} else {
|
||||
m.value = append(m.value[:i], m.value[m.pos:]...)
|
||||
}
|
||||
blink = m.SetCursor(i)
|
||||
|
||||
return
|
||||
return m.setCursor(i)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// cursor blink should be reset. If input is masked, move input to the start
|
||||
// so as not to reveal word breaks in the masked input.
|
||||
func (m *Model) wordLeft() bool {
|
||||
if m.pos == 0 || len(m.value) == 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
i := m.pos - 1
|
||||
if m.EchoMode != EchoNormal {
|
||||
return m.cursorStart()
|
||||
}
|
||||
|
||||
blink := false
|
||||
i := m.pos - 1
|
||||
for i >= 0 {
|
||||
if unicode.IsSpace(m.value[i]) {
|
||||
blink = m.SetCursor(m.pos - 1)
|
||||
blink = m.setCursor(m.pos - 1)
|
||||
i--
|
||||
} else {
|
||||
break
|
||||
@@ -398,28 +427,33 @@ func (m *Model) wordLeft() (blink bool) {
|
||||
|
||||
for i >= 0 {
|
||||
if !unicode.IsSpace(m.value[i]) {
|
||||
blink = m.SetCursor(m.pos - 1)
|
||||
blink = m.setCursor(m.pos - 1)
|
||||
i--
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
return blink
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// cursor blink should be reset. If the input is masked, move input to the end
|
||||
// so as not to reveal word breaks in the masked input.
|
||||
func (m *Model) wordRight() bool {
|
||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
i := m.pos
|
||||
if m.EchoMode != EchoNormal {
|
||||
return m.cursorEnd()
|
||||
}
|
||||
|
||||
blink := false
|
||||
i := m.pos
|
||||
for i < len(m.value) {
|
||||
if unicode.IsSpace(m.value[i]) {
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
blink = m.setCursor(m.pos + 1)
|
||||
i++
|
||||
} else {
|
||||
break
|
||||
@@ -428,14 +462,14 @@ func (m *Model) wordRight() (blink bool) {
|
||||
|
||||
for i < len(m.value) {
|
||||
if !unicode.IsSpace(m.value[i]) {
|
||||
blink = m.SetCursor(m.pos + 1)
|
||||
blink = m.setCursor(m.pos + 1)
|
||||
i++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
return blink
|
||||
}
|
||||
|
||||
func (m Model) echoTransform(v string) string {
|
||||
@@ -469,7 +503,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
if len(m.value) > 0 {
|
||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||
if m.pos > 0 {
|
||||
resetBlink = m.SetCursor(m.pos - 1)
|
||||
resetBlink = m.setCursor(m.pos - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -479,33 +513,30 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
break
|
||||
}
|
||||
if m.pos > 0 { // left arrow, ^F, back one character
|
||||
resetBlink = m.SetCursor(m.pos - 1)
|
||||
resetBlink = m.setCursor(m.pos - 1)
|
||||
}
|
||||
case tea.KeyRight, tea.KeyCtrlF:
|
||||
if msg.Alt { // alt+right arrow, forward one word
|
||||
resetBlink = m.wordRight()
|
||||
break
|
||||
}
|
||||
if m.pos < len(m.value) { // right arrow, ^F, forward one word
|
||||
resetBlink = m.SetCursor(m.pos + 1)
|
||||
if m.pos < len(m.value) { // right arrow, ^F, forward one character
|
||||
resetBlink = m.setCursor(m.pos + 1)
|
||||
}
|
||||
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
||||
resetBlink = m.deleteWordLeft()
|
||||
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
||||
resetBlink = m.CursorStart()
|
||||
resetBlink = m.cursorStart()
|
||||
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
||||
if len(m.value) > 0 && m.pos < len(m.value) {
|
||||
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
||||
}
|
||||
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
||||
resetBlink = m.CursorEnd()
|
||||
resetBlink = m.cursorEnd()
|
||||
case tea.KeyCtrlK: // ^K, kill text after cursor
|
||||
m.value = m.value[:m.pos]
|
||||
resetBlink = m.SetCursor(len(m.value))
|
||||
resetBlink = m.deleteAfterCursor()
|
||||
case tea.KeyCtrlU: // ^U, kill text before cursor
|
||||
m.value = m.value[m.pos:]
|
||||
resetBlink = m.SetCursor(0)
|
||||
m.offset = 0
|
||||
resetBlink = m.deleteBeforeCursor()
|
||||
case tea.KeyCtrlV: // ^V paste
|
||||
return m, Paste
|
||||
case tea.KeyRunes: // input regular characters
|
||||
@@ -527,7 +558,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
// Input a regular character
|
||||
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
||||
m.value = append(m.value[:m.pos], append(msg.Runes, m.value[m.pos:]...)...)
|
||||
resetBlink = m.SetCursor(m.pos + len(msg.Runes))
|
||||
resetBlink = m.setCursor(m.pos + len(msg.Runes))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -565,13 +596,15 @@ func (m Model) View() string {
|
||||
return m.placeholderView()
|
||||
}
|
||||
|
||||
styleText := m.TextStyle.Inline(true).Render
|
||||
|
||||
value := m.value[m.offset:m.offsetRight]
|
||||
pos := max(0, m.pos-m.offset)
|
||||
v := m.colorText(m.echoTransform(string(value[:pos])))
|
||||
v := styleText(m.echoTransform(string(value[:pos])))
|
||||
|
||||
if pos < len(value) {
|
||||
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.cursorView(m.echoTransform(string(value[pos]))) // cursor and text under it
|
||||
v += styleText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
||||
} else {
|
||||
v += m.cursorView(" ")
|
||||
}
|
||||
@@ -579,56 +612,44 @@ func (m Model) View() string {
|
||||
// If a max width and background color were set fill the empty spaces with
|
||||
// the background color.
|
||||
valWidth := rw.StringWidth(string(value))
|
||||
if m.Width > 0 && len(m.BackgroundColor) > 0 && valWidth <= m.Width {
|
||||
if m.Width > 0 && valWidth <= m.Width {
|
||||
padding := max(0, m.Width-valWidth)
|
||||
if valWidth+padding <= m.Width && pos < len(value) {
|
||||
padding++
|
||||
}
|
||||
v += strings.Repeat(
|
||||
termenv.String(" ").Background(color(m.BackgroundColor)).String(),
|
||||
padding,
|
||||
)
|
||||
v += styleText(strings.Repeat(" ", padding))
|
||||
}
|
||||
|
||||
return m.Prompt + v
|
||||
return m.PromptStyle.Render(m.Prompt) + v
|
||||
}
|
||||
|
||||
// placeholderView returns the prompt and placeholder view, if any.
|
||||
func (m Model) placeholderView() string {
|
||||
var (
|
||||
v string
|
||||
p = m.Placeholder
|
||||
v string
|
||||
p = m.Placeholder
|
||||
style = m.PlaceholderStyle.Inline(true).Render
|
||||
)
|
||||
|
||||
// Cursor
|
||||
if m.blink && m.PlaceholderColor != "" {
|
||||
v += m.cursorView(m.colorPlaceholder(p[:1]))
|
||||
if m.blink {
|
||||
v += m.cursorView(style(p[:1]))
|
||||
} else {
|
||||
v += m.cursorView(p[:1])
|
||||
}
|
||||
|
||||
// The rest of the placeholder text
|
||||
v += m.colorPlaceholder(p[1:])
|
||||
v += style(p[1:])
|
||||
|
||||
return m.Prompt + v
|
||||
return m.PromptStyle.Render(m.Prompt) + v
|
||||
}
|
||||
|
||||
// cursorView styles the cursor.
|
||||
func (m Model) cursorView(v string) string {
|
||||
if m.blink {
|
||||
if m.TextColor != "" || m.BackgroundColor != "" {
|
||||
return termenv.String(v).
|
||||
Foreground(color(m.TextColor)).
|
||||
Background(color(m.BackgroundColor)).
|
||||
String()
|
||||
}
|
||||
return v
|
||||
return m.TextStyle.Render(v)
|
||||
}
|
||||
return termenv.String(v).
|
||||
Foreground(color(m.CursorColor)).
|
||||
Background(color(m.BackgroundColor)).
|
||||
Reverse().
|
||||
String()
|
||||
return m.CursorStyle.Inline(true).Reverse(true).Render(v)
|
||||
}
|
||||
|
||||
// blinkCmd is an internal command used to manage cursor blinking.
|
||||
|
@@ -7,10 +7,12 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
const spacebar = " "
|
||||
|
||||
// MODEL
|
||||
const (
|
||||
spacebar = " "
|
||||
mouseWheelDelta = 3
|
||||
)
|
||||
|
||||
// Model is the Bubble Tea model for this viewport element.
|
||||
type Model struct {
|
||||
Width int
|
||||
Height int
|
||||
@@ -52,7 +54,7 @@ func (m Model) PastBottom() bool {
|
||||
return m.YOffset > len(m.lines)-1-m.Height
|
||||
}
|
||||
|
||||
// Scrollpercent returns the amount scrolled as a float between 0 and 1.
|
||||
// ScrollPercent returns the amount scrolled as a float between 0 and 1.
|
||||
func (m Model) ScrollPercent() float64 {
|
||||
if m.Height >= len(m.lines) {
|
||||
return 1.0
|
||||
@@ -69,6 +71,10 @@ func (m Model) ScrollPercent() float64 {
|
||||
func (m *Model) SetContent(s string) {
|
||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||
m.lines = strings.Split(s, "\n")
|
||||
|
||||
if m.YOffset > len(m.lines)-1 {
|
||||
m.GotoBottom()
|
||||
}
|
||||
}
|
||||
|
||||
// Return the lines that should currently be visible in the viewport.
|
||||
@@ -215,7 +221,7 @@ func (m *Model) GotoTop() (lines []string) {
|
||||
return lines
|
||||
}
|
||||
|
||||
// GotoTop sets the viewport to the bottom position.
|
||||
// GotoBottom sets the viewport to the bottom position.
|
||||
func (m *Model) GotoBottom() (lines []string) {
|
||||
m.YOffset = max(len(m.lines)-1-m.Height, 0)
|
||||
|
||||
@@ -333,13 +339,13 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
case tea.MouseMsg:
|
||||
switch msg.Type {
|
||||
case tea.MouseWheelUp:
|
||||
lines := m.LineUp(3)
|
||||
lines := m.LineUp(mouseWheelDelta)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewUp(m, lines)
|
||||
}
|
||||
|
||||
case tea.MouseWheelDown:
|
||||
lines := m.LineDown(3)
|
||||
lines := m.LineDown(mouseWheelDelta)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
|
Reference in New Issue
Block a user