Skip to content

Instantly share code, notes, and snippets.

@Sasszem
Created April 16, 2018 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sasszem/ddc9b71a820a041f6cd259e6d153f550 to your computer and use it in GitHub Desktop.
Save Sasszem/ddc9b71a820a041f6cd259e6d153f550 to your computer and use it in GitHub Desktop.
A curses-like terminal class in java. Done in a boring IT class. Was not tested
package rpg;
import java.util.ArrayList;
class Terminal
{
public static final Integer BLACK = 0;
public static final Integer RED = 1;
public static final Integer GREEN = 2;
public static final Integer YELLOW = 3;
public static final Integer BLUE = 4;
public static final Integer MAGENTA = 5;
public static final Integer CYAN = 6;
public static final Integer WHITE = 7;
public static final Integer RESET = 9;
private static final int ESC = "\x1b";
private List<Integer[2]> colors;
public Terminal()
{
colors = new ArrayList<Integer[2]>();
init_pair(WHITE,BLACK);
System.out.println("\x1b[?47h"); // save screen
}
public void init_pair(Integer fore, Integer back)
{
Integer[2] pair = new Integer[2];
pair[0]=fore;
pair[1]=back;
color.add(pair);
}
public void setPair(Integer index, Integer fore, Integer back) throws IllegalArgumentException
{
if (index>=colors.size())
{
throw new IllegalArgumentException("Color pair #"+index.toString() + " is not initiated!");
}
else
{
Integer[2] pair = colors.get(index);
pair[0]=fore;
pair[1]=back;
}
}
public void endwin()
{
System.out.println('\033[?47l'); // save screen
}
public void putString(int x, int y, String text, int cpair) throws IllegalArgumentException
{
setColor(cpair);
moveCursor(x,y);
}
private void moveCursor(int x, int y)
{
System.out.printf(ESC+"[ %d ; %d f",x,y);
}
private void setColor(int cpair) throws IllegalArgumentException
{
if (index>=colors.size())
{
throw new IllegalArgumentException("Color pair #"+index.toString() + " is not initiated!");
}
else
{
Integer[2] pair = colors.get(index);
System.out.printf(ESC + "[ 3 %d m",pair[0]);
System.out.printf(ESC + "[ 4 %d m",pair[0]);
}
}
public static void main(String[] args)
{
Terminal t = new Terminal();
t.init_pair(GREEN, BLACK);
try {
t.putstring(5,5,"TEST TEXT TEST TEXT!",1);
}
finally
{t.endwin();}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment