Fixed color clearing ANSI codes still appearing when colors are off

This commit is contained in:
Patrick Marsee 2019-02-01 09:37:46 -05:00
parent ab5d01908e
commit e96bdfaead
2 changed files with 36 additions and 32 deletions

View file

@ -20,7 +20,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Ver. 0.1.0026
# Ver. 0.1.0028
import types as _types
@ -37,6 +37,7 @@ class Shell(object):
self.ps1 = '> '
self.ps2 = '> '
self.colorMode = 0 # bits of color depth. Supports: 0, 3, 4, 8, 24
self.prevColor = '\x1b[0m'
self.__exit = False
def __color24(self, r, g, b, fg = True):
@ -78,42 +79,45 @@ class Shell(object):
"""expects string formmatted like 3377DD"""
return int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16)
def color(self, r, g = 0, b = 0, fg = True):
def color(self, r, g = 0, b = 0, fg = True, setPrevColor = True):
if isinstance(r, str):
r, g, b = self.colorFromHex(r)
ret = ''
if self.colorMode == 0: # no color
return ''
ret = ''
elif self.colorMode == 3:
return self.__color3(r, g, b, fg)
ret = self.__color3(r, g, b, fg)
elif self.colorMode == 4:
return self.__color4(r, g, b, fg)
ret = self.__color4(r, g, b, fg)
elif self.colorMode == 8:
return self.__color8(r, g, b, fg)
ret = self.__color8(r, g, b, fg)
elif self.colorMode == 24:
return self.__color24(r, g, b, fg)
ret = self.__color24(r, g, b, fg)
else:
ret = ''
if ret == self.prevColor:
return ''
if setPrevColor:
self.prevColor = ret
return ret
def setColor(self, r, g = 0, b = 0, fg = True):
"""Set the text color."""
if isinstance(r, str):
r, g, b = self.colorFromHex(r)
if self.colorMode == 0: # no color
return
elif self.colorMode == 3:
print(self.__color3(r, g, b, fg), end = '')
elif self.colorMode == 4:
print(self.__color4(r, g, b, fg), end = '')
elif self.colorMode == 8:
print(self.__color8(r, g, b, fg), end = '')
elif self.colorMode == 24:
print(self.__color24(r, g, b, fg), end = '')
else:
return
print(color(r, g, b, fg), end = '')
return
def clearColor(self):
print('\x1b[0m', end = '')
def clearColor(self, setPrevColor = True):
ret = ''
if self.colorMode > 0:
ret = '\x1b[0m'
if ret == self.prevColor:
return ''
if setPrevColor:
self.prevColor = ret
return ret
def setClearColor(self):
print(clearColor())
return
def run(self):