From 10159fe5a779ec6b6e3935dcfb84f731ac0652e8 Mon Sep 17 00:00:00 2001 From: tadeas Date: Sun, 12 Sep 2021 16:19:17 +0200 Subject: [PATCH] minor modules/git improvements through more config options (#1102) * more config for modules/git: sections, showModuleName, branchInTitle, showFilesIfEmpty, lastFolderTitle * gofmt * fix lint issues * fix lint issues #2 --- modules/git/display.go | 46 +++++++++++++++++++++++++++++++---------- modules/git/settings.go | 31 ++++++++++++++++++++------- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/modules/git/display.go b/modules/git/display.go index 896c2e4c8..051f01c68 100644 --- a/modules/git/display.go +++ b/modules/git/display.go @@ -16,20 +16,44 @@ func (widget *Widget) content() (string, string, bool) { return widget.CommonSettings().Title, " Git repo data is unavailable ", false } - title := fmt.Sprintf( - "%s - %s[white]", - widget.CommonSettings().Title, - repoData.Repository, - ) + widgetTitle := "" + if widget.settings.lastFolderTitle { + pathParts := strings.Split(repoData.Repository, "/") + widgetTitle += pathParts[len(pathParts)-1] + + } else { + widgetTitle = repoData.Repository + } + if widget.settings.branchInTitle { + widgetTitle += fmt.Sprintf(" <%s>", repoData.Branch) + } + title := "" + if widget.settings.showModuleName { + title = fmt.Sprintf( + "%s - %s[white]", + widget.CommonSettings().Title, + widgetTitle, + ) + } else { + title = fmt.Sprintf( + "%s[white]", + widgetTitle, + ) + } _, _, width, _ := widget.View.GetRect() str := widget.settings.PaginationMarker(len(widget.GitRepos), widget.Idx, width) + "\n" - str += fmt.Sprintf(" [%s]Branch[white]\n", widget.settings.Colors.Subheading) - str += fmt.Sprintf(" %s", repoData.Branch) - str += "\n" - str += widget.formatChanges(repoData.ChangedFiles) - str += "\n" - str += widget.formatCommits(repoData.Commits) + for _, v := range widget.settings.sections { + if v == "branch" { + str += fmt.Sprintf(" [%s]Branch[white]\n", widget.settings.Colors.Subheading) + str += fmt.Sprintf(" %s", repoData.Branch) + } else if v == "files" && (widget.settings.showFilesIfEmpty || len(repoData.ChangedFiles) > 1) { + str += widget.formatChanges(repoData.ChangedFiles) + } else if v == "commits" { + str += widget.formatCommits(repoData.Commits) + } + str += "\n" + } return title, str, false } diff --git a/modules/git/settings.go b/modules/git/settings.go index 1d9c0eee8..318c1a6c4 100644 --- a/modules/git/settings.go +++ b/modules/git/settings.go @@ -14,20 +14,35 @@ const ( type Settings struct { *cfg.Common - commitCount int `help:"The number of past commits to display." values:"A positive integer, 0..n." optional:"true"` - commitFormat string `help:"The string format for the commit message." optional:"true"` - dateFormat string `help:"The string format for the date/time in the commit message." optional:"true"` - repositories []interface{} `help:"Defines which git repositories to watch." values:"A list of zero or more local file paths pointing to valid git repositories."` + commitCount int `help:"The number of past commits to display." values:"A positive integer, 0..n." optional:"true"` + sections []interface{} `help:"Sections to show" optional:"true"` + showModuleName bool `help:"Whether to show 'Git - ' before information in title" optional:"true" default:"true"` + branchInTitle bool `help:"Whether to show branch name in title instead of the widget body itself" optional:"true" default:"false"` + showFilesIfEmpty bool `help:"Whether to show Changed Files section if no changed files" optional:"true" default:"true"` + lastFolderTitle bool `help:"Whether to show only last part of directory path instead of full path" optional:"true" default:"false"` + commitFormat string `help:"The string format for the commit message." optional:"true"` + dateFormat string `help:"The string format for the date/time in the commit message." optional:"true"` + repositories []interface{} `help:"Defines which git repositories to watch." values:"A list of zero or more local file paths pointing to valid git repositories."` } func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings { settings := Settings{ Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig), - commitCount: ymlConfig.UInt("commitCount", 10), - commitFormat: ymlConfig.UString("commitFormat", "[forestgreen]%h [white]%s [grey]%an on %cd[white]"), - dateFormat: ymlConfig.UString("dateFormat", "%b %d, %Y"), - repositories: ymlConfig.UList("repositories"), + commitCount: ymlConfig.UInt("commitCount", 10), + sections: ymlConfig.UList("sections"), + showModuleName: ymlConfig.UBool("showModuleName", true), + branchInTitle: ymlConfig.UBool("branchInTitle", false), + showFilesIfEmpty: ymlConfig.UBool("showFilesIfEmpty", true), + lastFolderTitle: ymlConfig.UBool("lastFolderTitle", false), + commitFormat: ymlConfig.UString("commitFormat", "[forestgreen]%h [white]%s [grey]%an on %cd[white]"), + dateFormat: ymlConfig.UString("dateFormat", "%b %d, %Y"), + repositories: ymlConfig.UList("repositories"), + } + if len(settings.sections) == 0 { + for _, v := range []string{"branch", "files", "commits"} { + settings.sections = append(settings.sections, v) + } } return &settings