|
| 1 | +import { h } from "vue"; |
| 2 | +import { manager } from "../../../globals"; |
| 3 | + |
| 4 | +const spacers = { |
| 5 | + hexadecimal: { every1: " ", every8: " " }, |
| 6 | + decimal: { every1: " ", every8: " " }, |
| 7 | + octal: { every1: " ", every8: " " }, |
| 8 | + bits: { every1: " ", every8: " " }, |
| 9 | + ascii: { every1: null, every8: " " }, |
| 10 | + ebcdic: { every1: null, every8: " " }, |
| 11 | +}; |
| 12 | + |
| 13 | +// TODO: possible reactivity issues here |
| 14 | + |
| 15 | +export default { |
| 16 | + props: { |
| 17 | + bytesPerLine: { |
| 18 | + type: Number, |
| 19 | + required: true, |
| 20 | + }, |
| 21 | + sourceIndex: { |
| 22 | + type: Number, |
| 23 | + required: true, |
| 24 | + }, |
| 25 | + }, |
| 26 | + setup(props) { |
| 27 | + return () => { |
| 28 | + const bytes = manager.activeFrameDetails.getSourceData(props.sourceIndex); |
| 29 | + const lineCount = bytes.length / props.bytesPerLine; |
| 30 | + const groups = manager.activeFrameDetails.getGroupsForSource( |
| 31 | + props.sourceIndex |
| 32 | + ); |
| 33 | + |
| 34 | + const nodes = []; |
| 35 | + |
| 36 | + const activeGroups = new Map(); |
| 37 | + |
| 38 | + let groupIdx = 0; |
| 39 | + let changed = true; |
| 40 | + for (let line = 0; line < lineCount; line++) { |
| 41 | + // start groups |
| 42 | + const till = (line + 1) * props.bytesPerLine; |
| 43 | + while (groupIdx < groups.length && groups[groupIdx].start < till) { |
| 44 | + const { start, length, id } = groups[groupIdx++]; |
| 45 | + |
| 46 | + const end = Math.floor((start + length - 1) / props.bytesPerLine); |
| 47 | + |
| 48 | + if (!activeGroups.has(end)) activeGroups.set(end, []); |
| 49 | + activeGroups.get(end).push(id); |
| 50 | + changed = true; |
| 51 | + } |
| 52 | + |
| 53 | + // line number (in hex) |
| 54 | + const lineNumber = |
| 55 | + (line * props.bytesPerLine).toString(16).padStart(4, "0") + "\n"; |
| 56 | + |
| 57 | + if (changed) { |
| 58 | + const ids = [...activeGroups.values()].flat(); |
| 59 | + const color = ids.reduce( |
| 60 | + (colorVar, id) => `var(--ws-linenumber-fg-${id}, ${colorVar})`, |
| 61 | + "inherit" |
| 62 | + ); |
| 63 | + nodes.push({ color, html: lineNumber }); |
| 64 | + } else nodes.at(-1).html += lineNumber; |
| 65 | + |
| 66 | + changed = false; |
| 67 | + |
| 68 | + // end groups |
| 69 | + if (activeGroups.has(line)) { |
| 70 | + activeGroups.delete(line); |
| 71 | + changed = true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const vnodes = nodes.map(({ html, color }) => |
| 76 | + h("span", { style: { color } }, html) |
| 77 | + ); |
| 78 | + return h("div", vnodes); |
| 79 | + }; |
| 80 | + }, |
| 81 | +}; |
0 commit comments