mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-17 16:15:58 +03:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
74e84eca55 | ||
|
ccf4c90b6b | ||
|
f341e3c896 | ||
|
c9196e5407 | ||
|
f967f6a87f | ||
|
35c3cd626d | ||
|
2525319d72 | ||
|
e9dd6b06e0 | ||
|
7cef3a6f59 | ||
|
1cdc2045c7 | ||
|
cc480dd2f3 | ||
|
0c1781fbb3 | ||
|
5255143e87 | ||
|
1629afe087 | ||
|
da3150ded7 | ||
|
b82cf5071d | ||
|
d095a6554c | ||
|
34ac608122 | ||
|
09ae5da7c3 | ||
|
5572542e2e | ||
|
3688351ddf | ||
|
d9c03fc0b0 | ||
|
3321ac12a9 | ||
|
0eaea5cc5d | ||
|
0243dff9d3 | ||
|
68ec6c7ffc | ||
|
f332bf2cc2 | ||
|
0b19d41e0a | ||
|
786ec557d4 |
12
go.mod
12
go.mod
@@ -3,9 +3,11 @@ module github.com/charmbracelet/bubbles
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbletea v0.8.0
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83
|
||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
|
||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
|
||||
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.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
|
||||
|
18
go.sum
18
go.sum
@@ -1,5 +1,3 @@
|
||||
github.com/charmbracelet/bubbletea v0.8.0 h1:ruZFaFF+2kgCI1IwNG40KTYDW5ZvE2+hPy4odlBdUko=
|
||||
github.com/charmbracelet/bubbletea v0.8.0/go.mod h1:DzNhKkQQJI30eb+kBcaOs1+z86zTSqcMgSHoFY+uCsg=
|
||||
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 +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.20200526053627-d728968dcf83 h1:AfshZBlqAwhCZ27NJ1aPlMcPBihF1squ1GpaollhLQk=
|
||||
github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83/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-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=
|
||||
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
|
||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/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=
|
||||
|
@@ -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{}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@@ -135,6 +135,9 @@ func (m *Model) handleOverflow() {
|
||||
return
|
||||
}
|
||||
|
||||
// Correct right offset if we've deleted characters
|
||||
m.offsetRight = min(m.offsetRight, len(m.value))
|
||||
|
||||
if m.pos < m.offset {
|
||||
|
||||
m.offset = m.pos
|
||||
@@ -358,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)
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package viewport
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
@@ -9,32 +10,30 @@ import (
|
||||
// MODEL
|
||||
|
||||
type Model struct {
|
||||
Err error
|
||||
Width int
|
||||
Height int
|
||||
Y int
|
||||
|
||||
// YOffset is the vertical scroll position.
|
||||
YOffset int
|
||||
|
||||
// YPosition is the position of the viewport in relation to the terminal
|
||||
// window. It's used in high performance rendering.
|
||||
YPosition int
|
||||
|
||||
// HighPerformanceRendering bypasses the normal Bubble Tea renderer to
|
||||
// provide higher performance rendering. Most of the time the normal Bubble
|
||||
// Tea rendering methods will suffice, but if you're passing content with
|
||||
// a lot of ANSI escape codes you may see improved rendering in certain
|
||||
// terminals with this enabled.
|
||||
//
|
||||
// This should only be used in program occupying the entire terminal,
|
||||
// which is usually via the alternate screen buffer.
|
||||
HighPerformanceRendering bool
|
||||
|
||||
lines []string
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
y := float64(m.Y)
|
||||
h := float64(m.Height)
|
||||
t := float64(len(m.lines))
|
||||
return y / (t - h)
|
||||
}
|
||||
|
||||
// SetContent set the pager's text content.
|
||||
func (m *Model) SetContent(s string) {
|
||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||
m.lines = strings.Split(s, "\n")
|
||||
}
|
||||
|
||||
// NewModel creates a new pager model. Pass the dimensions of the pager.
|
||||
// TODO: do we really need this?
|
||||
func NewModel(width, height int) Model {
|
||||
return Model{
|
||||
Width: width,
|
||||
@@ -42,42 +41,253 @@ func NewModel(width, height int) Model {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: do we really need this?
|
||||
func (m Model) SetSize(yPos, width, height int) {
|
||||
m.YPosition = yPos
|
||||
m.Width = width
|
||||
m.Height = height
|
||||
}
|
||||
|
||||
// AtTop returns whether or not the viewport is in the very top position.
|
||||
func (m Model) AtTop() bool {
|
||||
return m.YOffset <= 0
|
||||
}
|
||||
|
||||
// AtBottom returns whether or not the viewport is at the very botom position.
|
||||
func (m Model) AtBottom() bool {
|
||||
return m.YOffset >= len(m.lines)-m.Height-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
|
||||
}
|
||||
y := float64(m.YOffset)
|
||||
h := float64(m.Height)
|
||||
t := float64(len(m.lines))
|
||||
v := y / (t - h)
|
||||
return math.Max(0.0, math.Min(1.0, v))
|
||||
}
|
||||
|
||||
// SetContent set the pager's text content. For high performance rendering the
|
||||
// Sync command should also be called.
|
||||
func (m *Model) SetContent(s string) {
|
||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||
m.lines = strings.Split(s, "\n")
|
||||
}
|
||||
|
||||
// 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)
|
||||
bottom := min(len(m.lines), m.YOffset+m.Height)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// ViewDown moves the view down by the number of lines in the viewport.
|
||||
// Basically, "page down".
|
||||
func (m *Model) ViewDown() {
|
||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
|
||||
func (m *Model) ViewDown() []string {
|
||||
if m.AtBottom() {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
)
|
||||
|
||||
return m.visibleLines()
|
||||
}
|
||||
|
||||
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
||||
func (m *Model) ViewUp() {
|
||||
m.Y = max(0, m.Y-m.Height)
|
||||
}
|
||||
func (m *Model) ViewUp() []string {
|
||||
if m.AtTop() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HalfViewUp moves the view up by half the height of the viewport.
|
||||
func (m *Model) HalfViewUp() {
|
||||
m.Y = max(0, m.Y-m.Height/2)
|
||||
m.YOffset = max(
|
||||
m.YOffset-m.Height, // target
|
||||
0, // fallback
|
||||
)
|
||||
|
||||
return m.visibleLines()
|
||||
}
|
||||
|
||||
// HalfViewDown moves the view down by half the height of the viewport.
|
||||
func (m *Model) HalfViewDown() {
|
||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)
|
||||
func (m *Model) HalfViewDown() (lines []string) {
|
||||
if m.AtBottom() {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height/2, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := max(m.YOffset+m.Height/2, 0)
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// HalfViewUp moves the view up by half the height of the viewport.
|
||||
func (m *Model) HalfViewUp() (lines []string) {
|
||||
if m.AtTop() {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = max(
|
||||
m.YOffset-m.Height/2, // target
|
||||
0, // fallback
|
||||
)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := max(m.YOffset, 0)
|
||||
bottom := min(m.YOffset+m.Height/2, len(m.lines)-1)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// LineDown moves the view up by the given number of lines.
|
||||
func (m *Model) LineDown(n int) {
|
||||
m.Y = min(len(m.lines)-m.Height, m.Y+n)
|
||||
func (m *Model) LineDown(n int) (lines []string) {
|
||||
if m.AtBottom() || n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+n, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := max(0, m.YOffset+m.Height-n)
|
||||
bottom := min(len(m.lines)-1, m.YOffset+m.Height)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// LineDown moves the view down by the given number of lines.
|
||||
func (m *Model) LineUp(n int) {
|
||||
m.Y = max(0, m.Y-n)
|
||||
// LineUp moves the view down by the given number of lines. Returns the new
|
||||
// lines to show.
|
||||
func (m *Model) LineUp(n int) (lines []string) {
|
||||
if m.AtTop() || n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = max(m.YOffset-n, 0)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := max(0, m.YOffset)
|
||||
bottom := min(len(m.lines)-1, m.YOffset+n)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// COMMANDS
|
||||
|
||||
// Sync tells the renderer where the viewport will be located and requests
|
||||
// a render of the current state of the viewport. It should be called for the
|
||||
// first render and after a window resize.
|
||||
//
|
||||
// For high performance rendering only.
|
||||
func Sync(m Model) tea.Cmd {
|
||||
if len(m.lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
top := max(m.YOffset, 0)
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
||||
|
||||
return tea.SyncScrollArea(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
// ViewDown is a high performance command that moves the viewport up by one
|
||||
// viewport height. Use Model.ViewDown to get the lines that should be
|
||||
// rendered. For example:
|
||||
//
|
||||
// lines := model.ViewDown(1)
|
||||
// cmd := ViewDown(m, lines)
|
||||
//
|
||||
func ViewDown(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollDown(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// ViewUp is a high performance command the moves the viewport down by one
|
||||
// viewport height. Use Model.ViewDown to get the lines that should be
|
||||
// rendered.
|
||||
func ViewUp(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollUp(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// HalfViewDown is a high performance command the moves the viewport down by
|
||||
// half of the height of the viewport. Use Model.HalfViewDown to get the lines
|
||||
// that should be rendered.
|
||||
func HalfViewDown(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollDown(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// HalfViewUp is a high performance command the moves the viewport up by
|
||||
// half of the height of the viewport. Use Model.HalfViewUp to get the lines
|
||||
// that should be rendered.
|
||||
func HalfViewUp(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollUp(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// LineDown is a high performance command the moves the viewport down by
|
||||
// a given number of lines. Use Model.LineDown to get the lines that should be
|
||||
// rendered.
|
||||
func LineDown(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollDown(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// LineDown is a high performance command the moves the viewport up by a given
|
||||
// number of lines. Use Model.LineDown to get the lines that should be
|
||||
// rendered.
|
||||
func LineUp(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tea.ScrollUp(lines, m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
|
||||
// Update runs the update loop with default keybindings. To define your own
|
||||
// keybindings use the methods on Model.
|
||||
// Update runs the update loop with default keybindings similar to popular
|
||||
// pagers. To define your own keybindings use the methods on Model (i.e.
|
||||
// Model.LineDown()) and define your own update function.
|
||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case tea.KeyMsg:
|
||||
@@ -88,60 +298,87 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
case " ": // spacebar
|
||||
fallthrough
|
||||
case "f":
|
||||
m.ViewDown()
|
||||
return m, nil
|
||||
lines := m.ViewDown()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
|
||||
// Up one page
|
||||
case "pgup":
|
||||
fallthrough
|
||||
case "b":
|
||||
m.ViewUp()
|
||||
return m, nil
|
||||
lines := m.ViewUp()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewUp(m, lines)
|
||||
}
|
||||
|
||||
// Down half page
|
||||
case "d":
|
||||
m.HalfViewDown()
|
||||
return m, nil
|
||||
lines := m.HalfViewDown()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewDown(m, lines)
|
||||
}
|
||||
|
||||
// Up half page
|
||||
case "u":
|
||||
m.HalfViewUp()
|
||||
return m, nil
|
||||
lines := m.HalfViewUp()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewUp(m, lines)
|
||||
}
|
||||
|
||||
// Down one line
|
||||
case "down":
|
||||
fallthrough
|
||||
case "j":
|
||||
m.LineDown(1)
|
||||
return m, nil
|
||||
lines := m.LineDown(1)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = LineDown(m, lines)
|
||||
}
|
||||
|
||||
// Up one line
|
||||
case "up":
|
||||
fallthrough
|
||||
case "k":
|
||||
m.LineUp(1)
|
||||
return m, nil
|
||||
lines := m.LineUp(1)
|
||||
if m.HighPerformanceRendering {
|
||||
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, nil
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
// VIEW
|
||||
|
||||
// View renders the viewport into a string.
|
||||
func View(m Model) string {
|
||||
if m.Err != nil {
|
||||
return m.Err.Error()
|
||||
|
||||
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
|
||||
// height of this view so that the Bubble Tea standard renderer can
|
||||
// position anything below this view properly.
|
||||
return strings.Repeat("\n", m.Height-1)
|
||||
}
|
||||
|
||||
var lines []string
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := max(0, m.Y)
|
||||
bottom := min(len(m.lines), m.Y+m.Height)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
lines := m.visibleLines()
|
||||
|
||||
// Fill empty space with newlines
|
||||
extraLines := ""
|
||||
@@ -167,3 +404,7 @@ 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