tconsole

package module
v0.0.0-...-40d1a68 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

README

Go

Console prettification project

Every time, when you create a console application, you are potentially and quite often do interact with another human being who is a user of that tool. To have a meaningful, convenient and intuitive console user interface is still a responsibility of the instrument developer, and to use fmt.Println() as a primary tool for text UI creation is not a perfect choice. While some tools do not require any elaborate UI, as the output of that tool is in fact an input of other tool, sometimes, console output of some program do actually intended to be used by a human, and tconsole golang module will help you to make a better user experience.

Which type of the UI, you can create with tconsole ?

Or let say, what is tconsole "is not". tconsole module is not a "text User Interface" with text menu, windows and other elements of full screen tUI. This module rather help you to replace fmt.Println() with something more user friendly.

tconsole UI

tconsole provides you Print()-like interface that's permitting to print a prefixed messages on text console with indentation that assist with grouping of the output messages. Color-coded prefixes will help you to categorize different types of the messages. You can even add a spinner that will show you the timed messages at the bottom of the screen.

How to use it ?

First, you are going to install it

Installation
go get github.com/vulogov/tconsole

Full source code is available at https://github.com/vulogov/tconsole

Initializing console

You can pass cmap as a parameter to a .New(), or pass nil and tconsole will initialize configurator internally.

Display color-prefixed messages

Color-prefixed messages will help you to send an Info/Error/Warning/Debug messages on text console. tconsole is not a log replacement, it doesn't send anything except prefix and a message.


import "github.com/vulogov/tconsole"

func main() {

  c, _ := tconsole.New(nil)
  c.Info("Info message")
  c.Warning("Warning message")
  c.Error("Error message")
  c.Debug("Debug message")
}
Indenting the messages.

You can increase and decrease message indentation to create a visually identifiable message groups.


import "github.com/vulogov/tconsole"

func main() {

  c, _ := tconsole.New(nil)
  c.Info("Info message")
  c.Inc() // Increase message indentation
  c.Warning("Warning message")
  c.Error("Error message")
  c.Dec() // Deecrease message indentation
  c.Debug("Debug message")
}
Declaring indented sections in the output

You can create named indented sections in your output.


import "github.com/vulogov/tconsole"

func main() {

  c, _ := tconsole.New(nil)
  c.Info("Info message")
  c.Section("SectionName", "This section processing very important data") // Create section in output
  c.Warning("Warning message")
  c.Error("Error message")
  c.SectionEnd() // Ending section
  c.Debug("Debug message")
}
Debugging the functions

Oftentimes, you do need to create named sections specifically to send a debug output about some functions


import "github.com/vulogov/tconsole"

func main() {

  c, _ := tconsole.New(nil)
  c.Info("Info message")
  c.Function("functionName", "This function processing very important data") // Create indented section in output for the named function
  c.Warning("Warning message")
  c.F("Function is doing just fine") // Send some function-related messages
  c.Error("Error message")
  c.Fclose() // Ending function
  c.Debug("Debug message")
}
And adding a spinner

Spinned is a timed message box at the bottom of the screen. It is created by default and you have to stop it by calling .Stop() function.

tconsole spinner UI

and here is demonstration of what the spinner is looks like.


import "github.com/vulogov/tconsole"

func main() {
  c, _ := tconsole.New(nil)
  c.Message("Doing #1") // Sending a message to a spinner
  c.Warning("Warning message") // You can mix'n'match messages to a spinner and to a console
  c.Message("Doing #2") // Sending message to spinner again. Previous message duplicated on a console.
  c.Stop() // Do not forget to stop the spinner
}

Documentation

Index

Constants

View Source
const PSIZE = 10

Variables

View Source
var CriticalPrefixStyle = pterm.Style{pterm.FgLightYellow, pterm.BgLightRed}
View Source
var DebugPrefixStyle = pterm.Style{pterm.FgBlack, pterm.BgGray}
View Source
var DefaultSection = pterm.SectionPrinter{
	Style:           &DefaultStyle,
	Level:           0,
	TopPadding:      0,
	BottomPadding:   0,
	IndentCharacter: " ",
}
View Source
var DefaultSpinner = pterm.SpinnerPrinter{
	Sequence:            []string{"▀ ", " ▀", " ▄", "▄ "},
	Style:               &SpinnerStyle,
	Delay:               time.Millisecond * 200,
	ShowTimer:           true,
	TimerRoundingFactor: time.Second,
	TimerStyle:          &pterm.ThemeDefault.TimerStyle,
	MessageStyle:        &SpinnerTextStyle,
	SuccessPrinter:      &pterm.Success,
	FailPrinter:         &pterm.Error,
	WarningPrinter:      &pterm.Warning,
}
View Source
var DefaultStyle = pterm.Style{pterm.FgDefault}
View Source
var ErrorPrefixStyle = pterm.Style{pterm.FgBlack, pterm.BgLightRed}
View Source
var ErrorStyle = pterm.Style{pterm.FgLightRed}
View Source
var FunctionPrefixStyle = pterm.Style{pterm.FgLightWhite, pterm.BgGray}
View Source
var InfoPrefixStyle = pterm.Style{pterm.FgBlack, pterm.BgCyan}
View Source
var SpinnerStyle = pterm.Style{pterm.FgLightRed}
View Source
var SpinnerTextStyle = pterm.Style{pterm.FgDefault, pterm.BgDefault}
View Source
var WarningPrefixStyle = pterm.Style{pterm.FgBlack, pterm.BgYellow}

Functions

This section is empty.

Types

type TConsole

type TConsole struct {
	// contains filtered or unexported fields
}

func New

func New(cfg *cmap.Cmap) (*TConsole, error)

func (*TConsole) Critical

func (c *TConsole) Critical(msg ...interface{})

func (*TConsole) Debug

func (c *TConsole) Debug(msg ...interface{})

func (*TConsole) Dec

func (c *TConsole) Dec()

func (*TConsole) Disable

func (c *TConsole) Disable()

func (*TConsole) DisableColor

func (c *TConsole) DisableColor()

func (*TConsole) Enable

func (c *TConsole) Enable()

func (*TConsole) EnableColor

func (c *TConsole) EnableColor()

func (*TConsole) Error

func (c *TConsole) Error(msg ...interface{})

func (*TConsole) F

func (c *TConsole) F(msg string, args ...interface{})

func (*TConsole) Fclose

func (c *TConsole) Fclose(msg ...interface{})

func (*TConsole) Func

func (c *TConsole) Func(fname string, msg ...interface{})

func (*TConsole) Get

func (c *TConsole) Get(key string, value interface{}) interface{}

func (*TConsole) IfError

func (c *TConsole) IfError(err error, msg ...interface{})

func (*TConsole) Inc

func (c *TConsole) Inc()

func (*TConsole) Info

func (c *TConsole) Info(msg ...interface{})

func (*TConsole) Message

func (c *TConsole) Message(msg string)

func (*TConsole) Print

func (c *TConsole) Print(msg ...interface{})

func (*TConsole) Println

func (c *TConsole) Println(msg ...interface{})

func (*TConsole) ResetOffset

func (c *TConsole) ResetOffset()

func (*TConsole) Section

func (c *TConsole) Section(sname string, msg ...interface{})

func (*TConsole) SectionEnd

func (c *TConsole) SectionEnd(msg ...interface{})

func (*TConsole) Set

func (c *TConsole) Set(key string, value interface{})

func (*TConsole) Stop

func (c *TConsole) Stop(msg ...interface{})

func (*TConsole) Warning

func (c *TConsole) Warning(msg ...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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