Blame view

readline/src/pad/prac1/Console.java 4.13 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 
{
10
11
	private Coordinate currentXY;
	//private int numRows;
Imanol-Mikel Barba Sabariego authored
12
	private int numCols;
13
14
	public Console()
15
	{
16
		currentXY = new Coordinate(0,0);
17
18
	}
19
	public void moveCursorTo(int pos, int currentPos, int length)
Imanol-Mikel Barba Sabariego authored
20
	{
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		updateTermSize();
		updateCurrentPos();
		if(pos > currentPos)
		{
			while((pos-currentPos) >= numCols)
			{
				currentPos += numCols - currentXY.x()+1;
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print((char)EditableBufferedReader.DOWN);
				currentXY.setY(currentXY.y()+1);
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print(numCols-1);
				System.out.print((char)EditableBufferedReader.BACKWARD);
				currentXY.setX(1);
			}
			if(pos > currentPos)
			{
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print(pos - currentPos);
				System.out.print((char)EditableBufferedReader.FORWARD);
			}
		}
		else if(pos < currentPos)
		{
			System.err.println(currentPos);
			System.err.println(pos);
			System.err.println(currentXY);
			while((currentPos-pos) >= currentXY.x())
			{
				currentPos -= currentXY.x();
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print((char)EditableBufferedReader.UP);
				currentXY.setY(currentXY.y()-1);
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print(numCols);
				System.out.print((char)EditableBufferedReader.FORWARD);
				currentXY.setX(numCols);
			}
			if(pos < currentPos)
			{
				System.out.print((char)EditableBufferedReader.ESC);
				System.out.print((char)EditableBufferedReader.ESC_SEQ);
				System.out.print(currentPos-pos);
				System.out.print((char)EditableBufferedReader.BACKWARD);
			}
		}
Imanol-Mikel Barba Sabariego authored
72
73
	}
74
75
76
77
78
79
80
81
	public void updateCurrentPos()
    {
        Coordinate pos = getCurrentPos();
        currentXY.setY(pos.y());
        currentXY.setX(pos.x());
	}

	public void updateTermSize()
82
	{
83
84
85
86
87
88
89
90
        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));
91
92
	}
93
	public Coordinate getCurrentPos()
94
	{
95
96
97
98
99
100
101
102
103
        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));
        return new Coordinate(x,y);
104
	}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

	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)
	{
		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:
147
				moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length());
148
149
150
151
			break;
		}
	}
}