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 ¶
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 ¶
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.