From 0996f9ebf418d33fa6c0b1a8d7cb364b80d013fa Mon Sep 17 00:00:00 2001 From: GMkonan Date: Thu, 23 Jun 2022 08:26:09 -0300 Subject: [PATCH] Commit message length warning --- docs/Config.md | 3 ++- pkg/config/user_config.go | 8 ++++++-- pkg/gui/commit_message_panel.go | 29 ++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 42c4adc7d1b..862d43e0135 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -45,6 +45,7 @@ gui: - red commitLength: show: true + warningThreshold: 0 mouseEvents: true skipUnstageLineWarning: false skipStashWarning: false @@ -530,4 +531,4 @@ notARepository: 'create' ```yaml # to skip without creating a new repo notARepository: 'skip' -``` +``` \ No newline at end of file diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 59bfcc04294..411724df5b8 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -67,7 +67,8 @@ type ThemeConfig struct { } type CommitLengthConfig struct { - Show bool `yaml:"show"` + Show bool `yaml:"show"` + WarningThreshold int `yaml:"warningThreshold"` } type GitConfig struct { @@ -362,7 +363,10 @@ func GetDefaultConfig() *UserConfig { CherryPickedCommitFgColor: []string{"blue"}, UnstagedChangesColor: []string{"red"}, }, - CommitLength: CommitLengthConfig{Show: true}, + CommitLength: CommitLengthConfig{ + Show: true, + WarningThreshold: 0, + }, SkipNoStagedFilesWarning: false, ShowListFooter: true, ShowCommandLog: true, diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index 35ffc822f92..35129a6348f 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -1,11 +1,12 @@ package gui import ( - "strconv" + "bufio" "strings" - + "fmt" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/jesseduffield/lazygit/pkg/theme" ) func (gui *Gui) handleCommitMessageFocused() error { @@ -28,9 +29,27 @@ func (gui *Gui) RenderCommitLength() { return } - gui.Views.CommitMessage.Subtitle = getBufferLength(gui.Views.CommitMessage) + commitLength := getBufferLength(gui.Views.CommitMessage) + gui.Views.CommitMessage.Subtitle = fmt.Sprintf(" %d ", commitLength) + gui.checkCommitLengthWarning(commitLength) } -func getBufferLength(view *gocui.View) string { - return " " + strconv.Itoa(strings.Count(view.TextArea.GetContent(), "")-1) + " " +func getBufferLength(view *gocui.View) int { + return strings.Count(view.TextArea.GetContent(), "") - 1 } + +func (gui *Gui) checkCommitLengthWarning(commitLength int) { + if gui.c.UserConfig.Gui.CommitLength.WarningThreshold == 0 { + return + } + + scanner := bufio.NewScanner(strings.NewReader(gui.Views.CommitMessage.TextArea.GetContent())) + + for scanner.Scan() { + if len(scanner.Text()) > gui.c.UserConfig.Gui.CommitLength.WarningThreshold { + gui.Views.CommitMessage.FgColor = gocui.ColorRed + } else { + gui.Views.CommitMessage.FgColor = theme.GocuiDefaultTextColor + } + } +} \ No newline at end of file