1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-10 11:10:18 +02:00
lazygit/pkg/gui/global_handlers.go

196 lines
5.0 KiB
Go
Raw Normal View History

2020-03-29 01:31:34 +02:00
package gui
import (
"math"
"strings"
"github.com/jesseduffield/gocui"
2020-08-11 13:18:38 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
2020-03-29 01:31:34 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
2020-08-19 11:07:14 +02:00
// these views need to be re-rendered when the screen mode changes. The commits view,
// for example, will show authorship information in half and full screen mode.
2020-08-19 11:26:05 +02:00
func (gui *Gui) rerenderViewsWithScreenModeDependentContent() error {
for _, viewName := range []string{"branches", "commits"} {
2020-08-19 11:07:14 +02:00
if err := gui.rerenderView(viewName); err != nil {
return err
}
2020-03-29 01:31:34 +02:00
}
return nil
}
2020-08-19 11:26:05 +02:00
func (gui *Gui) nextScreenMode(g *gocui.Gui, v *gocui.View) error {
gui.State.ScreenMode = utils.NextIntInCycle([]int{SCREEN_NORMAL, SCREEN_HALF, SCREEN_FULL}, gui.State.ScreenMode)
return gui.rerenderViewsWithScreenModeDependentContent()
}
2020-03-29 01:31:34 +02:00
func (gui *Gui) prevScreenMode(g *gocui.Gui, v *gocui.View) error {
gui.State.ScreenMode = utils.PrevIntInCycle([]int{SCREEN_NORMAL, SCREEN_HALF, SCREEN_FULL}, gui.State.ScreenMode)
2020-08-19 11:07:14 +02:00
2020-08-19 11:26:05 +02:00
return gui.rerenderViewsWithScreenModeDependentContent()
2020-03-29 01:31:34 +02:00
}
func (gui *Gui) scrollUpView(viewName string) error {
2020-08-23 07:59:22 +02:00
mainView, err := gui.g.View(viewName)
if err != nil {
return nil
}
2020-03-29 01:31:34 +02:00
ox, oy := mainView.Origin()
2020-10-03 06:54:55 +02:00
newOy := int(math.Max(0, float64(oy-gui.Config.GetUserConfig().Gui.ScrollHeight)))
2020-03-29 01:31:34 +02:00
return mainView.SetOrigin(ox, newOy)
}
func (gui *Gui) scrollDownView(viewName string) error {
2020-08-23 07:59:22 +02:00
mainView, err := gui.g.View(viewName)
if err != nil {
return nil
}
2020-03-29 01:31:34 +02:00
ox, oy := mainView.Origin()
y := oy
2020-10-03 06:54:55 +02:00
if !gui.Config.GetUserConfig().Gui.ScrollPastBottom {
2020-03-29 01:31:34 +02:00
_, sy := mainView.Size()
y += sy
}
2020-10-03 06:54:55 +02:00
scrollHeight := gui.Config.GetUserConfig().Gui.ScrollHeight
2020-03-29 01:31:34 +02:00
if y < mainView.LinesHeight() {
if err := mainView.SetOrigin(ox, oy+scrollHeight); err != nil {
return err
}
}
if manager, ok := gui.viewBufferManagerMap[viewName]; ok {
manager.ReadLines(scrollHeight)
}
return nil
}
func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error {
if gui.canScrollMergePanel() {
gui.State.Panels.Merging.UserScrolling = true
}
2020-03-29 01:31:34 +02:00
return gui.scrollUpView("main")
}
func (gui *Gui) scrollDownMain(g *gocui.Gui, v *gocui.View) error {
if gui.canScrollMergePanel() {
gui.State.Panels.Merging.UserScrolling = true
}
2020-03-29 01:31:34 +02:00
return gui.scrollDownView("main")
}
func (gui *Gui) scrollUpSecondary(g *gocui.Gui, v *gocui.View) error {
return gui.scrollUpView("secondary")
}
func (gui *Gui) scrollDownSecondary(g *gocui.Gui, v *gocui.View) error {
return gui.scrollDownView("secondary")
}
func (gui *Gui) scrollUpConfirmationPanel(g *gocui.Gui, v *gocui.View) error {
if v.Editable {
return nil
}
return gui.scrollUpView("confirmation")
}
func (gui *Gui) scrollDownConfirmationPanel(g *gocui.Gui, v *gocui.View) error {
if v.Editable {
return nil
}
return gui.scrollDownView("confirmation")
}
func (gui *Gui) handleRefresh(g *gocui.Gui, v *gocui.View) error {
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}
func (gui *Gui) handleMouseDownMain(g *gocui.Gui, v *gocui.View) error {
if gui.popupPanelFocused() {
return nil
}
switch g.CurrentView().Name() {
case "files":
2020-08-16 10:22:55 +02:00
// set filename, set primary/secondary selected, set line number, then switch context
// I'll need to know it was changed though.
// Could I pass something along to the context change?
2020-03-29 01:31:34 +02:00
return gui.enterFile(false, v.SelectedLineIdx())
case "commitFiles":
return gui.enterCommitFile(v.SelectedLineIdx())
}
return nil
}
func (gui *Gui) handleMouseDownSecondary(g *gocui.Gui, v *gocui.View) error {
if gui.popupPanelFocused() {
return nil
}
switch g.CurrentView().Name() {
case "files":
return gui.enterFile(true, v.SelectedLineIdx())
}
return nil
}
func (gui *Gui) handleInfoClick(g *gocui.Gui, v *gocui.View) error {
if !gui.g.Mouse {
return nil
}
cx, _ := v.Cursor()
width, _ := v.Size()
2020-08-23 01:23:59 +02:00
for _, mode := range gui.modeStatuses() {
if mode.isActive() {
2020-10-04 02:00:48 +02:00
if width-cx > len(gui.Tr.ResetInParentheses) {
2020-08-28 01:52:56 +02:00
return nil
}
2020-08-23 01:23:59 +02:00
return mode.reset()
}
}
// if we're not in an active mode we show the donate button
2020-10-04 02:00:48 +02:00
if cx <= len(gui.Tr.Donate)+len(INFO_SECTION_PADDING) {
2020-03-29 01:31:34 +02:00
return gui.OSCommand.OpenLink("https://github.com/sponsors/jesseduffield")
}
return nil
}
2020-08-11 13:18:38 +02:00
func (gui *Gui) fetch(canPromptForCredentials bool) (err error) {
gui.Mutexes.FetchMutex.Lock()
defer gui.Mutexes.FetchMutex.Unlock()
2020-08-23 13:25:39 +02:00
2020-08-11 13:18:38 +02:00
fetchOpts := commands.FetchOptions{}
if canPromptForCredentials {
fetchOpts.PromptUserForCredential = gui.promptUserForCredential
}
err = gui.GitCommand.Fetch(fetchOpts)
2020-03-29 01:31:34 +02:00
2020-08-11 13:18:38 +02:00
if canPromptForCredentials && err != nil && strings.Contains(err.Error(), "exit status 128") {
2020-11-16 11:38:26 +02:00
_ = gui.createErrorPanel(gui.Tr.PassUnameWrong)
2020-03-29 01:31:34 +02:00
}
2020-11-16 11:38:26 +02:00
_ = gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, COMMITS, REMOTES, TAGS}, mode: ASYNC})
2020-03-29 01:31:34 +02:00
2020-08-11 12:18:50 +02:00
return err
2020-03-29 01:31:34 +02:00
}
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
// important to note that this assumes we've selected an item in a side context
itemId := gui.getSideContextSelectedItemId()
if itemId == "" {
return nil
}
return gui.OSCommand.CopyToClipboard(itemId)
}