Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Commit

Permalink
Template size calculation depends now on subscription & tenant name l…
Browse files Browse the repository at this point in the history
…ength
  • Loading branch information
StiviiK committed Apr 27, 2023
1 parent 8556704 commit 05efcc6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
35 changes: 21 additions & 14 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Masterminds/sprig/v3"
"github.com/StiviiK/azctx/azurecli"
"github.com/StiviiK/azctx/log"
"github.com/StiviiK/azctx/utils"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/manifoldco/promptui"
Expand All @@ -21,19 +22,30 @@ func BuildPrompt(subscriptions utils.ComparableNamedSlice[azurecli.Subscription]
if err != nil {
terminalWidth = 100 // Default width
terminalHeigth = 20 // Default height
log.Warn("Unable to get terminal dimensions, using default values (width: %d, height: %d)", terminalWidth, terminalHeigth)
}

// Fetch the correct template
tpl, shortPrompt := template(terminalWidth)

// Sort the subscriptions by name
sort.Sort(subscriptions)

// Build the prompt
// Prepare the prompt
subscriptionNames := utils.StringSlice(subscriptions.Names())
maxSubscriptionsLength := subscriptionNames.LongestLength()
maxTenantsLength := tenantNames(subscriptions, shortPrompt).LongestLength()
tenantNames := calculateTenantNames(subscriptions)
tenantNamesMaxLength := tenantNames.LongestLength()

// Fetch the correct template subscriptionId is 36 chars, + 3 for () and a space
tpl := template(terminalWidth, maxSubscriptionsLength, tenantNamesMaxLength, tenantNamesMaxLength+36+3)

// Determine the max length of the tenants
maxTenantsLength := 0
if tpl.IncludesIds {
maxTenantsLength = tenantNamesMaxLength + 36 + 3
} else {
maxTenantsLength = tenantNamesMaxLength
}

// Return the prompt
return promptui.Select{
Items: subscriptions,
Templates: &promptui.SelectTemplates{
Expand Down Expand Up @@ -66,16 +78,11 @@ func newTemplateFuncMap() templates.FuncMap {
return ret
}

// tenantNames returns the tenant names of the given subscriptions
func tenantNames(subscriptions []azurecli.Subscription, shortPrompt bool) utils.StringSlice {
var tenantNames []string
// calculateTenantNames returns the tenant names of the given subscriptions
func calculateTenantNames(subscriptions []azurecli.Subscription) (tenantNames utils.StringSlice) {
for _, subscription := range subscriptions {
if !shortPrompt {
tenantNames = append(tenantNames, fmt.Sprintf("%s (%s)", subscription.TenantName, subscription.Tenant))
} else {
tenantNames = append(tenantNames, subscription.TenantName)
}
tenantNames = append(tenantNames, subscription.TenantName)
}

return tenantNames
return
}
42 changes: 26 additions & 16 deletions prompt/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@ package prompt

var (
template_Long = promptTemplate{
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }} {{ repeat 38 \" \" | print \"SubscriptionId\" | trunc 38 }} {{ repeat %[2]d \" \" | print \"Tenant\" | trunc %[2]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat 36 \" \" | print .Id | trunc 36 | cyan | %[3]s }} | {{ repeat %[2]d \" \" | print \")\" | print .Tenant | print \" (\" | print .TenantName | trunc %[2]d | faint | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat 36 \" \" | print .Id | trunc 36 | cyan | %[3]s }} | {{ repeat %[2]d \" \" | print \")\" | print .Tenant | print \" (\" | print .TenantName | trunc %[2]d | faint | %[3]s }}",
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }} {{ repeat 38 \" \" | print \"SubscriptionId\" | trunc 38 }} {{ repeat %[2]d \" \" | print \"Tenant\" | trunc %[2]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat 36 \" \" | print .Id | trunc 36 | cyan | %[3]s }} | {{ repeat %[2]d \" \" | print \")\" | print .Tenant | print \" (\" | print .TenantName | trunc %[2]d | faint | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat 36 \" \" | print .Id | trunc 36 | cyan | %[3]s }} | {{ repeat %[2]d \" \" | print \")\" | print .Tenant | print \" (\" | print .TenantName | trunc %[2]d | faint | %[3]s }}",
IncludesIds: true,
}
template_Short = promptTemplate{
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }} {{ repeat %[2]d \" \" | print \"Tenant\" | trunc %[2]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat %[2]d \" \" | print .TenantName | trunc %[2]d | cyan | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat %[2]d \" \" | print .TenantName | trunc %[2]d | cyan | %[3]s }}",
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }} {{ repeat %[2]d \" \" | print \"Tenant\" | trunc %[2]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat %[2]d \" \" | print .TenantName | trunc %[2]d | cyan | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }} | {{ repeat %[2]d \" \" | print .TenantName | trunc %[2]d | cyan | %[3]s }}",
IncludesIds: false,
}
template_VeryShort = promptTemplate{
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }}",
Label: "{{ repeat 4 \" \" }}{{ repeat %[1]d \" \" | print \"Name\" | trunc %[1]d }}",
Active: "▸ {{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }}",
Inactive: "{{ repeat 2 \" \" }}{{ repeat %[1]d \" \" | print .Name | trunc %[1]d | green | %[3]s }}",
IncludesIds: false,
}
)

// templateName returns the template to use and a boolean indicating if the template is short
func template(terminalWidth int) (promptTemplate, bool) {
// templateName returns the template to use
// Todo: verify if the numbers for calculating the template are correct
func template(terminalWidth int, maxSubscriptionsLength, maxTenantsLength, maxTenantsWithIdLength int) promptTemplate {
// Determine the template based on the terminal width
switch {
case terminalWidth < 100:
return template_VeryShort, true
case terminalWidth < 200:
return template_Short, true

// +50, subscriptionId is 36 chars, + 4 spaces / seperator, + 10 from the previous case
case terminalWidth > maxSubscriptionsLength+maxTenantsWithIdLength+36+4+10:
return template_Long

// +10, arbitrary / magic number
case terminalWidth > maxSubscriptionsLength+maxTenantsLength+10:
return template_Short

default:
return template_Long, false
return template_VeryShort

}
}
3 changes: 3 additions & 0 deletions prompt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ type promptTemplate struct {
Label string
Active string
Inactive string

// IncludesIds indicates if the template includes the tenant ids & subscription ids
IncludesIds bool
}

0 comments on commit 05efcc6

Please sign in to comment.