Skip to content

Commit 6ded7fc

Browse files
committed
Rendering is a mess, need a different way to do it. TBC tomorrow
1 parent 3255915 commit 6ded7fc

File tree

9 files changed

+158
-45
lines changed

9 files changed

+158
-45
lines changed

tools/apput/src/Markdown/MarkdownDocument.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ void RenderSafe (MarkdownPresenter presenter, MarkdownElement element, bool plai
5252
}
5353
}
5454

55-
void RenderChildren (MarkdownPresenter presenter, MarkdownContainerElement element, bool plain)
55+
void RenderChildren (MarkdownPresenter presenter, MarkdownContainerElement element, bool plain,
56+
Action<MarkdownElement>? beforeElement = null, Action<MarkdownElement>? afterElement = null)
5657
{
5758
if (element.Children == null || element.Children.Count == 0) {
5859
return;
5960
}
6061

6162
foreach (MarkdownElement child in element.Children) {
63+
beforeElement?.Invoke (child);
6264
RenderSafe (presenter, child, plain);
65+
afterElement?.Invoke (child);
6366
}
6467
}
6568

@@ -71,11 +74,32 @@ void Render (MarkdownPresenter presenter, MarkdownElement element, bool plain)
7174
Render (presenter, section, plain);
7275
} else if (element is MarkdownParagraph para) {
7376
Render (presenter, para, plain);
77+
} else if (element is MarkdownList list) {
78+
Render (presenter, list, plain);
7479
} else {
7580
throw new InvalidOperationException ($"Internal error: Markdown element {element.GetType ()} not supported when rendering.");
7681
}
7782
}
7883

84+
void Render (MarkdownPresenter presenter, MarkdownList list, bool plain)
85+
{
86+
presenter.AddNewLine ();
87+
RenderChildren (
88+
presenter,
89+
list,
90+
plain,
91+
beforeElement: (MarkdownElement element) => {
92+
if (element is MarkdownList) {
93+
return;
94+
}
95+
presenter.Append (" * ");
96+
},
97+
afterElement: (MarkdownElement _) => presenter.AddNewLine ()
98+
);
99+
presenter.AddNewLine ();
100+
presenter.AddNewLine ();
101+
}
102+
79103
void Render (MarkdownPresenter presenter, MarkdownParagraph para, bool plain)
80104
{
81105
RenderChildren (presenter, para, plain);
@@ -105,18 +129,19 @@ void Render (MarkdownPresenter presenter, MarkdownTextSpan textSpan, bool plain)
105129
}
106130

107131
RenderSpan (textSpan);
132+
if (textSpan.Fragments == null) {
133+
return;
134+
}
135+
108136
foreach (MarkdownTextSpan fragment in textSpan.Fragments) {
109137
RenderSpan (fragment);
110138
}
111139

112140
void RenderSpan (MarkdownTextSpan span)
113141
{
114-
MarkdownTextStyle style = (!plain && span.Bold) switch {
115-
true => MarkdownTextStyle.Bold,
116-
false => MarkdownTextStyle.Plain
117-
};
142+
string? text = span.RemoveTailWhitespace ? span.Text?.TrimEnd () : span.Text;
118143

119-
presenter.Append (span.Text, style);
144+
presenter.Append (span.Text, span.Style);
120145
}
121146
}
122147

tools/apput/src/Markdown/MarkdownElement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ namespace ApplicationUtility;
33
abstract class MarkdownElement
44
{
55
public string? Text { get; set; }
6+
public MarkdownContainerElement? Parent { get; protected set; }
67
}

tools/apput/src/Markdown/MarkdownList.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public MarkdownList (MarkdownListKind kind = MarkdownListKind.Bullet)
88
{
99
Kind = kind;
1010
}
11+
12+
public void Add (string text) => AddChild (new MarkdownTextSpan (text) { RemoveTailWhitespace = true });
1113
}

