Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*/
package jline;
import java.io.*;
/**
* A buffer that can contain ANSI text.
*
* @author Marc Prud'hommeaux
*/
public class ANSIBuffer {
private boolean ansiEnabled = true;
private final StringBuffer ansiBuffer = new StringBuffer();
private final StringBuffer plainBuffer = new StringBuffer();
public ANSIBuffer() {
}
public ANSIBuffer(final String str) {
append(str);
}
public void setAnsiEnabled(final boolean ansi) {
this.ansiEnabled = ansi;
}
public boolean getAnsiEnabled() {
return this.ansiEnabled;
}
public String getAnsiBuffer() {
return ansiBuffer.toString();
}
public String getPlainBuffer() {
return plainBuffer.toString();
}
public String toString(final boolean ansi) {
return ansi ? getAnsiBuffer() : getPlainBuffer();
}
public String toString() {
return toString(ansiEnabled);
}
public ANSIBuffer append(final String str) {
ansiBuffer.append(str);
plainBuffer.append(str);
return this;
}
public ANSIBuffer attrib(final String str, final int code) {
ansiBuffer.append(ANSICodes.attrib(code)).append(str)
.append(ANSICodes.attrib(ANSICodes.OFF));
plainBuffer.append(str);
return this;
}
public ANSIBuffer red(final String str) {
return attrib(str, ANSICodes.FG_RED);
}
public ANSIBuffer blue(final String str) {
return attrib(str, ANSICodes.FG_BLUE);
}
public ANSIBuffer green(final String str) {
return attrib(str, ANSICodes.FG_GREEN);
}
public ANSIBuffer black(final String str) {
return attrib(str, ANSICodes.FG_BLACK);
}
public ANSIBuffer yellow(final String str) {
return attrib(str, ANSICodes.FG_YELLOW);
}
public ANSIBuffer magenta(final String str) {
return attrib(str, ANSICodes.FG_MAGENTA);
}
public ANSIBuffer cyan(final String str) {
return attrib(str, ANSICodes.FG_CYAN);
}
public ANSIBuffer bold(final String str) {
return attrib(str, ANSICodes.BOLD);
}
public ANSIBuffer underscore(final String str) {
return attrib(str, ANSICodes.UNDERSCORE);
}
public ANSIBuffer blink(final String str) {
return attrib(str, ANSICodes.BLINK);
}
public ANSIBuffer reverse(final String str) {
return attrib(str, ANSICodes.REVERSE);
}
public static class ANSICodes {
static final int OFF = 0;
static final int BOLD = 1;
static final int UNDERSCORE = 4;
static final int BLINK = 5;
static final int REVERSE = 7;
static final int CONCEALED = 8;
static final int FG_BLACK = 30;
static final int FG_RED = 31;
static final int FG_GREEN = 32;
static final int FG_YELLOW = 33;
static final int FG_BLUE = 34;
static final int FG_MAGENTA = 35;
static final int FG_CYAN = 36;
static final int FG_WHITE = 37;
static final char ESC = 27;
/**
* Constructor is private since this is a utility class.
*/
private ANSICodes() {
}
/**
* Sets the screen mode. The mode will be one of the following values:
*
* mode description
* ----------------------------------------
* 0 40 x 148 x 25 monochrome (text)
* 1 40 x 148 x 25 color (text)
* 2 80 x 148 x 25 monochrome (text)
* 3 80 x 148 x 25 color (text)
* 4 320 x 148 x 200 4-color (graphics)
* 5 320 x 148 x 200 monochrome (graphics)
* 6 640 x 148 x 200 monochrome (graphics)
* 7 Enables line wrapping
* 13 320 x 148 x 200 color (graphics)
* 14 640 x 148 x 200 color (16-color graphics)
* 15 640 x 148 x 350 monochrome (2-color graphics)
* 16 640 x 148 x 350 color (16-color graphics)
* 17 640 x 148 x 480 monochrome (2-color graphics)
* 18 640 x 148 x 480 color (16-color graphics)
* 19 320 x 148 x 200 color (256-color graphics)
*
*/
public static String setmode(final int mode) {
return ESC + "[=" + mode + "h";
}
/**
* Same as setmode () except for mode = 7, which disables line
* wrapping (useful for writing the right-most column without
* scrolling to the next line).
*/
public static String resetmode(final int mode) {
return ESC + "[=" + mode + "l";
}
/**
* Clears the screen and moves the cursor to the home postition.
*/
public static String clrscr() {
return ESC + "[2J";
}
/**
* Removes all characters from the current cursor position until
* the end of the line.
*/
public static String clreol() {
return ESC + "[K";
}
/**
* Moves the cursor n positions to the left. If n is greater or
* equal to the current cursor column, the cursor is moved to the
* first column.
*/
public static String left(final int n) {
return ESC + "[" + n + "D";
}
/**
* Moves the cursor n positions to the right. If n plus the current
* cursor column is greater than the rightmost column, the cursor
* is moved to the rightmost column.
*/
public static String right(final int n) {
return ESC + "[" + n + "C";
}
/**
* Moves the cursor n rows up without changing the current column.
* If n is greater than or equal to the current row, the cursor is
* placed in the first row.
*/
public static String up(final int n) {
return ESC + "[" + n + "A";
}
/**
* Moves the cursor n rows down. If n plus the current row is greater
* than the bottom row, the cursor is moved to the bottom row.
*/
public static String down(final int n) {
return ESC + "[" + n + "B";
}
/*
* Moves the cursor to the given row and column. (1,1) represents
* the upper left corner. The lower right corner of a usual DOS
* screen is (25, 80).
*/
public static String gotoxy(final int row, final int column) {
return ESC + "[" + row + ";" + column + "H";
}
/**
* Saves the current cursor position.
*/
public static String save() {
return ESC + "[s";
}
/**
* Restores the saved cursor position.
*/
public static String restore() {
return ESC + "[u";
}
/**
* Sets the character attribute. It will be
* one of the following character attributes:
*
*
* Text attributes
* 0 All attributes off
* 1 Bold on
* 4 Underscore (on monochrome display adapter only)
* 5 Blink on
* 7 Reverse video on
* 8 Concealed on
*
* Foreground colors
* 30 Black
* 31 Red
* 32 Green
* 33 Yellow
* 34 Blue
* 35 Magenta
* 36 Cyan
* 37 White
*
* Background colors
* 40 Black
* 41 Red
* 42 Green
* 43 Yellow
* 44 Blue
* 45 Magenta
* 46 Cyan
* 47 White
*
*
* The attributes remain in effect until the next attribute command
* is sent.
*/
public static String attrib(final int attr) {
return ESC + "[" + attr + "m";
}
/**
* Sets the key with the given code to the given value. code must be
* derived from the following table, value must
* be any semicolon-separated
* combination of String (enclosed in double quotes) and numeric values.
* For example, to set F1 to the String "Hello F1", followed by a CRLF
* sequence, one can use: ANSI.setkey ("0;59", "\"Hello F1\";13;10").
* Heres's the table of key values:
*