Skip to content

Commit 012a707

Browse files
committed
* HtmlPageDump now slices large text when rendering strings in page blocks
* PageType is now a Enum
1 parent 75693e2 commit 012a707

1 file changed

Lines changed: 86 additions & 78 deletions

File tree

LiteDB.Studio/Classes/Debugger/HtmlPageDump.cs

Lines changed: 86 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
using LiteDB;
2-
using LiteDB.Engine;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
5-
using System.Diagnostics;
6-
using System.Diagnostics.Contracts;
7-
using System.IO;
83
using System.Linq;
9-
using System.Linq.Expressions;
10-
using System.Net;
11-
using System.Reflection;
124
using System.Text;
13-
using System.Threading;
14-
using System.Threading.Tasks;
155

166
namespace LiteDB.Studio
177
{
188
public class HtmlPageDump
199
{
2010
private const int BLOCK_WIDTH = 30;
11+
private const int BLOCKS_PER_LINE = 32;
12+
private const int PAGE_HEADER_SIZE = 32;
13+
private enum PageType { Empty = 0, Header = 1, Collection = 2, Index = 3, Data = 4 }
2114

2215
private readonly BsonDocument _page;
2316
private readonly uint _pageID;
24-
private readonly byte _pageType;
17+
private readonly PageType _pageType;
2518
private readonly byte[] _buffer;
2619
private readonly StringBuilder _writer = new StringBuilder();
2720
private readonly List<PageItem> _items = new List<PageItem>();
@@ -32,7 +25,7 @@ public HtmlPageDump(BsonDocument page)
3225
_page = page;
3326
_buffer = page["buffer"].AsBinary;
3427
_pageID = BitConverter.ToUInt32(_buffer, 0);
35-
_pageType = (byte)_buffer[4];
28+
_pageType = (PageType)_buffer[4];
3629
page.Remove("buffer");
3730

3831
this.LoadItems();
@@ -57,11 +50,11 @@ private void SpanCaptionItems()
5750
this.SpanPageHeader();
5851
this.SpanSegments();
5952

60-
if (_pageType == 1)
53+
if (_pageType == PageType.Header)
6154
{
6255
this.SpanHeaderPage();
6356
}
64-
else if (_pageType == 2)
57+
else if (_pageType == PageType.Collection)
6558
{
6659
this.SpanCollectionPage();
6760
}
@@ -122,7 +115,7 @@ private void SpanSegments()
122115

123116
_items[position].Id = i.ToString();
124117

125-
if (_pageType == 4)
118+
if (_pageType == PageType.Data)
126119
{
127120
this.SpanItem<byte>(position, 0, null, "Extend", null);
128121
this.SpanPageID(position + 1, "NextBlockID", true, false);
@@ -181,7 +174,7 @@ private void SpanSegments()
181174

182175
private void SpanHeaderPage()
183176
{
184-
var h = 32;
177+
var h = PAGE_HEADER_SIZE;
185178
var color = 0;
186179

187180
h += this.SpanItem(h, 26, null, "HeaderInfo", (byte[] b, int i) => Encoding.UTF8.GetString(b, i, 27));
@@ -222,7 +215,7 @@ private void SpanCollectionPage()
222215

223216
for (var i = 0; i < 5; i++)
224217
{
225-
this.SpanPageID(32 + (i * 4), "DataPageList #" + i, false, true);
218+
this.SpanPageID(PAGE_HEADER_SIZE + (i * 4), "DataPageList #" + i, false, true);
226219
}
227220

228221
var h = 96;
@@ -253,7 +246,7 @@ private void SpanCollectionPage()
253246
}
254247
}
255248

256-
#region SpanItems Method Helpers
249+
#region SpanItems Helper Methods
257250

258251
private int SpanItem<T>(int index, int span, string href, string caption, Func<byte[], int, T> convert)
259252
{
@@ -390,105 +383,120 @@ private void RenderRules()
390383
{
391384
_writer.AppendLine("<div class='rules'>");
392385

393-
for (var i = 0; i < _items.Count; i++)
386+
for (var i = 0; i < _items.Count; i += BLOCKS_PER_LINE)
394387
{
395-
var item = _items[i];
396-
397-
if (i % 32 == 0)
398-
{
399-
_writer.AppendLine($"<div>{i}</div>");
400-
}
388+
_writer.AppendLine($"<div>{i}</div>");
401389
}
402390

403391
_writer.AppendLine("</div>");
404392
}
405393

406394
private void RenderBlocks()
407395
{
408-
var span = 32;
396+
var blocksLeft = BLOCKS_PER_LINE;
409397

410398
_writer.AppendLine("<div class='blocks'>");
411399
_writer.AppendLine("<div class='line'>");
412400

413401
for (var i = 0; i < _items.Count; i++)
414402
{
415403
var item = _items[i];
404+
blocksLeft = this.RenderItem(item, blocksLeft);
416405

417-
span -= (item.Span + 1);
406+
i += item.Span;
407+
}
418408

419-
var renderText = this.RenderItem(item, span);
409+
_writer.AppendLine("</div>");
410+
_writer.AppendLine("</div>");
411+
}
420412

421-
if (span <= 0)
422-
{
423-
_writer.AppendLine("</div><div class='line'>");
413+
private int RenderItem(PageItem item, int blocksLeftInLine)
414+
{
415+
var blocksToPrint = 1 + item.Span;
416+
var textToPrint = string.Empty;
417+
var textLeft = item.Text ?? item.Value.ToString();
424418

425-
if (span < 0)
426-
{
427-
var overflow = new PageItem { Color = item.Color, Span = Math.Abs(span + 1), Text = "&nbsp;" };
419+
while (blocksToPrint > 0)
420+
{
421+
var overflow = false;
428422

429-
if (renderText == false) overflow.Text = item.Text;
423+
blocksLeftInLine--;
424+
blocksToPrint--;
430425

431-
this.RenderItem(overflow, 0);
432-
}
426+
_writer.Append($"<a title='{item.Index}'");
433427

434-
span = 32 + span;
435-
}
428+
if (!string.IsNullOrEmpty(item.Href))
429+
_writer.Append($" href='{item.Href}'");
436430

437-
i += item.Span;
438-
}
431+
if (!string.IsNullOrEmpty(item.Target))
432+
_writer.Append($" target='{item.Target}'");
439433

440-
_writer.AppendLine("</div>");
441-
_writer.AppendLine("</div>");
442-
}
434+
if (item.Color >= 0)
435+
_writer.Append($" class='c{item.Color}'");
443436

444-
private bool RenderItem(PageItem item, int span)
445-
{
446-
var renderText = true;
437+
if (blocksToPrint > 0)
438+
{
439+
var span = blocksToPrint;
440+
if (blocksToPrint > blocksLeftInLine)
441+
{
442+
overflow = true;
443+
span = blocksLeftInLine;
447444

448-
_writer.Append($"<a title='{item.Index}'");
445+
textToPrint = SliceText(ref textLeft, blocksLeftInLine, blocksToPrint);
446+
}
449447

450-
if (!string.IsNullOrEmpty(item.Href))
451-
{
452-
_writer.Append($" href='{item.Href}'");
453-
}
448+
_writer.Append($" style='min-width: {BLOCK_WIDTH * (span + 1) + (span * 2)}px'");
454449

455-
if (!string.IsNullOrEmpty(item.Target))
456-
{
457-
_writer.Append($" target='{item.Target}'");
458-
}
450+
blocksLeftInLine -= span;
451+
blocksToPrint -= span;
452+
}
459453

460-
if (item.Color >= 0)
461-
{
462-
_writer.Append($" class='c{item.Color}'");
463-
}
454+
if (!string.IsNullOrEmpty(item.Caption))
455+
_writer.Append($" st='{item.Caption}'");
464456

465-
if (item.Span > 0)
466-
{
467-
var s = item.Span + (span < 0 ? span : 0);
457+
if (!string.IsNullOrEmpty(item.Id))
458+
_writer.Append($" id='{item.Id}'");
468459

469-
if (s < item.Span && Math.Abs(span) > s)
460+
_writer.Append(">");
461+
462+
if (overflow)
470463
{
471-
renderText = false;
464+
_writer.Append(textToPrint);
465+
_writer.Append(" &#8594;"); // right arrow '→'
472466
}
467+
else if (textLeft != string.Empty)
468+
_writer.Append(textLeft);
469+
else
470+
_writer.Append("&#8594;");
473471

474-
_writer.Append($" style='min-width: {BLOCK_WIDTH * (s + 1) + (s * 2)}px'");
475-
}
472+
_writer.Append("</a>");
476473

477-
if (!string.IsNullOrEmpty(item.Caption))
478-
{
479-
_writer.Append($" st='{item.Caption}'");
474+
if (blocksLeftInLine == 0)
475+
{
476+
// Line break
477+
_writer.AppendLine("</div><div class='line'>");
478+
blocksLeftInLine = BLOCKS_PER_LINE;
479+
}
480480
}
481481

482-
if (!string.IsNullOrEmpty(item.Id))
482+
return blocksLeftInLine;
483+
}
484+
485+
private string SliceText(ref string text, int blocksLeftInLine, int blocksToPrint)
486+
{
487+
var textToPrint = string.Empty;
488+
489+
if (blocksLeftInLine > 1 && (blocksLeftInLine > blocksToPrint - blocksLeftInLine || blocksLeftInLine > 30))
483490
{
484-
_writer.Append($" id='{item.Id}'");
491+
var len = 3 * blocksLeftInLine;
492+
if (len >= text.Length)
493+
textToPrint = text;
494+
else
495+
textToPrint = text.Substring(0, len);
485496
}
486497

487-
_writer.Append(">");
488-
_writer.Append(renderText ? (item.Text ?? item.Value.ToString()) : "&#8594;");
489-
_writer.Append("</a>");
490-
491-
return renderText;
498+
text = text.Substring(textToPrint.Length);
499+
return textToPrint;
492500
}
493501

494502
private void RenderFooter()

0 commit comments

Comments
 (0)