summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile10
-rw-r--r--src/client/cursor.cpp13
-rw-r--r--src/client/window.cpp16
-rw-r--r--src/common/ipc.cpp (renamed from ipc.cpp)0
-rw-r--r--src/jet.cpp (renamed from jet.cpp)12
-rw-r--r--src/jetc.cpp (renamed from jet2.cpp)56
-rw-r--r--src/server/buffer.cpp (renamed from buffer.cpp)0
-rw-r--r--src/server/client.cpp (renamed from client.cpp)0
-rw-r--r--src/server/page.cpp (renamed from page.cpp)0
-rw-r--r--src/server/point.cpp (renamed from point.cpp)0
11 files changed, 59 insertions, 50 deletions
diff --git a/.gitignore b/.gitignore
index a73d53b..efbd2bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
jet
-jet2
+jetc
diff --git a/Makefile b/Makefile
index be67256..7ead0a1 100644
--- a/Makefile
+++ b/Makefile
@@ -2,10 +2,10 @@ FLAGS=-ggdb -O0
_PHONY=all
-all:jet jet2
+all:jet jetc
-jet: Makefile *.cpp
- g++ $(FLAGS) jet.cpp -o jet
+jet: Makefile src/jet.cpp src/server/*.cpp src/common/*.cpp
+ g++ $(FLAGS) src/jet.cpp -o $@
-jet2: Makefile *.cpp
- g++ $(FLAGS) jet2.cpp -o jet2 $(shell pkg-config --cflags --libs ncurses)
+jetc: Makefile src/jetc.cpp src/client/*.cpp src/common/*.cpp
+ g++ $(FLAGS) src/jetc.cpp -o $@ $(shell pkg-config --cflags --libs ncurses)
diff --git a/src/client/cursor.cpp b/src/client/cursor.cpp
new file mode 100644
index 0000000..4e76151
--- /dev/null
+++ b/src/client/cursor.cpp
@@ -0,0 +1,13 @@
+struct Cursor {
+ int x;
+ int y;
+ Window *w;
+
+ void move_left() {
+ if (x > 0) x--;
+ }
+
+ void move_right() {
+ if (x < w->width - 1 && w->pos(x + 1, y) != 0) x++;
+ }
+};
diff --git a/src/client/window.cpp b/src/client/window.cpp
new file mode 100644
index 0000000..e718e63
--- /dev/null
+++ b/src/client/window.cpp
@@ -0,0 +1,16 @@
+struct Window {
+ char *view;
+ int width;
+ int height;
+
+ void init() {
+ view = new char[width * height];
+ for (int i = 0; i < width * height; i++) {
+ view[i] = 0;
+ }
+ }
+
+ char pos(size_t x, size_t y) {
+ return view[x + y * width];
+ }
+};
diff --git a/ipc.cpp b/src/common/ipc.cpp
index 7d46307..7d46307 100644
--- a/ipc.cpp
+++ b/src/common/ipc.cpp
diff --git a/jet.cpp b/src/jet.cpp
index 2006a77..6cc4470 100644
--- a/jet.cpp
+++ b/src/jet.cpp
@@ -12,11 +12,11 @@
#include <unistd.h>
#include <sys/epoll.h>
-#include "ipc.cpp"
-#include "page.cpp"
-#include "buffer.cpp"
-#include "point.cpp"
-#include "client.cpp"
+#include "common/ipc.cpp"
+#include "server/page.cpp"
+#include "server/buffer.cpp"
+#include "server/point.cpp"
+#include "server/client.cpp"
#define PORT 6969
#define MAX_EVENTS 10
@@ -33,7 +33,7 @@ int create_listener() {
int main() {
Buffer scratch("scratch");
- scratch.read_file("LICENSE");
+ scratch.read_file("test.txt");
int listener = create_listener();
diff --git a/jet2.cpp b/src/jetc.cpp
index 9fb4027..9650eb6 100644
--- a/jet2.cpp
+++ b/src/jetc.cpp
@@ -8,12 +8,13 @@
#include <arpa/inet.h>
#include <unistd.h>
-#include "ipc.cpp"
+#include "common/ipc.cpp"
+#include "client/window.cpp"
+#include "client/cursor.cpp"
#define NORMAL_MODE 0
#define INSERT_MODE 1
#define PORT 6969
-#define pos(x, y) (x) + (y) * window_width
int main(int argc, char *argv[]) {
initscr();
@@ -22,19 +23,16 @@ int main(int argc, char *argv[]) {
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
- int window_height, window_width;
- getmaxyx(stdscr, window_height, window_width);
-
- char *view = new char[window_width * window_height];
- for (int i = 0; i < window_width * window_height; view[i++] = 0);
+ Window window;
+ getmaxyx(stdscr, window.height, window.width);
+ window.init();
int s = socket(AF_INET, SOCK_STREAM, 0);
- sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)};
+ sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK) };
connect(s, (sockaddr *) &addr, sizeof(sockaddr_in));
int mode = NORMAL_MODE;
- int cursor_x = 0;
- int cursor_y = 0;
+ Cursor cursor = { 0, 0, &window };
int quit = 0;
while (!quit) {
@@ -42,14 +40,14 @@ int main(int argc, char *argv[]) {
int8_t msg[5];
msg[0] = OP_SHOW;
- encode2(window_width, msg, 1);
- encode2(window_height, msg, 3);
+ encode2(window.width, msg, 1);
+ encode2(window.height, msg, 3);
write(s, msg, 5);
- read(s, view, window_width * window_height);
- for (int i = 0; i < window_width * window_height; i++) {
- printw("%c", view[i]);
+ read(s, window.view, window.width * window.height);
+ for (int i = 0; i < window.width * window.height; i++) {
+ printw("%c", window.view[i]);
}
- move(cursor_y, cursor_x);
+ move(cursor.y, cursor.x);
int8_t mov[2];
int8_t del[1];
@@ -67,33 +65,13 @@ int main(int argc, char *argv[]) {
mov[0] = OP_MOVE1;
mov[1] = -1;
write(s, mov, 2);
- do {
- if (cursor_x <= 0) {
- if (cursor_y > 0) {
- cursor_x = window_width - 1;
- cursor_y--;
- }
- } else {
- cursor_x--;
- }
- } while (view[pos(cursor_x, cursor_y)] == 0);
+ cursor.move_left();
break;
case 'l':
mov[0] = OP_MOVE1;
mov[1] = 1;
write(s, mov, 2);
- do {
- if (cursor_x >= window_width - 1) {
- if (cursor_y < window_height - 1) {
- cursor_x = 0;
- cursor_y++;
- } else {
- break;
- }
- } else {
- cursor_x++;
- }
- } while (view[pos(cursor_x, cursor_y)] == 0);
+ cursor.move_right();
break;
}
} else {
@@ -113,6 +91,8 @@ int main(int argc, char *argv[]) {
}
}
+ close(s);
+
endwin();
return 0;
}
diff --git a/buffer.cpp b/src/server/buffer.cpp
index 7b4c92d..7b4c92d 100644
--- a/buffer.cpp
+++ b/src/server/buffer.cpp
diff --git a/client.cpp b/src/server/client.cpp
index bed62c5..bed62c5 100644
--- a/client.cpp
+++ b/src/server/client.cpp
diff --git a/page.cpp b/src/server/page.cpp
index 77cf686..77cf686 100644
--- a/page.cpp
+++ b/src/server/page.cpp
diff --git a/point.cpp b/src/server/point.cpp
index ab9f70f..ab9f70f 100644
--- a/point.cpp
+++ b/src/server/point.cpp