Skip to content

Commit

Permalink
Simplify colorbar using the same technique as html_image()
Browse files Browse the repository at this point in the history
Moving common code into html_colorbar() helper function.
  • Loading branch information
kvid committed Aug 13, 2020
1 parent 3d7f027 commit 9a65223
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write, \
html_image, html_caption, manufacturer_info_field
html_colorbar, html_image, html_caption, manufacturer_info_field
from collections import Counter
from typing import List
from pathlib import Path
Expand Down Expand Up @@ -96,17 +96,13 @@ def create_graph(self) -> Graph:
[html_line_breaks(connector.type),
html_line_breaks(connector.subtype),
f'{connector.pincount}-pin' if connector.show_pincount else None,
connector.color, '<!-- colorbar -->' if connector.color else None],
connector.color, html_colorbar(connector.color)],
'<!-- connector table -->' if connector.style != 'simple' else None,
[html_image(connector)],
[html_caption(connector)],
[html_line_breaks(connector.notes)]]
html.extend(nested_html_table(rows))

if connector.color: # add color bar next to color info, if present
colorbar = f' bgcolor="{wv_colors.translate_color(connector.color, "HEX")}" width="4"></td>' # leave out '<td' from string to preserve any existing attributes of the <td> tag
html = [row.replace('><!-- colorbar --></td>', colorbar) for row in html]

if connector.style != 'simple':
pinhtml = []
pinhtml.append('<table border="0" cellspacing="0" cellpadding="3" cellborder="1">')
Expand Down Expand Up @@ -173,17 +169,13 @@ def create_graph(self) -> Graph:
f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else None,
'+ S' if cable.shield else None,
f'{cable.length} m' if cable.length > 0 else None,
cable.color, '<!-- colorbar -->' if cable.color else None],
cable.color, html_colorbar(cable.color)],
'<!-- wire table -->',
[html_image(cable)],
[html_caption(cable)],
[html_line_breaks(cable.notes)]]
html.extend(nested_html_table(rows))

if cable.color: # add color bar next to color info, if present
colorbar = f' bgcolor="{wv_colors.translate_color(cable.color, "HEX")}" width="4"></td>' # leave out '<td' from string to preserve any existing attributes of the <td> tag
html = [row.replace('><!-- colorbar --></td>', colorbar) for row in html]

wirehtml = []
wirehtml.append('<table border="0" cellspacing="0" cellborder="0">') # conductor table
wirehtml.append(' <tr><td>&nbsp;</td></tr>')
Expand Down
4 changes: 4 additions & 0 deletions src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from wireviz import wv_colors
from typing import List

awg_equiv_table = {
Expand Down Expand Up @@ -55,6 +56,9 @@ def nested_html_table(rows):
html.append('</table>')
return html

def html_colorbar(color):
return f'<tdX bgcolor="{wv_colors.translate_color(color, "HEX")}" width="4">' if color else None

def html_image(node):
if not node.image:
return None
Expand Down

0 comments on commit 9a65223

Please sign in to comment.