termutils

package module
v0.0.0-...-7e26e7c Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 5 Imported by: 0

README

Termutils

Go Reference Go Report Card

Termutils is a Go library for building interactive command-line applications with enhanced terminal capabilities. The library provides utilities for colored output, interactive prompts, and keyboard event handling.

Features

  • 🎨 Colored Output: Print text in different colors to enhance readability and user experience
  • 📋 Interactive Selection: Create user-friendly selection menus for choosing from multiple options
  • ⌨️ Keyboard Event Handling: Listen for specific key presses and respond with custom actions
  • 🔍 Text Styling: Apply formatting like underline to emphasize important information

Installation

go get github.com/utsav-56/termutils

Quick Start

package main

import (
    "fmt"

    "github.com/fatih/color"
    "github.com/utsav-56/termutils"
)

func main() {
    // Print colored text
    termutils.PrintColor("This is red text!", color.FgRed)

    // Print underlined text
    termutils.PUnderline("This is important information")

    // Interactive selection menu
    options := []string{"Option 1", "Option 2", "Option 3"}
    selectedIndex := termutils.TakeListInput("Choose an option:", options)
    fmt.Printf("You selected: %s (index: %d)\n", options[selectedIndex], selectedIndex)

    // Listen for keyboard events
    termutils.ListenForKey('q', func() {
        fmt.Println("Quit key pressed!")
    })
}

API Reference

PrintColor

Prints a message in a specified color to the terminal.

func PrintColor(msg string, colorAttr color.Attribute)

Example:

termutils.PrintColor("Success!", color.FgGreen)
termutils.PrintColor("Error!", color.FgRed)
termutils.PrintColor("Warning!", color.FgYellow)
PUnderline

Prints a message with underline formatting.

func PUnderline(msg string)

Example:

termutils.PUnderline("Section Title")
TakeListInput

Prompts the user to select an item from a list of options.

func TakeListInput(question string, items []string) int

Example:

fruits := []string{"Apple", "Banana", "Orange", "Mango"}
selected := termutils.TakeListInput("Choose a fruit:", fruits)
fmt.Printf("You selected: %s\n", fruits[selected])
ListenForKey

Waits for a specific key press and executes a callback function when that key is pressed.

func ListenForKey(targetKey rune, callback func())

Example:

termutils.ListenForKey('q', func() {
    fmt.Println("Exiting application...")
    os.Exit(0)
})

Dependencies

This library depends on the following packages:

Examples

Check out the example directory for complete working examples.

To run the example:

cd example
go run main.go

Documentation

For full API documentation, visit pkg.go.dev.

License

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Release History

  • 0.1.0 - Initial release with basic terminal utilities

Documentation

Overview

Package termutils provides terminal utilities for enhanced command-line interfaces in Go.

Termutils simplifies common terminal UI tasks like displaying colored text, creating interactive selection menus, and handling keyboard events in command-line applications.

Installation

To install the termutils package, you need to have Go installed and set up on your machine. Then you can use the following go get command:

go get github.com/utsav-56/termutils

Usage Examples

## Colored Output

Print text in different colors using the PrintColor function:

import (
    "github.com/fatih/color"
    "github.com/utsav-56/termutils"
)

// Print text in red
termutils.PrintColor("Error message", color.FgRed)

// Print text in green
termutils.PrintColor("Success message", color.FgGreen)

## Underlined Text

Print underlined text using the PUnderline function:

import "github.com/utsav-56/termutils"

// Print underlined text
termutils.PUnderline("Important information")

## Interactive Selection

Create an interactive selection menu using the TakeListInput function:

import (
    "fmt"
    "github.com/utsav-56/termutils"
)

// Define options
options := []string{"Option 1", "Option 2", "Option 3"}

// Show selection menu and get selected index
selectedIndex := termutils.TakeListInput("Choose an option:", options)

// Use the selected option
fmt.Printf("You selected: %s\n", options[selectedIndex])

## Keyboard Event Handling

Listen for specific key presses using the ListenForKey function:

import (
    "fmt"
    "os"
    "github.com/utsav-56/termutils"
)

// Listen for 'q' key press
termutils.ListenForKey('q', func() {
    fmt.Println("Quit key pressed")
    os.Exit(0)
})

For more examples, see the example directory in the repository.

Package termutils provides terminal utilities for enhanced command-line interfaces in Go. It includes functions for colored output, interactive prompts, and keyboard event handling.

This library simplifies common terminal UI tasks like displaying colored text, creating interactive selection menus, and handling keyboard events in command-line applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenForKey

func ListenForKey(targetKey rune, callback func())

ListenForKey waits for a specific key press and executes a callback function when that key is pressed. It uses the eiannone/keyboard package to handle keyboard events.

Parameters:

  • targetKey: The rune representing the key to listen for
  • callback: A function to execute when the target key is pressed

Example:

termutils.ListenForKey('q', func() {
    fmt.Println("Quit key pressed")
})

func PUnderline

func PUnderline(msg string)

PUnderline prints a message with underline formatting. It uses the fatih/color package to apply underline formatting.

Parameters:

  • msg: The message to print with underline formatting

Example:

termutils.PUnderline("Important information")

func PrintColor

func PrintColor(msg string, colorAttr color.Attribute)

PrintColor prints a message in a specified color to the terminal. It uses the fatih/color package to display colored text.

Parameters:

  • msg: The message to print
  • colorAttr: The color attribute from the color package (e.g., color.FgRed, color.FgGreen)

Example:

termutils.PrintColor("Error message", color.FgRed)
termutils.PrintColor("Success message", color.FgGreen)
termutils.PrintColor("Warning message", color.FgYellow)

func TakeListInput

func TakeListInput(question string, items []string) int

TakeListInput prompts the user to select an item from a list of options. It uses the manifoldco/promptui package to create an interactive selection menu.

Parameters:

  • question: The prompt message to display to the user
  • items: A slice of strings representing the available options

Returns:

  • int: The index of the selected item in the items slice

Example:

options := []string{"Option 1", "Option 2", "Option 3"}
selected := termutils.TakeListInput("Choose an option:", options)
fmt.Println("You selected:", options[selected])

Types

This section is empty.

Jump to

Keyboard shortcuts

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