Files
lazygit/pkg/gui/layout.go
Stefan HallerandClaude Opus 5.5 89f269af58 Rewrite the update check that u in the status panel starts
Checking for an update with `u` has several problems:

- It offers to download and install the new version for any build
  that has a version, including builds from Homebrew or a Linux
  distribution. Overwriting those makes the package manager report the
  wrong version.
- It only logs its errors, so the user never learns why nothing
  happens. This includes "You already have the latest version". For a
  build from a git checkout, the version is a commit hash; comparing it
  with the release version fails, and the user sees nothing at all.
- Installing always fails on Windows.

Rewrite it on top of the functions from the previous commits. Show the
result of the check in all cases. If there's an update, show a menu
that depends on how lazygit was installed. We only offer "Install now"
for our release archives, and only if we can write to the folder of
the executable. For package managers that we know, we show the command
that updates lazygit. Otherwise we tell the user where to get the new
version. Disable `u` for dev builds and say why.

Installing replaces the executable with a rename, so quitting during
an update can no longer leave a broken binary behind. Instead of
asking for confirmation, wait for the rename to finish before exiting.

Remove the check at startup for now. It never ran, because it checked
for the build source "buildBinary" while .goreleaser.yml sets
"binaryRelease". A later commit adds a new one.

Use os.Executable instead of the osext package; os.Executable didn't
exist yet when this code was written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:11:57 +01:00

346 lines
11 KiB
Go

