Compare commits
4
Commits
07882d8ba5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cf32775cf | ||
|
|
974fde4a70 | ||
|
|
7fbe39b04f | ||
|
|
ec85525a55 |
@@ -1,9 +1,11 @@
|
|||||||
/*** includes ***/
|
/*** includes ***/
|
||||||
|
|
||||||
|
#include <asm-generic/ioctls.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -13,7 +15,13 @@
|
|||||||
|
|
||||||
/*** data ***/
|
/*** data ***/
|
||||||
|
|
||||||
struct termios orig_termios;
|
struct editorConfig {
|
||||||
|
int screenrows;
|
||||||
|
int screencols;
|
||||||
|
struct termios orig_termios;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct editorConfig E;
|
||||||
|
|
||||||
/*** terminal ***/
|
/*** terminal ***/
|
||||||
|
|
||||||
@@ -28,16 +36,16 @@ void die(const char* s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void disableRawMode() {
|
void disableRawMode() {
|
||||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
|
||||||
die("tcsetattr");
|
die("tcsetattr");
|
||||||
}
|
}
|
||||||
|
|
||||||
void enableRawMode() {
|
void enableRawMode() {
|
||||||
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
|
if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1)
|
||||||
die("tcgetattr");
|
die("tcgetattr");
|
||||||
atexit(disableRawMode);
|
atexit(disableRawMode);
|
||||||
|
|
||||||
struct termios raw = orig_termios;
|
struct termios raw = E.orig_termios;
|
||||||
|
|
||||||
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||||
raw.c_oflag &= ~(OPOST);
|
raw.c_oflag &= ~(OPOST);
|
||||||
@@ -63,11 +71,59 @@ char editorReadKey() {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getCursorPosition(int *rows, int *cols) {
|
||||||
|
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
unsigned int i = 0;
|
||||||
|
// Leave space for null terminator.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getWindowSize(int *rows, int *cols) {
|
||||||
|
struct winsize ws;
|
||||||
|
|
||||||
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
||||||
|
// Fallback if ioctl can't query size:
|
||||||
|
// Move cursor to the bottom right;
|
||||||
|
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// then query its position.
|
||||||
|
return getCursorPosition(rows, cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
*rows = ws.ws_row;
|
||||||
|
*cols = ws.ws_col;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*** output ***/
|
/*** output ***/
|
||||||
|
|
||||||
void editorDrawRows() {
|
void editorDrawRows() {
|
||||||
for (int y = 0; y < 24; y++) {
|
for (int y = 0; y < E.screenrows; y++) {
|
||||||
write(STDOUT_FILENO, "~\r\n", 3);
|
write(STDOUT_FILENO, "~", 1);
|
||||||
|
|
||||||
|
// Don't scroll on the last line.
|
||||||
|
if (y < E.screenrows - 1) {
|
||||||
|
write(STDOUT_FILENO, "\r\n", 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,8 +157,15 @@ void editorProcessKeypress() {
|
|||||||
|
|
||||||
/*** init ***/
|
/*** init ***/
|
||||||
|
|
||||||
|
void initEditor() {
|
||||||
|
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
|
||||||
|
die("getWindowSize");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
|
initEditor();
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
editorRefreshScreen();
|
editorRefreshScreen();
|
||||||
|
|||||||
Reference in New Issue
Block a user