Skip to content

Commit

Permalink
Add fallback to non bright colors for 8-color palette
Browse files Browse the repository at this point in the history
  • Loading branch information
nbedos committed Jun 25, 2018
1 parent 49c7874 commit 61aef23
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ A Linux terminal recorder written in Python which renders your command
line sessions as standalone SVG animations.

<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/awesome.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/awesome.svg">
</p>

More examples of recordings [here](https://github.com/nbedos/termtosvg/blob/0.2.0/examples/examples.md)
More examples of recordings [here](https://github.com/nbedos/termtosvg/blob/0.2.1/examples/examples.md)

## Motivation
I really like the clean look of SVG animations and I also wanted to see
Expand Down
10 changes: 5 additions & 5 deletions examples/examples.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Examples
## awesome.svg
<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/awesome.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/awesome.svg">
</p>

## colors.svg
<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/colors.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/colors.svg">
</p>

## ipython.svg
<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/ipython.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/ipython.svg">
</p>

## htop.svg
<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/htop.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/htop.svg">
</p>

## unittest.svg
<p align="center">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.0/examples/unittest.svg">
<img src="https://cdn.rawgit.com/nbedos/termtosvg/0.2.1/examples/unittest.svg">
</p>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='termtosvg',
version='0.2.0',
version='0.2.1',
description='Record terminal sessions as SVG animations',
long_description='A Linux terminal recorder written in Python '
'which renders your command line sessions as '
Expand Down
18 changes: 12 additions & 6 deletions termtosvg/anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ def from_pyte(cls, char, palette):
else:
search_color = char.fg

if search_color in color_numbers and color_numbers[search_color] in palette:
# Named colors
text_color = palette[color_numbers[search_color]]
if search_color in color_numbers:
# NAMED COLORS
if color_numbers[search_color] in palette:
# Case for color numbers < 8 (since the palette has at least the first 8 colors)
# or for 16-color palette (all named colors in the palette)
text_color = palette[color_numbers[search_color]]
else:
# Case for color numbers >= 8 and 8-color palette: fallback to non bright color
text_color = palette[color_numbers[char.fg]]
elif len(char.fg) == 6:
# Hexadecimal colors
# raise ValueError if char.bg hexadecimal number
# HEXADECIMAL COLORS
# raise ValueError if char.fg is not an hexadecimal number
int(char.fg, 16)
text_color = '#{}'.format(char.fg)
else:
Expand All @@ -65,7 +71,7 @@ def from_pyte(cls, char, palette):
background_color = palette[color_numbers[char.bg]]
elif len(char.bg) == 6:
# Hexadecimal colors
# raise ValueError if char.bg hexadecimal number
# raise ValueError if char.bg is not an hexadecimal number
int(char.bg, 16)
background_color = '#{}'.format(char.bg)
else:
Expand Down
6 changes: 3 additions & 3 deletions termtosvg/asciicast.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def from_xresources(cls, xresources):
res_db = rdb.ResourceDB(string=xresources)

colors = {}
names = [('foreground', True), ('background', True)] + \
[('color{}'.format(index), index < 8) for index in range(16)]
for name, required in names:
names = ['foreground', 'background'] + \
['color{}'.format(index) for index in range(16)]
for name in names:
res_name = 'termtosvg.' + name
res_class = res_name
colors[name] = res_db.get(res_name, res_class, None)
Expand Down
10 changes: 10 additions & 0 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import termtosvg.__main__ as __main__
import termtosvg.term as term
from tests.test_term import xresources_minimal

SHELL_COMMANDS = [
'echo $SHELL && sleep 0.1;\r\n',
Expand All @@ -19,6 +20,8 @@
'a',
'm',
'i\r\n',
"\033[1;31mbright red fg\033[0m\r\n",
"\033[1;41mbright red bg\033[0m\r\n",
'exit;\r\n'
]

Expand Down Expand Up @@ -121,4 +124,11 @@ def test_main(self):
get_x_mock = Mock(return_value=xresources_dracula)
with unittest.mock.patch('termtosvg.term._get_xresources', get_x_mock):
args = ['termtosvg', svg_filename, '--theme', 'circus', '--verbose']
TestMain.run_main(SHELL_COMMANDS, args)

with self.subTest(case='8 color palette'):
# Mock color info gathering
get_x_mock = Mock(return_value=xresources_minimal)
with unittest.mock.patch('termtosvg.term._get_xresources', get_x_mock):
args = ['termtosvg', svg_filename]
TestMain.run_main(SHELL_COMMANDS, args)
5 changes: 5 additions & 0 deletions tests/test_anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def test_from_pyte(self):
pyte.screens.Char('G', '008700', 'ABCDEF'),
# Bright and bold
pyte.screens.Char('H', 'brightgreen', 'ABCDEF', bold=True),
# Bright but not in the palette --> fallback to the non bright color
# for compatibility with an 8-color palette (issue #1)
pyte.screens.Char('I', 'brown', 'ABCDEF', bold=True),
]

char_cells = [
Expand All @@ -37,12 +40,14 @@ def test_from_pyte(self):
anim.CharacterCell('F', 'foreground', 'background'),
anim.CharacterCell('G', '#008700', '#ABCDEF'),
anim.CharacterCell('H', 'color10', '#ABCDEF'),
anim.CharacterCell('I', 'color3', '#ABCDEF'),
]

palette = {
'foreground': 'foreground',
'background': 'background',
1: 'color1',
3: 'color3',
4: 'color4',
9: 'color9',
10: 'color10',
Expand Down

0 comments on commit 61aef23

Please sign in to comment.