mirror of
https://github.com/Maks1mS/bubbles.git
synced 2024-12-24 14:44:38 +03:00
Viewport now can use Bubble Tea high performance scroll renderer (ish)
This commit is contained in:
parent
d9c03fc0b0
commit
3688351ddf
5
go.mod
5
go.mod
@ -5,7 +5,8 @@ go 1.13
|
||||
require (
|
||||
github.com/charmbracelet/bubbletea v0.8.1
|
||||
github.com/mattn/go-runewidth v0.0.9
|
||||
github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83
|
||||
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924
|
||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
|
||||
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
|
||||
)
|
||||
|
||||
replace github.com/charmbracelet/bubbletea => ../bubbletea
|
||||
|
12
go.sum
12
go.sum
@ -1,5 +1,3 @@
|
||||
github.com/charmbracelet/bubbletea v0.8.1 h1:Q7qdKOl4LJa9QV/LDwFWa9DFlZwwJkQipXrrfu/+dJM=
|
||||
github.com/charmbracelet/bubbletea v0.8.1/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,8 +6,8 @@ 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.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=
|
||||
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/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=
|
||||
@ -21,8 +19,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
|
||||
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-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
|
||||
golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
|
||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@ -1,7 +1,6 @@
|
||||
package viewport
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
@ -17,34 +16,36 @@ type Model struct {
|
||||
// YOffset is the vertical scroll position.
|
||||
YOffset int
|
||||
|
||||
// Y is the position of the viewport in relation to the terminal window.
|
||||
// It's used in high performance rendering.
|
||||
Y int
|
||||
// YPosition is the position of the viewport in relation to the terminal
|
||||
// window. It's used in high performance rendering.
|
||||
YPosition int
|
||||
|
||||
// UseInternalRenderer specifies whether or not to use the pager's
|
||||
// internal, high performance renderer to paint the screen.
|
||||
UseInternalRenderer bool
|
||||
// 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 width,
|
||||
// usually via the alternate screen buffer.
|
||||
HighPerformanceRendering bool
|
||||
|
||||
lines []string
|
||||
|
||||
r renderer
|
||||
}
|
||||
|
||||
func NewModel(yPos, width, height, terminalWidth, terminalHeight int) Model {
|
||||
func NewModel(width, height int) Model {
|
||||
return Model{
|
||||
Width: width,
|
||||
Height: height,
|
||||
UseInternalRenderer: true,
|
||||
r: renderer{
|
||||
Y: yPos,
|
||||
Height: height,
|
||||
TerminalWidth: terminalWidth,
|
||||
TerminalHeight: terminalHeight,
|
||||
Out: os.Stdout,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) SetSize(yPos int, 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
|
||||
@ -71,10 +72,10 @@ func (m *Model) SetContent(s string) {
|
||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||
m.lines = strings.Split(s, "\n")
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset, 0)
|
||||
bottom := min(m.YOffset+m.Height, len(m.lines))
|
||||
m.r.sync(m.lines[top:bottom])
|
||||
if m.HighPerformanceRendering {
|
||||
//top := max(m.YOffset, 0)
|
||||
//bottom := min(m.YOffset+m.Height, len(m.lines))
|
||||
//m.r.sync(m.lines[top:bottom])
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,12 +86,6 @@ func (m *Model) ViewDown() {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset+m.Height, 0)
|
||||
bottom := min(top+m.Height, len(m.lines)-1)
|
||||
m.r.insertBottom(m.lines[top:bottom])
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
@ -103,12 +98,6 @@ func (m *Model) ViewUp() {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset-m.Height, 0)
|
||||
bottom := min(m.YOffset, len(m.lines))
|
||||
m.r.insertTop(m.lines[top:bottom])
|
||||
}
|
||||
|
||||
m.YOffset = max(
|
||||
m.YOffset-m.Height, // target
|
||||
0, // fallback
|
||||
@ -121,12 +110,6 @@ func (m *Model) HalfViewDown() {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset+m.Height/2, 0)
|
||||
bottom := min(top+m.Height, len(m.lines)-1)
|
||||
m.r.insertBottom(m.lines[top:bottom])
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+m.Height/2, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
@ -139,12 +122,6 @@ func (m *Model) HalfViewUp() {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset-m.Height/2, 0)
|
||||
bottom := clamp(m.YOffset, top, len(m.lines))
|
||||
m.r.insertTop(m.lines[top:bottom])
|
||||
}
|
||||
|
||||
m.YOffset = max(
|
||||
m.YOffset-m.Height/2, // target
|
||||
0, // fallback
|
||||
@ -157,12 +134,6 @@ func (m *Model) LineDown(n int) {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
bottom := min(m.YOffset+m.Height+n, len(m.lines))
|
||||
top := max(bottom-n, 0)
|
||||
m.r.insertBottom(m.lines[top:bottom])
|
||||
}
|
||||
|
||||
m.YOffset = min(
|
||||
m.YOffset+n, // target
|
||||
len(m.lines)-m.Height, // fallback
|
||||
@ -175,13 +146,103 @@ func (m *Model) LineUp(n int) {
|
||||
return
|
||||
}
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
top := max(m.YOffset-n, 0)
|
||||
bottom := min(top+n, len(m.lines))
|
||||
m.r.insertTop(m.lines[top:bottom])
|
||||
m.YOffset = max(m.YOffset-n, 0)
|
||||
}
|
||||
|
||||
// COMMANDS
|
||||
|
||||
func Sync(m Model) tea.Cmd {
|
||||
return tea.ReplaceIgnoredLines(m.YPosition, m.YPosition+m.Height)
|
||||
}
|
||||
|
||||
func ViewDown(m Model) tea.Cmd {
|
||||
if m.AtBottom() {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.YOffset = max(m.YOffset-n, 0)
|
||||
top := max(m.YOffset+m.Height, 0)
|
||||
bottom := min(top+m.Height, len(m.lines)-1)
|
||||
|
||||
return tea.ScrollDown(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
func ViewUp(m Model) tea.Cmd {
|
||||
if m.AtTop() {
|
||||
return nil
|
||||
}
|
||||
|
||||
top := max(m.YOffset-m.Height, 0)
|
||||
bottom := min(m.YOffset, len(m.lines))
|
||||
|
||||
return tea.ScrollUp(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
func HalfViewDown(m Model) tea.Cmd {
|
||||
if m.AtBottom() {
|
||||
return nil
|
||||
}
|
||||
|
||||
top := max(m.YOffset+m.Height/2, 0)
|
||||
bottom := min(top+m.Height, len(m.lines)-1)
|
||||
|
||||
return tea.ScrollDown(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
func HalfViewUp(m Model) tea.Cmd {
|
||||
if m.AtTop() {
|
||||
return nil
|
||||
}
|
||||
|
||||
top := max(m.YOffset-m.Height/2, 0)
|
||||
bottom := clamp(m.YOffset, top, len(m.lines))
|
||||
|
||||
return tea.ScrollUp(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
func LineDown(m Model, n int) tea.Cmd {
|
||||
if m.AtBottom() || n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
bottom := min(m.YOffset+m.Height+n, len(m.lines))
|
||||
top := max(bottom-n, 0)
|
||||
|
||||
return tea.ScrollDown(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
func LineUp(m Model, n int) tea.Cmd {
|
||||
if m.AtTop() || n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
top := max(m.YOffset-n, 0)
|
||||
bottom := min(top+n, len(m.lines))
|
||||
|
||||
return tea.ScrollUp(
|
||||
m.lines[top:bottom],
|
||||
m.YPosition,
|
||||
m.YPosition+m.Height,
|
||||
)
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
@ -189,7 +250,15 @@ func (m *Model) LineUp(n int) {
|
||||
// Update runs the update loop with default keybindings. To define your own
|
||||
// keybindings use the methods on Model 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.WindowSizeMsg:
|
||||
m.Width = msg.Width
|
||||
m.Height = msg.Height
|
||||
//if m.HighPerformanceRendering {
|
||||
//cmd = tea.ReplaceIgnoredLines(m.YPosition, m.YPosition+m.Height)
|
||||
//}
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
@ -199,43 +268,55 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
case " ": // spacebar
|
||||
fallthrough
|
||||
case "f":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewDown(m)
|
||||
}
|
||||
m.ViewDown()
|
||||
return m, nil
|
||||
|
||||
// Up one page
|
||||
case "pgup":
|
||||
fallthrough
|
||||
case "b":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = ViewUp(m)
|
||||
}
|
||||
m.ViewUp()
|
||||
return m, nil
|
||||
|
||||
// Down half page
|
||||
case "d":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewDown(m)
|
||||
}
|
||||
m.HalfViewDown()
|
||||
return m, nil
|
||||
|
||||
// Up half page
|
||||
case "u":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = HalfViewUp(m)
|
||||
}
|
||||
m.HalfViewUp()
|
||||
return m, nil
|
||||
|
||||
// Down one line
|
||||
case "down":
|
||||
fallthrough
|
||||
case "j":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = LineDown(m, 1)
|
||||
}
|
||||
m.LineDown(1)
|
||||
return m, nil
|
||||
|
||||
// Up one line
|
||||
case "up":
|
||||
fallthrough
|
||||
case "k":
|
||||
if m.HighPerformanceRendering {
|
||||
cmd = LineUp(m, 1)
|
||||
}
|
||||
m.LineUp(1)
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
// VIEW
|
||||
@ -243,7 +324,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
||||
// View renders the viewport into a string.
|
||||
func View(m Model) string {
|
||||
|
||||
if m.UseInternalRenderer {
|
||||
if m.HighPerformanceRendering {
|
||||
// Skip over the area that would normally be rendered
|
||||
return cursorDownString(m.Height)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user