tools/apput/src/Markdown/MarkdownParagraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class MarkdownParagraph : MarkdownContainerElement
77
{
88
public override void AddChild (MarkdownElement element)
99
{
10-
if (element is MarkdownTextSpan) {
10+
if (element is MarkdownTextSpan || element is MarkdownList) {
1111
base.AddChild (element);
1212
return;
1313
}

tools/apput/src/Markdown/MarkdownPresenter.Console.cs

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ sealed class ColorState
1313
public ConsoleColor Background;
1414
public bool BoldEnabled;
1515
public bool ItalicEnabled;
16+
public bool MonospaceEnabled;
1617
}
1718

18-
const ConsoleColor Bold = ConsoleColor.White;
19-
const ConsoleColor Italic = ConsoleColor.Cyan;
20-
const ConsoleColor BoldItalicFg = ConsoleColor.Gray;
21-
const ConsoleColor BoldItalicBg = ConsoleColor.DarkRed;
19+
const ConsoleColor Normal = ConsoleColor.Gray;
20+
const ConsoleColor NormalBold = ConsoleColor.White;
21+
const ConsoleColor Italic = ConsoleColor.DarkCyan;
22+
const ConsoleColor ItalicBold = ConsoleColor.Cyan;
23+
const ConsoleColor Monospace = ConsoleColor.DarkGreen;
24+
const ConsoleColor MonospaceBold = ConsoleColor.Green;
2225

2326
readonly bool useColor;
2427

@@ -27,6 +30,9 @@ sealed class ColorState
2730
public ConsolePresenter (bool useColor)
2831
{
2932
this.useColor = useColor;
33+
if (useColor) {
34+
Console.ForegroundColor = Normal;
35+
}
3036
}
3137

3238
public override void Append (string? text) => Console.Write (text);
@@ -64,34 +70,25 @@ public override void StartBold (object? state)
6470

6571
var colors = state as ColorState;
6672
if (colors == null) {
67-
Console.ForegroundColor = Bold;
73+
Console.ForegroundColor = NormalBold;
6874
return;
6975
}
7076

7177
colors.BoldEnabled = true;
72-
if (colors.ItalicEnabled) {
73-
Console.ForegroundColor = BoldItalicFg;
74-
Console.BackgroundColor = BoldItalicBg;
75-
} else {
76-
Console.ForegroundColor = Bold;
77-
}
78+
UpdateColors (colors);
7879
}
7980

8081
public override void EndBold (object? state)
8182
{
8283
var colors = state as ColorState;
8384
if (colors == null) {
85+
Console.ForegroundColor = Normal;
8486
base.EndBold (state);
8587
return;
8688
}
8789

88-
if (colors.ItalicEnabled) {
89-
Console.ForegroundColor = Italic;
90-
Console.BackgroundColor = colors.Background;
91-
} else {
92-
Console.ForegroundColor = colors.Foreground;
93-
}
9490
colors.BoldEnabled = false;
91+
UpdateColors (colors);
9592
}
9693

9794
public override void StartItalic (object? state)
@@ -108,29 +105,59 @@ public override void StartItalic (object? state)
108105
}
109106

110107
colors.ItalicEnabled = true;
111-
if (colors.BoldEnabled) {
112-
Console.ForegroundColor = BoldItalicFg;
113-
Console.BackgroundColor = BoldItalicBg;
114-
} else {
115-
Console.ForegroundColor = Italic;
116-
}
108+
UpdateColors (colors);
117109
}
118110

