github.com/andrewrech/lazygit@v0.8.1/pkg/gui/menu_panel.go (about)

     1  package gui
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jesseduffield/gocui"
     7  	"github.com/jesseduffield/lazygit/pkg/utils"
     8  )
     9  
    10  // list panel functions
    11  
    12  func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
    13  	return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
    14  }
    15  
    16  func (gui *Gui) handleMenuNextLine(g *gocui.Gui, v *gocui.View) error {
    17  	panelState := gui.State.Panels.Menu
    18  	gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), false)
    19  
    20  	return gui.handleMenuSelect(g, v)
    21  }
    22  
    23  func (gui *Gui) handleMenuPrevLine(g *gocui.Gui, v *gocui.View) error {
    24  	panelState := gui.State.Panels.Menu
    25  	gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), true)
    26  
    27  	return gui.handleMenuSelect(g, v)
    28  }
    29  
    30  // specific functions
    31  
    32  func (gui *Gui) renderMenuOptions() error {
    33  	optionsMap := map[string]string{
    34  		"esc/q": gui.Tr.SLocalize("close"),
    35  		"↑ ↓":   gui.Tr.SLocalize("navigate"),
    36  		"space": gui.Tr.SLocalize("execute"),
    37  	}
    38  	return gui.renderOptionsMap(optionsMap)
    39  }
    40  
    41  func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
    42  	for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter} {
    43  		if err := g.DeleteKeybinding("menu", key, gocui.ModNone); err != nil {
    44  			return err
    45  		}
    46  	}
    47  	err := g.DeleteView("menu")
    48  	if err != nil {
    49  		return err
    50  	}
    51  	return gui.returnFocus(g, v)
    52  }
    53  
    54  func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handlePress func(int) error) error {
    55  	isFocused := gui.g.CurrentView().Name() == "menu"
    56  	gui.State.MenuItemCount = itemCount
    57  	list, err := utils.RenderList(items, isFocused)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, false, list)
    63  	menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0)
    64  	menuView.Title = title
    65  	menuView.FgColor = gocui.ColorWhite
    66  	menuView.Clear()
    67  	fmt.Fprint(menuView, list)
    68  	gui.State.Panels.Menu.SelectedLine = 0
    69  
    70  	wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error {
    71  		selectedLine := gui.State.Panels.Menu.SelectedLine
    72  		if err := handlePress(selectedLine); err != nil {
    73  			return err
    74  		}
    75  		if _, err := gui.g.View("menu"); err == nil {
    76  			if _, err := gui.g.SetViewOnBottom("menu"); err != nil {
    77  				return err
    78  			}
    79  		}
    80  
    81  		return gui.returnFocus(gui.g, menuView)
    82  	}
    83  
    84  	for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter} {
    85  		_ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone)
    86  
    87  		if err := gui.g.SetKeybinding("menu", key, gocui.ModNone, wrappedHandlePress); err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	gui.g.Update(func(g *gocui.Gui) error {
    93  		if _, err := gui.g.View("menu"); err == nil {
    94  			if _, err := g.SetViewOnTop("menu"); err != nil {
    95  				return err
    96  			}
    97  		}
    98  		currentView := gui.g.CurrentView()
    99  		return gui.switchFocus(gui.g, currentView, menuView)
   100  	})
   101  	return nil
   102  }