#!/usr/bin/env python3
#
# ---
# see: https://overflow.adminforge.de/questions/4842424/placeholder#33206814

import sys

# output
COLOR_CODES = {
    "error": [1, 31],
    "success": [32],
    "info": [33],
    "important_info": [1, 33],
}

# debugging
RUN = True
DEBUG = False

print("\\033[XXm")
print("-- Use Ctrl-V ESC if not using printf")

for i in range(30, 37 + 1):
    print("\033[%dm%d\t\t\033[%dm%d" % (i, i, i + 30, i + 30))

print("\033[39m\\033[39m - Reset colour (0 to reset all)")
print("\\033[2K - Clear Line")
print("\\033[<L>;<C>H OR \\033[<L>;<C>f puts the cursor at line L and column C.")
print("\\033[<N>A Move the cursor up N lines")
print("\\033[<N>B Move the cursor down N lines")
print("\\033[<N>C Move the cursor forward N columns")
print("\\033[<N>D Move the cursor backward N columns")
print("\\033[2J Clear the screen, move to (0,0)")
print("\\033[K Erase to end of line")
print("\\033[s Save cursor position")
print("\\033[u Restore cursor position")
print(" ")


def log(color, *args):
    if color is not None:
        print("\033[", ";".join(map(str, COLOR_CODES[color])), "m", sep="", end="")
    print(*args, sep="", end="")
    if color is not None:
        print("\033[0m", end="")
    sys.stdout.flush()

    return True  # allows use as log(xyz) and fail()


def success_log(*args, important=False):
    log("important_info" if important else "info", *args)
    try:
        yield
    except Exception:
        log("error", *args)
        raise
    else:
        log("success", *args)


def illustrate(command, *text):
    print("\033[", command, "m\\033[", command, "m - ", *text, sep="", end="\t")


def toggle(command, text, add=20):
    illustrate(command, text, " on")
    illustrate(command + add, text, " off\033[0m")
    print()


toggle(1, "Bold", 21)
toggle(2, "Grey")
toggle(3, "Italic")
toggle(4, "Underline")
toggle(7, "Highlight")
toggle(9, "Strike")
