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

141 lines
3.9 KiB
Go
Raw Normal View History

2018-08-13 12:26:02 +02:00
package gui
2018-06-02 00:35:49 +02:00
import (
2021-07-27 22:03:37 +02:00
"errors"
2018-08-06 07:37:14 +02:00
"fmt"
2018-09-23 06:13:10 +02:00
"strings"
2018-06-02 00:35:49 +02:00
2021-12-30 04:35:10 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
2021-04-11 15:32:20 +02:00
"github.com/jesseduffield/lazygit/pkg/constants"
2020-02-25 11:55:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2018-08-13 13:16:21 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-06-02 00:35:49 +02:00
)
2019-11-10 07:20:35 +02:00
func runeCount(str string) int {
return len([]rune(str))
}
func cursorInSubstring(cx int, prefix string, substring string) bool {
return cx >= runeCount(prefix) && cx < runeCount(prefix+substring)
}
func (gui *Gui) handleCheckForUpdate() error {
return gui.c.WithWaitingStatus(gui.c.Tr.CheckingForUpdates, func() error {
2022-01-28 11:44:36 +02:00
gui.Updater.CheckForNewUpdate(gui.onUserUpdateCheckFinish, true)
return nil
})
2018-08-25 07:55:49 +02:00
}
func (gui *Gui) handleStatusClick() error {
2020-08-19 14:27:31 +02:00
// TODO: move into some abstraction (status is currently not a listViewContext where a lot of this code lives)
2022-02-06 06:54:26 +02:00
currentBranch := gui.helpers.Refs.GetCheckedOutRef()
2020-08-23 09:28:28 +02:00
if currentBranch == nil {
// need to wait for branches to refresh
return nil
}
2019-11-10 07:20:35 +02:00
if err := gui.c.PushContext(gui.State.Contexts.Status); err != nil {
2020-08-17 13:58:30 +02:00
return err
}
2021-04-04 15:51:59 +02:00
cx, _ := gui.Views.Status.Cursor()
2022-03-24 08:49:25 +02:00
upstreamStatus := presentation.BranchStatus(currentBranch, gui.Tr)
2019-11-10 07:20:35 +02:00
repoName := utils.GetCurrentRepoName()
workingTreeState := gui.git.Status.WorkingTreeState()
2022-01-02 08:27:01 +02:00
switch workingTreeState {
2021-12-30 04:35:10 +02:00
case enums.REBASE_MODE_REBASING, enums.REBASE_MODE_MERGING:
2022-01-08 05:26:51 +02:00
workingTreeStatus := fmt.Sprintf("(%s)", formatWorkingTreeState(workingTreeState))
2019-11-10 07:20:35 +02:00
if cursorInSubstring(cx, upstreamStatus+" ", workingTreeStatus) {
2022-02-06 06:54:26 +02:00
return gui.helpers.MergeAndRebase.CreateRebaseOptionsMenu()
2019-11-10 07:20:35 +02:00
}
if cursorInSubstring(cx, upstreamStatus+" "+workingTreeStatus+" ", repoName) {
2020-08-15 09:23:16 +02:00
return gui.handleCreateRecentReposMenu()
2019-11-10 07:20:35 +02:00
}
default:
if cursorInSubstring(cx, upstreamStatus+" ", repoName) {
2020-08-15 09:23:16 +02:00
return gui.handleCreateRecentReposMenu()
2019-11-10 07:20:35 +02:00
}
}
return nil
2019-11-10 07:20:35 +02:00
}
2022-01-08 05:26:51 +02:00
func formatWorkingTreeState(rebaseMode enums.RebaseMode) string {
switch rebaseMode {
case enums.REBASE_MODE_REBASING:
return "rebasing"
case enums.REBASE_MODE_MERGING:
return "merging"
default:
return "none"
}
}
func (gui *Gui) statusRenderToMain() error {
2018-09-23 06:13:10 +02:00
dashboardString := strings.Join(
[]string{
lazygitTitle(),
2022-01-02 08:27:35 +02:00
"Copyright 2022 Jesse Duffield",
2021-04-11 15:32:20 +02:00
fmt.Sprintf("Keybindings: %s", constants.Links.Docs.Keybindings),
fmt.Sprintf("Config Options: %s", constants.Links.Docs.Config),
fmt.Sprintf("Tutorial: %s", constants.Links.Docs.Tutorial),
fmt.Sprintf("Raise an Issue: %s", constants.Links.Issues),
2021-04-20 10:34:47 +02:00
fmt.Sprintf("Release Notes: %s", constants.Links.Releases),
2022-01-02 08:27:35 +02:00
style.FgMagenta.Sprintf("Become a sponsor: %s", constants.Links.Donate), // caffeine ain't free
2018-09-23 06:13:10 +02:00
}, "\n\n")
2018-08-18 07:30:56 +02:00
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-18 14:02:35 +02:00
main: &viewUpdateOpts{
title: "",
2021-04-04 15:51:59 +02:00
task: NewRenderStringTask(dashboardString),
2020-08-18 14:02:35 +02:00
},
})
}
2021-07-27 22:03:37 +02:00
func (gui *Gui) askForConfigFile(action func(file string) error) error {
2021-10-16 03:18:49 +02:00
confPaths := gui.Config.GetUserConfigPaths()
switch len(confPaths) {
2021-07-27 22:03:37 +02:00
case 0:
return errors.New(gui.c.Tr.NoConfigFileFoundErr)
2021-07-27 22:03:37 +02:00
case 1:
2021-10-16 03:18:49 +02:00
return action(confPaths[0])
2021-07-27 22:03:37 +02:00
default:
2022-01-29 10:09:20 +02:00
menuItems := make([]*types.MenuItem, len(confPaths))
2021-10-16 03:18:49 +02:00
for i, file := range confPaths {
2021-07-27 22:03:37 +02:00
i := i
2022-01-29 10:09:20 +02:00
menuItems[i] = &types.MenuItem{
2022-01-28 11:44:36 +02:00
DisplayString: file,
OnPress: func() error {
2021-10-16 03:18:49 +02:00
return action(confPaths[i])
2021-07-27 22:03:37 +02:00
},
}
}
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{
2022-02-06 06:54:26 +02:00
Title: gui.c.Tr.SelectConfigFile,
Items: menuItems,
2022-01-28 11:44:36 +02:00
})
2021-07-27 22:03:37 +02:00
}
}
func (gui *Gui) handleOpenConfig() error {
2022-01-31 13:11:34 +02:00
return gui.askForConfigFile(gui.helpers.Files.OpenFile)
}
func (gui *Gui) handleEditConfig() error {
2022-01-31 13:11:34 +02:00
return gui.askForConfigFile(gui.helpers.Files.EditFile)
}
2018-08-18 07:30:56 +02:00
func lazygitTitle() string {
return `
_ _ _
| | (_) |
| | __ _ _____ _ __ _ _| |_
| |/ _` + "`" + ` |_ / | | |/ _` + "`" + ` | | __|
| | (_| |/ /| |_| | (_| | | |_
|_|\__,_/___|\__, |\__, |_|\__|
__/ | __/ |
|___/ |___/ `
}