Blame view

readline/src/pad/prac1/Console.java 3.01 KB
1
2
3
4
package pad.prac1;

import java.util.Observable;
import java.util.Observer;
5
6
import java.util.Scanner;
import java.util.regex.Pattern;
7
8
9

public class Console implements Observer 
{
Imanol-Mikel Barba Sabariego authored
10
11
	private int numCols;
	private int numRows;
12
13
14
	private int currentRow;

	public void updateCurrentPos()
15
	{
16
17
18
19
		Coordinate pos = getCurrentPos();
		currentRow = pos.y();
	}
Imanol-Mikel Barba Sabariego authored
20
21
	public void updateTermSize()
	{
22
		// If we could just handle SIGWINCH, this would not be necessary and it would run A LOT faster
Imanol-Mikel Barba Sabariego authored
23
24
25
26
27
28
29
30
		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));
31
        System.err.println("TERMSIZE:" + numCols + " " + numRows);
Imanol-Mikel Barba Sabariego authored
32
33
	}
34
35
36
37
38
	public Coordinate getCurrentPos()
	{
		System.out.print((char)EditableBufferedReader.ESC);
		System.out.print((char)EditableBufferedReader.ESC_SEQ);
		System.out.print("6n");
Imanol-Mikel Barba Sabariego authored
39
		Pattern pattern = Pattern.compile("(\\d+);(\\d+)");
40
		Scanner scanner = new Scanner(System.in);
Imanol-Mikel Barba Sabariego authored
41
        scanner.findWithinHorizon(pattern,0);
42
43
        int x = Integer.parseInt(scanner.match().group(2));
        int y = Integer.parseInt(scanner.match().group(1));
44
        System.err.println("POS:" + x + " " + y);
45
		return new Coordinate(x,y);
46
47
48
49
50
51
52
53
54
55
	}

	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);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	}

	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)
	{
83
		updateCurrentPos();
Imanol-Mikel Barba Sabariego authored
84
		updateTermSize();
85
86
87
88
89
90
91
92
93
94
95
96
97
		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:
Imanol-Mikel Barba Sabariego authored
98
99
100
101
102
103
				currentRow += cmd.getValue()/numCols;
				if(currentRow > numRows)
				{
					currentRow = numRows;
				}
				Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow);
104
				moveCursorTo(xy);
105
			break;
106
107
108
109
110

			case Command.NEWLINE:
				System.out.print((char)EditableBufferedReader.LINE_FEED);
				System.out.print((char)EditableBufferedReader.RETURN_KEY);
				currentRow++;				
111
112
113
		}
	}
}