mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-17 16:15:58 +03:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f11ca377f4 | ||
|
10022c964c | ||
|
5357dd61bd | ||
|
5a26cb0d8e | ||
|
88469a499e | ||
|
b130d96434 | ||
|
185a19e56f | ||
|
dbb1d93970 | ||
|
2f909886c1 | ||
|
5720cfb35a | ||
|
3c07b97d37 | ||
|
9b3e5476c3 | ||
|
74e84eca55 | ||
|
ccf4c90b6b | ||
|
f341e3c896 | ||
|
c9196e5407 | ||
|
f967f6a87f | ||
|
35c3cd626d |
31
README.md
31
README.md
@@ -1,8 +1,33 @@
|
||||
# Tea Party
|
||||
# Bubbles
|
||||
|
||||
Components for [Tea](https://github.com/charmbraclet/tea)
|
||||
Some components for [Bubble Tea](https://github.com/charmbraclet/bubbletea):
|
||||
|
||||
⚠️ This project is a pre-release! Check back later.
|
||||
* Spinner
|
||||
* Text Input
|
||||
* Paginator
|
||||
* Viewport
|
||||
|
||||
## Spinner
|
||||
|
||||
A spinner, useful for indicating that some kind of invisible operation is
|
||||
happening. There are a couple default ones, but you can also pass your own
|
||||
”frames.”
|
||||
|
||||
## Text Input
|
||||
|
||||
A text input field, akin to an `<input type="text">` in HTML.
|
||||
|
||||
## Paginator
|
||||
|
||||
A component for handling pagination logic and optionally drawing pagination UI.
|
||||
|
||||
## Viewport
|
||||
|
||||
A viewport for vertically scrolling content which optionally includes standard
|
||||
pager keybindings and mouse wheel support. A high performance mode is available
|
||||
for applications which make use of the alterate screen buffer. This is
|
||||
generally only necessary when dealing with content with a very large amount of
|
||||
ANSI escape sequences.
|
||||
|
||||
## License
|
||||
|
||||
|
8
go.mod
8
go.mod
@@ -3,10 +3,12 @@ module github.com/charmbracelet/bubbles
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbletea v0.8.3-0.20200622174044-8bab4bce9a84
|
||||
github.com/atotto/clipboard v0.1.2 // 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.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
|
||||
|
16
go.sum
16
go.sum
@@ -1,3 +1,5 @@
|
||||
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
||||
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
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=
|
||||
@@ -6,19 +8,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=
|
||||
|
@@ -21,17 +21,18 @@ const (
|
||||
|
||||
// Model is the Tea model for this user interface.
|
||||
type Model struct {
|
||||
Type Type
|
||||
Page int
|
||||
PerPage int
|
||||
TotalPages int
|
||||
ActiveDot string
|
||||
InactiveDot string
|
||||
ArabicFormat string
|
||||
UseLeftRightKeys bool
|
||||
UseUpDownKeys bool
|
||||
UseHLKeys bool
|
||||
UseJKKeys bool
|
||||
Type Type
|
||||
Page int
|
||||
PerPage int
|
||||
TotalPages int
|
||||
ActiveDot string
|
||||
InactiveDot string
|
||||
ArabicFormat string
|
||||
UsePgUpPgDownKeys bool
|
||||
UseLeftRightKeys bool
|
||||
UseUpDownKeys bool
|
||||
UseHLKeys bool
|
||||
UseJKKeys bool
|
||||
}
|
||||
|
||||
// SetTotalPages is a helper function for calculatng the total number of pages
|
||||
@@ -98,17 +99,18 @@ func (m Model) OnLastPage() bool {
|
||||
// NewModel creates a new model with defaults.
|
||||
func NewModel() Model {
|
||||
return Model{
|
||||
Type: Arabic,
|
||||
Page: 0,
|
||||
PerPage: 1,
|
||||
TotalPages: 1,
|
||||
ActiveDot: "•",
|
||||
InactiveDot: "○",
|
||||
ArabicFormat: "%d/%d",
|
||||
UseLeftRightKeys: true,
|
||||
UseUpDownKeys: false,
|
||||
UseHLKeys: true,
|
||||
UseJKKeys: false,
|
||||
Type: Arabic,
|
||||
Page: 0,
|
||||
PerPage: 1,
|
||||
TotalPages: 1,
|
||||
ActiveDot: "•",
|
||||
InactiveDot: "○",
|
||||
ArabicFormat: "%d/%d",
|
||||
UsePgUpPgDownKeys: true,
|
||||
UseLeftRightKeys: true,
|
||||
UseUpDownKeys: false,
|
||||
UseHLKeys: true,
|
||||
UseJKKeys: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +118,14 @@ func NewModel() Model {
|
||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
if m.UsePgUpPgDownKeys {
|
||||
switch msg.String() {
|
||||
case "pgup":
|
||||
m.PrevPage()
|
||||
case "pgdown":
|
||||
m.NextPage()
|
||||
}
|
||||
}
|
||||
if m.UseLeftRightKeys {
|
||||
switch msg.String() {
|
||||
case "left":
|
||||
|
@@ -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{}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
rw "github.com/mattn/go-runewidth"
|
||||
"github.com/muesli/termenv"
|
||||
@@ -126,6 +127,47 @@ func (m *Model) Reset() {
|
||||
m.blink = false
|
||||
}
|
||||
|
||||
// Paste pastes the contents of the clipboard into the text area (if supported).
|
||||
func (m *Model) Paste() {
|
||||
pasteString, err := clipboard.ReadAll()
|
||||
if err != nil {
|
||||
m.Err = err
|
||||
}
|
||||
paste := []rune(pasteString)
|
||||
|
||||
availSpace := m.CharLimit - len(m.value)
|
||||
|
||||
// If the char limit's been reached cancel
|
||||
if m.CharLimit > 0 && availSpace <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// If there's not enough space to paste the whole thing cut the pasted
|
||||
// runes down so they'll fit
|
||||
if availSpace < len(paste) {
|
||||
paste = paste[:len(paste)-availSpace]
|
||||
}
|
||||
|
||||
// Stuff before and after the cursor
|
||||
head := m.value[:m.pos]
|
||||
tailSrc := m.value[m.pos:]
|
||||
tail := make([]rune, len(tailSrc))
|
||||
copy(tail, tailSrc)
|
||||
|
||||
// Insert pasted runes
|
||||
for _, r := range paste {
|
||||
head = append(head, r)
|
||||
availSpace--
|
||||
m.pos++
|
||||
if m.CharLimit > 0 && availSpace <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Put it all back together
|
||||
m.value = append(head, tail...)
|
||||
}
|
||||
|
||||
// If a max width is defined, perform some logic to treat the visible area
|
||||
// as a horizontally scrolling viewport.
|
||||
func (m *Model) handleOverflow() {
|
||||
@@ -282,8 +324,10 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
fallthrough
|
||||
case tea.KeyDelete:
|
||||
if len(m.value) > 0 {
|
||||
m.value = append(m.value[:m.pos-1], m.value[m.pos:]...)
|
||||
m.pos--
|
||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||
if m.pos > 0 {
|
||||
m.pos--
|
||||
}
|
||||
}
|
||||
case tea.KeyLeft:
|
||||
if msg.Alt { // alt+left arrow, back one word
|
||||
@@ -320,6 +364,8 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
m.value = m.value[m.pos:]
|
||||
m.pos = 0
|
||||
m.offset = 0
|
||||
case tea.KeyCtrlV: // ^V paste
|
||||
m.Paste()
|
||||
case tea.KeyRune: // input a regular character
|
||||
|
||||
if msg.Alt {
|
||||
@@ -361,12 +407,12 @@ func View(model tea.Model) string {
|
||||
}
|
||||
|
||||
// Placeholder text
|
||||
if m.value == nil && m.Placeholder != "" {
|
||||
if len(m.value) == 0 && m.Placeholder != "" {
|
||||
return placeholderView(m)
|
||||
}
|
||||
|
||||
value := m.value[m.offset:m.offsetRight]
|
||||
pos := m.pos - m.offset
|
||||
pos := max(0, m.pos-m.offset)
|
||||
|
||||
v := m.colorText(string(value[:pos]))
|
||||
|
||||
|
@@ -7,6 +7,10 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
const (
|
||||
spacebar = " "
|
||||
)
|
||||
|
||||
// MODEL
|
||||
|
||||
type Model struct {
|
||||
@@ -53,9 +57,16 @@ func (m Model) AtTop() bool {
|
||||
return m.YOffset <= 0
|
||||
}
|
||||
|
||||
// AtBottom returns whether or not the viewport is at the very botom position.
|
||||
// AtBottom returns whether or not the viewport is at or past the very bottom
|
||||
// position.
|
||||
func (m Model) AtBottom() bool {
|
||||
return m.YOffset >= len(m.lines)-m.Height-1
|
||||
return m.YOffset >= len(m.lines)-1-m.Height
|
||||
}
|
||||
|
||||
// PastBottom returns whether or not the viewport is scrolled beyond the last
|
||||
// line. This can happen when adjusting the viewport height.
|
||||
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.
|
||||
@@ -65,7 +76,7 @@ func (m Model) ScrollPercent() float64 {
|
||||
}
|
||||
y := float64(m.YOffset)
|
||||
h := float64(m.Height)
|
||||
t := float64(len(m.lines))
|
||||
t := float64(len(m.lines) - 1)
|
||||
v := y / (t - h)
|
||||
return math.Max(0.0, math.Min(1.0, v))
|
||||
}
|
||||
@@ -95,8 +106,8 @@ func (m *Model) ViewDown() []string {
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
m.YOffset+m.Height, // target
|
||||
len(m.lines)-1-m.Height, // fallback
|
||||
)
|
||||
|
||||
return m.visibleLines()
|
||||
@@ -123,8 +134,8 @@ func (m *Model) HalfViewDown() (lines []string) {
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height/2, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
m.YOffset+m.Height/2, // target
|
||||
len(m.lines)-1-m.Height, // fallback
|
||||
)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
@@ -156,20 +167,26 @@ func (m *Model) HalfViewUp() (lines []string) {
|
||||
return lines
|
||||
}
|
||||
|
||||
// LineDown moves the view up by the given number of lines.
|
||||
// LineDown moves the view down by the given number of lines.
|
||||
func (m *Model) LineDown(n int) (lines []string) {
|
||||
if m.AtBottom() || n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Make sure the number of lines by which we're going to scroll isn't
|
||||
// greater than the number of lines we actually have left before we reach
|
||||
// the bottom.
|
||||
maxDelta := (len(m.lines) - 1) - (m.YOffset + m.Height) // number of lines - viewport bottom edge
|
||||
n = min(n, maxDelta)
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+n, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
m.YOffset+n, // target
|
||||
len(m.lines)-1-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)
|
||||
top := max(m.YOffset+m.Height-n, 0)
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
@@ -183,11 +200,45 @@ func (m *Model) LineUp(n int) (lines []string) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Make sure the number of lines by which we're going to scroll isn't
|
||||
// greater than the number of lines we are from the top.
|
||||
n = min(n, m.YOffset)
|
||||
|
||||
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)
|
||||
bottom := min(m.YOffset+n, len(m.lines)-1)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// GotoTop sets the viewport to the top position.
|
||||
func (m *Model) GotoTop() (lines []string) {
|
||||
if m.AtTop() {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = 0
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := m.YOffset
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// GotoTop sets the viewport to the bottom position.
|
||||
func (m *Model) GotoBottom() (lines []string) {
|
||||
m.YOffset = max(len(m.lines)-1-m.Height, 0)
|
||||
|
||||
if len(m.lines) > 0 {
|
||||
top := m.YOffset
|
||||
bottom := max(len(m.lines)-1, 0)
|
||||
lines = m.lines[top:bottom]
|
||||
}
|
||||
|
||||
@@ -206,6 +257,8 @@ func Sync(m Model) tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: we should probably use m.visibleLines() rather than these two
|
||||
// expressions.
|
||||
top := max(m.YOffset, 0)
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
||||
|
||||
@@ -216,9 +269,9 @@ func Sync(m Model) tea.Cmd {
|
||||
)
|
||||
}
|
||||
|
||||
// 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:
|
||||
// ViewDown is a high performance command that moves the viewport up by a given
|
||||
// numer of lines. Use Model.ViewDown to get the lines that should be rendered.
|
||||
// For example:
|
||||
//
|
||||
// lines := model.ViewDown(1)
|
||||
// cmd := ViewDown(m, lines)
|
||||
@@ -230,8 +283,8 @@ func ViewDown(m Model, lines []string) tea.Cmd {
|
||||
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
|
||||
// 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
|
||||
// rendered.
|
||||
func ViewUp(m Model, lines []string) tea.Cmd {
|
||||
if len(lines) == 0 {
|
||||
@@ -240,46 +293,6 @@ func ViewUp(m Model, lines []string) tea.Cmd {
|
||||
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 similar to popular
|
||||
@@ -293,20 +306,14 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
// Down one page
|
||||
case "pgdown":
|
||||
fallthrough
|
||||
case " ": // spacebar
|
||||
fallthrough
|
||||
case "f":
|
||||
case "pgdown", spacebar, "f":
|
||||
lines := m.ViewDown()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
|
||||
// Up one page
|
||||
case "pgup":
|
||||
fallthrough
|
||||
case "b":
|
||||
case "pgup", "b":
|
||||
lines := m.ViewUp()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewUp(m, lines)
|
||||
@@ -316,32 +323,44 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
case "d":
|
||||
lines := m.HalfViewDown()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewDown(m, lines)
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
|
||||
// Up half page
|
||||
case "u":
|
||||
lines := m.HalfViewUp()
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewUp(m, lines)
|
||||
cmd = ViewUp(m, lines)
|
||||
}
|
||||
|
||||
// Down one line
|
||||
case "down":
|
||||
fallthrough
|
||||
case "j":
|
||||
case "down", "j":
|
||||
lines := m.LineDown(1)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = LineDown(m, lines)
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
|
||||
// Up one line
|
||||
case "up":
|
||||
fallthrough
|
||||
case "k":
|
||||
case "up", "k":
|
||||
lines := m.LineUp(1)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = LineUp(m, lines)
|
||||
cmd = ViewUp(m, lines)
|
||||
}
|
||||
}
|
||||
|
||||
case tea.MouseMsg:
|
||||
switch msg.Button {
|
||||
|
||||
case tea.MouseWheelUp:
|
||||
lines := m.LineUp(3)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewUp(m, lines)
|
||||
}
|
||||
|
||||
case tea.MouseWheelDown:
|
||||
lines := m.LineDown(3)
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewDown(m, lines)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user