#include "ConsoleOutput.h"
#include "CoreMinimal.h"

//////////////////////////////////////////////////////////////
//
// ConsoleOutput
//
// Storing the text that goes in the command console.
//
//////////////////////////////////////////////////////////////


void UlxConsoleOutput::Truncate() {
	int lines = 50;
	int csize = Content.Len();
	int total = 0;
	for (int i = csize - 1; i >= 0; i--) {
		if (Content[i] == '\n') {
			total += 1;
			if (total == lines) {
				Content = Content.RightChop(i + 1);
				return;
			}
		}
	}
}

bool UlxConsoleOutput::MaybeAppendNewline() {
	int csize = Content.Len();
	if ((csize > 0) && (Content[csize - 1] != '\n')) {
		Content += TEXT("\n");
		return true;
	}
	else {
		return false;
	}
}

bool UlxConsoleOutput::MaybeAppendText(const FString& text) {
	if (!text.IsEmpty()) {
		Content += text;
		return true;
	}
	else {
		return false;
	}
}

void UlxConsoleOutput::Append(const FString& text, bool SeparateLine) {
	bool modified = false;
	if (SeparateLine) modified |= MaybeAppendNewline();
	modified |= MaybeAppendText(text);
	if (SeparateLine) modified |= MaybeAppendNewline();
	if (modified) {
		Dirty = true;
		Truncate();
	}
}
