Skip to content

Commit

Permalink
minor modules/git improvements through more config options (#1102)
Browse files Browse the repository at this point in the history
* more config for modules/git: sections, showModuleName, branchInTitle, showFilesIfEmpty, lastFolderTitle

* gofmt

* fix lint issues

* fix lint issues #2
  • Loading branch information
tadeaspaule authored Sep 12, 2021
1 parent cd1be85 commit 10159fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
46 changes: 35 additions & 11 deletions modules/git/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 23 additions & 8 deletions modules/git/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 10159fe

Please sign in to comment.