diff --git a/prompt/prompt.go b/prompt/prompt.go index 79dcb5d..d9e8fc0 100644 --- a/prompt/prompt.go +++ b/prompt/prompt.go @@ -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" @@ -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{ @@ -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 } diff --git a/prompt/templates.go b/prompt/templates.go index afee582..b589747 100644 --- a/prompt/templates.go +++ b/prompt/templates.go @@ -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 + } } diff --git a/prompt/types.go b/prompt/types.go index 300458b..cec6c3e 100644 --- a/prompt/types.go +++ b/prompt/types.go @@ -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 }