﻿#include "Utilities.h"

// Function to clear the console as safe as I can
void Utilities::ClearConsole()
{
    system("cls");
}

// Thanks to this https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#file-ansi-md for helping me with ASCII codes to move the cursor up and such

// Function to clear a set amount of lines
void Utilities::ClearLine(const int numberOfLines)
{
    for (int i = 0; i < numberOfLines; i++)
    {
        std::cout
        // Move cursor up one line
        << "\x1b[1A"
        // Delete the entire line the cursor is on
        << "\x1b[2K";
    }
    std::cout << "\r"; // Resume the cursor at beginning of line
}

// Function to make the program wait until the user presses the enter key
bool Utilities::Wait(const bool bShowText)
{
    if (bShowText)
    {
        std::cout << "Press 'Enter' to continue...\n";
    }
    while(true)
    {
        if (_getch() == 13)
        {
            return false;
        }
    }
}

// Function to hide the console cursor
void Utilities::SetCursor(const bool& bShowFlag)
{
    // Creates a console handle that the code is currently is running inside, so I can change things about the console such as info about the cursor such as the visibility
    const HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);

    // This macro is from the windows.h file, it allows me to create a variable to store the cursor info
    CONSOLE_CURSOR_INFO cursorInfo;
    // Gets the cursor info from the console handle we created earlier
    GetConsoleCursorInfo(consoleHandle, &cursorInfo);

    // Sets the cursor visibility to whatever is passed through the function
    cursorInfo.bVisible = bShowFlag;
    // Then sets the cursor info with everything given but the cursor visibility modified
    SetConsoleCursorInfo(consoleHandle, &cursorInfo);
}
