1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-09 13:47:11 +02:00

minor refactor

This commit is contained in:
Jesse Duffield 2018-09-17 21:11:47 +10:00
parent bd04ecff69
commit a66ac8092e
2 changed files with 49 additions and 22 deletions

View File

@ -49,12 +49,11 @@ func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
return gui.returnFocus(g, v)
}
func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
func (gui *Gui) getBindings(v *gocui.View) []*Binding {
var (
bindingsGlobal, bindingsPanel []*Binding
)
// clear keys slice, so we don't have ghost elements
gui.State.Keys = gui.State.Keys[:0]
bindings := gui.GetKeybindings()
for _, binding := range bindings {
@ -71,7 +70,11 @@ func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
// append dummy element to have a separator between
// panel and global keybindings
bindingsPanel = append(bindingsPanel, &Binding{})
gui.State.Keys = append(bindingsPanel, bindingsGlobal...)
return append(bindingsPanel, bindingsGlobal...)
}
func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
gui.State.Keys = gui.getBindings(v)
list, err := utils.RenderList(gui.State.Keys)
if err != nil {

View File

@ -141,35 +141,59 @@ func RenderList(slice interface{}) (string, error) {
// each item's string representation on its own line, with appropriate horizontal
// padding between the item's own strings
func renderDisplayableList(items []Displayable) (string, error) {
displayStrings := make([][]string, len(items))
for i, item := range items {
displayStrings[i] = item.GetDisplayStrings()
}
if len(displayStrings) == 0 {
if len(items) == 0 {
return "", nil
}
// use first element to determine how many times to do this
padWidths := make([]int, len(displayStrings[0]))
stringArrays := getDisplayStringArrays(items)
if !displayArraysAligned(stringArrays) {
return "", errors.New("Each item must return the same number of strings to display")
}
padWidths := getPadWidths(stringArrays)
paddedDisplayStrings := getPaddedDisplayStrings(stringArrays, padWidths)
return strings.Join(paddedDisplayStrings, "\n"), nil
}
func getPadWidths(stringArrays [][]string) []int {
padWidths := make([]int, len(stringArrays[0]))
for i, _ := range padWidths {
for _, strings := range displayStrings {
if len(strings) != len(padWidths) {
return "", errors.New("Each item must return the same number of strings to display")
}
for _, strings := range stringArrays {
if len(strings[i]) > padWidths[i] {
padWidths[i] = len(strings[i])
}
}
}
return padWidths
}
paddedDisplayStrings := make([]string, len(displayStrings))
for i, strings := range displayStrings {
func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) []string {
paddedDisplayStrings := make([]string, len(stringArrays))
for i, stringArray := range stringArrays {
for j, padWidth := range padWidths {
paddedDisplayStrings[i] += WithPadding(strings[j], padWidth) + " "
paddedDisplayStrings[i] += WithPadding(stringArray[j], padWidth) + " "
}
}
return strings.Join(paddedDisplayStrings, "\n"), nil
return paddedDisplayStrings
}
// displayArraysAligned returns true if every string array returned from our
// list of displayables has the same length
func displayArraysAligned(stringArrays [][]string) bool {
for _, strings := range stringArrays {
if len(strings) != len(stringArrays[0]) {
return false
}
}
return true
}
func getDisplayStringArrays(displayables []Displayable) [][]string {
stringArrays := make([][]string, len(displayables))
for i, item := range displayables {
stringArrays[i] = item.GetDisplayStrings()
}
return stringArrays
}