Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lVoidi committed Oct 2, 2021
0 parents commit 5867a60
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2021 Rodrigo
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holders, nor those of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Empty file added README.md
Empty file.
17 changes: 17 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
This code will only work on Linux and Mac OS X systems,
and only in terminal emulators with true color available,
How to check if your terminal has true color, write down this
command in your terminal:
printf "\x1b[38;2;255;100;0mTRUECOLOR\x1b[0m\n"
If the terminal does NOT print "TRUECOLOR" in red, your terminal
does not support it.
What terminal emulator support true color?
Check this gist:
https://gist.github.com/XVilka/8346728#terminals--true-color
"""
from theming.themify.theme import Theme
Binary file added __pycache__/__init__.cpython-39.pyc
Binary file not shown.
Empty file added setup.cfg
Empty file.
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from distutils.core import setup
setup(
name = 'theming',
packages = ['theming'],
version = '0.1',
license='MIT',
description = 'A package to theme terminal scripts with custom colors and text formatting',
author = 'Rodrigo',
author_email = 'roarba011@gmail.com',
url = 'https://github.com/mrjakesir/theming',
download_url = 'https://github.com/user/reponame/archive/v_01.tar.gz',
keywords = ['COLORS', 'SCRIPTING', 'THEME', 'THEMING', 'USELESS'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)
Binary file added themify/__pycache__/theme.cpython-39.pyc
Binary file not shown.
142 changes: 142 additions & 0 deletions themify/theme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from sys import platform as OS
import re

class Theme:
"""
Theming class
available methods:
self.bold (text: str)
self.dim (text: str)
self.italic (text: str)
self.underlined (text: str)
self.blinking (text: str)
self.reversed (text: str)
self.invisible (text: str)
self.colorize (text: str, **kwargs):
"""
def __init__(self):

self._INTEGERS = {
'Normal': 0,
'Bold': 1,
'Dim': 2,
'Italic': 3,
'Underlined': 4,
'Blinking': 5,
'Reverse': 7,
'Invisible': 8
}

self._COLORS = {
'Default': 9,
'Black': 0,
'Red': 1,
'Green': 2,
'Yellow': 3,
'Blue': 4,
'Magenta': 5,
'Cyan': 6,
'Light gray': 7,
'Dark gray': 60,
'Light red': 61,
'Light green': 62,
'Light yellow': 63,
'Light blue': 64,
'Light magenta': 65,
'Light cyan': 66,
'White': 67
}

self._escape = '\x1B[' if OS == 'darwin' else '\033['
self._no_color = '\033[0m'
self._to_rgb = lambda h: tuple(int(h[i:i+2], 16) for i in (0, 2, 4))

self._format = lambda code, text: self._escape + \
str(code) + \
'm' + \
text + \
self._no_color

self.bold = lambda text: self._format(self._INTEGERS['Bold'], text)
self.dim = lambda text: self._format(self._INTEGERS['Dim'], text)
self.italic = lambda text: self._format(self._INTEGERS['Italic'], text)
self.underlined = lambda text: self._format(self._INTEGERS['Underlined'],text)
self.blinking = lambda text: self._format(self._INTEGERS['Blinking'], text)
self.reversed = lambda text: self._format(self._INTEGERS['Reverse'], text)
self.invisible = lambda text: self._format(self._INTEGERS['Invisible'], text)


def _to_escape(self, r, g, b, bg=False):
if not bg:
return f'38;2;{r};{g};{b}'

return f'48;2;{r};{g};{b}'

def colorize(self, text: str, **kwargs):
"""
self.colorize(text: str, **kwargs)
kwargs options:
bg = hex value | rgb tuple code | color name
fg = hex value | rgb tuple code | color name
hex value example:
'#ffffff' -> White color
-> It also should always have the hash symbol
rgb tuple code example:
(255, 255, 255)
-> White color
-> It should have only three items
-> First item represents red, second item represents green and last item represents blue
color name example:
'White' -> White color
-> Available colors are:
[
'Default',
'Black',
'Red',
'Green',
'Yellow',
'Blue',
'Magenta',
'Cyan',
'Light gray',
'Dark gray',
'Light red',
'Light green',
'Light yellow',
'Light blue',
'Light magenta',
'Light cyan'
]
"""

colors = self._COLORS
fg = colors['Default']*30
bg = colors['Default']*40

for key, value in kwargs.items():
if type(value) == tuple:
if key == 'bg':
bg = self._to_escape(*value, bg=True)
else:
fg =self._to_escape(*value)

elif re.match('#[a-fA-F0-9]{6}', value):
value = value.replace('#', '')
if key == 'bg':
bg = self._to_escape(*self._to_rgb(value), bg=True)
else:
fg = self._to_escape(*self._to_rgb(value))

elif value in self._COLORS.keys():
if key == 'bg':
bg = self._COLORS[value]+40
else:
fg = self._COLORS[value]+30


return self._format(fg, self._format(bg, text))

0 comments on commit 5867a60

Please sign in to comment.