A utility library to help developing command-line applications in Dart. Provides screen manipulation, ANSI escape codes, and more.
- Getting Started
- Cursor Manipulation
- Screen Manipulation
- Colors
- Dimensions
- User Input
- Components
- ANSI Escape Codes
- Mocking
- Examples
# pubspec.yaml
dependencies:
consola: <version>
You can move the cursor around.
void main() {
Console.moveUp();
Console.moveTo(22, 41);
Console.moveToTopLeft();
}
You can clear a section of the screen.
void main() {
Console.clearScreen();
Console.clearLine();
Console.clearCurrentToScreenEnd();
}
You can colorize the text.
void main() {
Console.write('Hello ', foregroundColor: SimpleColor.green);
Console.write('World', backgroundColor: SimpleColor.red);
}
You can get the dimensions of the terminal.
void main() {
int width = Console.getWindowWidth();
int height = Console.getWindowHeight();
}
You can read the user input in a type-safe way.
void main() {
int? age = Console.readInt(prompt: 'Enter your age: ');
Console.writeLine('You are $age years old.');
}
There is a set of components that you can use to speed up the development of your command-line applications.
The state is stored in the component. You can draw the component by calling Console.draw(component)
.
A horizontal progress bar.
Progress A: [################## ] 58%
Progress B: [=================>--------------] 58%
Progress C: |##################..............| 12 MB/s 58%
void main() {
Console.clearScreen();
final progressBar = ProgressBar.atPosition(
total: 50,
width: 100,
position: ConsoleCoordinate(1, 2),
head: 'Progress A: ',
barFillCharacter: '#',
tailBuilder: (_, __, percent) => ' ${percent.toStringAsFixed(0)}%',
);
for (var i = 0; i <= 50; i++) {
progressBar.current = i;
Console.draw(progressBar);
sleep(Duration(milliseconds: 50));
}
}
You can access the underlying ANSI escape codes by accessing the ConsoleStrings
class.
void main() {
String eraseScreen = ConsoleStrings.eraseScreen;
String esc = ConsoleStrings.escape;
String csi = ConsoleStrings.csi;
}
You can also mock the Console
by setting Console.instance
to a mocked object.
import 'package:your_package/gen/env.g.dart';
class MockConsoleExecutor extends ConsoleExecutor {
@override
void clearScreen({bool resetCursor = true}) {
print('Screen cleared');
}
}
void main() {
Console.clearScreen(); // clears the screen
Console.instance = MockConsoleExecutor();
Console.clearScreen(); // prints "Screen cleared"
}
To rerender a section of the screen, you need to save and restore the cursor position.
void main() {
// Saving and restoring cursor position is relative.
// It doesn't work if the cursor is already at the bottom so
// we need to add some empty lines to make sure it works.
Console.addEmptyLinesToBottom(2);
Console.saveCursorPosition();
Console.writeLine('Survey (1/2)');
final name = Console.readLine(prompt: 'Enter name: ');
Console.restoreCursorPosition();
Console.clearCurrentToScreenEnd();
Console.writeLine('Survey (2/2)');
final age = Console.readInt(prompt: 'Enter age: ');
Console.writeLine('Hello, $name! You are $age years old.');
}
MIT License
Copyright (c) 2024 Tien Do Nam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.