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

392 lines
9.2 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
package gui
2018-05-26 05:23:39 +02:00
import (
"fmt"
"sort"
"strings"
2020-03-26 14:48:11 +02:00
"sync"
2018-05-26 05:23:39 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/spkg/bom"
2018-05-26 05:23:39 +02:00
)
2020-08-16 05:58:29 +02:00
func (gui *Gui) getCyclableWindows() []string {
2020-05-17 13:54:51 +02:00
return []string{"status", "files", "branches", "commits", "stash"}
}
2018-06-06 04:17:49 +02:00
2020-03-28 02:32:31 +02:00
// models/views that we can refresh
type RefreshableView int
const (
COMMITS RefreshableView = iota
BRANCHES
FILES
STASH
REFLOG
TAGS
REMOTES
2020-03-28 00:57:36 +02:00
STATUS
2020-09-30 00:27:23 +02:00
SUBMODULES
)
func getScopeNames(scopes []RefreshableView) []string {
scopeNameMap := map[RefreshableView]string{
2020-09-30 00:27:23 +02:00
COMMITS: "commits",
BRANCHES: "branches",
FILES: "files",
SUBMODULES: "submodules",
STASH: "stash",
REFLOG: "reflog",
TAGS: "tags",
REMOTES: "remotes",
STATUS: "status",
}
scopeNames := make([]string, len(scopes))
for i, scope := range scopes {
scopeNames[i] = scopeNameMap[scope]
}
return scopeNames
}
func getModeName(mode RefreshMode) string {
switch mode {
case SYNC:
return "sync"
case ASYNC:
return "async"
case BLOCK_UI:
return "block-ui"
default:
return "unknown mode"
}
}
type RefreshMode int
const (
SYNC RefreshMode = iota // wait until everything is done before returning
ASYNC // return immediately, allowing each independent thing to update itself
BLOCK_UI // wrap code in an update call to ensure UI updates all at once and keybindings aren't executed till complete
)
type refreshOptions struct {
then func()
scope []RefreshableView // e.g. []int{COMMITS, BRANCHES}. Leave empty to refresh everything
mode RefreshMode // one of SYNC (default), ASYNC, and BLOCK_UI
}
func arrToMap(arr []RefreshableView) map[RefreshableView]bool {
output := map[RefreshableView]bool{}
for _, el := range arr {
output[el] = true
}
return output
}
func (gui *Gui) refreshSidePanels(options refreshOptions) error {
if options.scope == nil {
gui.Log.Infof(
"refreshing all scopes in %s mode",
getModeName(options.mode),
)
} else {
gui.Log.Infof(
"refreshing the following scopes in %s mode: %s",
getModeName(options.mode),
strings.Join(getScopeNames(options.scope), ","),
)
}
2020-03-26 14:48:11 +02:00
wg := sync.WaitGroup{}
f := func() {
var scopeMap map[RefreshableView]bool
if len(options.scope) == 0 {
scopeMap = arrToMap([]RefreshableView{COMMITS, BRANCHES, FILES, STASH, REFLOG, TAGS, REMOTES, STATUS})
} else {
scopeMap = arrToMap(options.scope)
}
if scopeMap[COMMITS] || scopeMap[BRANCHES] || scopeMap[REFLOG] {
wg.Add(1)
func() {
if options.mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils.Safe(func() { _ = gui.refreshCommits() })
} else {
2020-11-16 11:38:26 +02:00
_ = gui.refreshCommits()
}
wg.Done()
}()
}
2020-03-26 14:48:11 +02:00
2020-09-30 00:27:23 +02:00
if scopeMap[FILES] || scopeMap[SUBMODULES] {
wg.Add(1)
func() {
if options.mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils.Safe(func() { _ = gui.refreshFilesAndSubmodules() })
} else {
2020-11-16 11:38:26 +02:00
_ = gui.refreshFilesAndSubmodules()
}
wg.Done()
}()
}
if scopeMap[STASH] {
wg.Add(1)
func() {
if options.mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils.Safe(func() { _ = gui.refreshStashEntries() })
} else {
2020-11-16 11:38:26 +02:00
_ = gui.refreshStashEntries()
}
wg.Done()
}()
}
2020-03-26 14:48:11 +02:00
if scopeMap[TAGS] {
wg.Add(1)
func() {
if options.mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils.Safe(func() { _ = gui.refreshTags() })
} else {
2020-11-16 11:38:26 +02:00
_ = gui.refreshTags()
}
wg.Done()
}()
}
if scopeMap[REMOTES] {
wg.Add(1)
func() {
if options.mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils.Safe(func() { _ = gui.refreshRemotes() })
} else {
2020-11-16 11:38:26 +02:00
_ = gui.refreshRemotes()
}
wg.Done()
}()
}
wg.Wait()
2020-03-26 14:48:11 +02:00
gui.refreshStatus()
if options.then != nil {
options.then()
}
}
if options.mode == BLOCK_UI {
gui.g.Update(func(g *gocui.Gui) error {
f()
return nil
})
} else {
f()
}
2020-03-26 14:48:11 +02:00
return nil
2018-06-06 04:17:49 +02:00
}
2018-08-14 11:05:26 +02:00
func (gui *Gui) resetOrigin(v *gocui.View) error {
2019-04-25 21:37:19 +02:00
_ = v.SetCursor(0, 0)
return v.SetOrigin(0, 0)
2018-06-09 11:06:33 +02:00
}
func (gui *Gui) cleanString(s string) string {
output := string(bom.Clean([]byte(s)))
return utils.NormalizeLinefeeds(output)
}
func (gui *Gui) setViewContent(v *gocui.View, s string) {
v.Clear()
fmt.Fprint(v, gui.cleanString(s))
}
// renderString resets the origin of a view and sets its content
2020-08-15 08:36:39 +02:00
func (gui *Gui) renderString(viewName, s string) {
gui.g.Update(func(*gocui.Gui) error {
return gui.renderStringSync(viewName, s)
})
2018-05-26 05:23:39 +02:00
}
func (gui *Gui) renderStringSync(viewName, s string) error {
v, err := gui.g.View(viewName)
if err != nil {
return nil // return gracefully if view has been deleted
}
if err := v.SetOrigin(0, 0); err != nil {
return err
}
if err := v.SetCursor(0, 0); err != nil {
return err
}
gui.setViewContent(v, s)
return nil
}
2018-08-14 11:05:26 +02:00
func (gui *Gui) optionsMapToString(optionsMap map[string]string) string {
optionsArray := make([]string, 0)
for key, description := range optionsMap {
optionsArray = append(optionsArray, key+": "+description)
}
sort.Strings(optionsArray)
return strings.Join(optionsArray, ", ")
2018-06-09 11:06:33 +02:00
}
func (gui *Gui) renderOptionsMap(optionsMap map[string]string) {
2020-08-15 08:36:39 +02:00
gui.renderString("options", gui.optionsMapToString(optionsMap))
2018-06-09 11:06:33 +02:00
}
2018-08-14 11:05:26 +02:00
func (gui *Gui) trimmedContent(v *gocui.View) string {
return strings.TrimSpace(v.Buffer())
}
2019-02-25 13:11:35 +02:00
func (gui *Gui) currentViewName() string {
currentView := gui.g.CurrentView()
2020-05-16 04:35:19 +02:00
if currentView == nil {
return ""
}
2018-08-14 11:05:26 +02:00
return currentView.Name()
}
2018-09-05 11:07:46 +02:00
2020-08-15 09:23:16 +02:00
func (gui *Gui) resizeCurrentPopupPanel() error {
v := gui.g.CurrentView()
2020-08-18 14:41:14 +02:00
if v == nil {
return nil
}
if gui.isPopupPanel(v.Name()) {
2020-08-15 09:23:16 +02:00
return gui.resizePopupPanel(v)
2018-09-05 11:07:46 +02:00
}
return nil
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) resizePopupPanel(v *gocui.View) error {
2018-09-05 11:07:46 +02:00
// If the confirmation panel is already displayed, just resize the width,
// otherwise continue
content := v.Buffer()
2020-08-15 09:23:16 +02:00
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(v.Wrap, content)
2018-09-05 11:07:46 +02:00
vx0, vy0, vx1, vy1 := v.Dimensions()
if vx0 == x0 && vy0 == y0 && vx1 == x1 && vy1 == y1 {
return nil
}
2020-08-15 09:23:16 +02:00
_, err := gui.g.SetView(v.Name(), x0, y0, x1, y1, 0)
2018-09-05 11:07:46 +02:00
return err
}
func (gui *Gui) changeSelectedLine(panelState IListPanelState, total int, change int) {
2019-11-16 05:00:27 +02:00
// TODO: find out why we're doing this
line := panelState.GetSelectedLineIdx()
if line == -1 {
2019-11-16 05:00:27 +02:00
return
}
var newLine int
if line+change < 0 {
newLine = 0
} else if line+change >= total {
newLine = total - 1
} else {
newLine = line + change
}
panelState.SetSelectedLineIdx(newLine)
}
func (gui *Gui) refreshSelectedLine(panelState IListPanelState, total int) {
line := panelState.GetSelectedLineIdx()
if line == -1 && total > 0 {
panelState.SetSelectedLineIdx(0)
} else if total-1 < line {
panelState.SetSelectedLineIdx(total - 1)
}
}
2018-12-07 09:52:31 +02:00
2020-02-25 11:11:07 +02:00
func (gui *Gui) renderDisplayStrings(v *gocui.View, displayStrings [][]string) {
gui.g.Update(func(g *gocui.Gui) error {
list := utils.RenderDisplayStrings(displayStrings)
v.Clear()
fmt.Fprint(v, list)
return nil
})
}
func (gui *Gui) globalOptionsMap() map[string]string {
2020-10-03 06:54:55 +02:00
keybindingConfig := gui.Config.GetUserConfig().Keybinding
return map[string]string{
2020-10-04 02:00:48 +02:00
fmt.Sprintf("%s/%s", gui.getKeyDisplay(keybindingConfig.Universal.ScrollUpMain), gui.getKeyDisplay(keybindingConfig.Universal.ScrollDownMain)): gui.Tr.LcScroll,
fmt.Sprintf("%s %s %s %s", gui.getKeyDisplay(keybindingConfig.Universal.PrevBlock), gui.getKeyDisplay(keybindingConfig.Universal.NextBlock), gui.getKeyDisplay(keybindingConfig.Universal.PrevItem), gui.getKeyDisplay(keybindingConfig.Universal.NextItem)): gui.Tr.LcNavigate,
gui.getKeyDisplay(keybindingConfig.Universal.Return): gui.Tr.LcCancel,
gui.getKeyDisplay(keybindingConfig.Universal.Quit): gui.Tr.LcQuit,
gui.getKeyDisplay(keybindingConfig.Universal.OptionMenu): gui.Tr.LcMenu,
"1-5": gui.Tr.LcJump,
}
2020-03-29 01:31:34 +02:00
}
func (gui *Gui) isPopupPanel(viewName string) bool {
return viewName == "commitMessage" || viewName == "credentials" || viewName == "confirmation" || viewName == "menu"
}
2019-02-25 13:11:35 +02:00
func (gui *Gui) popupPanelFocused() bool {
return gui.isPopupPanel(gui.currentViewName())
2019-02-25 13:11:35 +02:00
}
2019-11-10 07:20:35 +02:00
2020-05-18 14:00:07 +02:00
// secondaryViewFocused tells us whether it appears that the secondary view is focused. The view is actually never focused for real: we just swap the main and secondary views and then you're still focused on the main view so that we can give you access to all its keybindings for free. I will probably regret this design decision soon enough.
func (gui *Gui) secondaryViewFocused() bool {
2020-10-07 23:01:04 +02:00
state := gui.State.Panels.LineByLine
return state != nil && state.SecondaryFocused
2020-05-18 14:00:07 +02:00
}
2020-08-17 13:58:30 +02:00
func (gui *Gui) clearEditorView(v *gocui.View) {
v.Clear()
_ = v.SetCursor(0, 0)
_ = v.SetOrigin(0, 0)
}
2020-08-19 11:31:58 +02:00
func (gui *Gui) onViewTabClick(viewName string, tabIndex int) error {
context := gui.State.ViewTabContextMap[viewName][tabIndex].contexts[0]
2020-08-19 11:31:58 +02:00
return gui.pushContext(context)
2020-08-19 11:31:58 +02:00
}
func (gui *Gui) handleNextTab() error {
v := gui.g.CurrentView()
2021-04-02 11:51:52 +02:00
if v == nil {
return nil
}
2020-08-19 11:31:58 +02:00
return gui.onViewTabClick(
v.Name(),
utils.ModuloWithWrap(v.TabIndex+1, len(v.Tabs)),
)
}
func (gui *Gui) handlePrevTab() error {
v := gui.g.CurrentView()
2021-04-02 11:51:52 +02:00
if v == nil {
return nil
}
2020-08-19 11:31:58 +02:00
return gui.onViewTabClick(
v.Name(),
utils.ModuloWithWrap(v.TabIndex-1, len(v.Tabs)),
)
}
// this is the distance we will move the cursor when paging up or down in a view
func (gui *Gui) pageDelta(view *gocui.View) int {
_, height := view.Size()
delta := height - 1
if delta == 0 {
return 1
}
return delta
}