mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-24 05:36:19 +02:00
centralise logic for information section
WIP
This commit is contained in:
parent
e73937c2bd
commit
1fd35f3824
@ -1,5 +1,9 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
|
||||
func (gui *Gui) mainSectionChildren() []*box {
|
||||
currentViewName := gui.currentViewName()
|
||||
|
||||
@ -61,7 +65,49 @@ func (gui *Gui) getMidSectionWeights() (int, int) {
|
||||
return sideSectionWeight, mainSectionWeight
|
||||
}
|
||||
|
||||
func (gui *Gui) getViewDimensions() map[string]dimensions {
|
||||
func (gui *Gui) infoSectionChildren(informationStr string, appStatus string) []*box {
|
||||
if gui.State.Searching.isSearching {
|
||||
return []*box{
|
||||
{
|
||||
viewName: "searchPrefix",
|
||||
size: len(SEARCH_PREFIX),
|
||||
},
|
||||
{
|
||||
viewName: "search",
|
||||
weight: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
result := []*box{}
|
||||
|
||||
if len(appStatus) > 0 {
|
||||
result = append(result,
|
||||
&box{
|
||||
viewName: "appStatus",
|
||||
size: len(appStatus) + len(INFO_SECTION_PADDING),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
result = append(result,
|
||||
[]*box{
|
||||
{
|
||||
viewName: "options",
|
||||
weight: 1,
|
||||
},
|
||||
{
|
||||
viewName: "information",
|
||||
// unlike appStatus, informationStr has various colors so we need to decolorise before taking the length
|
||||
size: len(INFO_SECTION_PADDING) + len(utils.Decolorise(informationStr)),
|
||||
},
|
||||
}...,
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (gui *Gui) getViewDimensions(informationStr string, appStatus string) map[string]dimensions {
|
||||
width, height := gui.g.Size()
|
||||
|
||||
sideSectionWeight, mainSectionWeight := gui.getMidSectionWeights()
|
||||
@ -98,10 +144,10 @@ func (gui *Gui) getViewDimensions() map[string]dimensions {
|
||||
},
|
||||
},
|
||||
},
|
||||
// TODO: actually handle options here. Currently we're just hard-coding it to be set on the bottom row in our layout function given that we need some custom logic to have it share space with other views on that row.
|
||||
{
|
||||
viewName: "options",
|
||||
size: 1,
|
||||
direction: COLUMN,
|
||||
size: 1,
|
||||
children: gui.infoSectionChildren(informationStr, appStatus),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -161,7 +161,7 @@ func (gui *Gui) handleInfoClick(g *gocui.Gui, v *gocui.View) error {
|
||||
}
|
||||
}
|
||||
|
||||
if cx <= len(gui.Tr.SLocalize("Donate")) {
|
||||
if cx <= len(gui.Tr.SLocalize("Donate"))+len(INFO_SECTION_PADDING) {
|
||||
return gui.OSCommand.OpenLink("https://github.com/sponsors/jesseduffield")
|
||||
}
|
||||
return nil
|
||||
|
@ -9,6 +9,9 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
|
||||
const SEARCH_PREFIX = "search: "
|
||||
const INFO_SECTION_PADDING = " "
|
||||
|
||||
// getFocusLayout returns a manager function for when view gain and lose focus
|
||||
func (gui *Gui) getFocusLayout() func(g *gocui.Gui) error {
|
||||
var previousView *gocui.View
|
||||
@ -91,8 +94,6 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
g.Highlight = true
|
||||
width, height := g.Size()
|
||||
|
||||
information := gui.informationStr()
|
||||
|
||||
minimumHeight := 9
|
||||
minimumWidth := 10
|
||||
if height < minimumHeight || width < minimumWidth {
|
||||
@ -108,15 +109,10 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
viewDimensions := gui.getViewDimensions()
|
||||
|
||||
optionsVersionBoundary := width - max(len(utils.Decolorise(information)), 1)
|
||||
|
||||
informationStr := gui.informationStr()
|
||||
appStatus := gui.statusManager.getStatusString()
|
||||
appStatusOptionsBoundary := 0
|
||||
if appStatus != "" {
|
||||
appStatusOptionsBoundary = len(appStatus) + 2
|
||||
}
|
||||
|
||||
viewDimensions := gui.getViewDimensions(informationStr, appStatus)
|
||||
|
||||
_, _ = g.SetViewOnBottom("limit")
|
||||
_ = g.DeleteView("limit")
|
||||
@ -139,19 +135,23 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
}
|
||||
}
|
||||
|
||||
setViewFromDimensions := func(viewName string, boxName string) (*gocui.View, error) {
|
||||
setViewFromDimensions := func(viewName string, boxName string, frame bool) (*gocui.View, error) {
|
||||
dimensionsObj := viewDimensions[boxName]
|
||||
frameOffset := 1
|
||||
if frame {
|
||||
frameOffset = 0
|
||||
}
|
||||
return g.SetView(
|
||||
viewName,
|
||||
dimensionsObj.x0,
|
||||
dimensionsObj.y0,
|
||||
dimensionsObj.x1,
|
||||
dimensionsObj.y1,
|
||||
dimensionsObj.x0-frameOffset,
|
||||
dimensionsObj.y0-frameOffset,
|
||||
dimensionsObj.x1+frameOffset,
|
||||
dimensionsObj.y1+frameOffset,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
v, err := setViewFromDimensions("main", "main")
|
||||
v, err := setViewFromDimensions("main", "main", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -162,7 +162,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
v.IgnoreCarriageReturns = true
|
||||
}
|
||||
|
||||
secondaryView, err := setViewFromDimensions("secondary", "secondary")
|
||||
secondaryView, err := setViewFromDimensions("secondary", "secondary", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -175,7 +175,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
|
||||
hiddenViewOffset := 9999
|
||||
|
||||
if v, err := setViewFromDimensions("status", "status"); err != nil {
|
||||
if v, err := setViewFromDimensions("status", "status", true); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
@ -183,7 +183,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
v.FgColor = textColor
|
||||
}
|
||||
|
||||
filesView, err := setViewFromDimensions("files", "files")
|
||||
filesView, err := setViewFromDimensions("files", "files", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -194,7 +194,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
filesView.ContainsList = true
|
||||
}
|
||||
|
||||
branchesView, err := setViewFromDimensions("branches", "branches")
|
||||
branchesView, err := setViewFromDimensions("branches", "branches", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -206,7 +206,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
branchesView.ContainsList = true
|
||||
}
|
||||
|
||||
commitFilesView, err := setViewFromDimensions("commitFiles", "commits")
|
||||
commitFilesView, err := setViewFromDimensions("commitFiles", "commits", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -217,7 +217,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
commitFilesView.ContainsList = true
|
||||
}
|
||||
|
||||
commitsView, err := setViewFromDimensions("commits", "commits")
|
||||
commitsView, err := setViewFromDimensions("commits", "commits", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -229,7 +229,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
commitsView.ContainsList = true
|
||||
}
|
||||
|
||||
stashView, err := setViewFromDimensions("stash", "stash")
|
||||
stashView, err := setViewFromDimensions("stash", "stash", true)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -240,14 +240,6 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
stashView.ContainsList = true
|
||||
}
|
||||
|
||||
if v, err := g.SetView("options", appStatusOptionsBoundary-1, height-2, optionsVersionBoundary-1, height, 0); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
v.Frame = false
|
||||
v.FgColor = theme.OptionsColor
|
||||
}
|
||||
|
||||
if gui.getCommitMessageView() == nil {
|
||||
// doesn't matter where this view starts because it will be hidden
|
||||
if commitMessageView, err := g.SetView("commitMessage", hiddenViewOffset, hiddenViewOffset, hiddenViewOffset+10, hiddenViewOffset+10, 0); err != nil {
|
||||
@ -278,14 +270,21 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
}
|
||||
}
|
||||
|
||||
searchViewOffset := hiddenViewOffset
|
||||
if gui.State.Searching.isSearching {
|
||||
searchViewOffset = 0
|
||||
if v, err := setViewFromDimensions("options", "options", false); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
v.Frame = false
|
||||
v.FgColor = theme.OptionsColor
|
||||
|
||||
// doing this here because it'll only happen once
|
||||
if err := gui.onInitialViewsCreation(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// this view takes up one character. Its only purpose is to show the slash when searching
|
||||
searchPrefix := "search: "
|
||||
if searchPrefixView, err := g.SetView("searchPrefix", appStatusOptionsBoundary-1+searchViewOffset, height-2+searchViewOffset, len(searchPrefix)+searchViewOffset, height+searchViewOffset, 0); err != nil {
|
||||
if searchPrefixView, err := setViewFromDimensions("searchPrefix", "searchPrefix", false); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
@ -293,10 +292,10 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
searchPrefixView.BgColor = gocui.ColorDefault
|
||||
searchPrefixView.FgColor = gocui.ColorGreen
|
||||
searchPrefixView.Frame = false
|
||||
gui.setViewContent(searchPrefixView, searchPrefix)
|
||||
gui.setViewContent(searchPrefixView, SEARCH_PREFIX)
|
||||
}
|
||||
|
||||
if searchView, err := g.SetView("search", appStatusOptionsBoundary-1+searchViewOffset+len(searchPrefix), height-2+searchViewOffset, optionsVersionBoundary+searchViewOffset, height+searchViewOffset, 0); err != nil {
|
||||
if searchView, err := setViewFromDimensions("search", "search", false); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
@ -307,7 +306,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
searchView.Editable = true
|
||||
}
|
||||
|
||||
if appStatusView, err := g.SetView("appStatus", -1, height-2, width, height, 0); err != nil {
|
||||
if appStatusView, err := setViewFromDimensions("appStatus", "appStatus", false); err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
}
|
||||
@ -319,7 +318,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
}
|
||||
}
|
||||
|
||||
informationView, err := g.SetView("information", optionsVersionBoundary-1, height-2, width, height, 0)
|
||||
informationView, err := setViewFromDimensions("information", "information", false)
|
||||
if err != nil {
|
||||
if err.Error() != "unknown view" {
|
||||
return err
|
||||
@ -327,16 +326,11 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||
informationView.BgColor = gocui.ColorDefault
|
||||
informationView.FgColor = gocui.ColorGreen
|
||||
informationView.Frame = false
|
||||
gui.renderString(g, "information", information)
|
||||
|
||||
// doing this here because it'll only happen once
|
||||
if err := gui.onInitialViewsCreation(); err != nil {
|
||||
return err
|
||||
}
|
||||
gui.renderString(g, "information", INFO_SECTION_PADDING+informationStr)
|
||||
}
|
||||
if gui.State.OldInformation != information {
|
||||
gui.setViewContent(informationView, information)
|
||||
gui.State.OldInformation = information
|
||||
if gui.State.OldInformation != informationStr {
|
||||
gui.setViewContent(informationView, informationStr)
|
||||
gui.State.OldInformation = informationStr
|
||||
}
|
||||
|
||||
if gui.g.CurrentView() == nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user