Quellcode durchsuchen

fix console info line for linux

Kolja Strohm vor 1 Jahr
Ursprung
Commit
e4f47c55cf
1 geänderte Dateien mit 18 neuen und 27 gelöschten Zeilen
  1. 18 27
      FactoryCraft/Start.cpp

+ 18 - 27
FactoryCraft/Start.cpp

@@ -6,7 +6,6 @@
 #include <iostream>
 #include <Klient.h>
 #ifndef _WINDOWS
-#    include <curses.h>
 #    include <sys/resource.h>
 #endif
 #include <AsynchronCall.h>
@@ -37,20 +36,6 @@ private:
     Critical cs;
     int infoLength;
 
-    int getCursorColumn()
-    {
-#ifdef _WINDOWS
-        CONSOLE_SCREEN_BUFFER_INFO csbi;
-        int columns, rows;
-
-        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
-        return csbi.dwCursorPosition.X;
-#else
-        int x = getcurx(curscr);
-        return x;
-#endif
-    }
-
 public:
     DuplicatingStreamBuf(std::ostream& console, std::ostream& file)
         : std::stringbuf(),
@@ -76,19 +61,21 @@ public:
             cs.unlock();
             return 0;
         }
-        int column = getCursorColumn();
         if (infoLength > 0)
         {
-            console << /* move cursor a line up*/ "\033[1A"
+            console << /* store cursor position */ "\033[s"
+                    << /* move cursor a line up*/ "\033[1A"
                     << /* clear the line */ "\x1b[2K"
                     << /* return to beginning of line */ "\r";
         }
+        int newLines = Text(str().c_str()).anzahlVon('\n');
         if (str().length() > 0)
         {
             console << str();
             if (str().c_str()[str().length() - 1] != '\n')
             {
                 console << "\n";
+                newLines++;
             }
         }
         if (Game::INSTANCE)
@@ -110,13 +97,18 @@ public:
             { // green
                 infoLine += "\033[1;32m";
             }
-            infoLine.append()
-                << tps << /* reset color */ "\033[0m"
-                << "\tAverage Tick Time: "
-                << Game::INSTANCE->getAverageTickTime()
-                << /* move cursor a line down */ "\033[1B"
-                // << /* move cursor to beginning of line */ "\r"
-                << /* set cursor position back */ "\033[" << column + 1 << "G";
+            infoLine.append() << tps << /* reset color */ "\033[0m"
+                              << "\tAverage Tick Time: "
+                              << Game::INSTANCE->getAverageTickTime() << "\n";
+            if (infoLength > 0)
+            {
+                infoLine.append()
+                    << /* restore cursor position */ "\033[u"
+                    << /* set cursor down by amount of new lines */
+                           (newLines > 0 ? (Text("\033[") + newLines + "B").getText()
+                    : "");
+            //        << "\x1b[0K";
+            }
             infoLength = infoLine.getLength();
             console << infoLine << std::flush;
         }
@@ -138,7 +130,6 @@ int main()
     initializeMultiblockTypes();
 
 #ifndef _WINDOWS
-    initscr();
     struct rlimit core_limits;
     core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
     setrlimit(RLIMIT_CORE, &core_limits);
@@ -208,9 +199,9 @@ int main()
             std::string line;
             std::getline(std::cin, line);
             if (!mserver) return;
-            std::cout << "\033[1A"
+            std::cout << std::flush << "\033[1A"
                       << "\x1b[2K"
-                      << "\r" << line << std::flush;
+                      << "\x1b[0G" << line << "\n\033[1A\033[s" << std::flush;
             if (Text(line.c_str()) == Text("exit"))
             {
                 std::cout << "The server will be terminated and the game "