6 Commits

Author SHA1 Message Date
Christian Rocha
74e84eca55 Bump Bubbletea dependency 2020-07-14 18:26:59 -04:00
Christian Rocha
ccf4c90b6b Rework spinner to allow for user-defined custom spinners 2020-07-14 18:21:48 -04:00
Christian Rocha
f341e3c896 Correct mouse wheel behavior 2020-06-23 13:10:50 -04:00
Christian Rocha
c9196e5407 Add basic mouse wheel support to viewport 2020-06-23 12:00:17 -04:00
Christian Rocha
f967f6a87f Fix a bug where placeholder text would not reappear post-input 2020-06-22 15:28:38 -04:00
Christian Rocha
35c3cd626d Fix spinner frame skipping and remove custom message functionality 2020-06-22 14:49:21 -04:00
5 changed files with 55 additions and 55 deletions

7
go.mod
View File

@@ -3,10 +3,11 @@ module github.com/charmbracelet/bubbles
go 1.13
require (
github.com/charmbracelet/bubbletea v0.8.3-0.20200622174044-8bab4bce9a84
github.com/charmbracelet/bubbletea v0.9.1-0.20200713153904-2f53eeb54b90
github.com/mattn/go-runewidth v0.0.9
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
)
replace github.com/charmbracelet/bubbletea => ../bubbletea

14
go.sum
View File

@@ -6,19 +6,21 @@ 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-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/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM=
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04 h1:Wr876oXlAk6avTWi0daXAriOr+r5fqIuyDmtNc/KwY0=
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU=
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 h1:DZhuSZLsGlFL4CmhA8BcRA0mnthyA/nZ00AqCUo7vHg=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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-20200620081246-981b61492c35 h1:wb/9mP8eUAmHfkM8RmpeLq6nUA7c2i5+bQOtcDftjaE=
golang.org/x/sys v0.0.0-20200620081246-981b61492c35/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 h1:5/PjkGUjvEU5Gl6BxmvKRPpqo2uNMv4rcHBMwzk/st8=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@@ -7,25 +7,17 @@ import (
"github.com/muesli/termenv"
)
const (
defaultFPS = time.Second / 10
)
// Spinner is a set of frames used in animating the spinner.
type Spinner = int
// Available types of spinners
const (
Line Spinner = iota
Dot
)
const (
defaultFPS = 9
)
type Spinner = []string
var (
// Spinner frames
spinners = map[Spinner][]string{
Line: {"|", "/", "-", "\\"},
Dot: {"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
}
// Some spinners to choose from. You could also make your own.
Line = Spinner([]string{"|", "/", "-", "\\"})
Dot = Spinner([]string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "", "", "⣯ ", "⣷ "})
color = termenv.ColorProfile().Color
)
@@ -35,10 +27,10 @@ var (
type Model struct {
// Type is the set of frames to use. See Spinner.
Type Spinner
Frames Spinner
// FPS is the speed at which the ticker should tick
FPS int
FPS time.Duration
// 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
@@ -52,20 +44,14 @@ type Model struct {
// (per github.com/muesli/termenv).
BackgroundColor string
// CustomMsgFunc can be used to a custom message on tick. This can be
// useful when you have spinners in different parts of your application and
// want to differentiate between the messages for clarity and simplicity.
// If nil, this setting is ignored.
CustomMsgFunc func() tea.Msg
frame int
}
// NewModel returns a model with default values.
func NewModel() Model {
return Model{
Type: Line,
FPS: defaultFPS,
Frames: Line,
FPS: defaultFPS,
}
}
@@ -76,43 +62,38 @@ type TickMsg struct{}
// every time it's called, regardless the message passed, so be sure the logic
// is setup so as not to call this Update needlessly.
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
m.frame++
if m.frame >= len(spinners[m.Type]) {
m.frame = 0
}
if m.CustomMsgFunc != nil {
if _, ok := msg.(TickMsg); ok {
m.frame++
if m.frame >= len(m.Frames) {
m.frame = 0
}
return m, Tick(m)
}
return m, Tick(m)
return m, nil
}
// View renders the model's view.
func View(model Model) string {
s := spinners[model.Type]
if model.frame >= len(s) {
return "[error]"
if model.frame >= len(model.Frames) {
return "error"
}
str := s[model.frame]
frame := model.Frames[model.frame]
if model.ForegroundColor != "" || model.BackgroundColor != "" {
return termenv.
String(str).
String(frame).
Foreground(color(model.ForegroundColor)).
Background(color(model.BackgroundColor)).
String()
}
return str
return frame
}
// Tick is the command used to advance the spinner one frame.
func Tick(model Model) tea.Cmd {
return func() tea.Msg {
time.Sleep(time.Second / time.Duration(model.FPS))
if model.CustomMsgFunc != nil {
return model.CustomMsgFunc()
}
func Tick(m Model) tea.Cmd {
return tea.Tick(m.FPS, func(time.Time) tea.Msg {
return TickMsg{}
}
})
}

View File

@@ -361,7 +361,7 @@ func View(model tea.Model) string {
}
// Placeholder text
if m.value == nil && m.Placeholder != "" {
if len(m.value) == 0 && m.Placeholder != "" {
return placeholderView(m)
}

View File

@@ -344,6 +344,22 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
cmd = LineUp(m, lines)
}
}
case tea.MouseMsg:
switch msg.Button {
case tea.MouseWheelUp:
lines := m.LineUp(3)
if m.HighPerformanceRendering {
cmd = LineUp(m, lines)
}
case tea.MouseWheelDown:
lines := m.LineDown(3)
if m.HighPerformanceRendering {
cmd = LineDown(m, lines)
}
}
}
return m, cmd