styledconsole

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: Apache-2.0 Imports: 13 Imported by: 1

README

StyledConsole

💅⚡️🖥

Make your GUI tools user-friendly with methods to pretty-print text, lists, tables, user prompts, progress bars...

This project is heavily inspired on the awesome php helper SymfonyStyle from SensioLabs.

❯ Install

Simply run:

go get github.com/corentindeboisset/styledconsole

❯ Usage

example of styledconsole usage in a terminal

See the full usage documentation here.

❯ About styling tags

Any text can be augmented with style, by enclosing the text with tags like this:

<fg=blue>Blue foreground text</fg=blue>

It is possible to imbricate the tags like this:

<fg=blue>Text with blue foreground, <bg=red>text with blue foreground and red background</bg=red>, text with blue foreground again</fg=blue>

If you want to, you can use the </> shorthand to close the last opened tag. The previous text thus becomes:

<fg=blue>Text with blue foreground, <bg=red>text with blue foreground and red background</>, text with blue foreground again</>

One can set multiple properties in a single tag, by separating them with ;:

<fg=blue;bg=red>text with blue fg and red bg</>

The available properties are the following:

  • fg=color: sets the foreground color (value must be in the set below).
  • bg=color: sets the background color (the value must be in the set below).
  • href=http://link/to/resource: adds a hypertext link to the given location.
  • options=opt1,opt2,opt3: Adds additionnal text decorations. Available options are bold, underscore, blink, reverse and conceal.

The available colors are the standard ANSI set:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • default (use the terminal's default)

❯ Contributing

If you want to open an MR, be sure to run the tests with:

golangci-lint run
go test ./...

If you want to run all these tests automatically before every commit, add the custom git-hooks with:

git config core.hooksPath .githooks

Documentation

Overview

Package styledconsole helps to make your GUI tools user-friendly with methods to pretty-print text, lists, tables, user prompts, progress bars...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ask

func Ask(label string, validator func(string) bool) (string, error)

Ask prompts a question with the given label. A function can be given to ensure the validity of the response. To allow any response (even empty), put nil as validator.

func AskHidden

func AskHidden(label string, validator func(string) bool) (string, error)

Same as Ask() but the characters typed by the user are not printed in the output, in a linux-style password prompt.

func AskWithDefault

func AskWithDefault(label string, defaultAnswer string, validator func(string) bool) (string, error)

Same as Ask() but if the user's answer is empty, the given defaultAnswer is chosen instead.

func Choice

func Choice(label string, choices []string) (string, error)

Choice prints a list of choices the user can choose between. The prompts adapts itself to the size of the terminal.

func ChoiceWithDefault

func ChoiceWithDefault(label string, choices []string, defaultAnswer int) (string, error)

ChoiceWithDefault is the same as Choice() but a specific answer index should be given to highlight by default.

func Confirm

func Confirm(label string) (bool, error)

Confirm prompts a yes/no question.

func ConfirmWithDefault

func ConfirmWithDefault(label string, defaultAnswer bool) (bool, error)

ConfirmWithDefault prompts a yes/no question, with a given answer by default if the user's answer is empty.

func Error

func Error(content string)

Error displays the given string highlighted as an error message (with a red background and the Error label).

func Listing

func Listing(items []string)

Listing displays an list of elements

func NewLine

func NewLine()

NewLine prints a line break.

func NewLines

func NewLines(newLineCount int)

NewLines print the given amount of new breaks.

func ProgressAdvance

func ProgressAdvance(stepCount int)

ProgressAdvance advances the current progress bar of a given stepCount. If there is no progressBar it does nothing

func ProgressFinish

func ProgressFinish()

ProgressFinish finishes the current progress bar. If there is no progressBar it does nothing

func ProgressStart

func ProgressStart(totalSteps int)

ProgressStart starts a progress bar of a given duration

func Section

func Section(title string)

Section displays the given string as the title of some command section.

func Success

func Success(content string)

Success displays the given string highlighted as a successful message (with a green background and an [OK] label).

func Table

func Table(headers []string, rows [][]string)

Table pretty-prints a table with headers. It does not support multiline cells or sytling.

func Text

func Text(content string)

Text displays the given string as regular text. This is useful to render help messages and instructions for the user running the command. This methods support style tags such as "<fg=blue>blue text</>".

func Warning

func Warning(content string)

Warning displays the given string highlighted as a warning message (with yellow text and a Warning label).

Types

This section is empty.

Directories

Path Synopsis
Package styledprinter helps to do some pretty-printing of text in terminal using ANSI escape sequences
Package styledprinter helps to do some pretty-printing of text in terminal using ANSI escape sequences

Jump to

Keyboard shortcuts

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