Skip to content

Commit fdaef3f

Browse files
committed
Added a better way to control colors
1 parent 1da0aaa commit fdaef3f

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/ascii.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,36 @@ def color_map(im, colors):
143143

144144

145145
#{{{ Display ascii data
146-
def render(char_map, characters, color_map, file=sys.stdout):
146+
def render(char_map, characters, color_map=None):
147147
"""
148148
Takes ascii & color data, and display it on the screen
149149
"""
150150
import curses
151151

152152
curses.setupterm()
153-
creset = curses.tigetstr('sc')
154-
ccolor = curses.tigetstr('setaf')
155-
cbold = curses.tigetstr('bold')
156-
157-
def set_color(n):
158-
if n in range(0, 8):
159-
return curses.tparm(ccolor, n)
160-
if n in range(8, 16):
161-
return curses.tparm(ccolor, n) + curses.tparm(cbold)
153+
fg_normal = curses.tigetstr('sgr0')
154+
fg_colors = [curses.tparm(curses.tigetstr('setaf'), i) for i in range(8)]
155+
attr_bold = curses.tparm(curses.tigetstr('bold'), curses.A_BOLD)
156+
attr_normal = curses.tparm(curses.tigetstr('sgr0'), curses.A_NORMAL)
157+
158+
def set_color(fg=None):
159+
if fg is None:
160+
return fg_normal + attr_normal
161+
if fg in range(0, 8):
162+
return fg_colors[fg]
163+
if fg in range(8, 16):
164+
return fg_colors[fg-8] + attr_bold
162165
return ''
163166

164-
def reset_colors():
165-
return curses.tparm(creset)
166-
167167
for y in range(len(char_map)):
168168
for x in range(len(char_map[y])):
169-
file.write(set_color(color_map[y][x]))
170-
file.write(characters[char_map[y][x]])
171-
file.write('\n')
169+
if color_map is not None:
170+
print(set_color(color_map[y][x]), end='')
171+
print(characters[char_map[y][x]], end='')
172+
print('')
172173

173-
file.write(reset_colors())
174+
if color_map is not None:
175+
print(set_color(), end='')
174176
#}}}
175177

176178

0 commit comments

Comments
 (0)