Console.java 3.01 KB
package pad.prac1;

import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Console implements Observer 
{
	private int numCols;
	private int numRows;
	private int currentRow;
	
	public void updateCurrentPos()
	{
		Coordinate pos = getCurrentPos();
		currentRow = pos.y();
	}
	
	public void updateTermSize()
	{
		// If we could just handle SIGWINCH, this would not be necessary and it would run A LOT faster
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print("18t");
		Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)");
		Scanner scanner = new Scanner(System.in);
        scanner.findWithinHorizon(pattern,0);
        numCols = Integer.parseInt(scanner.match().group(2));
        numRows = Integer.parseInt(scanner.match().group(1));
        System.err.println("TERMSIZE:" + numCols + " " + numRows);
	}
	
	public Coordinate getCurrentPos()
	{
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print("6n");
		Pattern pattern = Pattern.compile("(\\d+);(\\d+)");
		Scanner scanner = new Scanner(System.in);
        scanner.findWithinHorizon(pattern,0);
        int x = Integer.parseInt(scanner.match().group(2));
        int y = Integer.parseInt(scanner.match().group(1));
        System.err.println("POS:" + x + " " + y);
		return new Coordinate(x,y);
	}
	
	public void moveCursorTo(Coordinate xy)
	{
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print(xy.y());
		System.out.print(';');
		System.out.print(xy.x());
		System.out.print((char)EditableBufferedReader.GOTO);
	}
	
	private void insertSpace()
	{
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print((char)EditableBufferedReader.INS_SPACE);
	}
	
	private void delChar()
	{
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print((char)EditableBufferedReader.DEL_CHAR);
	}
	
	public void addChar(char c, Line line)
	{
		if(line.getCursorPosition() != line.length()+1)
		{
			insertSpace();
		}
		System.out.print(c);
	}
	
	public void update(Observable obs, Object arg)
	{
		updateCurrentPos();
		updateTermSize();
		Line line = (Line)obs;
		Command cmd = (Command)arg;		
		switch(cmd.getType())
		{
			case Command.INSERT_CHAR:
				addChar((char)cmd.getValue(),line);
			break;
			
			case Command.DELETE_CHAR:
				delChar();
			break;
			
			case Command.MOVE_CURSOR:
				currentRow += cmd.getValue()/numCols;
				if(currentRow > numRows)
				{
					currentRow = numRows;
				}
				Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow);
				moveCursorTo(xy);
			break;
			
			case Command.NEWLINE:
				System.out.print((char)EditableBufferedReader.LINE_FEED);
				System.out.print((char)EditableBufferedReader.RETURN_KEY);
				currentRow++;				
		}
	}
}