Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ toolchain go1.23.7
require (
github.com/fatih/color v1.18.0
github.com/gdamore/tcell/v2 v2.8.1
github.com/mattn/go-runewidth v0.0.16
github.com/ohler55/ojg v1.12.8
github.com/onsi/gomega v1.36.2
github.com/oracle/coherence-go-client/v2 v2.1.0
Expand All @@ -31,7 +32,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
Expand Down
31 changes: 26 additions & 5 deletions pkg/cmd/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/mattn/go-runewidth"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/constants"
"github.com/oracle/coherence-cli/pkg/utils"
Expand Down Expand Up @@ -1465,14 +1466,15 @@ func FormatMembers(members []config.Member, verbose bool, storageMap map[int]boo
fmt.Sprintf("Total cluster members: %d\n", memberCount) +
fmt.Sprintf("Storage enabled count: %d\n", storageCount) +
fmt.Sprintf("Departure count: %d\n\n", departureCount) +
fmt.Sprintf("Cluster Heap - Total: %s Used: %s Available: %s (%4.1f%%)\n",
fmt.Sprintf("Cluster Heap - Total: %s Used: %s Available: %s - %s Avail\n",
strings.TrimSpace(formattingFunction(int64(totalMaxMemoryMB)*MB)),
strings.TrimSpace(formattingFunction(int64(totalUsedMB)*MB)),
strings.TrimSpace(formattingFunction(int64(totalAvailMemoryMB)*MB)), availablePercent) +
fmt.Sprintf("Storage Heap - Total: %s Used: %s Available: %s (%4.1f%%)\n\n",
strings.TrimSpace(formattingFunction(int64(totalAvailMemoryMB)*MB)),
formatPercent(float64(availablePercent/100))) +
fmt.Sprintf("Storage Heap - Total: %s Used: %s Available: %s - %s Avail\n\n",
strings.TrimSpace(formattingFunction(int64(totalMaxStorageMemoryMB)*MB)),
strings.TrimSpace(formattingFunction(int64(totalUsedStorageMB)*MB)),
strings.TrimSpace(formattingFunction(int64(totalAvailStorageMemoryMB)*MB)), availableStoragePercent)
strings.TrimSpace(formattingFunction(int64(totalAvailStorageMemoryMB)*MB)), formatPercent(float64(availableStoragePercent/100)))
}

if summary {
Expand Down Expand Up @@ -2236,6 +2238,12 @@ func formatPercent(value float64) string {
if value == -1 {
return na
}
if includePercentageBar {
if percentageBarWidth < 10 {
percentageBarWidth = 10
}
return generatePercentageBar(value*100, percentageBarWidth)
}
return strings.TrimSpace(printer.Sprintf("%6.2f%%", value*100))
}

Expand Down Expand Up @@ -2696,7 +2704,7 @@ func (t *formattedTable) getMaxColumnLen() []int {
for _, value := range columns {
for j, entry := range value {
if len(entry) > lengths[j] {
lengths[j] = len(entry)
lengths[j] = runewidth.StringWidth(entry)
}
}
}
Expand All @@ -2711,3 +2719,16 @@ func (t *formattedTable) getCombined() [][]string {

return columns
}

// generatePercentageBar generates a percentage bar.
func generatePercentageBar(percent float64, width int) string {
if percent < 0 {
percent = 0
}
if percent > 100 {
percent = 100
}
full := int((percent / 100) * float64(width))
empty := width - full
return fmt.Sprintf("[%s%s] %6.2f%%", strings.Repeat("█", full), strings.Repeat("░", empty), percent)
}
17 changes: 13 additions & 4 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ const (
connectionNameOption = "connection"
clusterNameOptionShort = "c"

serviceNameDescription = "Service name"
serviceNameOption = "service"
serviceNameOptionShort = "s"
ignoreCachesDescription = "ignore system caches or caches with $"
serviceNameDescription = "Service name"
serviceNameOption = "service"
serviceNameOptionShort = "s"
ignoreCachesDescription = "ignore system caches or caches with $"
includePercentDescription = "show percentage as bar"

userNameDescription = "basic auth username if authentication is required"
usernameOption = "username"
Expand Down Expand Up @@ -154,6 +155,12 @@ var (
// global sort direction
descendingFlag bool

// global, include percentage as bar
includePercentageBar bool

// global percentage bar width
percentageBarWidth int

Logger *zap.Logger

// Version is the cohctl version injected by the Go linker at build time.
Expand Down Expand Up @@ -255,6 +262,8 @@ func SetRootCommandFlags(command *cobra.Command) {
command.PersistentFlags().StringVarP(&clusterConnection, connectionNameOption, clusterNameOptionShort, "", clusterConnectionDescription)
command.PersistentFlags().StringVarP(&tableSorting, "sort", "", "", "specify a sorting column name or number for tables")
command.PersistentFlags().BoolVarP(&descendingFlag, "desc", "", false, "indicates descending sort for tables, default is ascending")
command.PersistentFlags().BoolVarP(&includePercentageBar, "percent-bar", "", false, includePercentDescription)
command.PersistentFlags().IntVarP(&percentageBarWidth, "percent-bar-width", "", 30, "set percentage bar width")

command.PersistentFlags().BoolVarP(&kbFormat, "kb", "k", false, "show sizes in kilobytes (default is bytes)")
command.PersistentFlags().BoolVarP(&mbFormat, "mb", "m", false, "show sizes in megabytes (default is bytes)")
Expand Down
Loading