1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-04 03:48:07 +02:00

Only reset origin in main view when handling the selection of a new file

This commit is contained in:
Jesse Duffield 2019-01-15 20:12:31 +11:00
parent 695b092c41
commit 3a607061a2
3 changed files with 34 additions and 26 deletions

View File

@ -26,7 +26,7 @@ func (gui *Gui) getSelectedFile(g *gocui.Gui) (*commands.File, error) {
return gui.State.Files[selectedLine], nil
}
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View, alreadySelected bool) error {
file, err := gui.getSelectedFile(g)
if err != nil {
if err != gui.Errors.ErrNoFiles {
@ -44,10 +44,18 @@ func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
}
content := gui.GitCommand.Diff(file, false)
if alreadySelected {
g.Update(func(*gocui.Gui) error {
return gui.setViewContent(gui.g, gui.getMainView(gui.g), content)
})
return nil
}
return gui.renderString(g, "main", content)
}
func (gui *Gui) refreshFiles(g *gocui.Gui) error {
selectedFile, _ := gui.getSelectedFile(gui.g)
filesView, err := g.View("files")
if err != nil {
return err
@ -64,7 +72,9 @@ func (gui *Gui) refreshFiles(g *gocui.Gui) error {
fmt.Fprint(filesView, list)
if filesView == g.CurrentView() {
return gui.handleFileSelect(g, filesView)
newSelectedFile, _ := gui.getSelectedFile(gui.g)
alreadySelected := newSelectedFile.Name == selectedFile.Name
return gui.handleFileSelect(g, filesView, alreadySelected)
}
return nil
})
@ -76,14 +86,14 @@ func (gui *Gui) handleFilesNextLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Files
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Files), false)
return gui.handleFileSelect(gui.g, v)
return gui.handleFileSelect(gui.g, v, false)
}
func (gui *Gui) handleFilesPrevLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Files
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.Files), true)
return gui.handleFileSelect(gui.g, v)
return gui.handleFileSelect(gui.g, v, false)
}
// specific functions
@ -163,7 +173,7 @@ func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
return err
}
return gui.handleFileSelect(g, v)
return gui.handleFileSelect(g, v, true)
}
func (gui *Gui) allFilesStaged() bool {
@ -186,11 +196,7 @@ func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error {
_ = gui.createErrorPanel(g, err.Error())
}
if err := gui.refreshFiles(g); err != nil {
return err
}
return gui.handleFileSelect(g, v)
return gui.refreshFiles(g)
}
func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {

View File

@ -462,7 +462,7 @@ func (gui *Gui) updateLoader(g *gocui.Gui) error {
content := gui.trimmedContent(view)
if strings.Contains(content, "...") {
staticContent := strings.Split(content, "...")[0] + "..."
if err := gui.synchronousRenderString(g, "confirmation", staticContent+" "+utils.Loader()); err != nil {
if err := gui.setViewContent(g, view, staticContent+" "+utils.Loader()); err != nil {
return err
}
}

View File

@ -90,7 +90,7 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
case "status":
return gui.handleStatusSelect(g, v)
case "files":
return gui.handleFileSelect(g, v)
return gui.handleFileSelect(g, v, false)
case "branches":
return gui.handleBranchSelect(g, v)
case "commits":
@ -222,26 +222,28 @@ func (gui *Gui) focusPoint(cx int, cy int, v *gocui.View) error {
return nil
}
func (gui *Gui) synchronousRenderString(g *gocui.Gui, viewName, s string) error {
v, err := g.View(viewName)
// just in case the view disappeared as this function was called, we'll
// silently return if it's not found
if err != nil {
return nil
}
v.Clear()
if err := v.SetOrigin(0, 0); err != nil {
return err
}
func (gui *Gui) cleanString(s string) string {
output := string(bom.Clean([]byte(s)))
output = utils.NormalizeLinefeeds(output)
fmt.Fprint(v, output)
return utils.NormalizeLinefeeds(output)
}
func (gui *Gui) setViewContent(g *gocui.Gui, v *gocui.View, s string) error {
v.Clear()
fmt.Fprint(v, gui.cleanString(s))
return nil
}
// renderString resets the origin of a view and sets its content
func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error {
g.Update(func(*gocui.Gui) error {
return gui.synchronousRenderString(gui.g, viewName, s)
v, err := g.View(viewName)
if err != nil {
return nil // return gracefully if view has been deleted
}
if err := v.SetOrigin(0, 0); err != nil {
return err
}
return gui.setViewContent(gui.g, v, s)
})
return nil
}