summaryrefslogtreecommitdiff
path: root/client.cpp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-07 13:04:26 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-07 13:04:26 -0300
commit81dd6b162351d93dae5d71a45df56ee1a74a36ca (patch)
tree18585513a9fcc9fdb0f2bfbd36643b08a1786bb4 /client.cpp
parentc835a9729091445bb68c5d376a8b8fd7a379cb32 (diff)
downloadjet-81dd6b162351d93dae5d71a45df56ee1a74a36ca.tar.gz
jet-81dd6b162351d93dae5d71a45df56ee1a74a36ca.zip
Split application into server and client
Diffstat (limited to 'client.cpp')
-rw-r--r--client.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/client.cpp b/client.cpp
new file mode 100644
index 0000000..f99dc5d
--- /dev/null
+++ b/client.cpp
@@ -0,0 +1,79 @@
+#define MAX_ARGS 16
+#define pos(x, y) (x) + (y) * window_w
+
+struct ArgList {
+ int len, cap;
+ int *args;
+
+ ArgList(int cap) : len(0), cap(cap) {
+ args = new int[cap];
+ for (int i = 0; i < cap; args[i++] = 0);
+ }
+ ~ArgList() { delete[] args; }
+
+ int pop() {
+ return args[--len];
+ }
+
+ void push(int value) {
+ args[len++] = value;
+ }
+
+};
+
+struct Client {
+ int sockfd;
+ Point cursor;
+ Point window_start;
+ ArgList args;
+
+ Client(const Buffer &b) :
+ cursor(b.storage, 0),
+ window_start(cursor),
+ args(MAX_ARGS)
+ {}
+
+ void show() {
+ int window_h = args.pop();
+ int window_w = args.pop();
+ char *view = new char[window_w * window_h];
+
+ Point window_end(window_start);
+ for (int i = 0; i < window_h; i++) {
+ for (int j = 0; j < window_w; j++) {
+ if (window_end == cursor) {
+ view[pos(j, i)] = '$';
+ } else {
+ view[pos(j, i)] = window_end.element();
+ }
+ if (window_end.element() == '\n') {
+ for (int k = j + 1; k < window_w; k++) {
+ view[pos(k, i)] = 0;
+ }
+ j = window_w;
+ } else if (window_end.element() == '\t') {
+ for (int k = j + 1; k < j + 8; k++) {
+ view[pos(k, i)] = 0;
+ }
+ j = j + 7;
+ }
+ window_end++;
+ }
+ }
+
+ write(sockfd, view, window_w * window_h);
+ delete[] view;
+ }
+
+ void move() {
+ int target = args.pop();
+ while (target > 0) {
+ cursor++;
+ target--;
+ }
+ while (target < 0) {
+ cursor--;
+ target++;
+ }
+ }
+};