console

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 14 Imported by: 1

README

Console

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.

Installation

go get github.com/dtylman/console

Usage

Prompts

console provides several functions to interact with the user via the command line.

Simple Prompt

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)
}
Password Prompt

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.")
Prompt with Options

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)
String Array Prompt

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)
Tables

Render structured data (structs, maps, or slices) as formatted ASCII tables.

Table from Struct or Map
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)
Table from Slice of Structs
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")

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ask

func Ask(prompt string) (string, error)

Ask asks the user something

func AskBool

func AskBool(prompt string, target *bool, current bool) error

AskBool asks for a boolean value

func AskInt added in v0.0.4

func AskInt(prompt string, target *int, current int) error

AskInt asks for an integer value

func AskOptions

func AskOptions(prompt string, def string, required bool, mask bool) (string, error)

AskOptions ...

func AskPassword

func AskPassword(prompt string) (string, error)

AskPassword asks a password

func AskString

func AskString(prompt string, target *string, current string) error

AskString if current is empty, asks for prompt, stores the answer in target

func AskStringArray

func AskStringArray(prompt string, def []string, required bool) ([]string, error)

AskStringArray convert string array to string and asks

func WriteTable

func WriteTable(obj interface{}, header ...string)

WriteTable render the provided object as a table, object can be map, struct or array of struct(s)

func WriteTableTo

func WriteTableTo(w io.Writer, obj interface{}, header ...string)

WriteTableTo render the provided object to writer as a table, object can be map, struct or array of struct(s)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址