mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-12 11:15:00 +02:00
d84dfc23e7
Currently when we want to focus a point on a view (i.e. highlight a line and ensure it's within the bounds of a view's box, we use the LinesHeight method on the view to work out how many lines in total there are. This is bad because for example if we come back from editing a file, the view will have no contents so LinesHeight == 0, but we might be trying to select line 10 because there are actual ten things we expect to be rendered already. This causes a crash when e.g. 10 is greater than the height of the view. So we need to pass in to our FocusPoint method the actual number of items we want to render, rather than having the method rely on the LinesHeight, so that the method knows to scroll a bit before setting the cursor's y position. Unfortunately this makes for some awkward code with our current setup. We don't have a good interface type on these state objects so we now need to explicitly obtain the len() of whatever array we're rendering. In the case of the menu panel this is even more awkward because the items list is just an interface{} and it's not easy to get the list of that, so now when we instantiate a menu we need to pass in the count of items as well. The better solution would be to define an interface with a getItems and getLength method and have all these item arrays become structs implementing the interface, but I am too lazy to do this right now :)
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
)
|
|
|
|
func (gui *Gui) getSelectedCommitFile(g *gocui.Gui) *commands.CommitFile {
|
|
selectedLine := gui.State.Panels.CommitFiles.SelectedLine
|
|
if selectedLine == -1 {
|
|
return nil
|
|
}
|
|
|
|
return gui.State.CommitFiles[selectedLine]
|
|
}
|
|
|
|
func (gui *Gui) handleCommitFileSelect(g *gocui.Gui, v *gocui.View) error {
|
|
commitFile := gui.getSelectedCommitFile(g)
|
|
if commitFile == nil {
|
|
return gui.renderString(g, "commitFiles", gui.Tr.SLocalize("NoCommiteFiles"))
|
|
}
|
|
|
|
if err := gui.focusPoint(0, gui.State.Panels.CommitFiles.SelectedLine, len(gui.State.CommitFiles), v); err != nil {
|
|
return err
|
|
}
|
|
commitText, err := gui.GitCommand.ShowCommitFile(commitFile.Sha, commitFile.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return gui.renderString(g, "main", commitText)
|
|
}
|
|
|
|
func (gui *Gui) handleCommitFilesNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
panelState := gui.State.Panels.CommitFiles
|
|
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.CommitFiles), false)
|
|
|
|
return gui.handleCommitFileSelect(gui.g, v)
|
|
}
|
|
|
|
func (gui *Gui) handleCommitFilesPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
panelState := gui.State.Panels.CommitFiles
|
|
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.CommitFiles), true)
|
|
|
|
return gui.handleCommitFileSelect(gui.g, v)
|
|
}
|
|
|
|
func (gui *Gui) handleSwitchToCommitsPanel(g *gocui.Gui, v *gocui.View) error {
|
|
commitsView, err := g.View("commits")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return gui.switchFocus(g, v, commitsView)
|
|
}
|
|
|
|
func (gui *Gui) handleCheckoutCommitFile(g *gocui.Gui, v *gocui.View) error {
|
|
file := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLine]
|
|
|
|
if err := gui.GitCommand.CheckoutFile(file.Sha, file.Name); err != nil {
|
|
return gui.createErrorPanel(gui.g, err.Error())
|
|
}
|
|
|
|
return gui.refreshFiles()
|
|
}
|
|
|
|
func (gui *Gui) handleDiscardOldFileChange(g *gocui.Gui, v *gocui.View) error {
|
|
fileName := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLine].Name
|
|
|
|
return gui.createConfirmationPanel(gui.g, v, gui.Tr.SLocalize("DiscardFileChangesTitle"), gui.Tr.SLocalize("DiscardFileChangesPrompt"), func(g *gocui.Gui, v *gocui.View) error {
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
|
|
if err := gui.GitCommand.DiscardOldFileChanges(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, fileName); err != nil {
|
|
if err := gui.handleGenericMergeCommandResult(err); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return gui.refreshSidePanels(gui.g)
|
|
})
|
|
}, nil)
|
|
}
|
|
|
|
func (gui *Gui) refreshCommitFilesView() error {
|
|
commit := gui.getSelectedCommit(gui.g)
|
|
if commit == nil {
|
|
return nil
|
|
}
|
|
|
|
files, err := gui.GitCommand.GetCommitFiles(commit.Sha)
|
|
if err != nil {
|
|
return gui.createErrorPanel(gui.g, err.Error())
|
|
}
|
|
|
|
gui.State.CommitFiles = files
|
|
|
|
gui.refreshSelectedLine(&gui.State.Panels.CommitFiles.SelectedLine, len(gui.State.CommitFiles))
|
|
|
|
if err := gui.renderListPanel(gui.getCommitFilesView(), gui.State.CommitFiles); err != nil {
|
|
return err
|
|
}
|
|
|
|
return gui.handleCommitFileSelect(gui.g, gui.getCommitFilesView())
|
|
}
|
|
|
|
func (gui *Gui) handleOpenOldCommitFile(g *gocui.Gui, v *gocui.View) error {
|
|
file := gui.getSelectedCommitFile(g)
|
|
return gui.openFile(file.Name)
|
|
}
|