diff --git a/README.md b/README.md index cd47e904..9b1e57e3 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,8 @@ Usage of powerline-go: (default "fancy") -duration string The elapsed clock-time of the previous command + -duration-low-precision + Use low precision timing for duration with milliseconds as maximum resolution -duration-min string The minimal time a command has to take before the duration segment is shown (default "0") -east-asian-width @@ -280,6 +282,11 @@ Usage of powerline-go: Shortens names for EKS Kube clusters. -shorten-gke-names Shortens names for GKE Kube clusters. + -shorten-kube-names-regex-match string + Shortens names for Kube clusters matching a custom regex. + -shorten-kube-names-regex-template string + String template to use with -shorten-kube-names-regex-match. + (default "${1}") -static-prompt-indicator Always show the prompt indicator with the default color, never with the error color -theme string diff --git a/args.go b/args.go index 1d5cc88f..0e71c117 100644 --- a/args.go +++ b/args.go @@ -6,46 +6,48 @@ import ( ) type arguments struct { - CwdMode *string - CwdMaxDepth *int - CwdMaxDirSize *int - ColorizeHostname *bool - HostnameOnlyIfSSH *bool - SshAlternateIcon *bool - EastAsianWidth *bool - PromptOnNewLine *bool - StaticPromptIndicator *bool - VenvNameSizeLimit *int - GitAssumeUnchangedSize *int64 - GitDisableStats *string - GitMode *string - Jobs *int - Mode *string - Theme *string - Shell *string - Modules *string - ModulesRight *string - Priority *string - MaxWidthPercentage *int - TruncateSegmentWidth *int - PrevError *int - NumericExitCodes *bool - IgnoreRepos *string - ShortenGKENames *bool - ShortenEKSNames *bool - ShortenOpenshiftNames *bool - ShellVar *string - ShellVarNoWarnEmpty *bool - TrimADDomain *bool - PathAliases *string - Duration *string - DurationMin *string - DurationLowPrecision *bool - Eval *bool - Condensed *bool - IgnoreWarnings *bool - Time *string - ViMode *string + CwdMode *string + CwdMaxDepth *int + CwdMaxDirSize *int + ColorizeHostname *bool + HostnameOnlyIfSSH *bool + SshAlternateIcon *bool + EastAsianWidth *bool + PromptOnNewLine *bool + StaticPromptIndicator *bool + VenvNameSizeLimit *int + GitAssumeUnchangedSize *int64 + GitDisableStats *string + GitMode *string + Jobs *int + Mode *string + Theme *string + Shell *string + Modules *string + ModulesRight *string + Priority *string + MaxWidthPercentage *int + TruncateSegmentWidth *int + PrevError *int + NumericExitCodes *bool + IgnoreRepos *string + ShortenGKENames *bool + ShortenEKSNames *bool + ShortenKubeNamesRegexMatch *string + ShortenKubeNamesRegexTemplate *string + ShortenOpenshiftNames *bool + ShellVar *string + ShellVarNoWarnEmpty *bool + TrimADDomain *bool + PathAliases *string + Duration *string + DurationMin *string + DurationLowPrecision *bool + Eval *bool + Condensed *bool + IgnoreWarnings *bool + Time *string + ViMode *string } var args = arguments{ @@ -173,6 +175,14 @@ var args = arguments{ "shorten-openshift-names", defaults.ShortenOpenshiftNames, comments("Shortens names for Openshift Kube clusters.")), + ShortenKubeNamesRegexMatch: flag.String( + "shorten-kube-names-regex-match", + "", + comments("Shortens names for Kube clusters matching a custom regex.")), + ShortenKubeNamesRegexTemplate: flag.String( + "shorten-kube-names-regex-template", + defaults.ShortenKubeNamesRegexTemplate, + commentsWithDefaults("String template to use with -shorten-kube-names-regex-match.")), ShellVar: flag.String( "shell-var", defaults.ShellVar, diff --git a/config.go b/config.go index 0660cd42..84052a12 100644 --- a/config.go +++ b/config.go @@ -13,49 +13,51 @@ type ThemeMap map[string]Theme type AliasMap map[string]string type Config struct { - CwdMode string `json:"cwd-mode"` - CwdMaxDepth int `json:"cwd-max-depth"` - CwdMaxDirSize int `json:"cwd-max-dir-size"` - ColorizeHostname bool `json:"colorize-hostname"` - HostnameOnlyIfSSH bool `json:"hostname-only-if-ssh"` - SshAlternateIcon bool `json:"alternate-ssh-icon"` - EastAsianWidth bool `json:"east-asian-width"` - PromptOnNewLine bool `json:"newline"` - StaticPromptIndicator bool `json:"static-prompt-indicator"` - VenvNameSizeLimit int `json:"venv-name-size-limit"` - Jobs int `json:"-"` - GitAssumeUnchangedSize int64 `json:"git-assume-unchanged-size"` - GitDisableStats []string `json:"git-disable-stats"` - GitMode string `json:"git-mode"` - Mode string `json:"mode"` - Theme string `json:"theme"` - Shell string `json:"shell"` - Modules []string `json:"modules"` - ModulesRight []string `json:"modules-right"` - Priority []string `json:"priority"` - MaxWidthPercentage int `json:"max-width-percentage"` - TruncateSegmentWidth int `json:"truncate-segment-width"` - PrevError int `json:"-"` - NumericExitCodes bool `json:"numeric-exit-codes"` - IgnoreRepos []string `json:"ignore-repos"` - ShortenGKENames bool `json:"shorten-gke-names"` - ShortenEKSNames bool `json:"shorten-eks-names"` - ShortenOpenshiftNames bool `json:"shorten-openshift-names"` - ShellVar string `json:"shell-var"` - ShellVarNoWarnEmpty bool `json:"shell-var-no-warn-empty"` - TrimADDomain bool `json:"trim-ad-domain"` - PathAliases AliasMap `json:"path-aliases"` - Duration string `json:"-"` - DurationMin string `json:"duration-min"` - DurationLowPrecision bool `json:"duration-low-precision"` - Eval bool `json:"eval"` - Condensed bool `json:"condensed"` - IgnoreWarnings bool `json:"ignore-warnings"` - Modes SymbolMap `json:"modes"` - Shells ShellMap `json:"shells"` - Themes ThemeMap `json:"themes"` - Time string `json:"-"` - ViMode string `json:"vi-mode"` + CwdMode string `json:"cwd-mode"` + CwdMaxDepth int `json:"cwd-max-depth"` + CwdMaxDirSize int `json:"cwd-max-dir-size"` + ColorizeHostname bool `json:"colorize-hostname"` + HostnameOnlyIfSSH bool `json:"hostname-only-if-ssh"` + SshAlternateIcon bool `json:"alternate-ssh-icon"` + EastAsianWidth bool `json:"east-asian-width"` + PromptOnNewLine bool `json:"newline"` + StaticPromptIndicator bool `json:"static-prompt-indicator"` + VenvNameSizeLimit int `json:"venv-name-size-limit"` + Jobs int `json:"-"` + GitAssumeUnchangedSize int64 `json:"git-assume-unchanged-size"` + GitDisableStats []string `json:"git-disable-stats"` + GitMode string `json:"git-mode"` + Mode string `json:"mode"` + Theme string `json:"theme"` + Shell string `json:"shell"` + Modules []string `json:"modules"` + ModulesRight []string `json:"modules-right"` + Priority []string `json:"priority"` + MaxWidthPercentage int `json:"max-width-percentage"` + TruncateSegmentWidth int `json:"truncate-segment-width"` + PrevError int `json:"-"` + NumericExitCodes bool `json:"numeric-exit-codes"` + IgnoreRepos []string `json:"ignore-repos"` + ShortenGKENames bool `json:"shorten-gke-names"` + ShortenEKSNames bool `json:"shorten-eks-names"` + ShortenOpenshiftNames bool `json:"shorten-openshift-names"` + ShortenKubeNamesRegexMatch string `json:"shorten-kube-names-regex-match"` + ShortenKubeNamesRegexTemplate string `json:"shorten-kube-names-regex-template"` + ShellVar string `json:"shell-var"` + ShellVarNoWarnEmpty bool `json:"shell-var-no-warn-empty"` + TrimADDomain bool `json:"trim-ad-domain"` + PathAliases AliasMap `json:"path-aliases"` + Duration string `json:"-"` + DurationMin string `json:"duration-min"` + DurationLowPrecision bool `json:"duration-low-precision"` + Eval bool `json:"eval"` + Condensed bool `json:"condensed"` + IgnoreWarnings bool `json:"ignore-warnings"` + Modes SymbolMap `json:"modes"` + Shells ShellMap `json:"shells"` + Themes ThemeMap `json:"themes"` + Time string `json:"-"` + ViMode string `json:"vi-mode"` } func (mode *SymbolTemplate) UnmarshalJSON(data []byte) error { diff --git a/defaults.go b/defaults.go index 83c91ad1..ed4002d8 100644 --- a/defaults.go +++ b/defaults.go @@ -47,23 +47,25 @@ var defaults = Config{ "exit", "cwd-path", }, - MaxWidthPercentage: 0, - TruncateSegmentWidth: 16, - PrevError: 0, - NumericExitCodes: false, - IgnoreRepos: []string{}, - ShortenGKENames: false, - ShortenEKSNames: false, - ShellVar: "", - ShellVarNoWarnEmpty: false, - TrimADDomain: false, - PathAliases: AliasMap{}, - Duration: "", - DurationMin: "0", - DurationLowPrecision: false, - Eval: false, - Condensed: false, - IgnoreWarnings: false, + MaxWidthPercentage: 0, + TruncateSegmentWidth: 16, + PrevError: 0, + NumericExitCodes: false, + IgnoreRepos: []string{}, + ShortenGKENames: false, + ShortenEKSNames: false, + ShortenKubeNamesRegexMatch: "", + ShortenKubeNamesRegexTemplate: "${1}", + ShellVar: "", + ShellVarNoWarnEmpty: false, + TrimADDomain: false, + PathAliases: AliasMap{}, + Duration: "", + DurationMin: "0", + DurationLowPrecision: false, + Eval: false, + Condensed: false, + IgnoreWarnings: false, Modes: SymbolMap{ "compatible": { Lock: "RO", diff --git a/main.go b/main.go index c1b37868..f8b2a56c 100644 --- a/main.go +++ b/main.go @@ -189,6 +189,10 @@ func main() { cfg.ShortenEKSNames = *args.ShortenEKSNames case "shorten-openshift-names": cfg.ShortenOpenshiftNames = *args.ShortenOpenshiftNames + case "shorten-kube-names-regex-match": + cfg.ShortenKubeNamesRegexMatch = *args.ShortenKubeNamesRegexMatch + case "shorten-kube-names-regex-template": + cfg.ShortenKubeNamesRegexTemplate = *args.ShortenKubeNamesRegexTemplate case "shell-var": cfg.ShellVar = *args.ShellVar case "shell-var-no-warn-empty": diff --git a/segment-kube.go b/segment-kube.go index 5a28663a..a5ea2964 100644 --- a/segment-kube.go +++ b/segment-kube.go @@ -2,7 +2,6 @@ package main import ( "fmt" - pwl "github.com/justjanne/powerline-go/powerline" "io/ioutil" "os" "path" @@ -10,6 +9,8 @@ import ( "regexp" "strings" + pwl "github.com/justjanne/powerline-go/powerline" + "gopkg.in/yaml.v2" ) @@ -106,6 +107,13 @@ func segmentKube(p *powerline) []pwl.Segment { if arnMatches := arnRe.FindStringSubmatch(cluster); arnMatches != nil && p.cfg.ShortenEKSNames { cluster = arnMatches[1] } + + // Shorten Kubernetes cluster names using a custom regex and optionally a custom string template + if p.cfg.ShortenKubeNamesRegexMatch != "" { + nameRe := regexp.MustCompile(p.cfg.ShortenKubeNamesRegexMatch) + cluster = nameRe.ReplaceAllString(cluster, p.cfg.ShortenKubeNamesRegexTemplate) + } + segments := []pwl.Segment{} // Only draw the icon once kubeIconHasBeenDrawnYet := false