Skip to content

Commit

Permalink
implement inverse attribute
Browse files Browse the repository at this point in the history
There is no HTML "inverse" style, so we have to fake it by explicitly
swapping foreground and background colors.  However, this is a problem
if the character does not have explicit foreground and background
colors, and if the <pre> tag's foreground and background were also not
set.  So, we will introduce a new option to give the fallback colors in
that case.
  • Loading branch information
MarkLodato committed Sep 5, 2011
1 parent 8cc7953 commit f323d77
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,6 +1,7 @@
Version 0.4 - ????-??-??
- Add --geometry option.
- Add reading of configuration from ~/.vt100rc.
- Implement the "inverse" graphics attribute.
- Internal refactoring. Of note, now the entire input file is read at once.
- Fix a few bugs.

Expand Down
10 changes: 10 additions & 0 deletions README.rst
Expand Up @@ -92,6 +92,16 @@ geometry = {WxH, detect}
current terminal's geometry is detected using ``stty size``.
Default is '80x24'.

inverse_bg = COLOR
Background color to use for the "inverse" attribute when neither the
character's foreground color attribute nor the ``foreground`` option is
set. Default is 'black'.

inverse_fg = COLOR
Foreground color to use for the "inverse" attribute when neither the
character's background color attribute nor the ``background`` option is
set. Default is 'white'.

verbosity = INT
Act as those ``-v`` or ``-q`` was given abs(INT) times, if INT positive or
negative, respectively. Default is '0'.
Expand Down
2 changes: 0 additions & 2 deletions TODO
Expand Up @@ -38,8 +38,6 @@ Options
Output
======

* 'inverse' attribute for HTML

* Xterm keeps track of long lines so that you can select wrapped lines as a
single line. This is not possible for text output, but it might be in HTML.
It is possible to set the ``pre``\ 's style to wrap using the following::
Expand Down
6 changes: 6 additions & 0 deletions test/t0220-SGR_inverse.html
@@ -0,0 +1,6 @@
<pre>
This is <span style="background-color: black; color: white">inverse text</span> with default fg and bg.
<span style="color: #cd0000">This is </span><span style="color: white; background-color: #cd0000">inverse text</span><span style="color: #cd0000"> with red fg and default bg.</span>
<span style="background-color: #cd0000">This is </span><span style="background-color: black; color: #cd0000">inverse text</span><span style="background-color: #cd0000"> with default fg and red bg.</span>
<span style="background-color: #cd0000; color: #00cd00">This is </span><span style="background-color: #00cd00; color: #cd0000">inverse text</span><span style="background-color: #cd0000; color: #00cd00"> with green fg and red bg.</span>
</pre>
4 changes: 4 additions & 0 deletions test/t0220-SGR_inverse.in
@@ -0,0 +1,4 @@
This is inverse text with default fg and bg.
This is inverse text with red fg and default bg.
This is inverse text with default fg and red bg.
This is inverse text with green fg and red bg.
27 changes: 27 additions & 0 deletions vt100.py
Expand Up @@ -93,6 +93,16 @@
current terminal's geometry is detected using ``stty size``.
Default is '80x24'.
inverse_bg = COLOR
Background color to use for the "inverse" attribute when neither the
character's foreground color attribute nor the ``foreground`` option is
set. Default is 'black'.
inverse_fg = COLOR
Foreground color to use for the "inverse" attribute when neither the
character's background color attribute nor the ``background`` option is
set. Default is 'white'.
verbosity = INT
Act as those ``-v`` or ``-q`` was given abs(INT) times, if INT positive or
negative, respectively. Default is '0'.
Expand Down Expand Up @@ -267,6 +277,8 @@ class HtmlFormatter (TextFormatter):
default_options = {
'foreground' : '',
'background' : '',
'inverse_fg' : 'white',
'inverse_bg' : 'black',
}

# [black, red, green, brown/yellow, blue, magenta, cyan, white]
Expand Down Expand Up @@ -302,6 +314,10 @@ def set_color(self, index, value):

def parse_config(self, config):
self._parse_config(config, config.default_section, set())
if self.options['foreground']:
self.options['inverse_bg'] = self.options['foreground']
if self.options['background']:
self.options['inverse_fg'] = self.options['background']

def _parse_config(self, config, section, seen):
if config.has_option(section, 'colorscheme'):
Expand All @@ -328,6 +344,17 @@ def _parse_config(self, config, section, seen):
def _compute_style(self, attr):
# TODO implement inverse
out = []
if attr.pop('inverse', None):
fg = attr.pop('fg_color', None)
bg = attr.pop('bg_color', None)
if fg is not None:
attr['bg_color'] = fg
else:
out.append('background-color: %s' % self.options['inverse_bg'])
if bg is not None:
attr['fg_color'] = bg
else:
out.append('color: %s' % self.options['inverse_fg'])
for key in sorted(attr):
value = attr[key]
try:
Expand Down

0 comments on commit f323d77

Please sign in to comment.