/* Generated by Nim Compiler v2.2.10 */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix
typedef struct tyObject_PTerminalcolonObjectType___xEiAH1Ie7g9cRuFfj2NQJxg tyObject_PTerminalcolonObjectType___xEiAH1Ie7g9cRuFfj2NQJxg;
typedef struct NimStrPayload NimStrPayload;
typedef struct NimStringV2 NimStringV2;
typedef NU16 tySet_tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA;
typedef NU8 tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA;
struct NimStrPayload {
	NI cap;
	NIM_CHAR data[SEQ_DECL_SIZE];
};
struct NimStringV2 {
	NI len;
	NimStrPayload* p;
};
typedef NU8 tyEnum_ForegroundColor__prdrXkAw6H7iUBRVoWE3cg;
N_LIB_PRIVATE N_NIMCALL(int, getFileHandle__stdZsyncio_u280)(FILE* f_p0);
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void);
N_LIB_PRIVATE N_NIMCALL(void, write__stdZsyncio_u254)(FILE* f_p0, NimStringV2 s_p1);
N_LIB_PRIVATE N_NIMCALL(NimStringV2, ansiStyleCode__pureZterminal_u391)(NI style_p0);
N_NIMCALL(NimStringV2, rawNewString)(NI cap_p0);
N_LIB_PRIVATE N_NIMCALL(void, formatValue__pureZterminal_u266)(NimStringV2* result_p0, NimStringV2 value_p1);
N_LIB_PRIVATE N_NIMCALL(void, formatValue__pureZterminal_u279)(NimStringV2* result_p0, NI value_p1);
static N_INLINE(void, appendString)(NimStringV2* dest_p0, NimStringV2 src_p1);
static N_INLINE(void, copyMem__system_u1742)(void* dest_p0, void* source_p1, NI size_p2);
static N_INLINE(void, nimCopyMem)(void* dest_p0, void* source_p1, NI size_p2);
N_LIB_PRIVATE N_NIMCALL(void, prepareAdd)(NimStringV2* s_p0, NI addLen_p1);
N_LIB_PRIVATE N_NIMCALL(void, eqdestroy___system_u2691)(NimStringV2 dest_p0);
static const struct {
  NI cap; NIM_CHAR data[2+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_2 = { 2 | NIM_STRLIT_FLAG, "\033[" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_3 = {2, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_2};
static const struct {
  NI cap; NIM_CHAR data[1+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_4 = { 1 | NIM_STRLIT_FLAG, "m" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_5 = {1, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_4};
static const struct {
  NI cap; NIM_CHAR data[4+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_6 = { 4 | NIM_STRLIT_FLAG, "\033[0m" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_7 = {4, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_6};
N_LIB_PRIVATE tyObject_PTerminalcolonObjectType___xEiAH1Ie7g9cRuFfj2NQJxg* gTerm__pureZterminal_u12;
N_LIB_PRIVATE NI gFG__pureZterminal_u374;
N_LIB_PRIVATE NI gBG__pureZterminal_u375;
extern NIM_BOOL nimInErrorMode__system_u4143;
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void) {
	NIM_BOOL* result;
	result = (&nimInErrorMode__system_u4143);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NIM_BOOL, isatty__pureZterminal_u637)(FILE* f_p0) {
	NIM_BOOL result;
	int T1_;
	int T2_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NIM_BOOL)0;
	T1_ = (int)0;
	T1_ = getFileHandle__stdZsyncio_u280(f_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	T2_ = (int)0;
	T2_ = isatty(T1_);
	result = !((T2_ == ((NI32)0)));
	}BeforeRet_: ;
	return result;
}
static N_INLINE(void, nimCopyMem)(void* dest_p0, void* source_p1, NI size_p2) {
	void* T1_;
	T1_ = (void*)0;
	T1_ = memcpy(dest_p0, source_p1, ((size_t) (size_p2)));
}
static N_INLINE(void, copyMem__system_u1742)(void* dest_p0, void* source_p1, NI size_p2) {
	nimCopyMem(dest_p0, source_p1, size_p2);
}
static N_INLINE(void, appendString)(NimStringV2* dest_p0, NimStringV2 src_p1) {
	{
		if (!(((NI)0) < src_p1.len)) goto LA3_;
		copyMem__system_u1742(((void*) ((&(*(*dest_p0).p).data[(*dest_p0).len]))), ((void*) ((&(*src_p1.p).data[((NI)0)]))), (src_p1.len));
		(*dest_p0).len += src_p1.len;
		(*(*dest_p0).p).data[(*dest_p0).len] = 0;
	}
LA3_: ;
}
N_LIB_PRIVATE N_NIMCALL(NimStringV2, ansiStyleCode__pureZterminal_u391)(NI style_p0) {
	NimStringV2 result;
	NimStringV2 T1_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result.len = 0; result.p = NIM_NIL;
	T1_.len = 0; T1_.p = NIM_NIL;
	{
		NimStringV2 fmtRes_1;
		fmtRes_1.len = 0; fmtRes_1.p = NIM_NIL;
		fmtRes_1 = rawNewString(((NI)41));
		formatValue__pureZterminal_u266((&fmtRes_1), TM__CN3raB9atujkEiPpxYAdlgQ_3);
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
		formatValue__pureZterminal_u279((&fmtRes_1), style_p0);
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
		prepareAdd((&fmtRes_1), 1);
appendString((&fmtRes_1), TM__CN3raB9atujkEiPpxYAdlgQ_5);
		result = fmtRes_1;
	}
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(void, setStyle__pureZterminal_u415)(FILE* f_p0, tySet_tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA style_p1) {
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	{
		tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA s_1;
		NI i_1;
		s_1 = (tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA)0;
		i_1 = ((NI)1);
		{
			while (1) {
				if (!(i_1 <= ((NI)9))) goto LA3;
				{
					NimStringV2 colontmpD_;
					if (!((style_p1 &((NU16)1<<((NU)((((tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA) (i_1)) - 1))&15U)))!=0)) goto LA6_;
					colontmpD_.len = 0; colontmpD_.p = NIM_NIL;
					s_1 = ((tyEnum_Style__eUA1W69bfnUNuraRJtJ0MmA) (i_1));
					colontmpD_ = ansiStyleCode__pureZterminal_u391(((NI) (s_1)));
					if (NIM_UNLIKELY(*nimErr_)) goto LA8_;
					write__stdZsyncio_u254(f_p0, colontmpD_);
					if (NIM_UNLIKELY(*nimErr_)) goto LA8_;
					{
						LA8_:;
					}
					{
						eqdestroy___system_u2691(colontmpD_);
					}
					if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
				}
LA6_: ;
				i_1 += ((NI)1);
			} LA3: ;
		}
	}
	}BeforeRet_: ;
}
N_LIB_PRIVATE N_NIMCALL(void, resetAttributes__pureZterminal_u376)(FILE* f_p0) {
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	write__stdZsyncio_u254(f_p0, TM__CN3raB9atujkEiPpxYAdlgQ_7);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	gFG__pureZterminal_u374 = ((NI)0);
	gBG__pureZterminal_u375 = ((NI)0);
	}BeforeRet_: ;
}
N_LIB_PRIVATE N_NIMCALL(void, setForegroundColor__pureZterminal_u511)(FILE* f_p0, tyEnum_ForegroundColor__prdrXkAw6H7iUBRVoWE3cg fg_p1, NIM_BOOL bright_p2) {
	NimStringV2 colontmpD_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	colontmpD_.len = 0; colontmpD_.p = NIM_NIL;
	gFG__pureZterminal_u374 = fg_p1;
	{
		if (!bright_p2) goto LA4_;
		gFG__pureZterminal_u374 += ((NI)60);
	}
LA4_: ;
	colontmpD_ = ansiStyleCode__pureZterminal_u391(gFG__pureZterminal_u374);
	if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	write__stdZsyncio_u254(f_p0, colontmpD_);
	if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	{
		LA1_:;
	}
	{
		eqdestroy___system_u2691(colontmpD_);
	}
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
}
