mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-17 16:15:58 +03:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a498b857d6 | ||
|
82097a1c79 | ||
|
a8ec421800 | ||
|
9c10cd0586 | ||
|
154f3763f7 | ||
|
eff931bdea | ||
|
0d7b3ed8e8 | ||
|
cbafae84f9 | ||
|
959598c9aa | ||
|
5f7c8b5375 | ||
|
75df6b96a1 | ||
|
084d0acefd | ||
|
e8526ec4ef | ||
|
68f11d0b63 | ||
|
f78a02cc29 | ||
|
9a4fbb3be1 | ||
|
d4a0ecd3ee | ||
|
34b1f47068 |
@@ -6,12 +6,12 @@ Bubbles
|
||||
<a href="https://github.com/charmbracelet/bubbles/actions"><img src="https://github.com/charmbracelet/glow/workflows/build/badge.svg" alt="Build Status"></a>
|
||||
</p>
|
||||
|
||||
Some components for [Bubble Tea](https://github.com/charmbraclet/bubbletea) applications.
|
||||
Some components for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications.
|
||||
|
||||
These components are used in production in [Glow][glow] and [Charm][charm].
|
||||
|
||||
[glow]: https://github.com/charmbraclet/glow
|
||||
[charm]: https://github.com/charmbraclet/charm
|
||||
[glow]: https://github.com/charmbracelet/glow
|
||||
[charm]: https://github.com/charmbracelet/charm
|
||||
|
||||
|
||||
## Spinner
|
||||
|
11
go.mod
11
go.mod
@@ -3,12 +3,9 @@ module github.com/charmbracelet/bubbles
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/atotto/clipboard v0.1.2 // indirect
|
||||
github.com/charmbracelet/bubbletea v0.9.1-0.20200713153904-2f53eeb54b90
|
||||
github.com/atotto/clipboard v0.1.2
|
||||
github.com/charmbracelet/bubbletea v0.12.0
|
||||
github.com/mattn/go-runewidth v0.0.9
|
||||
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
|
||||
github.com/muesli/termenv v0.7.4
|
||||
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
|
||||
)
|
||||
|
||||
replace github.com/charmbracelet/bubbletea => ../bubbletea
|
||||
|
29
go.sum
29
go.sum
@@ -1,5 +1,9 @@
|
||||
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.0 h1:/pUHp1GWRDyK1TJAWkXrnRH1u8Xc5076oH/J0NHxH+M=
|
||||
github.com/charmbracelet/bubbletea v0.12.0/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg=
|
||||
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=
|
||||
@@ -8,21 +12,22 @@ 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.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=
|
||||
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.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
|
||||
github.com/muesli/termenv v0.7.4/go.mod h1:pZ7qY9l3F7e5xsAOS0zCew2tME+p7bWeBkotCEcIIcc=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
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/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/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-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/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-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
|
||||
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@@ -13,7 +13,7 @@ import (
|
||||
// Type specifies the way we render pagination.
|
||||
type Type int
|
||||
|
||||
// Pagination rendering options
|
||||
// Pagination rendering options.
|
||||
const (
|
||||
Arabic Type = iota
|
||||
Dots
|
||||
@@ -164,11 +164,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
// View renders the pagination to a string.
|
||||
func View(model tea.Model) string {
|
||||
m, ok := model.(Model)
|
||||
if !ok {
|
||||
return "could not perform assertion on model"
|
||||
}
|
||||
func View(m Model) string {
|
||||
switch m.Type {
|
||||
case Dots:
|
||||
return dotsView(m)
|
||||
|
@@ -29,7 +29,7 @@ type Model struct {
|
||||
// Type is the set of frames to use. See Spinner.
|
||||
Frames Spinner
|
||||
|
||||
// FPS is the speed at which the ticker should tick
|
||||
// FPS is the speed at which the ticker should tick.
|
||||
FPS time.Duration
|
||||
|
||||
// ForegroundColor sets the background color of the spinner. It can be a
|
||||
@@ -44,7 +44,76 @@ type Model struct {
|
||||
// (per github.com/muesli/termenv).
|
||||
BackgroundColor string
|
||||
|
||||
frame int
|
||||
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
||||
// logic around this can be implemented in view that implements this
|
||||
// spinner. If HideFor is set MinimumLifetime will be added on top of
|
||||
// HideFor. In other words, if HideFor is 100ms and MinimumLifetime is
|
||||
// 200ms then MinimumLifetime will expire after 300ms.
|
||||
//
|
||||
// MinimumLifetime is optional.
|
||||
//
|
||||
// This is considered experimental and may not appear in future versions of
|
||||
// this library.
|
||||
MinimumLifetime time.Duration
|
||||
|
||||
// HideFor can be used to wait to show the spinner until a certain amount
|
||||
// of time has passed. This can be useful for preventing flicking when load
|
||||
// times are very fast.
|
||||
// Optional.
|
||||
//
|
||||
// This is considered experimental and may not appear in future versions of
|
||||
// this library.
|
||||
HideFor time.Duration
|
||||
|
||||
frame int
|
||||
startTime time.Time
|
||||
}
|
||||
|
||||
// Start resets resets the spinner start time. For use with MinimumLifetime and
|
||||
// MinimumStartTime. Optional.
|
||||
//
|
||||
// This is considered experimental and may not appear in future versions of
|
||||
// this library.
|
||||
func (m *Model) Start() {
|
||||
m.startTime = time.Now()
|
||||
}
|
||||
|
||||
// hidden returns whether or not Model.HideFor is in effect.
|
||||
func (m Model) hidden() bool {
|
||||
if m.startTime.IsZero() {
|
||||
return false
|
||||
}
|
||||
if m.HideFor == 0 {
|
||||
return false
|
||||
}
|
||||
return m.startTime.Add(m.HideFor).After(time.Now())
|
||||
}
|
||||
|
||||
// finished returns whether Model.MinimumLifetimeReached has been met.
|
||||
func (m Model) finished() bool {
|
||||
if m.startTime.IsZero() {
|
||||
return true
|
||||
}
|
||||
if m.MinimumLifetime == 0 {
|
||||
return true
|
||||
}
|
||||
return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now())
|
||||
}
|
||||
|
||||
// Visible returns whether or not the view should be rendered. Works in
|
||||
// conjunction with Model.HideFor and Model.MinimumLifetimeReached. You should
|
||||
// use this message directly to determine whether or not to render this view in
|
||||
// the parent view and whether to continue sending spin messaging in the
|
||||
// parent update function.
|
||||
//
|
||||
// Also note that using this function is optional and generally considered for
|
||||
// advanced use only. Most of the time your application logic will determine
|
||||
// whether or not this view should be used.
|
||||
//
|
||||
// This is considered experimental and may not appear in future versions of
|
||||
// this library.
|
||||
func (m Model) Visible() bool {
|
||||
return !m.hidden() && !m.finished()
|
||||
}
|
||||
|
||||
// NewModel returns a model with default values.
|
||||
@@ -56,7 +125,9 @@ func NewModel() Model {
|
||||
}
|
||||
|
||||
// TickMsg indicates that the timer has ticked and we should render a frame.
|
||||
type TickMsg struct{}
|
||||
type TickMsg struct {
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -93,7 +164,9 @@ func View(model Model) string {
|
||||
|
||||
// Tick is the command used to advance the spinner one frame.
|
||||
func Tick(m Model) tea.Cmd {
|
||||
return tea.Tick(m.FPS, func(time.Time) tea.Msg {
|
||||
return TickMsg{}
|
||||
return tea.Tick(m.FPS, func(t time.Time) tea.Msg {
|
||||
return TickMsg{
|
||||
Time: t,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@@ -16,15 +16,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// color is a helper for returning colors
|
||||
// color is a helper for returning colors.
|
||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||
)
|
||||
|
||||
// ErrMsg indicates there's been an error. We don't handle errors in the this
|
||||
// package; we're expecting errors to be handle in the program that implements
|
||||
// this text input.
|
||||
type ErrMsg error
|
||||
|
||||
// Model is the Tea model for this text input element.
|
||||
type Model struct {
|
||||
Err error
|
||||
@@ -181,7 +176,6 @@ func (m *Model) handleOverflow() {
|
||||
m.offsetRight = min(m.offsetRight, len(m.value))
|
||||
|
||||
if m.pos < m.offset {
|
||||
|
||||
m.offset = m.pos
|
||||
|
||||
w := 0
|
||||
@@ -196,9 +190,7 @@ func (m *Model) handleOverflow() {
|
||||
}
|
||||
|
||||
m.offsetRight = m.offset + i
|
||||
|
||||
} else if m.pos >= m.offsetRight {
|
||||
|
||||
m.offsetRight = m.pos
|
||||
|
||||
w := 0
|
||||
@@ -213,7 +205,6 @@ func (m *Model) handleOverflow() {
|
||||
}
|
||||
|
||||
m.offset = m.offsetRight - (len(runes) - 1 - i)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,7 +262,7 @@ func (m *Model) wordRight() {
|
||||
i := m.pos
|
||||
|
||||
for i < len(m.value) {
|
||||
if unicode.IsSpace(rune(m.value[i])) {
|
||||
if unicode.IsSpace(m.value[i]) {
|
||||
m.pos++
|
||||
i++
|
||||
} else {
|
||||
@@ -280,7 +271,7 @@ func (m *Model) wordRight() {
|
||||
}
|
||||
|
||||
for i < len(m.value) {
|
||||
if !unicode.IsSpace(rune(m.value[i])) {
|
||||
if !unicode.IsSpace(m.value[i]) {
|
||||
m.pos++
|
||||
i++
|
||||
} else {
|
||||
@@ -386,9 +377,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
}
|
||||
}
|
||||
|
||||
case ErrMsg:
|
||||
m.Err = msg
|
||||
|
||||
case BlinkMsg:
|
||||
m.blink = !m.blink
|
||||
return m, Blink(m)
|
||||
@@ -400,12 +388,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
// View renders the textinput in its current state.
|
||||
func View(model tea.Model) string {
|
||||
m, ok := model.(Model)
|
||||
if !ok {
|
||||
return "could not perform assertion on model"
|
||||
}
|
||||
|
||||
func View(m Model) string {
|
||||
// Placeholder text
|
||||
if len(m.value) == 0 && m.Placeholder != "" {
|
||||
return placeholderView(m)
|
||||
@@ -440,7 +423,7 @@ func View(model tea.Model) string {
|
||||
return m.Prompt + v
|
||||
}
|
||||
|
||||
// placeholderView
|
||||
// placeholderView.
|
||||
func placeholderView(m Model) string {
|
||||
var (
|
||||
v string
|
||||
@@ -449,10 +432,7 @@ func placeholderView(m Model) string {
|
||||
|
||||
// Cursor
|
||||
if m.blink && m.PlaceholderColor != "" {
|
||||
v += cursorView(
|
||||
m.colorPlaceholder(p[:1]),
|
||||
m,
|
||||
)
|
||||
v += cursorView(m.colorPlaceholder(p[:1]), m)
|
||||
} else {
|
||||
v += cursorView(p[:1], m)
|
||||
}
|
||||
|
@@ -73,7 +73,7 @@ func (m *Model) SetContent(s string) {
|
||||
m.lines = strings.Split(s, "\n")
|
||||
}
|
||||
|
||||
// Return the lines that should currently be visible in the viewport
|
||||
// Return the lines that should currently be visible in the viewport.
|
||||
func (m Model) visibleLines() (lines []string) {
|
||||
if len(m.lines) > 0 {
|
||||
top := max(0, m.YOffset)
|
||||
@@ -287,7 +287,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
// Down one page
|
||||
@@ -334,8 +333,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
case tea.MouseMsg:
|
||||
switch msg.Button {
|
||||
|
||||
switch msg.Type {
|
||||
case tea.MouseWheelUp:
|
||||
lines := m.LineUp(3)
|
||||
if m.HighPerformanceRendering {
|
||||
@@ -357,7 +355,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
|
||||
// View renders the viewport into a string.
|
||||
func View(m Model) string {
|
||||
|
||||
if m.HighPerformanceRendering {
|
||||
// Just send newlines since we're doing to be rendering the actual
|
||||
// content seprately. We still need send something that equals the
|
||||
@@ -392,7 +389,3 @@ func max(a, b int) int {
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func clamp(val, low, high int) int {
|
||||
return max(low, min(high, val))
|
||||
}
|
||||
|
Reference in New Issue
Block a user