# BootCamp Text Game engine
#
# by Jiří Kubík (jiri.kub@gmail.com, kubikji2@fel.cvut.cz)
# (c) 2017
#  
# current version 0.0.5 
# 
# 
# History:
# 0.0.3 -- added print with wait, color as constants,
#          pseudo private and public functions,
#          dependencies added -- time
# 
# 0.0.4 -- added readChar and readArrow
#          arrows as constants   
#             
# 0.0.5 -- added backgroudcolor option to printft and printftp
#
# using syscall found on:
# https://stackoverflow.com/questions/89228/calling-an-external-command-in-python
#
# credit for echo colors goes to Petr Stepan for his task in course
# operetion system and databases

from subprocess import call as syscall
import time
import sys

BLACK  = 0
RED    = 1
GREEN  = 2
YELLOW = 3
BLUE   = 4
PURPLE = 5
CYAN   = 6
WHITE  = 7

UP    = 'A'
DOWN  = 'B'
LEFT  = 'D'
RIGHT = 'C'



# "private" function
# used to create formating string for echo
def _echoFormater(color,bold,background=-1):
    back = ";4"+str(background) if background >= 0 else ""
    return  "\\e["+str(bold)+";3"+str(color)+back+"m"
# "private" function
# used to write formated text on one line
def _printft_sameLine(text):
    syscall(["echo","-e", "-n", text])


def clear():
    syscall(["clear"])

def resetFormat():
    printft("\e[0m")



# "public" function
# Prints text formated by method textf
def printft(text):
   syscall(["echo","-e",text])

def printFormatedText(text):
    printft(text)
    

# "public" function
# Prints formated text and waits for x seconds
def printftp(text,speed=0.1,color=7,bold=0,background=-1):
    arr = list(text)
    
    # setting text color
    _printft_sameLine(_echoFormater(color,bold,background))
    
    # printing char by char
    for i in range(len(arr)):
        _printft_sameLine(arr[i])
        time.sleep(speed)
    
    # resetting text color to default state
    resetFormat()
    

def printFormatedTextPiecewise(text,speed=0.1,color=7,bold=0,background=0):
    printftp(text,speed,color,bold)

# "public" function
# Colors text. Only argument needed is text to be formated.
# If format is not specified it uses white color without bold
def textf(text, color = 7, bold = 0,background=-1):
   return _echoFormater(color,bold,background)+text+"\e[0m"


# "public" function
# eneables read raw input from keyboard
# needs to be called BEFORE calling readChar() or readArrow()
# but just once (to be init)
def enableRaw():
   syscall(["stty","-echo"])
   syscall(["stty","raw"])

# "public" function
# disables read raw input from keyboard
# should be called at least on the end of your program
# to reset command line to previous state
def disableRaw():
   syscall(["stty","-raw"])
   syscall(["stty","echo"])

# "public" function
# reads char from stdin (standard input, e.g. keyboard)
def readChar():
    return sys.stdin.read(1)

# "public" function
# reads 3 chars from stdin, third (with index 2!) specify arrow
def readArrow():
    return sys.stdin.read(3)


########################################


# "public" example
# Example of usage
# shows all cool features such as clear, printft and printftp
if __name__ == '__main__':
   
    for i in range(1,8):
        clear()
        printft(textf("Vypravec: ",i,1)+textf("Ahoj cizince!",i))
        printft(textf("Vypravec: ",0,1,i)+textf("Ahoj cizince!",0,0,i))
        printftp("Velmi dlouhy textovy retezec",0.05,i)
        
    clear()
    
    for i in range(0,10):
        
        enableRaw()
        a = readArrow()
        c = a[2]
        disableRaw()
                
        s = ""
        if c == UP:
            s = "nahoru"
        elif c == DOWN:
            s = "dolu"
        elif c == LEFT:
            s = "vlevo"
        elif c == RIGHT:
            s = "doprava"
        else:
            s = textf("neznama", RED)   
        
        printft("Stiskl jsi sipku " + s)
        
                
    
