mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
- Add some README file to give missing documentations or update existing documentation file Package Archive: - Add some comments to godoc information - Moving NopWriterCloser interface to ioutils package Package IOUtils: - New package NopWriterCloser to implement interfac like NopReader Package Database: - KVMap: fix missing function following update of kvdriver Package Duration: - Rename BDD testing Package Context/Gin: - Moving function New between model & interface file Package AWS: - rework Walk function to use more generic with standard walk caller function - func walk will now no more return and include error (can be catched into the given func) - func walk will now return a bool to continue or stop the loop - func walk with many input function will now stop when all given function return false - func walk will now return error only about main process and not given function Package errors: - Add interface error into interface Error Package IOUtils: - Moving IOWrapper as subPackage and optimize process + allow thread safe
69 lines
2.4 KiB
Markdown
69 lines
2.4 KiB
Markdown
## `console` Package
|
|
|
|
The `console` package provides utilities for enhanced console input/output in Go applications. It offers colored output, user prompts, and string formatting helpers to improve CLI user experience.
|
|
|
|
### Features
|
|
|
|
- Colored printing and formatting using [fatih/color](https://github.com/fatih/color)
|
|
- Customizable color types for standard and prompt outputs
|
|
- User input prompts for strings, integers, booleans, URLs, and passwords (with hidden input)
|
|
- String padding and tabulated printing helpers
|
|
- Error handling with custom error codes
|
|
|
|
---
|
|
|
|
### Main Types & Functions
|
|
|
|
#### Color Output
|
|
|
|
- **SetColor(col colorType, value ...int)**: Set color attributes for a color type.
|
|
- **ColorPrint / ColorPrompt**: Predefined color types for standard and prompt outputs.
|
|
- **Print, Println, Printf, Sprintf**: Print text with or without color.
|
|
- **BuffPrintf**: Print formatted text to an `io.Writer` with color support.
|
|
|
|
#### User Prompts
|
|
|
|
- **PromptString(text string) (string, error)**: Prompt user for a string.
|
|
- **PromptInt(text string) (int64, error)**: Prompt user for an integer.
|
|
- **PromptBool(text string) (bool, error)**: Prompt user for a boolean.
|
|
- **PromptUrl(text string) (\*url.URL, error)**: Prompt user for a URL.
|
|
- **PromptPassword(text string) (string, error)**: Prompt user for a password (input hidden).
|
|
|
|
#### String Formatting Helpers
|
|
|
|
- **PadLeft/PadRight/PadCenter(str string, len int, pad string) string**: Pad a string to a given length.
|
|
- **PrintTabf(tablLevel int, format string, args ...interface{})**: Print formatted text with indentation.
|
|
|
|
#### Error Handling
|
|
|
|
- Custom error codes for parameter validation and I/O errors.
|
|
- Errors are wrapped using the `github.com/nabbar/golib/errors` package.
|
|
|
|
---
|
|
|
|
### Example Usage
|
|
|
|
```go
|
|
import "github.com/nabbar/golib/console"
|
|
|
|
func main() {
|
|
// Set prompt color to green
|
|
console.SetColor(console.ColorPrompt, 32)
|
|
name, _ := console.PromptString("Enter your name")
|
|
console.ColorPrint.Printf("Hello, %s!\n", name)
|
|
|
|
// Print padded and colored output
|
|
padded := console.PadCenter("Welcome", 20, "-")
|
|
console.ColorPrint.Println(padded)
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### Notes
|
|
|
|
- Colors can be customized using ANSI attributes.
|
|
- Password prompts use hidden input (no echo).
|
|
- All prompt functions return errors; always check them in production code.
|
|
- Deprecated functions are marked and will be removed in future versions.
|