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

157 lines
4.7 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 (
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
2018-08-06 07:37:14 +02:00
"github.com/fatih/color"
2020-11-16 11:38:26 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
2020-02-25 11:55:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2018-08-13 13:16:21 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-06-02 00:35:49 +02:00
)
2020-03-26 14:20:12 +02:00
// never call this on its own, it should only be called from within refreshCommits()
func (gui *Gui) refreshStatus() {
gui.Mutexes.RefreshingStatusMutex.Lock()
defer gui.Mutexes.RefreshingStatusMutex.Unlock()
2020-03-26 14:20:12 +02:00
currentBranch := gui.currentBranch()
if currentBranch == nil {
// need to wait for branches to refresh
return
}
2020-03-26 14:20:12 +02:00
status := ""
2020-03-26 14:20:12 +02:00
if currentBranch.Pushables != "" && currentBranch.Pullables != "" {
trackColor := color.FgYellow
2020-03-26 14:20:12 +02:00
if currentBranch.Pushables == "0" && currentBranch.Pullables == "0" {
trackColor = color.FgGreen
2020-03-26 14:20:12 +02:00
} else if currentBranch.Pushables == "?" && currentBranch.Pullables == "?" {
trackColor = color.FgRed
}
2020-03-26 14:20:12 +02:00
status = utils.ColoredString(fmt.Sprintf("↑%s↓%s ", currentBranch.Pushables, currentBranch.Pullables), trackColor)
}
2019-11-10 07:20:35 +02:00
2020-11-16 11:38:26 +02:00
if gui.GitCommand.WorkingTreeState() != commands.REBASE_MODE_NORMAL {
status += utils.ColoredString(fmt.Sprintf("(%s) ", gui.GitCommand.WorkingTreeState()), color.FgYellow)
2020-03-26 14:20:12 +02:00
}
2018-08-11 07:09:37 +02:00
2020-03-26 14:20:12 +02:00
name := utils.ColoredString(currentBranch.Name, presentation.GetBranchColor(currentBranch.Name))
repoName := utils.GetCurrentRepoName()
status += fmt.Sprintf("%s → %s ", repoName, name)
2019-11-10 07:20:35 +02:00
2021-04-11 02:05:19 +02:00
gui.setViewContent(gui.Views.Status, status)
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 {
2018-08-27 12:08:10 +02:00
gui.Updater.CheckForNewUpdate(gui.onUserUpdateCheckFinish, true)
2020-11-16 11:38:26 +02:00
return gui.createLoaderPanel(gui.Tr.CheckingForUpdates)
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)
if gui.popupPanelFocused() {
return nil
}
2020-03-26 14:20:12 +02:00
currentBranch := gui.currentBranch()
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
2021-04-03 06:56:11 +02:00
if err := gui.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()
2020-03-26 14:20:12 +02:00
upstreamStatus := fmt.Sprintf("↑%s↓%s", currentBranch.Pushables, currentBranch.Pullables)
2019-11-10 07:20:35 +02:00
repoName := utils.GetCurrentRepoName()
switch gui.GitCommand.WorkingTreeState() {
2020-11-16 11:38:26 +02:00
case commands.REBASE_MODE_REBASING, commands.REBASE_MODE_MERGING:
workingTreeStatus := fmt.Sprintf("(%s)", gui.GitCommand.WorkingTreeState())
2019-11-10 07:20:35 +02:00
if cursorInSubstring(cx, upstreamStatus+" ", workingTreeStatus) {
2020-08-15 09:23:16 +02:00
return gui.handleCreateRebaseOptionsMenu()
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
}
}
2020-08-15 08:54:02 +02:00
return gui.handleStatusSelect()
2019-11-10 07:20:35 +02:00
}
2020-08-15 08:54:02 +02:00
func (gui *Gui) handleStatusSelect() 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)
2019-02-25 13:11:35 +02:00
if gui.popupPanelFocused() {
return nil
}
2018-12-08 07:54:54 +02:00
magenta := color.New(color.FgMagenta)
2018-12-07 09:52:31 +02:00
2018-09-23 06:13:10 +02:00
dashboardString := strings.Join(
[]string{
lazygitTitle(),
"Copyright (c) 2018 Jesse Duffield",
2019-03-22 11:13:48 +02:00
"Keybindings: https://github.com/jesseduffield/lazygit/blob/master/docs/keybindings",
2018-09-23 06:13:10 +02:00
"Config Options: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md",
"Tutorial: https://youtu.be/VDXvbHZYeKY",
2018-09-23 06:13:10 +02:00
"Raise an Issue: https://github.com/jesseduffield/lazygit/issues",
2019-11-10 07:20:35 +02:00
magenta.Sprint("Become a sponsor (github is matching all donations for 12 months): https://github.com/sponsors/jesseduffield"), // caffeine ain't free
2020-10-09 15:10:12 +02:00
gui.Tr.ReleaseNotes,
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
},
})
}
func (gui *Gui) handleOpenConfig() error {
2020-10-03 06:54:55 +02:00
return gui.openFile(gui.Config.GetUserConfigPath())
}
func (gui *Gui) handleEditConfig() error {
2020-10-03 06:54:55 +02:00
filename := gui.Config.GetUserConfigPath()
return gui.editFile(filename)
}
2018-08-18 07:30:56 +02:00
func lazygitTitle() string {
return `
_ _ _
| | (_) |
| | __ _ _____ _ __ _ _| |_
| |/ _` + "`" + ` |_ / | | |/ _` + "`" + ` | | __|
| | (_| |/ /| |_| | (_| | | |_
|_|\__,_/___|\__, |\__, |_|\__|
__/ | __/ |
|___/ |___/ `
}
2018-12-08 07:54:54 +02:00
func (gui *Gui) workingTreeState() string {
rebaseMode, _ := gui.GitCommand.RebaseMode()
if rebaseMode != "" {
2020-11-16 11:38:26 +02:00
return commands.REBASE_MODE_REBASING
2018-12-08 07:54:54 +02:00
}
merging, _ := gui.GitCommand.IsInMergeState()
if merging {
2020-11-16 11:38:26 +02:00
return commands.REBASE_MODE_MERGING
2018-12-08 07:54:54 +02:00
}
2020-11-16 11:38:26 +02:00
return commands.REBASE_MODE_NORMAL
2018-12-08 07:54:54 +02:00
}