Skip to content

Commit

Permalink
renamed to pylor
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDaniel committed Oct 16, 2020
1 parent b495b76 commit d29328f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 43 deletions.
85 changes: 55 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ansiColor
# pylor

## *Library for outputting colorful text*

Expand All @@ -14,20 +14,20 @@

## QuickStart

Use: `pip install -i https://test.pypi.org/simple/ ansiColor` to install the module.
Use: `pip install pylor` to install the module.
### Imports
```py
import ansiColor
from ansiColor import colors # Imports class full of colors to choose from
from ansiColor import decorations # Imports class full of decorations
from ansiColor import cursor # Imports class for cursor movement
import pylor
from pylor import colors # Imports class full of colors to choose from
from pylor import decorations # Imports class full of decorations
from pylor import cursor # Imports class for cursor movement
```

### Testing the installation

```py
import ansiColor
print( ansiColor.test() ) # This will print the project logo
import pylor
print( pylor.test() ) # This will print the project logo
```

---
Expand All @@ -47,10 +47,10 @@ Arguments:
**Example:**

```py
import ansiColor
from ansiColor import colors
import pylor
from pylor import colors

print( ansiColor.color("Hello world!", colors.foreground.red) )
print( pylor.color("Hello world!", colors.foreground.red) )
```

Returns string
Expand All @@ -70,10 +70,10 @@ Arguments:
**Example:**

```py
import ansiColor
from ansiColor import colors
import pylor
from pylor import colors

print( ansiColor.colorNoReset("Hello world!", colors.foreground.red) )
print( pylor.colorNoReset("Hello world!", colors.foreground.red) )
```

Returns string
Expand All @@ -92,10 +92,10 @@ Arguments:
**Example:**

```py
import ansiColor
from ansiColor import decorations
import pylor
from pylor import decorations

print( ansiColor.formatting("Hello world!", decorations.bold) )
print( pylor.formatting("Hello world!", decorations.bold) )
```

Returns string
Expand All @@ -114,10 +114,10 @@ Arguments:
**Example:**

```py
import ansiColor
from ansiColor import decorations
import pylor
from pylor import decorations

print( ansiColor.formatting("Hello world!", decorations.bold) )
print( pylor.formatting("Hello world!", decorations.bold) )
```

Returns string
Expand All @@ -135,9 +135,9 @@ Arguments:
**Example:**

```py
import ansiColor
import pylor

print( ansiColor.reset() )
print( pylor.reset() )
```

Returns string
Expand All @@ -155,9 +155,9 @@ Arguments:
**Example:**

```py
import ansiColor
import pylor

print( ansiColor.test() )
print( pylor.test() )
```

Returns string
Expand Down Expand Up @@ -214,6 +214,8 @@ brightCyan
brightWhite
```

---

### **Decorations**

*decorations.*
Expand Down Expand Up @@ -244,17 +246,40 @@ Exception for when the argument given is not an ANSI code.
Example of code that would trigger this Exception:

```py
import ansiColor
from ansiColor import colors
import pylor
from pylor import colors

print( ansiColor.color("Hello world!", "red") ) # This is an example of bad code, do not use this, it will not work.
print( pylor.color("Hello world!", "red") ) # This is an example of bad code, do not use this, it will not work.
```

correct code:

```py
import ansiColor
from ansiColor import colors
import pylor
from pylor import colors

print( pylor.color("Hello world!", colors.foreground.red) )
```
---

### SameType

Exception for when the background/foreground colors are used twice.

Example of code that would trigger this Exception:

```py
import pylor
from pylor import colors

print( ansiColor.color("Hello world!", colors.foreground.red) )
print( pylor.color("Hello world!", colors.foreground.red, colors.foreground.green) ) # This is an example of bad code, do not use this, it will not work.
```

correct code:

```py
import pylor
from pylor import colors

print( pylor.color("Hello world!", colors.foreground.red, colors.background.red) )
```
66 changes: 56 additions & 10 deletions ansiColor/__init__.py → pylor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,53 @@ class cursor:

class Error(Exception):
"""Base class for other exceptions"""
pass


class NotAColor(Error):
"""Raised when the input is not a color"""
def __init__(self, foregroundColor,t):
self.color = foregroundColor
self.message = f"not an ANSI code color: {t}"
super().__init__(self.message)

class NotAColor(Error):
"""Raised when the input is not a color"""
pass
class SameType(Error):
"""Raised when user inputs foreground/background colors twice"""
def __init__(self,t = ""):
self.message = f"code used twice: {t}"
super().__init__(self.message)


def color(text,foregroundColor,backgroundColor = None):
fore = None
back = None

if foregroundColor.startswith(u"\u001b") == False:
raise NotAColor(foregroundColor,"foreground")

if backgroundColor != None:
if backgroundColor.startswith(u"\u001b") == False:
raise NotAColor(backgroundColor,"background")

try:
t = int(str(foregroundColor).replace("m","").replace(";1","")[-2:])
except:
raise NotAColor("foreground")
if t > 37:
back = True
else:
fore = True
if backgroundColor != None:
try:
t = int(str(backgroundColor).replace("m","").replace(";1","")[-2:])
except:
raise NotAColor("background")
if t > 37:
if back == True:
raise SameType("background")
else:
if fore == True:
raise SameType("foreground")

if backgroundColor == None:
data = f"{foregroundColor}{text}{colors.reset}"
else:
Expand All @@ -79,13 +108,30 @@ def color(text,foregroundColor,backgroundColor = None):
return data

def colorNoReset(text,foregroundColor,backgroundColor = None):
if foregroundColor.startswith(u"\\u001") == False:
raise NotAColor(foregroundColor)
fore = None
back = None

if foregroundColor.startswith(u"\u001b") == False:
raise NotAColor(foregroundColor,"foreground")

if backgroundColor.startswith(u"\\u001") == False:
if backgroundColor != None:
raise NotAColor(backgroundColor)
if backgroundColor != None:
if backgroundColor.startswith(u"\u001b") == False:
raise NotAColor(backgroundColor,"background")

t = int(str(foregroundColor).replace("m","").replace(";1","")[-2:])
if t > 37:
back = True
else:
fore = True
if backgroundColor != None:
t = int(str(backgroundColor).replace("m","").replace(";1","")[-2:])
if t > 37:
if back == True:
raise SameType("background")
else:
if fore == True:
raise SameType("foreground")

if backgroundColor == None:
data = f"{foregroundColor}{text}"
else:
Expand All @@ -107,4 +153,4 @@ def test():
two = f"{color('Hello',colors.foreground.white,colors.background.red)} {color('colors',colors.foreground.white,colors.background.green)}{color('!',colors.foreground.white,colors.background.blue)}"
return f"Hello world!\n{one}\n{two}"

print(formatting("test",decorations.bold))

6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
long_description = fh.read()

setuptools.setup(
name="ansiColor",
version="1.0.1",
name="pylor",
version="0.0.2",
author="MiranDaniel",
author_email="[email protected]",
description="Small package made for using ANSI codes",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/MiranDaniel/ansiColor",
url="https://github.com/MiranDaniel/pylor",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
Expand Down

0 comments on commit d29328f

Please sign in to comment.