Lay out the side panels with sizes from state.yml

The next commits let the user resize the side panels by dragging the
boundary between them and the main view, and remember the size in
state.yml. This commit turns the remembered sizes into a layout.

A ratio that works well on a laptop screen wastes a lot of space on a
large monitor, so the size is remembered separately for each screen
size at which the user set it. At other screen sizes, the size is
interpolated between the two neighboring ones, or keeps the ratio of
the nearest one beyond the smallest and the largest. With only one
remembered size, this is the same as a fixed ratio.

The size is also remembered separately for each layout, because e.g.
half screen mode and portrait mode need very different sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan HallerandClaude Opus 5.5 committed 2026-11-15 19:40:26 +01:00
1 parent f9b958441d
commit 3e7d633b5f
7 files changed
+409 -12

No files matched your search

+15
View File
@@ -245,6 +245,21 @@ type AppState struct {
ShellCommandsHistory []string `yaml:"customcommandshistory"`
HideCommandLog bool
// The sizes of the side panels that the user set by dragging the boundary
// between them and the main view, for each layout
SidePanelSizes map[SidePanelLayout][]SidePanelSize
}
// SidePanelSize is a size of the side panels that the user set by dragging the
// boundary between them and the main view.
type SidePanelSize struct {
// The size of the screen at the time: its width if the side panels are
// left of the main view, otherwise its height without the line at the
// bottom of the screen
ScreenSize int
// The fraction of ScreenSize that the side panels took up
Ratio float64
}
// SidePanelLayout names an arrangement of the side panels relative to the main
@@ -0,0 +1,78 @@
package helpers
import (
"math"
"slices"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/samber/lo"
)
// minSideSectionSize is the smallest size of the side section, and of the main
// section, when the user has set the size of the side panels. It leaves room
// for one line or column inside the frame.
const minSideSectionSize = 3
// sidePanelSizeForScreen returns the size of the side panels for a screen of the
// given size, based on the sizes that the user set at other screen sizes (sorted
// by ScreenSize). Between two of those screen sizes, it interpolates. Beyond the
// smallest or the largest one, it keeps the ratio of that one. It returns false
// if the user hasn't set any.
func sidePanelSizeForScreen(sizes []config.SidePanelSize, screenSize int) (int, bool) {
if len(sizes) == 0 {
return 0, false
}
sizeFromRatio := func(ratio float64) int {
return int(math.Round(ratio * float64(screenSize)))
}
first, last := sizes[0], sizes[len(sizes)-1]
if screenSize <= first.ScreenSize {
return sizeFromRatio(first.Ratio), true
}
if screenSize >= last.ScreenSize {
return sizeFromRatio(last.Ratio), true
}
// We interpolate the size rather than the ratio. Interpolating the ratio
// can result in a size that is outside the range of the two sizes, e.g.
// wider than both of them.
i := slices.IndexFunc(sizes, func(size config.SidePanelSize) bool {
return size.ScreenSize >= screenSize
})
lower, upper := sizes[i-1], sizes[i]
lowerSize := lower.Ratio * float64(lower.ScreenSize)
upperSize := upper.Ratio * float64(upper.ScreenSize)
t := float64(screenSize-lower.ScreenSize) / float64(upper.ScreenSize-lower.ScreenSize)
return int(math.Round(lowerSize + t*(upperSize-lowerSize))), true
}
func clampSideSectionSize(size int, screenSize int) int {
return max(minSideSectionSize, min(size, screenSize-minSideSectionSize))
}
// ValidSidePanelSizes returns the side panel sizes from state.yml without the
// ones that can't be used (e.g. because the file was edited by hand), sorted by
// screen size as sidePanelSizeForScreen expects them.
func ValidSidePanelSizes(
sizesByLayout map[config.SidePanelLayout][]config.SidePanelSize,
) map[config.SidePanelLayout][]config.SidePanelSize {
result := map[config.SidePanelLayout][]config.SidePanelSize{}
for layout, sizes := range sizesByLayout {
valid := lo.Filter(sizes, func(size config.SidePanelSize, _ int) bool {
return size.ScreenSize > 0 && size.Ratio > 0 && size.Ratio < 1
})
slices.SortStableFunc(valid, func(a, b config.SidePanelSize) int {
return a.ScreenSize - b.ScreenSize
})
// Interpolating between two sizes for the same screen size would divide
// by zero, so keep only the last one.
valid = lo.Filter(valid, func(size config.SidePanelSize, i int) bool {
return i == len(valid)-1 || valid[i+1].ScreenSize != size.ScreenSize
})
if len(valid) > 0 {
result[layout] = valid
}
}
return result
}
@@ -0,0 +1,120 @@
package helpers
import (
"testing"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestSidePanelSizeForScreen(t *testing.T) {
laptopAndMonitor := []config.SidePanelSize{
{ScreenSize: 160, Ratio: 0.3125}, // 50 columns
{ScreenSize: 380, Ratio: 0.2}, // 76 columns
}
scenarios := []struct {
name string
sizes []config.SidePanelSize
screenSize int
expectedSize int
expectedOk bool
}{
{
name: "no sizes",
sizes: nil,
screenSize: 100,
expectedOk: false,
},
{
name: "a single size keeps its ratio at any screen size",
sizes: []config.SidePanelSize{{ScreenSize: 160, Ratio: 0.25}},
screenSize: 80,
expectedSize: 20,
expectedOk: true,
},
{
name: "at a screen size that the user set it for",
sizes: laptopAndMonitor,
screenSize: 380,
expectedSize: 76,
expectedOk: true,
},
{
name: "below the smallest screen size, keeps its ratio",
sizes: laptopAndMonitor,
screenSize: 80,
expectedSize: 25,
expectedOk: true,
},
{
name: "above the largest screen size, keeps its ratio",
sizes: laptopAndMonitor,
screenSize: 500,
expectedSize: 100,
expectedOk: true,
},
{
name: "in between, interpolates the size",
sizes: laptopAndMonitor,
screenSize: 270,
expectedSize: 63,
expectedOk: true,
},
{
name: "in between, interpolates the size rather than the ratio",
sizes: []config.SidePanelSize{
{ScreenSize: 100, Ratio: 0.5}, // 50 columns
{ScreenSize: 400, Ratio: 0.1}, // 40 columns
},
screenSize: 250,
expectedSize: 45,
expectedOk: true,
},
{
name: "picks the two neighboring sizes",
sizes: []config.SidePanelSize{
{ScreenSize: 100, Ratio: 0.3},
{ScreenSize: 200, Ratio: 0.3},
{ScreenSize: 300, Ratio: 0.2},
},
screenSize: 250,
expectedSize: 60,
expectedOk: true,
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
size, ok := sidePanelSizeForScreen(s.sizes, s.screenSize)
assert.Equal(t, s.expectedOk, ok)
assert.Equal(t, s.expectedSize, size)
})
}
}
func TestValidSidePanelSizes(t *testing.T) {
input := map[config.SidePanelLayout][]config.SidePanelSize{
config.SidePanelLayoutNormal: {
{ScreenSize: 300, Ratio: 0.2},
{ScreenSize: 0, Ratio: 0.3},
{ScreenSize: 100, Ratio: 0.4},
{ScreenSize: 200, Ratio: 0},
{ScreenSize: 200, Ratio: 1},
{ScreenSize: 300, Ratio: 0.25},
},
config.SidePanelLayoutHalfScreen: {
{ScreenSize: -1, Ratio: 0.5},
},
}
assert.Equal(t,
map[config.SidePanelLayout][]config.SidePanelSize{
config.SidePanelLayoutNormal: {
{ScreenSize: 100, Ratio: 0.4},
{ScreenSize: 300, Ratio: 0.25},
},
},
ValidSidePanelSizes(input),
)
}
@@ -80,6 +80,9 @@ type WindowArrangementArgs struct {
InSearchPrompt bool
// One of '' (not searching), 'Search: ', and 'Filter: '
SearchPrefix string
// The sizes of the side panels that the user set, for each layout (see
// config.AppState.SidePanelSizes)
SidePanelSizes map[config.SidePanelLayout][]config.SidePanelSize
}
func (self *WindowArrangementHelper) GetWindowDimensions(informationStr string, appStatus string) map[string]boxlayout.Dimensions {
@@ -112,6 +115,7 @@ func (self *WindowArrangementHelper) GetWindowDimensions(informationStr string,
IsAnyModeActive: self.modeHelper.IsAnyModeActive(),
InSearchPrompt: repoState.InSearchPrompt(),
SearchPrefix: searchPrefix,
SidePanelSizes: self.c.State().GetSidePanelSizes(),
}
return GetWindowDimensions(args)
@@ -141,24 +145,29 @@ func GetWindowDimensions(args WindowArrangementArgs) map[string]boxlayout.Dimens
sidePanelsDirection = boxlayout.ROW
}
sideSectionBox := &boxlayout.Box{
Direction: boxlayout.ROW,
Weight: sideSectionWeight,
ConditionalChildren: sidePanelChildren(args),
}
mainSectionBox := &boxlayout.Box{
Direction: boxlayout.ROW,
Weight: mainSectionWeight,
Children: mainPanelChildren(args),
}
if size, ok := sideSectionSizeSetByUser(args); ok {
sideSectionBox.Weight = 0
sideSectionBox.Size = size
mainSectionBox.Weight = 1
}
root := &boxlayout.Box{
Direction: boxlayout.ROW,
Children: []*boxlayout.Box{
{
Direction: sidePanelsDirection,
Weight: 1,
Children: []*boxlayout.Box{
{
Direction: boxlayout.ROW,
Weight: sideSectionWeight,
ConditionalChildren: sidePanelChildren(args),
},
{
Direction: boxlayout.ROW,
Weight: mainSectionWeight,
Children: mainPanelChildren(args),
},
},
Children: []*boxlayout.Box{sideSectionBox, mainSectionBox},
},
{
Direction: boxlayout.COLUMN,
@@ -254,6 +263,32 @@ func sidePanelLayout(args WindowArrangementArgs) (config.SidePanelLayout, bool)
return config.SidePanelLayoutNormal, true
}
// sidePanelScreenSize returns the size of the screen that config.SidePanelSize
// refers to.
func sidePanelScreenSize(args WindowArrangementArgs) int {
if shouldUsePortraitMode(args) {
return args.Height - infoSectionSize(args)
}
return args.Width
}
// sideSectionSizeSetByUser returns the size of the side section if the user has
// set the size of the side panels for the current layout. Otherwise, the weights
// from getMidSectionWeights apply.
func sideSectionSizeSetByUser(args WindowArrangementArgs) (int, bool) {
layout, ok := sidePanelLayout(args)
if !ok {
return 0, false
}
screenSize := sidePanelScreenSize(args)
size, ok := sidePanelSizeForScreen(args.SidePanelSizes[layout], screenSize)
if !ok {
return 0, false
}
return clampSideSectionSize(size, screenSize), true
}
func isMainWindow(window string) bool {
return window == "main" || window == "secondary"
}
@@ -439,6 +439,140 @@ func TestGetWindowDimensions(t *testing.T) {
B: information
`,
},
{
name: "side panel size set by user",
mutateArgs: func(args *WindowArrangementArgs) {
args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{
config.SidePanelLayoutNormal: {{ScreenSize: 75, Ratio: 0.2}},
}
},
expected: `
╭status───────╮╭main──────────────────────────────────────────────────────╮
│ ││ │
╰─────────────╯│ │
╭files────────╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─────────────╯│ │
╭branches─────╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─────────────╯│ │
╭commits──────╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─────────────╯│ │
╭stash────────╮│ │
│ ││ │
╰─────────────╯╰──────────────────────────────────────────────────────────╯
<options──────────────────────────────────────────────────────>A<B────────>
A: statusSpacer1
B: information
`,
},
{
name: "side panel size set by user is too small",
mutateArgs: func(args *WindowArrangementArgs) {
args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{
config.SidePanelLayoutNormal: {{ScreenSize: 75, Ratio: 0.01}},
}
},
expected: `
╭A╮╭main──────────────────────────────────────────────────────────────────╮
│ ││ │
╰─╯│ │
╭B╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─╯│ │
╭C╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─╯│ │
╭D╮│ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰─╯│ │
╭E╮│ │
│ ││ │
╰─╯╰──────────────────────────────────────────────────────────────────────╯
<options──────────────────────────────────────────────────────>F<G────────>
A: status
B: files
C: branches
D: commits
E: stash
F: statusSpacer1
G: information
`,
},
{
name: "side panel size set by user in half screen mode, enlargedSideViewLocation top",
mutateArgs: func(args *WindowArrangementArgs) {
args.ScreenMode = types.SCREEN_HALF
args.UserConfig.Gui.EnlargedSideViewLocation = "top"
// The screen size doesn't include the line at the bottom
args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{
config.SidePanelLayoutHalfScreenTop: {{ScreenSize: 29, Ratio: 0.4}},
}
},
expected: `
╭status───────────────────────────────────────────────────────────────────╮
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
╰─────────────────────────────────────────────────────────────────────────╯
╭main─────────────────────────────────────────────────────────────────────╮
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
╰─────────────────────────────────────────────────────────────────────────╯
<options──────────────────────────────────────────────────────>A<B────────>
A: statusSpacer1
B: information
`,
},
{
name: "half screen mode, enlargedSideViewLocation left",
mutateArgs: func(args *WindowArrangementArgs) {
+14
View File
@@ -121,6 +121,10 @@ type Gui struct {
// the extras window contains things like the command log
ShowExtrasWindow bool
// The sizes of the side panels that the user set, for each layout (see
// config.AppState.SidePanelSizes)
SidePanelSizes map[config.SidePanelLayout][]config.SidePanelSize
PopupHandler types.IPopupHandler
// Bumped every time we switch to a different repository (in resetState).
@@ -202,6 +206,10 @@ func (self *StateAccessor) SetShowExtrasWindow(value bool) {
self.gui.ShowExtrasWindow = value
}
func (self *StateAccessor) GetSidePanelSizes() map[config.SidePanelLayout][]config.SidePanelSize {
return self.gui.SidePanelSizes
}
func (self *StateAccessor) GetRetainOriginalDir() bool {
return self.gui.RetainOriginalDir
}
@@ -874,11 +882,17 @@ func NewGui(
gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui}
gui.stateAccessor = &StateAccessor{gui: gui}
gui.loadSidePanelSizes()
gui.diffRendererConfig = config.NewDiffRendererConfigManager(func() *config.UserConfig { return gui.UserConfig() })
return gui, nil
}
func (gui *Gui) loadSidePanelSizes() {
gui.SidePanelSizes = helpers.ValidSidePanelSizes(gui.c.ReadAppState().SidePanelSizes)
}
var RuneReplacements = map[rune]string{
// for the commit graph
graph.MergeSymbol: "M",
+1
View File
@@ -441,6 +441,7 @@ type IStateAccessor interface {
SetUpdating(bool)
GetShowExtrasWindow() bool
SetShowExtrasWindow(bool)
GetSidePanelSizes() map[config.SidePanelLayout][]config.SidePanelSize
GetRetainOriginalDir() bool
SetRetainOriginalDir(bool)
GetItemOperation(item HasUrn) ItemOperation