1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/context/view_trait.go

87 lines
1.9 KiB
Go
Raw Normal View History

2022-01-29 10:09:20 +02:00
package context
import (
"github.com/jesseduffield/gocui"
2022-02-05 08:04:10 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-01-29 10:09:20 +02:00
)
const HORIZONTAL_SCROLL_FACTOR = 3
type ViewTrait struct {
2022-02-05 08:04:10 +02:00
view *gocui.View
2022-01-29 10:09:20 +02:00
}
2022-02-05 08:04:10 +02:00
var _ types.IViewTrait = &ViewTrait{}
func NewViewTrait(view *gocui.View) *ViewTrait {
return &ViewTrait{view: view}
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) FocusPoint(yIdx int) {
2022-02-05 08:04:10 +02:00
self.view.FocusPoint(self.view.OriginX(), yIdx)
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) SetViewPortContent(content string) {
2022-02-05 08:04:10 +02:00
_, y := self.view.Origin()
self.view.OverwriteLines(y, content)
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) SetContent(content string) {
2022-02-05 08:04:10 +02:00
self.view.SetContent(content)
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) SetHighlight(highlight bool) {
self.view.Highlight = highlight
}
2022-01-29 10:09:20 +02:00
func (self *ViewTrait) SetFooter(value string) {
2022-02-05 08:04:10 +02:00
self.view.Footer = value
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) SetOriginX(value int) {
2022-02-05 08:04:10 +02:00
_ = self.view.SetOriginX(value)
2022-01-29 10:09:20 +02:00
}
2022-04-16 07:27:56 +02:00
// tells us the start of line indexes shown in the view currently as well as the capacity of lines shown in the viewport.
2022-01-29 10:09:20 +02:00
func (self *ViewTrait) ViewPortYBounds() (int, int) {
2022-04-16 07:27:56 +02:00
_, start := self.view.Origin()
length := self.view.InnerHeight() + 1
return start, length
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) ScrollLeft() {
2022-04-16 07:27:56 +02:00
self.view.ScrollLeft(self.horizontalScrollAmount())
2022-01-29 10:09:20 +02:00
}
func (self *ViewTrait) ScrollRight() {
2022-04-16 07:27:56 +02:00
self.view.ScrollRight(self.horizontalScrollAmount())
}
func (self *ViewTrait) horizontalScrollAmount() int {
return self.view.InnerWidth() / HORIZONTAL_SCROLL_FACTOR
}
func (self *ViewTrait) ScrollUp(value int) {
self.view.ScrollUp(value)
2022-04-16 07:27:56 +02:00
}
func (self *ViewTrait) ScrollDown(value int) {
self.view.ScrollDown(value)
2022-01-29 10:09:20 +02:00
}
// this returns the amount we'll scroll if we want to scroll by a page.
func (self *ViewTrait) PageDelta() int {
2022-02-05 08:04:10 +02:00
_, height := self.view.Size()
2022-01-29 10:09:20 +02:00
delta := height - 1
if delta == 0 {
return 1
}
return delta
}
func (self *ViewTrait) SelectedLineIdx() int {
2022-02-05 08:04:10 +02:00
return self.view.SelectedLineIdx()
2022-01-29 10:09:20 +02:00
}