-
Notifications
You must be signed in to change notification settings - Fork 301
feat: introduce vertical scrollbar #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nervo
wants to merge
1
commit into
charmbracelet:master
Choose a base branch
from
nervo:vertical-scrollbar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Output example.gif | ||
|
||
Require go | ||
|
||
Hide | ||
|
||
Type@0 "go run main.go" Enter | ||
Sleep 3s | ||
|
||
Show | ||
|
||
Space@100ms 100 | ||
Up@100ms 50 | ||
Down@100ms 30 | ||
|
||
Sleep 3s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/charmbracelet/bubbles/scrollbar" | ||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
func newModel() model { | ||
// Viewport | ||
vp := viewport.New(0, 0) | ||
|
||
// Scrollbar | ||
sb := scrollbar.NewVertical() | ||
sb.Style = sb.Style.Border(lipgloss.RoundedBorder(), true) | ||
|
||
return model{ | ||
viewport: vp, | ||
scrollbar: sb, | ||
} | ||
} | ||
|
||
type model struct { | ||
content string | ||
viewport viewport.Model | ||
scrollbar tea.Model | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
cmd tea.Cmd | ||
cmds []tea.Cmd | ||
) | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c", "q", "esc": | ||
return m, tea.Quit | ||
case " ": | ||
if m.content != "" { | ||
m.content += "\n" | ||
} | ||
m.content += fmt.Sprintf("%02d: Lorem ipsum dolor sit amet, consectetur adipiscing elit.", lipgloss.Height(m.content)-1) | ||
} | ||
case tea.WindowSizeMsg: | ||
// Update viewport size | ||
m.viewport.Width = msg.Width - 3 | ||
m.viewport.Height = msg.Height | ||
|
||
// Update scrollbar height | ||
m.scrollbar, cmd = m.scrollbar.Update(scrollbar.HeightMsg(msg.Height)) | ||
cmds = append(cmds, cmd) | ||
} | ||
|
||
m.viewport.SetContent(m.content) | ||
m.viewport, cmd = m.viewport.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
// Update scrollbar viewport | ||
m.scrollbar, cmd = m.scrollbar.Update(m.viewport) | ||
cmds = append(cmds, cmd) | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m model) View() string { | ||
if m.viewport.TotalLineCount() > m.viewport.VisibleLineCount() { | ||
return lipgloss.JoinHorizontal(lipgloss.Left, | ||
m.viewport.View(), | ||
m.scrollbar.View(), | ||
) | ||
} | ||
|
||
return m.viewport.View() | ||
} | ||
|
||
func main() { | ||
p := tea.NewProgram( | ||
newModel(), | ||
tea.WithAltScreen(), | ||
tea.WithMouseCellMotion(), | ||
) | ||
|
||
if _, err := p.Run(); err != nil { | ||
fmt.Println("could not run program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scrollbar | ||
|
||
// Msg signals that scrollbar parameters must be updated. | ||
type Msg struct { | ||
Total int | ||
Visible int | ||
Offset int | ||
} | ||
|
||
// HeightMsg signals that scrollbar height must be updated. | ||
type HeightMsg int | ||
|
||
func min(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package scrollbar | ||
|
||
import ( | ||
"math" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
// NewVertical create a new vertical scrollbar. | ||
func NewVertical() Vertical { | ||
return Vertical{ | ||
Style: lipgloss.NewStyle().Width(1), | ||
ThumbStyle: lipgloss.NewStyle().SetString("█"), | ||
TrackStyle: lipgloss.NewStyle().SetString("░"), | ||
} | ||
} | ||
|
||
// Vertical is the base struct for a vertical scrollbar. | ||
type Vertical struct { | ||
Style lipgloss.Style | ||
ThumbStyle lipgloss.Style | ||
TrackStyle lipgloss.Style | ||
height int | ||
thumbHeight int | ||
thumbOffset int | ||
} | ||
|
||
// Init initializes the scrollbar model. | ||
func (m Vertical) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
// Update updates the scrollbar model. | ||
func (m Vertical) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case Msg: | ||
m.thumbHeight, m.thumbOffset = m.computeThumb(msg.Total, msg.Visible, msg.Offset) | ||
case HeightMsg: | ||
m.height = m.computeHeight(int(msg)) | ||
case viewport.Model: | ||
m.thumbHeight, m.thumbOffset = m.computeThumb(msg.TotalLineCount(), msg.VisibleLineCount(), msg.YOffset) | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m Vertical) computeHeight(height int) int { | ||
return height - m.Style.GetVerticalFrameSize() | ||
} | ||
|
||
func (m Vertical) computeThumb(total, visible, offset int) (int, int) { | ||
ratio := float64(m.height) / float64(total) | ||
|
||
thumbHeight := max(1, int(math.Round(float64(visible)*ratio))) | ||
thumbOffset := max(0, min(m.height-thumbHeight, int(math.Round(float64(offset)*ratio)))) | ||
|
||
return thumbHeight, thumbOffset | ||
} | ||
|
||
// View renders the scrollbar to a string. | ||
func (m Vertical) View() string { | ||
bar := strings.TrimRight( | ||
strings.Repeat(m.TrackStyle.String()+"\n", m.thumbOffset)+ | ||
strings.Repeat(m.ThumbStyle.String()+"\n", m.thumbHeight)+ | ||
strings.Repeat(m.TrackStyle.String()+"\n", max(0, m.height-m.thumbOffset-m.thumbHeight)), | ||
"\n", | ||
) | ||
|
||
return m.Style.Render(bar) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package scrollbar | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
"testing" | ||
) | ||
|
||
func TestVerticalView(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
total int | ||
visible int | ||
offset int | ||
view string | ||
}{ | ||
{ | ||
name: "ThirdTop", | ||
total: 9, | ||
visible: 3, | ||
offset: 0, | ||
view: "█\n░\n░", | ||
}, | ||
{ | ||
name: "ThirdMiddle", | ||
total: 9, | ||
visible: 3, | ||
offset: 3, | ||
view: "░\n█\n░", | ||
}, | ||
{ | ||
name: "ThirdBottom", | ||
total: 9, | ||
visible: 3, | ||
offset: 6, | ||
view: "░\n░\n█", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var scrollbar tea.Model | ||
scrollbar = NewVertical() | ||
scrollbar, _ = scrollbar.Update(HeightMsg(test.visible)) | ||
scrollbar, _ = scrollbar.Update(Msg{ | ||
Total: test.total, | ||
Visible: test.visible, | ||
Offset: test.offset, | ||
}) | ||
view := scrollbar.View() | ||
|
||
if view != test.view { | ||
t.Errorf("expected:\n%s\ngot:\n%s", test.view, view) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use
lipgloss.Top
rather thanlipgloss.Left