A simple and lightweight Go library for creating console user interfaces effectively. It provides tools for prompting user input and rendering structured data as tables.
go get github.com/dtylman/consoleconsole provides several functions to interact with the user via the command line.
Ask the user for input with a simple prompt.
package main
import (
"fmt"
"log"
"github.com/dtylman/console"
)
func main() {
name, err := console.Ask("What is your name?")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Hello, %s!\n", name)
}Ask for sensitive information like passwords. The input will be masked.
password, err := console.AskPassword("Enter your password")
if err != nil {
log.Fatal(err)
}
fmt.Println("Password received.")Ask with more control, such as setting a default value, making the input required, or masking it.
// Prompt: "Enter your mood (happy): "
// Default: "happy"
// Required: true
// Mask: false
mood, err := console.AskOptions("Enter your mood", "happy", true, false)Ask for a comma-separated list of strings.
// Prompt: "Enter tags (tag1,tag2): "
tags, err := console.AskStringArray("Enter tags", []string{"tag1", "tag2"}, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Tags: %v\n", tags)Render structured data (structs, maps, or slices) as formatted ASCII tables.
type User struct {
ID int
Name string
Email string
}
user := User{ID: 1, Name: "Alice", Email: "alice@example.com"}
// Output:
// +-------+-------------------+
// | FIELD | VALUE |
// +-------+-------------------+
// | ID | 1 |
// | Name | Alice |
// | Email | alice@example.com |
// +-------+-------------------+
console.WriteTable(user)users := []User{
{ID: 1, Name: "Alice", Email: "alice@example.com"},
{ID: 2, Name: "Bob", Email: "bob@example.com"},
}
// Output:
// +----+-------+-------------------+
// | ID | NAME | EMAIL |
// +----+-------+-------------------+
// | 1 | Alice | alice@example.com |
// | 2 | Bob | bob@example.com |
// +----+-------+-------------------+
console.WriteTable(users)You can also specify headers explicitly:
console.WriteTable(users, "User ID", "Full Name", "Contact Email")This project is licensed under the MIT License - see the LICENSE file for details.