1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-17 00:18:05 +02:00

centralise logic for rendering options map

This commit is contained in:
Jesse Duffield
2020-08-23 10:50:27 +10:00
parent f876d8fdc8
commit fda9f4ea7a
6 changed files with 43 additions and 37 deletions

View File

@ -47,17 +47,26 @@ type Context interface {
GetKey() string GetKey() string
SetParentContext(Context) SetParentContext(Context)
GetParentContext() Context GetParentContext() Context
GetOptionsMap() map[string]string
} }
type BasicContext struct { type BasicContext struct {
OnFocus func() error OnFocus func() error
OnFocusLost func() error OnFocusLost func() error
OnRender func() error OnRender func() error
OnGetOptionsMap func() map[string]string
Kind int Kind int
Key string Key string
ViewName string ViewName string
} }
func (c BasicContext) GetOptionsMap() map[string]string {
if c.OnGetOptionsMap != nil {
return c.OnGetOptionsMap()
}
return nil
}
func (c BasicContext) SetWindowName(windowName string) { func (c BasicContext) SetWindowName(windowName string) {
panic("can't set window name on basic context") panic("can't set window name on basic context")
} }
@ -247,6 +256,7 @@ func (gui *Gui) contextTree() ContextTree {
Kind: MAIN_CONTEXT, Kind: MAIN_CONTEXT,
ViewName: "main", ViewName: "main",
Key: MAIN_MERGING_CONTEXT_KEY, Key: MAIN_MERGING_CONTEXT_KEY,
OnGetOptionsMap: gui.getMergingOptions,
}, },
}, },
Credentials: SimpleContextNode{ Credentials: SimpleContextNode{
@ -470,10 +480,12 @@ func (gui *Gui) activateContext(c Context) error {
gui.g.Cursor = newView.Editable gui.g.Cursor = newView.Editable
// TODO: move this logic to the context // render the options available for the current context at the bottom of the screen
if err := gui.renderPanelOptions(); err != nil { optionsMap := c.GetOptionsMap()
return err if optionsMap == nil {
optionsMap = gui.globalOptionsMap()
} }
gui.renderOptionsMap(optionsMap)
if err := c.HandleFocus(); err != nil { if err := c.HandleFocus(); err != nil {
return err return err

View File

@ -101,7 +101,7 @@ func (gui *Gui) refreshFiles() error {
return err return err
} }
if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && g.CurrentView().Context == "merging") { if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && g.CurrentView().Context == MAIN_MERGING_CONTEXT_KEY) {
newSelectedFile := gui.getSelectedFile() newSelectedFile := gui.getSelectedFile()
alreadySelected := selectedFile != nil && newSelectedFile != nil && newSelectedFile.Name == selectedFile.Name alreadySelected := selectedFile != nil && newSelectedFile != nil && newSelectedFile.Name == selectedFile.Name
return gui.selectFile(alreadySelected) return gui.selectFile(alreadySelected)

View File

@ -13,6 +13,7 @@ type ListContext struct {
OnFocus func() error OnFocus func() error
OnFocusLost func() error OnFocusLost func() error
OnClickSelectedItem func() error OnClickSelectedItem func() error
OnGetOptionsMap func() map[string]string
// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection) // the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
SelectedItem func() (ListItem, bool) SelectedItem func() (ListItem, bool)
@ -70,6 +71,13 @@ func (lc *ListContext) GetSelectedItemId() string {
return item.ID() return item.ID()
} }
func (lc *ListContext) GetOptionsMap() map[string]string {
if lc.OnGetOptionsMap != nil {
return lc.OnGetOptionsMap()
}
return nil
}
// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view // OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func (lc *ListContext) OnRender() error { func (lc *ListContext) OnRender() error {
view, err := lc.Gui.g.View(lc.ViewName) view, err := lc.Gui.g.View(lc.ViewName)
@ -241,6 +249,7 @@ func (gui *Gui) menuListContext() *ListContext {
Gui: gui, Gui: gui,
ResetMainViewOriginOnFocus: false, ResetMainViewOriginOnFocus: false,
Kind: PERSISTENT_POPUP, Kind: PERSISTENT_POPUP,
OnGetOptionsMap: gui.getMenuOptions,
// no GetDisplayStrings field because we do a custom render on menu creation // no GetDisplayStrings field because we do a custom render on menu creation
} }

View File

@ -32,13 +32,12 @@ func (gui *Gui) handleMenuSelect() error {
// specific functions // specific functions
func (gui *Gui) renderMenuOptions() error { func (gui *Gui) getMenuOptions() map[string]string {
optionsMap := map[string]string{ return map[string]string{
gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("close"), gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("close"),
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"), fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"),
gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("execute"), gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("execute"),
} }
return gui.renderOptionsMap(optionsMap)
} }
func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {

View File

@ -292,14 +292,14 @@ func (gui *Gui) scrollToConflict(g *gocui.Gui) error {
return nil return nil
} }
func (gui *Gui) renderMergeOptions() error { func (gui *Gui) getMergingOptions() map[string]string {
return gui.renderOptionsMap(map[string]string{ return map[string]string{
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("selectHunk"), fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("selectHunk"),
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock")): gui.Tr.SLocalize("navigateConflicts"), fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock")): gui.Tr.SLocalize("navigateConflicts"),
gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("pickHunk"), gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("pickHunk"),
gui.getKeyDisplay("main.pickBothHunks"): gui.Tr.SLocalize("pickBothHunks"), gui.getKeyDisplay("main.pickBothHunks"): gui.Tr.SLocalize("pickBothHunks"),
gui.getKeyDisplay("universal.undo"): gui.Tr.SLocalize("undo"), gui.getKeyDisplay("universal.undo"): gui.Tr.SLocalize("undo"),
}) }
} }
func (gui *Gui) handleEscapeMerge() error { func (gui *Gui) handleEscapeMerge() error {

View File

@ -185,9 +185,8 @@ func (gui *Gui) optionsMapToString(optionsMap map[string]string) string {
return strings.Join(optionsArray, ", ") return strings.Join(optionsArray, ", ")
} }
func (gui *Gui) renderOptionsMap(optionsMap map[string]string) error { func (gui *Gui) renderOptionsMap(optionsMap map[string]string) {
gui.renderString("options", gui.optionsMapToString(optionsMap)) gui.renderString("options", gui.optionsMapToString(optionsMap))
return nil
} }
// TODO: refactor properly // TODO: refactor properly
@ -327,28 +326,15 @@ func (gui *Gui) renderDisplayStrings(v *gocui.View, displayStrings [][]string) {
}) })
} }
func (gui *Gui) renderPanelOptions() error { func (gui *Gui) globalOptionsMap() map[string]string {
currentView := gui.g.CurrentView() return map[string]string{
switch currentView.Name() {
case "menu":
return gui.renderMenuOptions()
case "main":
if gui.State.MainContext == "merging" {
return gui.renderMergeOptions()
}
}
return gui.renderGlobalOptions()
}
func (gui *Gui) renderGlobalOptions() error {
return gui.renderOptionsMap(map[string]string{
fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.scrollUpMain"), gui.getKeyDisplay("universal.scrollDownMain")): gui.Tr.SLocalize("scroll"), fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.scrollUpMain"), gui.getKeyDisplay("universal.scrollDownMain")): gui.Tr.SLocalize("scroll"),
fmt.Sprintf("%s %s %s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock"), gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"), fmt.Sprintf("%s %s %s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock"), gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"),
gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("cancel"), gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("cancel"),
gui.getKeyDisplay("universal.quit"): gui.Tr.SLocalize("quit"), gui.getKeyDisplay("universal.quit"): gui.Tr.SLocalize("quit"),
gui.getKeyDisplay("universal.optionMenu"): gui.Tr.SLocalize("menu"), gui.getKeyDisplay("universal.optionMenu"): gui.Tr.SLocalize("menu"),
"1-5": gui.Tr.SLocalize("jump"), "1-5": gui.Tr.SLocalize("jump"),
}) }
} }
func (gui *Gui) isPopupPanel(viewName string) bool { func (gui *Gui) isPopupPanel(viewName string) bool {