#!/usr/bin/env python3

import os

def main():
    clear()
    prompt()

def fnf():
    print('\033[1;33m File Not Found \033[0m')

def clear():
   os.system('clear')

def mkdir():
    os.mkdir(input(' >> '))
    clear()

def delete():
    try:
        thing=input(' >> ')
        os.system('rm -ri '+thing)
    except FileNotFoundError:
        if thing!='':
            fnf()
            delete()
        else:
            clear()
    else:
        clear()

def rename():
    try:
        thing=input(' >> ')
        os.system('mv '+thing)
    except FileNotFoundError:
        if thing!='':
            fnf()
            rename()
        else:
            clear()
    else:
        clear()
def open_with():
    try:
        thing=input(' >> ')
        command=input(' >> ')
        os.system(command+' '+thing)
    except FileNotFoundError:
        if thing!='':
            fnf()
            open_with()
        else:
            clear()
    else:
        clear()

def move():
    try:
        thing=input(' >> ')
        os.system('mv '+thing)
    except FileNotFoundError:
        if thing!='':
            fnf()
            move()
        else:
            clear()
    else:
        clear()

def copy():
    try:
        thing=input(' >> ')
        os.system('cp -R '+thing)
    except FileNotFoundError:
        if thing!='':
            fnf()
            copy()
        else:
            clear()
    else:
        clear()

def prompt_hidden():
        current_dir_name()
        items_in_current_dir_hidden()
        command=input('\n > ')
        if command=='mkdir':
            mkdir()
            prompt_hidden()
        elif command=='delete':
            delete()
            prompt_hidden()
        elif command=='rename':
            rename()
            prompt_hidden()
        elif command=='open':
            open_with()
            clear()
            prompt_hidden()
        elif command=='hide':
            clear()
            prompt()
        elif command=='move':
            move()
            prompt_hidden()
        elif command=='copy':
            copy()
            prompt_hidden()
        else:
            if command!=':q':
                if os.path.isdir(command)==True:
                    os.chdir(command)
                    clear()
                    prompt_hidden()
                elif str.endswith(command,'.jpg')==True or str.endswith(command,'.png')==True \
                        or str.endswith(command,'.gif')==True:
                    os.system('meh '+command)
                    clear()
                    prompt_hidden()
                elif str.endswith(command,'.flac')==True or str.endswith(command,'.mp3')==True:
                    os.system('mplayer '+command)
                    clear()
                    prompt()
                elif str.endswith(command,'.pdf')==True or str.endswith(command,'.epub')==True:
                    os.system('firefox '+command+' &')
                    clear()
                    prompt_hidden()
                elif str.endswith(command,'.tar.gz')==True:
                    os.system('tar xvzf '+command)
                    clear()
                    prompt_hidden()
                elif str.endswith(command,'.zip')==True:
                    os.system('unzip '+command)
                    clear()
                    prompt_hidden()
                else:
                    os.system('vim '+command)
                    clear()
                    prompt_hidden()
            else:
                clear()

def prompt():
    current_dir_name()
    items_in_current_dir()
    command=input('\n > ')
    if command=='mkdir':
        mkdir()
        prompt()
    elif command=='delete':
        delete()
        prompt()
    elif command=='rename':
        rename()
        prompt()
    elif command=='open':
        open_with()
        prompt()
    elif command=='hidden':
        clear()
        prompt_hidden()
    elif command=='move':
        move()
        prompt()
    elif command=='copy':
        copy()
        prompt()
    else:
        if command!=':q':
            if os.path.isdir(command)==True:
                os.chdir(command)
                clear()
                prompt()
            elif str.endswith(command,'.jpg')==True or str.endswith(command,'.png')==True \
                    or str.endswith(command,'.gif')==True:
                os.system('meh '+command)
                clear()
                prompt()
            elif str.endswith(command,'.flac')==True or str.endswith(command,'.mp3')==True:
                os.system('mplayer '+command)
                clear()
                prompt()
            elif str.endswith(command,'.pdf')==True or str.endswith(command,'.epub')==True:
                os.system('firefox '+command+' &')
                clear()
                prompt()
            elif str.endswith(command,'.tar.gz')==True:
                os.system('tar xvzf '+command)
                clear()
                prompt()
            elif str.endswith(command,'.zip')==True:
                os.system('unzip '+command)
                clear()
                prompt()
            else:
                os.system('vim '+command)
                clear()
                prompt()
        else:
            clear()

def current_dir_name():
    print('\033[4;33m\t',os.getcwd(),'\033[0m')

def items_in_current_dir_hidden():
    items_in_dir=os.listdir()
    items_in_dir.sort()
    for n in items_in_dir:
        if os.path.islink(n)==True: 
            if os.path.isdir(n)==True:
                print('\033[1;36m',n+' -> \033[36m'+os.readlink(n)+'\033[0m')
            else:
                print(' '+n+' -> \033[32m'+os.readlink(n)+'\033[0m')
        elif os.path.isdir(n)==True:
            print('\033[1;36m',n+'\033[0m')
        elif os.access(n,os.X_OK)==True:
            print('\033[36m',n+'\033[0m*')
        else:
            print(' '+n)

def items_in_current_dir():
    items_in_dir=os.listdir()
    items_in_dir.sort()
    for n in items_in_dir:
        if str.startswith(n,'.')!=True:
            if os.path.islink(n)==True: 
                if os.path.isdir(n)==True:
                    print('\033[1;36m',n+' -> \033[36m'+os.readlink(n)+'\033[0m')
                else:
                    print(' '+n+' -> \033[32m'+os.readlink(n)+'\033[0m')
            elif os.path.isdir(n)==True:
                print('\033[1;36m',n+'\033[0m')
            elif os.access(n,os.X_OK)==True:
                print('\033[36m',n+'\033[0m*')
            else:
                print(' '+n)

main()
