Giter Club home page Giter Club logo

consola's Introduction

Consola

pub package ci License: MIT

A utility library to help developing command-line applications in Dart. Provides screen manipulation, ANSI escape codes, and more.

Table of Contents

Getting Started

# pubspec.yaml
dependencies:
  consola: <version>

Cursor Manipulation

You can move the cursor around.

void main() {
  Console.moveUp();
  Console.moveTo(22, 41);
  Console.moveToTopLeft();
}

Screen Manipulation

You can clear a section of the screen.

void main() {
  Console.clearScreen();
  Console.clearLine();
  Console.clearCurrentToScreenEnd();
}

Colors

You can colorize the text.

void main() {
  Console.write('Hello ', foregroundColor: SimpleColor.green);
  Console.write('World', backgroundColor: SimpleColor.red);
}

Dimensions

You can get the dimensions of the terminal.

void main() {
  int width = Console.getWindowWidth();
  int height = Console.getWindowHeight();
}

User Input

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.');
}

Components

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).

➤ Progress Bar

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));
  }
}

ANSI Escape Codes

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;
}

Mocking

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"
}

Examples

➤ Rerender a section of the screen

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.');
}

License

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.

consola's People

Contributors

tienisto avatar

Stargazers

kamal Yadav avatar Robson Silva avatar Hasan Basri Bayat avatar Yousuf Omer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.