roc/devtools/smart_diff_utrace.html
Anton-4 ae26f9392a
made {} block foldable
Signed-off-by: Anton-4 <17049058+Anton-4@users.noreply.github.com>
2024-10-23 16:43:14 +02:00

215 lines
6.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Function Trace Diff Tool</title>
<style>
body {
font-family: monospace;
padding: 20px;
background-color: #f5f5f5;
max-width: 1200px;
margin: 0 auto;
}
h1 {
text-align: center;
color: #333;
}
#input-area {
margin-bottom: 20px;
}
#input-area textarea {
width: 100%;
height: 150px;
margin-bottom: 10px;
font-family: monospace;
padding: 10px;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-bottom: 20px;
}
button:hover {
background-color: #45a049;
}
#result-area {
display: flex;
gap: 20px;
}
#result1, #result2 {
flex: 1;
background-color: white;
padding: 5px;
padding-left: 15px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
white-space: pre;
overflow-x: auto;
}
.function-block {
position: relative;
display: flex;
flex-direction: column;
margin-left: 0 !important;
}
.function-header {
display: flex;
align-items: flex-start;
cursor: pointer;
}
.function-header:hover {
background-color: #f0f0f0;
}
.toggle-btn {
position: absolute;
left: -10px;
width: 10px;
cursor: pointer;
color: #666;
user-select: none;
}
.function-name {
flex-grow: 1;
}
.function-content {
margin-left: 20px;
}
.line {
margin-left: 0px;
}
.function-end {
margin-left: 0;
color: #666;
}
.collapsed .function-content {
display: none;
}
.collapsed .function-end {
display: none;
}
.collapsed .function-header::after {
content: " ... }";
color: #666;
}
.highlight {
background-color: #fff3b0;
}
</style>
</head>
<body>
<h1>Text Comparison Tool</h1>
<div id="input-area">
<textarea id="input1" placeholder="Enter first trace..."></textarea>
<textarea id="input2" placeholder="Enter second trace..."></textarea>
<button onclick="compareTraces()">Compare</button>
</div>
<div id="result-area">
<div id="result1"></div>
<div id="result2"></div>
</div>
<script>
function processTrace(trace, otherTrace, resultId) {
const lines = trace.trim().split('\n');
const otherLines = otherTrace.trim().split('\n');
let html = '';
let indentLevel = 0;
let blockStartLine = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
const shouldHighlight = !otherLines.some(otherLine => otherLine.trim() === line);
const highlightClass = shouldHighlight ? 'highlight' : '';
// Check if this line is the start of a block that has content
const isBlockStart = line.endsWith('{') && i < lines.length - 1;
if (isBlockStart) {
blockStartLine = i;
// Start of a function block with content
const functionName = line;
html += `<div class="function-block" style="margin-left: ${indentLevel * 20}px">
<div class="function-header ${highlightClass}">
<span class="toggle-btn">▼</span>
<span class="function-name">${functionName}</span>
</div>
<div class="function-content">`;
indentLevel++;
} else if (line.includes('}')) {
// End of a block
if (indentLevel > 0) {
indentLevel--;
html += `</div><span class="function-end ${highlightClass}">${line}</span></div>`;
} else {
html += `<div class="line ${highlightClass}">${line}</div>`;
}
} else {
// Regular line or a line ending with { at the end of input
const isLastLineBlock = line.endsWith('{') && i === lines.length - 1;
if (isLastLineBlock) {
html += `<div class="line ${highlightClass}">${line}</div>`;
} else {
html += `<div class="line ${highlightClass}">${line}</div>`;
}
}
}
return html;
}
function initializeCollapsible(containerId) {
const container = document.getElementById(containerId);
const functionBlocks = container.querySelectorAll('.function-block');
functionBlocks.forEach(block => {
const header = block.querySelector('.function-header');
const toggleBtn = block.querySelector('.toggle-btn');
header.addEventListener('click', (e) => {
block.classList.toggle('collapsed');
toggleBtn.textContent = block.classList.contains('collapsed') ? '▶' : '▼';
});
});
}
function compareTraces() {
const trace1 = document.getElementById('input1').value;
const trace2 = document.getElementById('input2').value;
document.getElementById('result1').innerHTML = processTrace(trace1, trace2, 'result1');
document.getElementById('result2').innerHTML = processTrace(trace2, trace1, 'result2');
initializeCollapsible('result1');
initializeCollapsible('result2');
}
compareTraces();
</script>
</body>
</html>