119111
public override void EndItalic (object? state)
120112
{
121113
var colors = state as ColorState;
122114
if (colors == null) {
123-
base.EndBold (state);
115+
base.EndItalic (state);
116+
return;
117+
}
118+
119+
colors.ItalicEnabled = false;
120+
UpdateColors (colors);
121+
}
122+
123+
public override void StartMonospace (object? state)
124+
{
125+
if (!useColor) {
126+
base.StartMonospace (state);
124127
return;
125128
}
126129

127-
if (colors.BoldEnabled) {
128-
Console.ForegroundColor = Bold;
129-
Console.BackgroundColor = colors.Background;
130+
var colors = state as ColorState;
131+
if (colors == null) {
132+
Console.ForegroundColor = Monospace;
133+
return;
134+
}
135+
136+
colors.MonospaceEnabled = true;
137+
UpdateColors (colors);
138+
}
139+
140+
public override void EndMonospace (object? state)
141+
{
142+
var colors = state as ColorState;
143+
if (colors == null) {
144+
base.EndMonospace (state);
145+
return;
146+
}
147+
148+
colors.MonospaceEnabled = false;
149+
UpdateColors (colors);
150+
}
151+
152+
void UpdateColors (ColorState colors)
153+
{
154+
if (colors.MonospaceEnabled) {
155+
Console.ForegroundColor = colors.BoldEnabled ? MonospaceBold : Monospace;
156+
} else if (colors.ItalicEnabled) {
157+
Console.ForegroundColor = colors.BoldEnabled ? ItalicBold : Italic;
130158
} else {
131-
Console.ForegroundColor = colors.Foreground;
159+
Console.ForegroundColor = colors.BoldEnabled ? NormalBold : Normal;
132160
}
133-
colors.ItalicEnabled = false;
134161
}
135162
}
136163
}

