AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 351 forks source link

Add optional headers to Select #343

Closed tarokkk closed 3 years ago

tarokkk commented 3 years ago

This small addition to the template helps to create a simple selection with a table view.

If you consider this as useful addition I can add tests and examples as well.

Example usage:

import "github.com/olekukonko/tablewriter"

headers, choices := buildTableSelect(data, []string{"A", "B", "C"})
err = survey.AskOne(&survey.Select{
        message: "Select an option",
        Options: choices,
        Headers: headers,},
    &indexChoice)

func buildTableSelect(data [][]string, columns []string) (headers []string, choices []string) {
    tableString := &strings.Builder{}
    table := tablewriter.NewWriter(tableString)
    table.SetBorder(false)
    table.SetCenterSeparator("")
    table.SetColumnSeparator("")
    table.SetRowSeparator("")
    table.SetHeaderLine(false)
    table.SetHeader(columns)
    table.AppendBulk(data)
    table.Render()
    // Split rendered table into lines
    lines := strings.Split(tableString.String(), "\n")
    // Get the headers
    headers = lines[:1]
    // Chomp headers and tailing newline
    choices = lines[1 : len(lines)-1]
    return headers, choices
}
AlecAivazis commented 3 years ago

Hey @tarokkk - can you pot a screenshot of what this looks like? Im struggling to find some time to look over this and would probably get the conversation rolling if i could see what it looks like easily.

AlecAivazis commented 3 years ago

I'm going to close this as its gone stale