Fixed color clearing ANSI codes still appearing when colors are off
This commit is contained in:
parent
ab5d01908e
commit
e96bdfaead
2 changed files with 36 additions and 32 deletions
16
gameshell.py
16
gameshell.py
|
@ -23,7 +23,7 @@ class GameShell(Shell):
|
||||||
super(GameShell, self).__init__()
|
super(GameShell, self).__init__()
|
||||||
self.outstream = _sys.stdout
|
self.outstream = _sys.stdout
|
||||||
self.gameBase = gameBase
|
self.gameBase = gameBase
|
||||||
self.colorMode = 24
|
self.colorMode = 0
|
||||||
|
|
||||||
# register functions
|
# register functions
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ If -l is given, a map legend will be printed under the map."""
|
||||||
textColor = level.wallColors[0]
|
textColor = level.wallColors[0]
|
||||||
floorColor = level.floorColors[0]
|
floorColor = level.floorColors[0]
|
||||||
for y in range(level.dimensions[1]):
|
for y in range(level.dimensions[1]):
|
||||||
rows.append(['\x1b[0m{0:2} {1}{2}'.format(y, self.color(textColor[1:]), self.color(floorColor[1:], fg = False))])
|
rows.append(['{0}{1:2} {2}{3}'.format(self.clearColor(), y, self.color(textColor[1:]), self.color(floorColor[1:], fg = False))])
|
||||||
rows[-1].append(self.color(textColor[1:]))
|
rows[-1].append(self.color(textColor[1:]))
|
||||||
for x in range(level.dimensions[0]):
|
for x in range(level.dimensions[0]):
|
||||||
pos = level.mapMatrix[y][x]
|
pos = level.mapMatrix[y][x]
|
||||||
|
@ -163,24 +163,24 @@ If -l is given, a map legend will be printed under the map."""
|
||||||
rows[-1] = ''.join(rows[-1])
|
rows[-1] = ''.join(rows[-1])
|
||||||
|
|
||||||
print(xAxis)
|
print(xAxis)
|
||||||
print('\x1b[0m\n'.join(rows) + '\x1b[0m')
|
print('{0}\n'.format(self.clearColor()).join(rows) + self.clearColor())
|
||||||
self.clearColor()
|
self.clearColor()
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
if args[0] == '-l' or args[0] == 'l' or args[0] == 'legend':
|
if args[0] == '-l' or args[0] == 'l' or args[0] == 'legend':
|
||||||
legend = ["\n---Legend---\n", "{0}()\x1b[0m - {1}".format(self.color('0000FF'), self.gameBase.playerName),
|
legend = ["\n---Legend---\n", "{0}(){1} - {2}".format(self.color('0000FF'), self.clearColor(), self.gameBase.playerName),
|
||||||
"Xn - Exit to another area"]
|
"Xn - Exit to another area"]
|
||||||
for i in exits:
|
for i in exits:
|
||||||
legend.append(' {0}X{1}\x1b[0m - {2}'.format(self.color(exits[i][1][1:]), i, exits[i][0]))
|
legend.append(' {0}X{1}{2} - {3}'.format(self.color(exits[i][1][1:]), i, self.clearColor(), exits[i][0]))
|
||||||
legend.append("Un - Useable object")
|
legend.append("Un - Useable object")
|
||||||
for i in useables:
|
for i in useables:
|
||||||
legend.append(' {0}U{1}\x1b[0m - {2}'.format(self.color(useables[i][1][1:]), i, useables[i][0]))
|
legend.append(' {0}U{1}{2} - {3}'.format(self.color(useables[i][1][1:]), i, self.clearColor(), useables[i][0]))
|
||||||
legend.append("In - Item")
|
legend.append("In - Item")
|
||||||
for i in items:
|
for i in items:
|
||||||
legend.append(' {0}I{1}\x1b[0m - {2}'.format(self.color(items[i][1][1:]), i, items[i][0]))
|
legend.append(' {0}I{1}{2} - {3}'.format(self.color(items[i][1][1:]), i, self.clearColor(), items[i][0]))
|
||||||
legend.append("Dn - Door")
|
legend.append("Dn - Door")
|
||||||
for i in doors:
|
for i in doors:
|
||||||
legend.append(' {0}D{1}\x1b[0m - {2}'.format(self.color(doors[i][1][1:]), i, doors[i][0]))
|
legend.append(' {0}D{1}{2} - {3}'.format(self.color(doors[i][1][1:]), i, self.clearColor(), doors[i][0]))
|
||||||
print('\n'.join(legend))
|
print('\n'.join(legend))
|
||||||
heapq.heappush(self.gameBase.eventQueue, (self.gameBase.gameTime, gameevents.NoOpEvent()))
|
heapq.heappush(self.gameBase.eventQueue, (self.gameBase.gameTime, gameevents.NoOpEvent()))
|
||||||
return
|
return
|
||||||
|
|
52
shell.py
52
shell.py
|
@ -20,7 +20,7 @@
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
# MA 02110-1301, USA.
|
# MA 02110-1301, USA.
|
||||||
#
|
#
|
||||||
# Ver. 0.1.0026
|
# Ver. 0.1.0028
|
||||||
|
|
||||||
|
|
||||||
import types as _types
|
import types as _types
|
||||||
|
@ -37,6 +37,7 @@ class Shell(object):
|
||||||
self.ps1 = '> '
|
self.ps1 = '> '
|
||||||
self.ps2 = '> '
|
self.ps2 = '> '
|
||||||
self.colorMode = 0 # bits of color depth. Supports: 0, 3, 4, 8, 24
|
self.colorMode = 0 # bits of color depth. Supports: 0, 3, 4, 8, 24
|
||||||
|
self.prevColor = '\x1b[0m'
|
||||||
self.__exit = False
|
self.__exit = False
|
||||||
|
|
||||||
def __color24(self, r, g, b, fg = True):
|
def __color24(self, r, g, b, fg = True):
|
||||||
|
@ -78,42 +79,45 @@ class Shell(object):
|
||||||
"""expects string formmatted like 3377DD"""
|
"""expects string formmatted like 3377DD"""
|
||||||
return int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16)
|
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):
|
if isinstance(r, str):
|
||||||
r, g, b = self.colorFromHex(r)
|
r, g, b = self.colorFromHex(r)
|
||||||
|
ret = ''
|
||||||
if self.colorMode == 0: # no color
|
if self.colorMode == 0: # no color
|
||||||
return ''
|
ret = ''
|
||||||
elif self.colorMode == 3:
|
elif self.colorMode == 3:
|
||||||
return self.__color3(r, g, b, fg)
|
ret = self.__color3(r, g, b, fg)
|
||||||
elif self.colorMode == 4:
|
elif self.colorMode == 4:
|
||||||
return self.__color4(r, g, b, fg)
|
ret = self.__color4(r, g, b, fg)
|
||||||
elif self.colorMode == 8:
|
elif self.colorMode == 8:
|
||||||
return self.__color8(r, g, b, fg)
|
ret = self.__color8(r, g, b, fg)
|
||||||
elif self.colorMode == 24:
|
elif self.colorMode == 24:
|
||||||
return self.__color24(r, g, b, fg)
|
ret = self.__color24(r, g, b, fg)
|
||||||
else:
|
else:
|
||||||
|
ret = ''
|
||||||
|
if ret == self.prevColor:
|
||||||
return ''
|
return ''
|
||||||
|
if setPrevColor:
|
||||||
|
self.prevColor = ret
|
||||||
|
return ret
|
||||||
|
|
||||||
def setColor(self, r, g = 0, b = 0, fg = True):
|
def setColor(self, r, g = 0, b = 0, fg = True):
|
||||||
"""Set the text color."""
|
"""Set the text color."""
|
||||||
if isinstance(r, str):
|
print(color(r, g, b, fg), end = '')
|
||||||
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
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def clearColor(self):
|
def clearColor(self, setPrevColor = True):
|
||||||
print('\x1b[0m', end = '')
|
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
|
return
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue