feat: Support showBits

This commit is contained in:
Sidharth Vinod
2023-09-14 23:12:42 +05:30
parent 75f1f9228d
commit 043729f557
4 changed files with 39 additions and 6 deletions

View File

@@ -35,6 +35,32 @@
192-223: "data" 192-223: "data"
</pre> </pre>
<pre class="mermaid">
---
config:
packet:
showBits: false
---
packet-beta
0-15: "Source Port"
16-31: "Destination Port"
32-63: "Sequence Number"
64-95: "Acknowledgment Number"
96-99: "Data Offset"
100-105: "Reserved"
106: "URG"
107: "ACK"
108: "PSH"
109: "RST"
110: "SYN"
111: "FIN"
112-127: "Window"
128-143: "Checksum"
144-159: "Urgent Pointer"
160-191: "(Options and Padding)"
192-223: "data"
</pre>
<script type="module"> <script type="module">
import mermaid from '/mermaid.esm.mjs'; import mermaid from '/mermaid.esm.mjs';
mermaid.initialize({ mermaid.initialize({

View File

@@ -16,10 +16,14 @@ let data: PacketData = structuredClone(defaultPacketData);
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet; export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
export const getConfig = (): Required<PacketDiagramConfig> => { export const getConfig = (): Required<PacketDiagramConfig> => {
return structuredClone({ const config = structuredClone({
...DEFAULT_PACKET_CONFIG, ...DEFAULT_PACKET_CONFIG,
...commonGetConfig().packet, ...commonGetConfig().packet,
}); });
if (config.showBits) {
config.paddingY += 10;
}
return config;
}; };
export const getPacket = (): Row[] => data.packet; export const getPacket = (): Row[] => data.packet;

View File

@@ -27,7 +27,7 @@ const drawWord = (
svg: SVG, svg: SVG,
row: Row, row: Row,
rowNumber: number, rowNumber: number,
{ rowHeight, paddingX, paddingY, bitWidth, bitsPerRow }: Required<PacketDiagramConfig> { rowHeight, paddingX, paddingY, bitWidth, bitsPerRow, showBits }: Required<PacketDiagramConfig>
) => { ) => {
const group: Group = svg.append('g'); const group: Group = svg.append('g');
const wordY = rowNumber * (rowHeight + paddingY) + paddingY; const wordY = rowNumber * (rowHeight + paddingY) + paddingY;
@@ -53,13 +53,16 @@ const drawWord = (
.attr('text-anchor', 'middle') .attr('text-anchor', 'middle')
.text(block.label); .text(block.label);
if (!showBits) {
continue;
}
// Start byte count // Start byte count
const isSingleBlock = block.end === block.start; const isSingleBlock = block.end === block.start;
const byteNumberY = wordY - 2; const bitNumberY = wordY - 2;
group group
.append('text') .append('text')
.attr('x', blockX + (isSingleBlock ? width / 2 : 0)) .attr('x', blockX + (isSingleBlock ? width / 2 : 0))
.attr('y', byteNumberY) .attr('y', bitNumberY)
.attr('class', 'byte start') .attr('class', 'byte start')
.attr('dominant-baseline', 'auto') .attr('dominant-baseline', 'auto')
.attr('text-anchor', isSingleBlock ? 'middle' : 'start') .attr('text-anchor', isSingleBlock ? 'middle' : 'start')
@@ -70,7 +73,7 @@ const drawWord = (
group group
.append('text') .append('text')
.attr('x', blockX + width) .attr('x', blockX + width)
.attr('y', byteNumberY) .attr('y', bitNumberY)
.attr('class', 'byte end') .attr('class', 'byte end')
.attr('dominant-baseline', 'auto') .attr('dominant-baseline', 'auto')
.attr('text-anchor', 'end') .attr('text-anchor', 'end')

View File

@@ -1880,7 +1880,7 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file)
default: 5 default: 5
paddingY: paddingY:
description: The vertical padding between the rows. description: The vertical padding between the rows.
default: 15 default: 5
FontCalculator: FontCalculator:
title: Font Calculator title: Font Calculator