Getting Started

Adding PTerm to Your Project

Make sure to run this command inside your project, when you’re using go modules πŸ˜‰
PTerm Installation Command
1
go get github.com/pterm/pterm
Copied!

Write Your First Hello World App With PTerm

1
package main
2
​
3
import (
4
"github.com/pterm/pterm"
5
"github.com/pterm/pterm/putils"
6
)
7
​
8
func main() {
9
// Create a new header as a fork from pterm.DefaultHeader.
10
// β”Œ new header variable
11
// β”‚ β”Œ Fork it from the default header
12
// β”‚ β”‚ β”Œ Set options
13
header := pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgRed))
14
​
15
// Print the header centered in your terminal.
16
// β”Œ Use the default CenterPrinter
17
// β”‚ β”Œ Print a string ending with a new line
18
// β”‚ β”‚ β”Œ Use our new header to format the input string
19
pterm.DefaultCenter.Println(header.Sprint("Hello, World"))
20
​
21
// Print a big text to the terminal.
22
// β”Œ Use the default BigTextPrinter
23
// β”‚ β”Œ Set the Letters option
24
// | | β”Œ Use the PTerm Utils (putils) package to create objects faster
25
// β”‚ β”‚ | β”Œ Generate Letters from string
26
// β”‚ β”‚ | | β”Œ Render output to the console
27
_ = pterm.DefaultBigText.WithLetters(putils.LettersFromString("Hello")).Render()
28
​
29
// β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
30
// β”‚There are many more features, which are waiting for you :)β”‚
31
// β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
32
​
33
// TODO: If you want, you can try to make the big text centered.
34
}
Copied!

The 4 Printer Categories

If you want to start with PTerm, you have to know the 4 printer categories.

Interactive Printers

Interactive printers respond to user input. Similar to live printers, interactive printers can update their output. This allows the creation of interactive menus and queries. The Show() method shows the interactive prompt in the terminal.
If you need a printer, which a user can interact with, look here:

Can be used for

  • Select Menus
  • Confirm Prompts
  • User Text Input
  • ...

Live Printers

Live printers can update their output dynamically and are therefore well suited to display changing data like progress bars and live charts. They feature a Start() and Stop() method.
If you need a printer, which can update its own content, look here:

Can be used for

  • Live Performance Tracking
  • Visualizing Progress
  • Live Charts
  • Live System Stats
  • ....

Renderable Printers

Renderable printers print text similarily to text printers, but they do not share the same functions as fmt. They have a Render() and Srender() function. This is because the output is to complex and the printer has to be configured to use specific data via options.
If you need a printer, which prints complex output from some sort of data, look here:

Can be used for

  • Charts
  • Tables
  • Statistics
  • ...

Text Printers

Text printers are printers, which can be used like the standard library's fmt package. They have functions, such as Print(), Sprint(), Println(), Sprintln(), Printf(), Sprintf(), Printfln(), Sprintfln(), etc.
If you need a printer, which has a single text input, look here:

Can be used for

  • Debug / Info / Warning / Error / ... Messages
  • Headers
  • Sections
  • Paragraphs
  • ...