Skip to content

Commit

Permalink
Add scrollOffEnabled config
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Aug 21, 2023
1 parent 125d4fa commit b2d629b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
11 changes: 10 additions & 1 deletion docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ gui:
windowSize: 'normal' # one of 'normal' | 'half' | 'full' default is 'normal'
scrollHeight: 2 # how many lines you scroll by
scrollPastBottom: true # enable scrolling past the bottom
scrollOffMargin: 2 # how many lines to keep before/after the cursor when it reaches the top/bottom of the view
scrollOffMargin: 2 # how many lines to keep before/after the cursor when it reaches the top/bottom of the view; see 'Scroll-off Margin' section below
scrollOffBehavior: 'margin' # one of 'margin' | 'jump'; see 'Scroll-off Margin' section below
sidePanelWidth: 0.3333 # number from 0 to 1
expandFocusedSidePanel: false
mainPanelSplitMode: 'flexible' # one of 'horizontal' | 'flexible' | 'vertical'
Expand Down Expand Up @@ -349,6 +350,14 @@ or
LG_CONFIG_FILE="$HOME/.base_lg_conf,$HOME/.light_theme_lg_conf" lazygit
```

## Scroll-off Margin

When the selected line gets close to the bottom of the window and you hit down-arrow, there's a feature called "scroll-off margin" that lets the view scroll a little earlier so that you can see a bit of what's coming in the direction that you are moving. This is controlled by the `gui.scrollOffMargin` setting (default: 2), so it keeps 2 lines below the selection visible as you scroll down. It can be set to 0 to scroll only when the selection reaches the bottom of the window.

That's the behavior when `gui.scrollOffBehavior` is set to "margin" (the default). If you set `gui.scrollOffBehavior` to "jump", then upon reaching the last line of a view and hitting down-arrow the view will scroll by half a page so that the selection ends up in the middle of the view. This may feel a little jarring because the cursor jumps around when continuously moving down, but it has the advantage that the view doesn't scroll as often.

This setting applies both to all list views (e.g. commits and branches etc), and to the staging view.

## Color Attributes

For color attributes you can choose an array of attributes (with max one color attribute)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type GuiConfig struct {
ScrollHeight int `yaml:"scrollHeight"`
ScrollPastBottom bool `yaml:"scrollPastBottom"`
ScrollOffMargin int `yaml:"scrollOffMargin"`
ScrollOffBehavior string `yaml:"scrollOffBehavior"`
MouseEvents bool `yaml:"mouseEvents"`
SkipDiscardChangeWarning bool `yaml:"skipDiscardChangeWarning"`
SkipStashWarning bool `yaml:"skipStashWarning"`
Expand Down Expand Up @@ -420,6 +421,7 @@ func GetDefaultConfig() *UserConfig {
ScrollHeight: 2,
ScrollPastBottom: true,
ScrollOffMargin: 2,
ScrollOffBehavior: "margin",
MouseEvents: true,
SkipDiscardChangeWarning: false,
SkipStashWarning: false,
Expand Down
24 changes: 14 additions & 10 deletions pkg/gui/controllers/scroll_off_margin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ import (
// To be called after pressing up-arrow; checks whether the cursor entered the
// top scroll-off margin, and so the view needs to be scrolled up one line
func checkScrollUp(view types.IViewTrait, userConfig *config.UserConfig, lineIdxBefore int, lineIdxAfter int) {
viewPortStart, viewPortHeight := view.ViewPortYBounds()
if userConfig.Gui.ScrollOffBehavior != "jump" {
viewPortStart, viewPortHeight := view.ViewPortYBounds()

linesToScroll := calculateLinesToScrollUp(
viewPortStart, viewPortHeight, userConfig.Gui.ScrollOffMargin, lineIdxBefore, lineIdxAfter)
if linesToScroll != 0 {
view.ScrollUp(linesToScroll)
linesToScroll := calculateLinesToScrollUp(
viewPortStart, viewPortHeight, userConfig.Gui.ScrollOffMargin, lineIdxBefore, lineIdxAfter)
if linesToScroll != 0 {
view.ScrollUp(linesToScroll)
}
}
}

// To be called after pressing down-arrow; checks whether the cursor entered the
// bottom scroll-off margin, and so the view needs to be scrolled down one line
func checkScrollDown(view types.IViewTrait, userConfig *config.UserConfig, lineIdxBefore int, lineIdxAfter int) {
viewPortStart, viewPortHeight := view.ViewPortYBounds()
if userConfig.Gui.ScrollOffBehavior != "jump" {
viewPortStart, viewPortHeight := view.ViewPortYBounds()

linesToScroll := calculateLinesToScrollDown(
viewPortStart, viewPortHeight, userConfig.Gui.ScrollOffMargin, lineIdxBefore, lineIdxAfter)
if linesToScroll != 0 {
view.ScrollDown(linesToScroll)
linesToScroll := calculateLinesToScrollDown(
viewPortStart, viewPortHeight, userConfig.Gui.ScrollOffMargin, lineIdxBefore, lineIdxAfter)
if linesToScroll != 0 {
view.ScrollDown(linesToScroll)
}
}
}

Expand Down

0 comments on commit b2d629b

Please sign in to comment.