promptkit

repository·main·Indexed 43 days ago

https://github.com/erikgeiser/promptkit

A Go library for building interactive, styled command-line prompts. Built on the Charm ecosystem (bubbletea, bubbles, termenv, and reflow), it provides pre-built components such as selection lists, text inputs, and confirmation dialogs that can be used standalone or as bubbletea widgets. Features include custom validation, auto-completion, and a UtilFuncMap for prompt templates.

Tokens
1.1K
Snippets
3
Records
8
Agent score
5%

What's inside promptkit

  1. Overview of promptkit

    main

    promptkit is a Go library designed for creating interactive, styled command-line prompts. It provides a collection of common prompt types with sensible defaults, re-mappable key bindings, and high customizability. It is built on top of the Charm ecosystem (bubbletea, bubbles, termenv, and reflow).

    Note: The API is currently unstable and may undergo significant changes before v1.0.0.

  2. Use Selection prompts

    main

    The selection module allows users to pick one or more items from a list. It supports:

    • Filtering and pagination.
    • Custom types (enabling filtering on specific custom fields).
    • Multi-selection (currently in preview).

    Note for Multi-selection: Because multi-selection is in preview, you must install the latest version from the main branch:

    go get github.com/erikgeiser/promptkit@main
  3. Use Text Input prompts

    main

    The text input module provides several interactive input modes:

    • Standard Text Input: Supports editable default values.
    • Custom Validation: Allows you to validate user input.
    • Hidden Mode: Useful for password prompts where input is masked.
    • Auto-completion: Provides suggestions as the user types.

    Note for Textarea: For longer inputs, a textarea prompt is available (currently in preview). To use it, install from the main branch:

    go get github.com/erikgeiser/promptkit@main
  4. Apply text wrapping and truncation modes

    main

    The WrapMode type defines a function signature func(string, int) string used to control how text is formatted within a prompt. You can use the following built-in implementations:

    • WordWrap(input string, width int): Performs a word wrap. If a word is larger than the width after soft wrapping, it forces a wrap at the width. Note: This may cause coloring issues in some terminals.
    • HardWrap(input string, width int): Performs a hard wrap at the specified width.
    • Truncate(input string, width int): Cuts the string at the specified width per line.
    // WordWrap example
    wrapped := promptkit.WordWrap("hello world", 5)
    
    // HardWrap example
    hard := promptkit.HardWrap("hello world", 5)
    
    // Truncate example
    truncated := promptkit.Truncate("hello world", 5)
  5. Use UtilFuncMap for prompt templates

    main

    The UtilFuncMap() function returns a template.FuncMap containing several utility functions designed for use within prompt templates. These functions allow for string manipulation and basic arithmetic directly inside your templates.

    Key functions include:

    • Len(string): Returns the length of the string as it appears on screen (aware of ANSI color codes).
    • Repeat(string, int): Repeats a string.
    • Join([]string, string): Joins a slice of strings.
    • ToUpper(string) / ToLower(string): Case conversion.
    • Min(int, int), Max(int, int), Add(int, int), Sub(int, int), Mul(int, int): Basic integer arithmetic.
    • TrimPrefix(string, string) / TrimSuffix(string, string): String trimming.
    // Example usage in a template context
    funcs := promptkit.UtilFuncMap()
    template := template.Must(template.New("prompt").Funcs(funcs).Parse("Length: {{ . | Len }}\nValue: {{ . | Add 10 }}"))
    // ... execute template
  6. Handle aborted prompts with ErrAborted

    main

    When a user aborts an interactive prompt (e.g., by pressing Ctrl+C), the prompt component will return promptkit.ErrAborted. You should check for this error to distinguish between a successful user interaction and an intentional cancellation.

    err := prompt.Run()
    if err == promptkit.ErrAborted {
        // Handle user cancellation
    }