From 849ab8a728cb6b92f6932158600b049979ddfb02 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Wed, 20 Oct 2021 22:36:30 -0400 Subject: [PATCH] init --- CMakeLists.txt | 6 ++++ kilo.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 kilo.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..01deae3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(editor) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(editor kilo.cpp) diff --git a/kilo.cpp b/kilo.cpp new file mode 100644 index 0000000..e623d1b --- /dev/null +++ b/kilo.cpp @@ -0,0 +1,78 @@ +/*** Includes ***/ +#include +#include +#include +#include +#include +#include + +/** defines ***/ +#define CTRL_KEY(k) ((k) & 0x1f) + + +/*** data ***/ +struct termios orig_termios; + +/*** terminal ***/ +void die(const char *s){ + perror(s); + exit(1); +} + +void disableRawMode() { + if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1){ + die("tcsetattr"); + } +} + +void enableRawMode() { + if(tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr"); + + atexit(disableRawMode); + + struct termios raw = orig_termios; + raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + raw.c_oflag &= ~(OPOST); + raw.c_cflag |= (CS8); + raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); + raw.c_cc[VMIN] = 0; + raw.c_cc[VTIME] = 1; + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr"); +} + +char editorReadKey() { + int nread; + char c; + while ((nread = read(STDIN_FILENO, &c, 1)) != 1){ + if (nread == -1 && errno != EAGAIN) die("read"); + } + return c; +} + +/*** output ***/ +void editorRefreshScreen() { + write(STDOUT_FILENO, "\x1b[2J", 4); // Clear Screen + write(STDOUT_FILENO, "\x1b[H",3); // Cursor Position home +} + + +/*** input ***/ +void editorProcessKeypress() { + char c = editorReadKey(); + switch (c) { + case CTRL_KEY('q'): + exit(0); + break; + } +} + +/*** init ***/ +int main() { + enableRawMode(); + + while(1) { + editorRefreshScreen(); + editorProcessKeypress(); + } + return 0; +} \ No newline at end of file