Selaa lähdekoodia

Improved rendering, normal/insert mode

Kajetan Johannes Hammerle 1 viikko sitten
vanhempi
sitoutus
8a39e028da
1 muutettua tiedostoa jossa 114 lisäystä ja 38 poistoa
  1. 114 38
      src/Main.cpp

+ 114 - 38
src/Main.cpp

@@ -10,24 +10,27 @@ import Core.Logger;
 
 
 using SizeVector = Core::Vector<2, size_t>;
 using SizeVector = Core::Vector<2, size_t>;
 
 
+enum class Mode { NORMAL, INSERT };
+
 struct Cell {
 struct Cell {
-    Core::String<16> content{};
+    Core::String<200> content{};
 };
 };
 
 
 static bool running = true;
 static bool running = true;
-static SizeVector size = {20, 10};
+static SizeVector tableSize = {40, 20};
 static Core::List<Cell> cells;
 static Core::List<Cell> cells;
 static SizeVector cursorPosition{0, 0};
 static SizeVector cursorPosition{0, 0};
+static Mode mode = Mode::NORMAL;
 
 
 static void init() {
 static void init() {
-    cells.resize(size[0] * size[1]);
+    cells.resize(tableSize[0] * tableSize[1]);
     for(Cell& c : cells) {
     for(Cell& c : cells) {
         c.content.format(" ");
         c.content.format(" ");
     }
     }
 }
 }
 
 
 static Cell& getCell(size_t x, size_t y) {
 static Cell& getCell(size_t x, size_t y) {
-    return cells[y * size[0] + x];
+    return cells[y * tableSize[0] + x];
 }
 }
 
 
 static Cell& getCell(const SizeVector& v) {
 static Cell& getCell(const SizeVector& v) {
@@ -40,72 +43,145 @@ static void print(const char* s) {
 
 
 static size_t getColumnWidth(size_t x) {
 static size_t getColumnWidth(size_t x) {
     size_t w = 0;
     size_t w = 0;
-    for(size_t y = 0; y < size[1]; y++) {
+    for(size_t y = 0; y < tableSize[1]; y++) {
         Cell& c = getCell(x, y);
         Cell& c = getCell(x, y);
         w = Core::max(w, strlen(c.content));
         w = Core::max(w, strlen(c.content));
     }
     }
     return w;
     return w;
 }
 }
 
 
+static void addString(Core::List<char>& line, const char* s) {
+    while(*s != '\0') {
+        line.add(*(s++));
+    }
+}
+
 static void render() {
 static void render() {
     static constexpr const char* lc[2] = {
     static constexpr const char* lc[2] = {
         Core::Terminal::BG_WHITE, Core::Terminal::BG_BRIGHT_WHITE};
         Core::Terminal::BG_WHITE, Core::Terminal::BG_BRIGHT_WHITE};
     Core::clearTerminal();
     Core::clearTerminal();
     Core::resetCursor();
     Core::resetCursor();
+
+    Core::IntVector2 size = Core::getTerminalSize();
     Core::List<size_t> columnWidths;
     Core::List<size_t> columnWidths;
-    for(size_t x = 0; x < size[0]; x++) {
+    for(size_t x = 0; x < tableSize[0]; x++) {
         columnWidths.add(getColumnWidth(x));
         columnWidths.add(getColumnWidth(x));
     }
     }
-    for(size_t y = 0; y < size[1]; y++) {
-        print(lc[y & 1]);
-        print("|");
-        for(size_t x = 0; x < size[0]; x++) {
+    size_t rangeMin = cursorPosition[0];
+    size_t rangeMax = cursorPosition[0];
+    int leftWidth = size[0];
+    leftWidth -= columnWidths[rangeMin] + 1;
+    int left = leftWidth / 2;
+    int right = left + (leftWidth & 1);
+    while(left > 0 && rangeMin > 0) {
+        rangeMin--;
+        left -= columnWidths[rangeMin];
+        left--;
+    }
+    while(right > 0 && rangeMax + 1 < tableSize[0]) {
+        rangeMax++;
+        right -= columnWidths[rangeMax];
+        right--;
+    }
+    while(left > 0 && rangeMax + 1 < tableSize[0]) {
+        rangeMax++;
+        left -= columnWidths[rangeMax];
+        left--;
+        if(left < 0) {
+            right += left;
+            left = 0;
+        }
+    }
+    while(right > 0 && rangeMin > 0) {
+        rangeMin--;
+        right -= columnWidths[rangeMin];
+        right--;
+        if(right < 0) {
+            left += right;
+            right = 0;
+        }
+    }
+
+    Core::List<char> line;
+    for(size_t y = 0; y < tableSize[1]; y++) {
+        line.clear();
+        addString(line, lc[y & 1]);
+        size_t removeIndex = line.getLength();
+        for(size_t x = rangeMin; x <= rangeMax; x++) {
             Cell& c = getCell(x, y);
             Cell& c = getCell(x, y);
             if(cursorPosition[0] == x && cursorPosition[1] == y) {
             if(cursorPosition[0] == x && cursorPosition[1] == y) {
-                fputs(Core::Terminal::BG_BRIGHT_BLUE, stdout);
+                addString(line, Core::Terminal::BG_BRIGHT_BLUE);
             }
             }
-            c.content.print();
+            addString(line, c.content);
             size_t filler = columnWidths[x] - strlen(c.content);
             size_t filler = columnWidths[x] - strlen(c.content);
             while(filler > 0) {
             while(filler > 0) {
                 filler--;
                 filler--;
-                putchar(' ');
+                addString(line, " ");
             }
             }
             if(cursorPosition[0] == x && cursorPosition[1] == y) {
             if(cursorPosition[0] == x && cursorPosition[1] == y) {
-                fputs(lc[y & 1], stdout);
+                addString(line, lc[y & 1]);
             }
             }
-            print("|");
+            addString(line, "|");
         }
         }
-        putchar('\n');
+        for(int i = 0; i > left; i--) {
+            line.remove(removeIndex);
+        }
+        for(int i = 0; i > right; i--) {
+            line.removeLast();
+        }
+        line.add('\0');
+        puts(line.begin());
+    }
+    print(Core::Terminal::RESET);
+    printf("%d %d\n", left, right);
+
+    Core::resetCursor();
+    Core::moveCursorDown(size[1] - 2);
+    switch(mode) {
+        case Mode::NORMAL: printf("Normal\n"); break;
+        case Mode::INSERT: printf("Insert\n"); break;
     }
     }
-    fputs(Core::Terminal::RESET, stdout);
 }
 }
 
 
 static void loop(u64 input) {
 static void loop(u64 input) {
-    // Core::IntVector2 size = Core::getTerminalSize();
-    if(input == 'q') {
-        running = false;
-        return;
-    } else if(input == Core::Terminal::KEY_ARROW_LEFT) {
-        if(cursorPosition[0] > 0) {
-            cursorPosition += SizeVector{-1, 0};
-        }
-    } else if(input == Core::Terminal::KEY_ARROW_RIGHT) {
-        if(cursorPosition[0] < size[0] - 1) {
-            cursorPosition += SizeVector{1, 0};
-        }
-    } else if(input == Core::Terminal::KEY_ARROW_UP) {
-        if(cursorPosition[1] > 0) {
-            cursorPosition += SizeVector{0, -1};
+    if(input == Core::Terminal::KEY_ALT) {
+        mode = Mode::NORMAL;
+    }
+    if(mode == Mode::NORMAL) {
+        if(input == 'q') {
+            running = false;
+            return;
+        } else if(input == Core::Terminal::KEY_ARROW_LEFT) {
+            if(cursorPosition[0] > 0) {
+                cursorPosition += SizeVector{-1, 0};
+            }
+        } else if(input == Core::Terminal::KEY_ARROW_RIGHT) {
+            if(cursorPosition[0] < tableSize[0] - 1) {
+                cursorPosition += SizeVector{1, 0};
+            }
+        } else if(input == Core::Terminal::KEY_ARROW_UP) {
+            if(cursorPosition[1] > 0) {
+                cursorPosition += SizeVector{0, -1};
+            }
+        } else if(input == Core::Terminal::KEY_ARROW_DOWN) {
+            if(cursorPosition[1] < tableSize[1] - 1) {
+                cursorPosition += SizeVector{0, 1};
+            }
+        } else if(input == 'i') {
+            mode = Mode::INSERT;
+        } else if(input == 'x') {
+            Cell& c = getCell(cursorPosition);
+            c.content.clear();
         }
         }
-    } else if(input == Core::Terminal::KEY_ARROW_DOWN) {
-        if(cursorPosition[1] < size[1] - 1) {
-            cursorPosition += SizeVector{0, 1};
+    } else if(mode == Mode::INSERT) {
+        if(!Core::isSpecialChar(input) && input >= ' ') {
+            Cell& c = getCell(cursorPosition);
+            c.content.add(static_cast<char>(input));
         }
         }
-    } else {
-        Cell& c = getCell(cursorPosition);
-        c.content.add(static_cast<char>(input));
     }
     }
     render();
     render();
+    printf("  %lu", input);
+    fflush(stdout);
 }
 }
 
 
 int main() {
 int main() {