-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursorUtilities.c
More file actions
64 lines (61 loc) · 1.11 KB
/
cursorUtilities.c
File metadata and controls
64 lines (61 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
/*
Author:Yashasvi Goel
Code Refactored on 21/3/20
*/
#include"cursorUtilities.h"
struct editorConfig E;
int cursorPosition(int* rows,int* cols)
{
char buf[32];
unsigned int i=0;
if(write(STDOUT_FILENO,"\x1b[6n",4)!=4)
return -1;
while(i<sizeof(buf) -1){
if(read(STDIN_FILENO,&buf[i],1)==1) break;
if(buf[i]=='R') break;
i++;
}
buf[i]='\0';
if(buf[0]!='\x1b' || buf[1]!='[') return -1;
if(sscanf(&buf[2], "%d;%d",rows,cols)!=2) return -1;
return 0;
}
void moveCursor(int key){
erow *row=(E.cy >= E.numRows) ? NULL : &E.row[E.cy];
switch (key) {
case ARROW_LEFT:
if(E.cx!=0)
E.cx--;
else if(E.cy>0){
E.cy--;
E.cx=E.row[E.cy].size;
}
break;
case ARROW_RIGHT:
if(row&& E.cx<row->size)
E.cx++;
else if(row && E.cx==row->size){
E.cy++;
E.cx=0;
}
break;
case ARROW_UP:
if(E.cy!=0)
E.cy--;
break;
case ARROW_DOWN:
if(E.cy<E.numRows)
E.cy++;
break;
case KILLED:
E.cy=0;
E.cx=0;
break;
}
row = (E.cy >= E.numRows) ? NULL : &E.row[E.cy];
int rowlen = row ? row->size : 0;
if (E.cx > rowlen) {
// E.cy++;
E.cx = rowlen;
}
}