Skip to content

Commit

Permalink
Merge pull request #14 from yjinjo/v0
Browse files Browse the repository at this point in the history
Complete login subcommand for LOCAL login
  • Loading branch information
yjinjo authored Nov 7, 2024
2 parents b740ece + 7f82c98 commit 3920b0f
Show file tree
Hide file tree
Showing 2 changed files with 431 additions and 81 deletions.
69 changes: 67 additions & 2 deletions cmd/apiResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,79 @@ func getConfigDirectory() string {
}

func renderTable(data [][]string) {
// Render the table using pterm
// Calculate the dynamic width for the "Verb" column
terminalWidth := pterm.GetTerminalWidth()
usedWidth := 30 + 20 + 15 // Estimated widths for Service, Resource, and Short Names
verbColumnWidth := terminalWidth - usedWidth
if verbColumnWidth < 20 {
verbColumnWidth = 20 // Minimum width for Verb column
}

// Use unique colors for each service and its associated data
serviceColors := []pterm.Color{
pterm.FgLightGreen, pterm.FgLightYellow, pterm.FgLightBlue,
pterm.FgLightMagenta, pterm.FgLightCyan, pterm.FgWhite,
}

serviceColorMap := make(map[string]pterm.Color)
colorIndex := 0

table := pterm.TableData{{"Service", "Resource", "Short Names", "Verb"}}

for _, row := range data {
table = append(table, row)
service := row[0]
// Assign a unique color to each service if not already assigned
if _, exists := serviceColorMap[service]; !exists {
serviceColorMap[service] = serviceColors[colorIndex]
colorIndex = (colorIndex + 1) % len(serviceColors)
}

// Get the color for this service
color := serviceColorMap[service]
coloredStyle := pterm.NewStyle(color)

// Color the entire row (Service, Resource, Short Names, Verb)
serviceColored := coloredStyle.Sprint(service)
resourceColored := coloredStyle.Sprint(row[1])
shortNamesColored := coloredStyle.Sprint(row[2])

verbs := splitIntoLinesWithComma(row[3], verbColumnWidth)
for i, line := range verbs {
if i == 0 {
table = append(table, []string{serviceColored, resourceColored, shortNamesColored, coloredStyle.Sprint(line)})
} else {
table = append(table, []string{"", "", "", coloredStyle.Sprint(line)})
}
}
}

// Render the table using pterm
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
}

func splitIntoLinesWithComma(text string, maxWidth int) []string {
words := strings.Split(text, ", ")
var lines []string
var currentLine string

for _, word := range words {
if len(currentLine)+len(word)+2 > maxWidth { // +2 accounts for the ", " separator
lines = append(lines, currentLine+",")
currentLine = word
} else {
if currentLine != "" {
currentLine += ", "
}
currentLine += word
}
}
if currentLine != "" {
lines = append(lines, currentLine)
}

return lines
}

func init() {
rootCmd.AddCommand(apiResourcesCmd)
apiResourcesCmd.Flags().StringVarP(&endpoints, "service", "s", "", "Specify the services to connect to, separated by commas (e.g., 'identity', 'identity,inventory')")
Expand Down
Loading

0 comments on commit 3920b0f

Please sign in to comment.