6 Commits

Author SHA1 Message Date
Christian Rocha
84f7b047bb Make Viewport more idomatic BubbleTea (per v0.12.x) 2020-11-08 21:20:15 -05:00
Christian Rocha
9e1e435bba Make Spinner more idomatic Bubble Tea (per v0.12.x) 2020-11-08 21:20:15 -05:00
Christian Rocha
0fd072ddcc Make Paginator more idomatic Bubble Tea (per v0.12.x) 2020-11-08 21:20:15 -05:00
ololosha228
a07ab1d6af Added more spinners 2020-11-04 14:48:03 +01:00
Christian Rocha
8e49ee609e Bump Bubble Tea dependency to v0.12.2 2020-11-02 10:24:20 -05:00
Christian Rocha
d02641f6b5 Update textinput for multi-char (IME) input in Bubble Tea master 2020-11-01 09:11:18 -05:00
7 changed files with 72 additions and 178 deletions

2
go.mod
View File

@@ -4,7 +4,7 @@ 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/mattn/go-runewidth v0.0.9 github.com/mattn/go-runewidth v0.0.9
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

4
go.sum
View File

@@ -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=

View File

@@ -1,118 +0,0 @@
package layouts
import (
tea "github.com/charmbracelet/bubbletea"
)
// Model is the Bubble Tea model for a vertical layout element.
type Model struct {
Index int
Items []tea.Model
// Focus indicates whether user focus should be on this component
focus bool
}
type FocusItem interface {
Focus() tea.Model
Blur() tea.Model
}
// NewModel creates a new model with default settings.
func NewModel() Model {
return Model{}
}
// Update is the Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
return m, nil
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "shift+tab", "up":
m.Index--
if m.Index < 0 {
m.Index = len(m.Items) - 1
}
m.updateFocus()
case "tab", "down":
m.Index++
if m.Index >= len(m.Items) {
m.Index = 0
}
m.updateFocus()
}
}
cmd := m.updateItems(msg)
return m, cmd
}
// View renders the layout in its current state.
func (m Model) View() string {
var view string
for _, v := range m.Items {
if mi, ok := v.(tea.Model); ok {
view += mi.View() + "\n"
}
}
return view
}
func (m *Model) updateFocus() {
for i, v := range m.Items {
if m.Index == i {
if fi, ok := v.(FocusItem); ok {
// new focused item
m.Items[i] = fi.Focus()
}
} else {
if fi, ok := v.(FocusItem); ok {
m.Items[i] = fi.Blur()
}
}
}
}
// Pass messages and models through to text input components. Only text inputs
// with Focus() set will respond, so it's safe to simply update all of them
// here without any further logic.
func (m *Model) updateItems(msg tea.Msg) tea.Cmd {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
for i, v := range m.Items {
if mi, ok := v.(tea.Model); ok {
m.Items[i], cmd = mi.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
}
}
return tea.Batch(cmds...)
}
// Focused returns the focus state on the model.
func (m Model) Focused() bool {
return m.focus
}
// Focus sets the focus state on the model.
func (m *Model) Focus() {
m.focus = true
m.updateFocus()
}
// Blur removes the focus state on the model.
func (m *Model) Blur() {
m.focus = false
}

View File

@@ -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)
} }

View File

