Progress bar
- Dart course
- Advanced
- Lesson 58/83
- 5 min
Learn progress bars with cli_util.
The cli_util package
cli_util provides helper utilities for Dart CLI applications. You get the configuration directory with applicationConfigHome, the terminal width with terminalWidth, and colored output with AnsiCode.
Example: import 'package:cli_util/cli_util.dart'; final config = applicationConfigHome; final width = terminalWidth;. applicationConfigHome returns a different path depending on the operating system (~/.config on Linux, ~/Library/Application Support on macOS). This is the standard place to store CLI tool configuration.
Terminal width
terminalWidth gives the number of columns in the terminal window. When printing a progress bar or a table you align the output to this width. Example: final w = terminalWidth; print('Width: $w');.
applicationConfigHome gives the configuration directory. A CLI tool stores its settings here. Example: final config = applicationConfigHome;. This is the way to keep user-specific data in a standard place; it gives the right directory on each operating system.
Practice Print cli_util info
Add import 'package:cli_util/cli_util.dart';. In main add print('cli_util: CLI helpers'), print('API: applicationConfigHome') and print('API: terminalWidth').
Starter code
// import the cli_util package
void main() {
// print cli_util helpers
}Sample solution
import 'package:cli_util/cli_util.dart';
void main() {
print('cli_util: CLI helpers');
print('API: applicationConfigHome');
print('API: terminalWidth');
}Expected output
cli_util: CLI helpers API: applicationConfigHome API: terminalWidth
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does cli_util provide?
- HTTP client
- CLI helpers Correct answer
- Database
- Network client
Explanation: cli_util provides helpers for CLI applications.
2 Match the cli_util parts
| Term | Meaning |
|---|---|
cli_util |
CLI helper |
applicationConfigHome |
Config dir |
terminalWidth |
Terminal width |
AnsiCode |
Color |
3 Accessing terminalWidth
final width = ____;
final width = terminalWidth;
Explanation: terminalWidth gives the terminal's column count.