Git Product home page Git Product logo

jansi's Introduction

Jansi

Description

Jansi is a small java library that allows you to use ANSI escape codes to format your console output which works even on Windows.

It also provides the required JNI libraries:

Maven Central

Features

  • Implements ANSI escape colorization/handling that is missing on the Windows platform, using integrated JNI library.
  • Strips ANSI escape codes if process output is is being redirected and not attached to a terminal.
  • Easy to use Ansi escape sequence builder API.
  • JNI access to low-level console features.

Synopsis

Most unix terminals support rendering ANSI escape codes when Java sends them via System.out, but when this is done on Windows, they don't get interpreted and you get garbage on the console.

Furthermore, even when on Unix, when process output is being redirected to a file, you typically don't want to output escape codes to the file since most file viewers and editors will not properly display the escape codes.

Jansi detects and abstracts the ANSI support provided by the attached terminal. When your Java application uses Jansi, it can always assume that standard out and error streams support ANSI sequences. Depending on the platform and if the application output is attached to a real terminal, Jansi will do one of the following with the ANSI escape codes that it receives:

  • Pass them through untouched
  • Filter them out
  • Use platform specific APIs to implement the terminal commands represented by the escape sequence

Example Usage

Enabling the Jansi ANSI support into your application is as simple as doing a simple static method call:

import org.fusesource.jansi.AnsiConsole;
...
AnsiConsole.systemInstall();

Disabling it is also done via a static method:

AnsiConsole.systemUninstall();

It is safe to call those methods multiple times, they keep track of how many times systemInstall() has been called and only uninstalls when the systemUninstall() method is called a corresponding number of times.

Using the Ansi escape sequence builder:

import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;
...
System.out.println( ansi().eraseScreen().fg(RED).a("Hello").fg(GREEN).a(" World").reset() );

The above will clear the screen, write Hello in red and World in green, then reset the color attributes so that subsequent data printed to the stream uses the default colors.

But there is an even simpler way to accomplish the above using the render method:

System.out.println( ansi().eraseScreen().render("@|red Hello|@ @|green World|@") );

Project Links

jansi's People

Contributors

arnaud-nauwynck avatar ccustine avatar chirino avatar christianschulte avatar davsclaus avatar dblock avatar dejan2609 avatar dependabot[bot] avatar garydgregory avatar ghquant avatar gnodet avatar hboutemy avatar jbonofre avatar jdbernard avatar jdillon avatar jycchoi avatar lacasseio avatar lanmaoxinqing avatar mildsunrise avatar mrdos avatar pmhahn avatar romge avatar slachiewicz avatar splatch avatar sschuberth avatar tszmytka avatar udalov avatar xuwei-k avatar ylangisc 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.