@@ -7,17 +7,36 @@ import (
"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 = []string
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{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "}) Dot = Spinner{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "}
Globe = Spinner{"🌍 ", "🌎 ", "🌏 "}
Moon = Spinner{"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "}
Monkey = Spinner{"🙈 ", "🙈 ", "🙉 ", "🙊 "}
Jump = Spinner{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"}
Bit8 = Spinner{
"", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇",
"⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏",
"⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗",
"⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟",
"⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧",
"⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯",
"⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷",
"⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿",
"⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇",
"⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏",
"⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗",
"⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟",
"⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧",
"⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
"⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷",
"⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"}
color = termenv.ColorProfile().Color color = termenv.ColorProfile().Color
) )
@@ -129,33 +148,39 @@ type TickMsg struct {
Time time.Time Time time.Time
} }
type startTick struct{}
// 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.(type) {
case startTick:
return m, m.tick()
case TickMsg:
m.frame++ m.frame++
if m.frame >= len(m.Frames) { if m.frame >= len(m.Frames) {
m.frame = 0 m.frame = 0
} }
return m, Tick(m) return m, m.tick()
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.Frames) {
return "error" return "error"
} }
frame := model.Frames[model.frame] frame := m.Frames[m.frame]
if model.ForegroundColor != "" || model.BackgroundColor != "" { 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()
} }
@@ -163,7 +188,11 @@ func View(model Model) string {
} }
// Tick is the command used to advance the spinner one frame. // Tick is the command used to advance the spinner one frame.
func Tick(m Model) tea.Cmd { func Tick() tea.Msg {
return startTick{}
}
func (m Model) tick() tea.Cmd {
return tea.Tick(m.FPS, func(t time.Time) tea.Msg { return tea.Tick(m.FPS, func(t time.Time) tea.Msg {
return TickMsg{ return TickMsg{
Time: t, Time: t,

View File

@@ -67,7 +67,6 @@ type Model struct {
Cursor string Cursor string
BlinkSpeed time.Duration BlinkSpeed time.Duration
TextColor string TextColor string
FocusedTextColor string
BackgroundColor string BackgroundColor string
PlaceholderColor string PlaceholderColor string
CursorColor string CursorColor string
@@ -115,7 +114,6 @@ func NewModel() Model {
Placeholder: "", Placeholder: "",
BlinkSpeed: defaultBlinkSpeed, BlinkSpeed: defaultBlinkSpeed,
TextColor: "", TextColor: "",
FocusedTextColor: "205",
PlaceholderColor: "240", PlaceholderColor: "240",
CursorColor: "", CursorColor: "",
EchoCharacter: '*', EchoCharacter: '*',
@@ -184,19 +182,15 @@ 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() tea.Model { func (m *Model) Focus() {
m.focus = true m.focus = true
m.blink = m.cursorMode == cursorHide // show the cursor unless we've explicitly hidden it m.blink = m.cursorMode == cursorHide // show the cursor unless we've explicitly hidden it
return m
} }
// Blur removes the focus state on the model. // Blur removes the focus state on the model.
func (m Model) Blur() tea.Model { func (m *Model) Blur() {
m.focus = false m.focus = false
m.blink = true m.blink = true
return m
} }
// Reset sets the input to its default state with no input. Returns whether // Reset sets the input to its default state with no input. Returns whether
@@ -456,12 +450,8 @@ func (m Model) echoTransform(v string) string {
} }
} }
func (m Model) Init() tea.Cmd {
return Blink
}
// Update is the Bubble Tea update loop. // Update is the Bubble Tea update loop.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus { if !m.focus {
m.blink = true m.blink = true
return m, nil return m, nil
@@ -518,17 +508,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.offset = 0 m.offset = 0
case tea.KeyCtrlV: // ^V paste case tea.KeyCtrlV: // ^V paste
return 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
resetBlink = 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
resetBlink = 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
resetBlink = m.wordRight() resetBlink = m.wordRight()
break break
} }
@@ -536,8 +526,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// 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:]...)...)
resetBlink = m.SetCursor(m.pos + 1) resetBlink = m.SetCursor(m.pos + len(msg.Runes))
} }
} }
@@ -570,14 +560,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// View renders the textinput in its current state. // View renders the textinput in its current state.
func (m Model) View() string { func (m Model) View() string {
prompt := termenv.String(m.Prompt)
if m.focus {
prompt = prompt.Foreground(color(m.FocusedTextColor))
}
// Placeholder text // Placeholder text
if len(m.value) == 0 && m.Placeholder != "" { if len(m.value) == 0 && m.Placeholder != "" {
return prompt.String() + m.placeholderView() return m.placeholderView()
} }
value := m.value[m.offset:m.offsetRight] value := m.value[m.offset:m.offsetRight]
@@ -605,7 +590,7 @@ func (m Model) View() string {
) )
} }
return prompt.String() + v return m.Prompt + v
} }
// placeholderView returns the prompt and placeholder view, if any. // placeholderView returns the prompt and placeholder view, if any.
@@ -625,7 +610,7 @@ func (m Model) placeholderView() string {
// The rest of the placeholder text // The rest of the placeholder text
v += m.colorPlaceholder(p[1:]) v += m.colorPlaceholder(p[1:])
return v return m.Prompt + v
} }
// cursorView styles the cursor. // cursorView styles the cursor.

View File

@@ -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