Ansicodes

This commit is contained in:
Imbus 2025-09-14 13:30:11 +02:00
parent d4da05253d
commit 80825e620c
3 changed files with 136 additions and 0 deletions

14
ansicodes/Makefile Normal file
View file

@ -0,0 +1,14 @@
CC = gcc
CFLAGS = -Wall -O2
TARGET = main.elf
SRC = main.c
#LDFLAGS =
$(TARGET): $(SRC)
@echo CC $@
@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
clean:
rm -f $(TARGET)

64
ansicodes/ansi.h Normal file
View file

@ -0,0 +1,64 @@
#ifndef ANSI_H
#define ANSI_H
/* Set this to make all the codes no-ops */
//#define ANSI_DISABLE
#ifndef ANSI_DISABLE
#define ESC(x) x
#else
#define ESC(x)
#endif
/* Reset */
#define ANSI_RESET ESC("\033[0m")
/* Styles */
#define ANSI_BOLD ESC("\033[1m")
#define ANSI_DIM ESC("\033[2m")
#define ANSI_UNDER ESC("\033[4m")
#define ANSI_BLINK ESC("\033[5m")
#define ANSI_REV ESC("\033[7m")
#define ANSI_HIDDEN ESC("\033[8m")
/* Foreground colors */
#define ANSI_FG_BLACK ESC("\033[30m")
#define ANSI_FG_RED ESC("\033[31m")
#define ANSI_FG_GREEN ESC("\033[32m")
#define ANSI_FG_YELLOW ESC("\033[33m")
#define ANSI_FG_BLUE ESC("\033[34m")
#define ANSI_FG_MAGENTA ESC("\033[35m")
#define ANSI_FG_CYAN ESC("\033[36m")
#define ANSI_FG_WHITE ESC("\033[37m")
/* Bright foreground colors */
#define ANSI_FG_BBLACK ESC("\033[90m")
#define ANSI_FG_BRED ESC("\033[91m")
#define ANSI_FG_BGREEN ESC("\033[92m")
#define ANSI_FG_BYELLOW ESC("\033[93m")
#define ANSI_FG_BBLUE ESC("\033[94m")
#define ANSI_FG_BMAGENTA ESC("\033[95m")
#define ANSI_FG_BCYAN ESC("\033[96m")
#define ANSI_FG_BWHITE ESC("\033[97m")
/* Background colors */
#define ANSI_BG_BLACK ESC("\033[40m")
#define ANSI_BG_RED ESC("\033[41m")
#define ANSI_BG_GREEN ESC("\033[42m")
#define ANSI_BG_YELLOW ESC("\033[43m")
#define ANSI_BG_BLUE ESC("\033[44m")
#define ANSI_BG_MAGENTA ESC("\033[45m")
#define ANSI_BG_CYAN ESC("\033[46m")
#define ANSI_BG_WHITE ESC("\033[47m")
/* Bright background colors */
#define ANSI_BG_BBLACK ESC("\033[100m")
#define ANSI_BG_BRED ESC("\033[101m")
#define ANSI_BG_BGREEN ESC("\033[102m")
#define ANSI_BG_BYELLOW ESC("\033[103m")
#define ANSI_BG_BBLUE ESC("\033[104m")
#define ANSI_BG_BMAGENTA ESC("\033[105m")
#define ANSI_BG_BCYAN ESC("\033[106m")
#define ANSI_BG_BWHITE ESC("\033[107m")
#endif // ANSI_H

58
ansicodes/main.c Normal file
View file

@ -0,0 +1,58 @@
#include "ansi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (!isatty(STDOUT_FILENO) || !(getenv("TERM") && strcmp(getenv("TERM"), "dumb") != 0)) {
printf("This terminal does not seem to support ANSI\n");
exit(EXIT_FAILURE);
}
printf(ANSI_BOLD "ABCDE" ANSI_RESET "\n");
printf(ANSI_DIM "ABCDE" ANSI_RESET "\n");
printf(ANSI_UNDER "ABCDE" ANSI_RESET "\n");
printf(ANSI_BLINK "ABCDE" ANSI_RESET "\n");
printf(ANSI_REV "ABCDE" ANSI_RESET "\n");
printf(ANSI_HIDDEN "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BLACK "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_RED "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_GREEN "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_YELLOW "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BLUE "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_MAGENTA "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_CYAN "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_WHITE "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BBLACK "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BRED "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BGREEN "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BYELLOW "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BBLUE "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BMAGENTA "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BCYAN "ABCDE" ANSI_RESET "\n");
printf(ANSI_FG_BWHITE "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BLACK "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_RED "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_GREEN "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_YELLOW "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BLUE "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_MAGENTA "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_CYAN "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_WHITE "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BBLACK "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BRED "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BGREEN "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BYELLOW "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BBLUE "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BMAGENTA "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BCYAN "ABCDE" ANSI_RESET "\n");
printf(ANSI_BG_BWHITE "ABCDE" ANSI_RESET "\n");
exit(EXIT_SUCCESS);
}