tools/apput/src/Markdown/MarkdownPresenter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public MarkdownPresenter Append (char ch, MarkdownTextStyle style = MarkdownText
4343

4444
public MarkdownPresenter Append (string? text, MarkdownTextStyle style = MarkdownTextStyle.Plain)
4545
{
46+
if (String.IsNullOrEmpty (text)) {
47+
return this;
48+
}
49+
4650
object? state = AddStartMarkup (style);
4751
presenter.Append (text);
4852
AddEndMarkup (style, state);
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1+
using System;
12
using System.Collections.Generic;
23

34
namespace ApplicationUtility;
45

56
class MarkdownTextSpan : MarkdownElement
67
{
7-
List<MarkdownTextSpan> fragments = new ();
8+
readonly List<MarkdownTextSpan> fragments = new ();
9+
MarkdownTextStyle textStyle;
810

911
public List<MarkdownTextSpan> Fragments => fragments;
10-
public bool IsEmpty => fragments.Count == 0;
12+
public bool IsEmpty => fragments.Count == 0 && String.IsNullOrEmpty (Text);
13+
public MarkdownTextStyle Style => textStyle;
1114

12-
public bool Bold { get; set; }
15+
public bool Bold {
16+
get => HasStyle (MarkdownTextStyle.Bold);
17+
set => SetStyle (MarkdownTextStyle.Bold, value);
18+
}
19+
20+
public bool Italic {
21+
get => HasStyle (MarkdownTextStyle.Italic);
22+
set => SetStyle (MarkdownTextStyle.Italic, value);
23+
}
24+
25+
public bool Monospace {
26+
get => HasStyle (MarkdownTextStyle.Monospace);
27+
set => SetStyle (MarkdownTextStyle.Monospace, value);
28+
}
1329

14-
public MarkdownTextSpan (string text)
30+
public bool RemoveTailWhitespace { get; set; }
31+
32+
public MarkdownTextSpan (string text, MarkdownTextStyle style = MarkdownTextStyle.Plain)
1533
{
34+
Console.WriteLine ($"New span '{text}'; style: {style}");
1635
Text = text;
36+
textStyle = style;
1737
}
1838

1939
public void AddNewline ()
@@ -25,4 +45,15 @@ public void AddText (string text)
2545
{
2646
fragments.Add (new MarkdownTextSpan (text));
2747
}
48+
49+
bool HasStyle (MarkdownTextStyle style) => textStyle.HasFlag (style);
50+
51+
void SetStyle (MarkdownTextStyle style, bool enable)
52+
{
53+
if (enable) {
54+
textStyle |= style;
55+
} else {
56+
textStyle &= ~style;
57+
}
58+
}
2859
}

tools/apput/src/Reporters/ApplicationPackageReporter.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ protected override void DoReport ()
5959
AddText (para, "No permissions specified");
6060
WriteItem ("Permissions", "none");
6161
} else {
62-
// TODO: markdown list here
62+
AddText (para, $"Application requests the following permission{GetCountable (Countable.Permission, package.Permissions.Count)}:");
63+
var plist = new MarkdownList ();
64+
para.AddChild (plist);
65+
6366
WriteLine (LabelColor, "Permissions:");
6467

6568
foreach (string permission in package.Permissions) {
69+
plist.AddChild (new MarkdownTextSpan ($"{permission}", MarkdownTextStyle.Monospace));
70+
6671
Write (LabelColor, " * ");
6772
WriteLine (ValidValueColor, permission);
6873
}
@@ -82,10 +87,15 @@ protected override void DoReport ()
8287
WriteItem ("Assembly stores", "none");
8388
AddText (para, "No assembly stores found");
8489
} else {
85-
// TODO: markdown list here
90+
AddText (para, $"Application contains the following {GetCountable (Countable.AssemblyStore, package.AssemblyStores.Count)}:");
91+
var storeList = new MarkdownList ();
92+
para.AddChild (storeList);
8693
WriteLine (LabelColor, "Assembly stores");
8794

8895
foreach (AssemblyStore store in package.AssemblyStores) {
96+
var storeText = new MarkdownTextSpan ($"{store.Architecture}", MarkdownTextStyle.Monospace);
97+
storeText.AddText ($" ({store.NumberOfAssemblies} {GetCountable (Countable.Assembly, store.NumberOfAssemblies)})");
98+
storeList.AddChild (storeText);
8999
var color = store.Architecture == AndroidTargetArch.None ? InvalidValueColor : ValidValueColor;
90100

91101
Write (LabelColor, " * ");
@@ -101,10 +111,20 @@ protected override void DoReport ()
101111
WriteItem ("Shared libraries", "none");
102112
AddText (para, "No shared libraries found in the package");
103113
} else {
104-
// TODO: markdown list (or perhaps better, table?) here
114+
AddText (para, $"Application contains the following {GetCountable (Countable.SharedLibrary, package.SharedLibraries.Count)}:");
115+
var libList = new MarkdownList ();
116+
para.AddChild (libList);
117+
105118
WriteLine (LabelColor, "Shared libraries:");
106119

107120
foreach (SharedLibrary lib in package.SharedLibraries) {
121+
libList.AddChild (new MarkdownTextSpan ($"{lib.Name}", MarkdownTextStyle.Monospace));
122+
var libAttrList = new MarkdownList ();
123+
libList.AddChild (libAttrList);
124+
libAttrList.Add ($"Alignment: {lib.Alignment}");
125+
libAttrList.Add ($"Debug info: {YesNo (lib.HasDebugInfo)}");
126+
libAttrList.Add ($"Size: {lib.Size}");
127+
108128
Write (LabelColor, " * ");
109129
WriteLine (ValidValueColor, $"{lib.Name}");
110130
WriteLine (LabelColor, $" * Alignment: {lib.Alignment}");

tools/apput/src/Reporters/BaseReporter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ abstract class BaseReporter : IReporter
1616
protected enum Countable
1717
{
1818
Assembly,
19-
SharedLibrary
19+
AssemblyStore,
20+
Permission,
21+
SharedLibrary,
2022
}
2123

2224
const string NativeArchitectureLabel = "Native target architecture";
2325
const string NativeArchitecturesLabel = NativeArchitectureLabel + "s";
2426

2527
static readonly Dictionary<Countable, (string singular, string plural)> Countables = new () {
2628
{ Countable.Assembly, ("assembly", "assemblies") },
29+
{ Countable.AssemblyStore, ("assembly store", "assembly stores") },
30+
{ Countable.Permission, ("permission", "permissions") },
2731
{ Countable.SharedLibrary, ("library", "libraries") },
2832
};
2933

@@ -210,7 +214,6 @@ protected string GetCountable (Countable countable, ulong count)
210214

211215
// Somehow I doubt we'll have more than Int64.MaxValue items of any kind... :)
212216
protected string GetCountable (Countable countable, long count) => GetCountable (countable, (ulong)count);
213-
214217
protected string YesNo (bool yes) => yes ? "yes" : "no";
215218
protected string ValueOrNone (string? s) => String.IsNullOrEmpty (s) ? "none" : s;
216219
}

0 commit comments

Comments
 (0)