package gui
import (
"errors"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)
// layout is called for every screen re-render e.g. when the screen is resized
func (gui *Gui) layout(g *gocui.Gui) error {
if !gui.ViewsSetup {
gui.printCommandLogHeader()
if _, err := gui.g.SetCurrentView(gui.defaultSideContext().GetViewName()); err != nil {
return err
}
}
g.Highlight = true
width, height := g.Size()
informationStr := gui.informationStr()
var appStatus string
appStatusView, err := g.View("appStatus")
if err == nil {
appStatus = utils.Decolorise(appStatusView.Buffer())
}
viewDimensions := gui.getWindowDimensions(informationStr, appStatus)
// reading more lines into main view buffers upon resize
prevMainView := gui.Views.Main
if prevMainView != nil {
prevMainHeight := prevMainView.Height()
newMainHeight := viewDimensions["main"].Y1 - viewDimensions["main"].Y0 + 1
if newMainHeight > prevMainHeight {
// The main views have grown taller, so make sure enough lines are
// loaded to fill them. The views haven't been resized yet at this
// point, so we can't rely on their current height; compute the target
// total from the new height instead. (Reading past the actual content
// is harmless: ReadLines stops at the end of input.)
linesToRead := prevMainView.OriginY() + newMainHeight
if manager := gui.getViewBufferManagerForView(gui.Views.Main); manager != nil {
manager.ReadLines(linesToRead)
}
if manager := gui.getViewBufferManagerForView(gui.Views.Secondary); manager != nil {
manager.ReadLines(linesToRead)
}
}
}
contextsToRerender := []types.Context{}
// we assume that the view has already been created.
setViewFromDimensions := func(context types.Context) (*gocui.View, error) {
viewName := context.GetViewName()
windowName := context.GetWindowName()
dimensionsObj, ok := viewDimensions[windowName]
view, err := g.View(viewName)
if err != nil {
return nil, err
}
if !ok {
// view not specified in dimensions object: so create the view and hide it
// making the view take up the whole space in the background in case it needs
// to render content as soon as it appears, because lazyloaded content (via a pty task)
// cares about the size of the view.
_, err := g.SetView(viewName, 0, 0, width, height, 0)
view.Visible = false
return view, err
}
frameOffset := 1
if view.Frame {
frameOffset = 0
}
mustRerender := false
newHeight := dimensionsObj.Y1 - dimensionsObj.Y0 + 2*frameOffset
maxOriginY := context.TotalContentHeight()
if !view.CanScrollPastBottom {
maxOriginY -= newHeight - 1
}
// Don't scroll up while the view's content is still being loaded: its
// height only reflects what has been read so far, so clamping to it now
// would yank the view to the top even though more content is on the way
// (e.g. when re-rendering a diff the user was scrolled into).
manager := gui.getViewBufferManagerForView(view)
stillLoading := manager != nil && manager.IsLoading()
if oldOriginY := view.OriginY(); oldOriginY > maxOriginY && !stillLoading {
view.ScrollUp(oldOriginY - maxOriginY)
// the view might not have scrolled actually (if it was at the limit
// already), so we need to check if it did
if oldOriginY != view.OriginY() && context.NeedsRerenderOnHeightChange() {
mustRerender = true
}
}
if context.NeedsRerenderOnWidthChange() == types.NEEDS_RERENDER_ON_WIDTH_CHANGE_WHEN_WIDTH_CHANGES {
oldWidth := view.Width()
newWidth := dimensionsObj.X1 - dimensionsObj.X0 + 1
if oldWidth != newWidth {
mustRerender = true
}
}
if context.NeedsRerenderOnHeightChange() {
oldHeight := view.Height()
newHeight := dimensionsObj.Y1 - dimensionsObj.Y0 + 1
if oldHeight != newHeight {
mustRerender = true
}
}
if mustRerender {
contextsToRerender = append(contextsToRerender, context)
}
_, err = g.SetView(
viewName,
dimensionsObj.X0-frameOffset,
dimensionsObj.Y0-frameOffset,
dimensionsObj.X1+frameOffset,
dimensionsObj.Y1+frameOffset,
0,
)
view.Visible = true
return view, err
}
for _, context := range gui.State.Contexts.Flatten() {
if !context.HasControlledBounds() {
continue
}
_, err := setViewFromDimensions(context)
if err != nil && !errors.Is(err, gocui.ErrUnknownView) {
return err
}
}
menuWithFilterRowVisible := gui.Views.Menu.Visible && gui.State.Contexts.Menu.FilterAsYouType()
minimumHeight := minimumScreenHeight(len(gui.helpers.Window.SideWindows()), menuWithFilterRowVisible)
minimumWidth := 10
gui.Views.Limit.Visible = height < minimumHeight || width < minimumWidth
filterRowVisible := gui.Views.Menu.Visible && gui.State.Contexts.Menu.FilterStarted()
gui.Views.MenuFilterFrame.Visible = filterRowVisible
gui.Views.MenuFilter.Visible = filterRowVisible
if gui.Views.Menu.Visible {
// Until the user types something there is no filter row to advertise the
// filter, so the menu says that typing is a thing.
gui.Views.Menu.Subtitle = lo.Ternary(menuWithFilterRowVisible && !filterRowVisible, gui.c.Tr.MenuFilterHint, "")
}
if menuWithFilterRowVisible {
// The filter input is the current view for as long as such a menu is open,
// so without this the cursor would sit on the menu's bottom border, where
// the filter row is yet to appear.
gui.g.Cursor = filterRowVisible
}
gui.Views.Tooltip.Visible = gui.Views.Menu.Visible && gui.Views.Tooltip.Buffer() != ""
for _, context := range gui.transientContexts() {
view, err := gui.g.View(context.GetViewName())
if err != nil && !errors.Is(err, gocui.ErrUnknownView) {
return err
}
// A transient view is visible if it is the view its window is currently
// showing — but only if that window is part of the layout at all. For a
// window without dimensions, setViewFromDimensions parks the view at full
// screen size in the background, so making it visible would cover all
// windows below it.
_, windowHasDimensions := viewDimensions[context.GetWindowName()]
view.Visible = windowHasDimensions &&
gui.helpers.Window.GetViewNameForWindow(context.GetWindowName()) == context.GetViewName()
}
if gui.PrevLayout.Information != informationStr {
gui.c.SetViewContent(gui.Views.Information, informationStr)
gui.PrevLayout.Information = informationStr
}
if !gui.ViewsSetup {
if err := gui.onInitialViewsCreation(); err != nil {
return err
}
gui.handleTestMode()
gui.ViewsSetup = true
}
if !gui.State.ViewsSetup {
if err := gui.onInitialViewsCreationForRepo(); err != nil {
return err
}
gui.State.ViewsSetup = true
}
mainViewWidth, mainViewHeight := gui.Views.Main.Size()
if mainViewWidth != gui.PrevLayout.MainWidth || mainViewHeight != gui.PrevLayout.MainHeight {
gui.PrevLayout.MainWidth = mainViewWidth
gui.PrevLayout.MainHeight = mainViewHeight
if err := gui.onResize(); err != nil {
return err
}
}
for _, context := range contextsToRerender {
context.HandleRender()
}
// here is a good place log some stuff
// if you run `lazygit --logs`
// this will let you see these branches as prettified json
// gui.c.Log.Info(utils.AsJson(gui.State.Model.Branches[0:4]))
gui.helpers.Confirmation.ResizeCurrentPopupPanels()
gui.renderContextOptionsMap()
outer:
for {
select {
case f := <-gui.afterLayoutFuncs:
if err := f(); err != nil {
return err
}
default:
break outer
}
}
return nil
}
// The height below which we show the "not enough space" view instead of the
// layout.
func minimumScreenHeight(sideWindowCount int, menuWithFilterRowVisible bool) int {
// When the screen is too short the side panels are squashed, with the
// unfocused ones taking one row each and the focused one taking the rest. The
// more panels there are, the more rows the unfocused ones reserve, so the
// floor below which there's no room left for the focused panel grows with the
// panel count. Keep the historical floor of 9 for the default five panels.
minimumHeight := max(9, sideWindowCount+4)
// A menu popup gets three quarters of the screen, of which its frame, the
// tooltip gap below it and a reserved filter row take seven rows, so below 11
// rows there is no room left for even one menu item.
if menuWithFilterRowVisible {
minimumHeight = max(minimumHeight, 11)
}
return minimumHeight
}
func (gui *Gui) prepareView(viewName string) (*gocui.View, error) {
// arbitrarily giving the view enough size so that we don't get an error, but
// it's expected that the view will be given the correct size before being shown
return gui.g.SetView(viewName, 0, 0, 10, 10, 0)
}
func (gui *Gui) onInitialViewsCreationForRepo() error {
if err := gui.onRepoViewReset(); err != nil {
return err
}
// hide any popup views. This only applies when we've just switched repos
for _, viewName := range gui.popupViewNames() {
view, err := gui.g.View(viewName)
if err == nil {
view.Visible = false
}
}
initialContext := gui.c.Context().Current()
gui.c.Context().Activate(initialContext, types.OnFocusOpts{})
return gui.loadNewRepo()
}
func (gui *Gui) popupViewNames() []string {
popups := lo.Filter(gui.State.Contexts.Flatten(), func(c types.Context, _ int) bool {
return c.GetKind() == types.PERSISTENT_POPUP || c.GetKind() == types.TEMPORARY_POPUP
})
return lo.Map(popups, func(c types.Context, _ int) string {
return c.GetViewName()
})
}
func (gui *Gui) onRepoViewReset() error {
// now we order the views (in order of bottom first)
for _, view := range gui.orderedViews() {
if _, err := gui.g.SetViewOnTop(view.Name()); err != nil {
return err
}
}
// The loop above orders views by a fixed list, which doesn't necessarily put
// each panel's first configured tab on top.
gui.moveDefaultTabsToTop()
return nil
}
func (gui *Gui) onInitialViewsCreation() error {
if !gui.c.UserConfig().DisableStartupPopups {
storedPopupVersion := gui.c.ReadAppState().StartupPopupVersion
if storedPopupVersion < StartupPopupVersion {
gui.showIntroPopupMessage()
} else {
gui.showBreakingChangesMessage()
}
}
gui.c.UpdateAppState(func(state *config.AppState) {
state.LastVersion = gui.Config.GetVersion()
})
if gui.showRecentRepos {
if err := gui.helpers.Repos.CreateRecentReposMenu(); err != nil {
return err
}
gui.showRecentRepos = false
}
gui.helpers.Update.OnStartup()
gui.waitForIntro.Done()
return nil
}
func (gui *Gui) transientContexts() []types.Context {
return lo.Filter(gui.State.Contexts.Flatten(), func(context types.Context, _ int) bool {
return context.IsTransient()
})
}