In this chapter, you will learn about writing command-line applications that have a user interface (UI). You will look at adding text styling, such as italic or bold text, text of a different color, a UI that uses a spinner, and more. This kind of user interface is possible by using ANSI escape codes, which contain code to do certain things in the terminal.
ANSI escape codes for UI
An open source library to write different kinds of UIs
Styling text, such as italic and bold
Source Code
The source code for this chapter is available from the https://github.com/Apress/Software-Development-Go repository.
ANSI Escape Code
Terminal-based applications that provide a user interface are normally built using ANSI escape codes. The Wikipedia page at https://en.wikipedia.org/wiki/ANSI_escape_code provides a comprehensive explanation of the ANSI code:
ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators.
With the help of ANSI code, there are a proliferation of terminal-based applications that provide a rich terminal-based user interface.
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig1_HTML.jpg)
A screenshot of bash output.
Bash output
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig2_HTML.jpg)
A screenshot of the 256-color output. The numbers are arranged from 1 to 249.
256 color output
Code Description
Code | Description |
---|---|
e | Escape character |
38;5 | ANSI code specifying foreground color |
228 | Color code for bright yellow |
In this section, you learned about ANSI codes and how to use them to print text with different colors by writing Bash scripts. This lays the foundation for the next section where you are going to use ANSI code to write different kinds of terminal-based user interfaces inside Go.
ANSI-Based UI
In the previous section, you looked at ANSI codes and how to use them in Bash. In this section, you are going to use the ANSI codes inside a Go application. You will use ANSI code to set text color, style the text such as italic, and more.
Color Table
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig3_HTML.jpg)
A screenshot of the different color text output. The text Aa is the combination of foreground and background. The foreground numbers are from 30 to 97, while the background numbers are from 40 to 107.
Different color text output
[31;40m: ANSI escape code for black background with red color text
Aa: The Aa text
[0m: Reset
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig4_HTML.jpg)
A screenshot with columns as FG, BG, Name, VGA, Windows XP Console, Windows power, visual studio code, windows 10, and terminal explains the foreground and background mapping.
Foreground and background mapping
In the next section, you will look at examples of how to use ANSI code to format text on a screen.
Styling Text
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig5_HTML.jpg)
The text reads Italics text at the top and Underline text at the bottom.
Text styling using ANSI
In this section, you used different ANSI codes to format text in the console with different colors and formats. Going through the sample code, it is obvious that writing a command-line application that uses ANSI codes is quite laborious because you need to specify the different ANSI codes that are required in the application.
In the next section, you will look at some open source projects that take care of the different aspects of command-line user interface development to make writing code easier.
Open Source Library
In this section, you will look at two different open source libraries that are useful when writing command-line user interfaces. You will look at examples of how to use the libraries and look at how the internals of the libraries work.
Gookit
This library provides a simple API for applications to print text in different foreground and background colors. It also provides text styling such as italics, superscript, etc. The following is the link to the library project: https://github.com/gookit/color.
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig6_HTML.jpg)
A screenshot of Gookit sample output. The text reads, Italing style, strikethrough style, Bold style. Warning colon warning message and Error colon error message.
Gookit sample output
prints the words Strikethrough style in the default foreground and background colors with the strikethrough text format.
Spinner
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig7_HTML.jpg)
The text on the left reads processing request. On the right are the dark-shaded blocks.
Spinner sample output
Calling spinner.New initializes a new spinner with the type specified, in this case spinner.CharSets[35], and the time delay rendering the spinner, which is 100 milliseconds.
You can specify the different spinners, which can be found inside the library in the file character_sets.go.
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Figa_HTML.png)
A screenshot with a text reads character sets = map left bracket int right bracket left bracket right bracket string. Symbols are listed from rows 0 to 90.
The library renders the spinner to the screen by printing through each character byte in the array specified after a certain amount of delay. By doing this, it gives the illusion of animation when seeing it printed on the screen.
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig8_HTML.jpg)
A screenshot of the spinner structure has different characters such as mu, delay, chars with 5 strings, and prefixes as processing requests.
Spinner struct containing spinner characters
The function fires off a goroutine and endlessly loops the animation on the screen until the stop() function is called by the application.
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_15_Chapter__527551_1_En_15_Fig9_HTML.jpg)
A screenshot of the output. S = left bracket star spinner dot spinner 0 x c 0 0 0 0 d 0 0 b 0 I = left bracket interest right bracket 0. outColor = left bracket string right bracket slash r.
outColor final output
Summary
In this chapter, you learn about ANSI codes and how they are useful for creating user interfaces in terminals. The available ANSI codes allow you to write text in color and apply different formatting to the text printed on the screen. You learned that the ANSI codes can be used inside a Bash script and inside Go code.
You explored deeper into the usage of ANSI codes by looking at different open source libraries that provide richer user interface functionality for terminal-based applications. The libraries you looked at provide text-based formatting such as color and styles and progress indicators.