Skip to content

Commit d5c13d8

Browse files
committed
Markdown rendering works now
1 parent b98fd26 commit d5c13d8

File tree

5 files changed

+60
-66
lines changed

5 files changed

+60
-66
lines changed

tools/apput/src/Common/Log.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@ static void WriteLineStderr (ConsoleColor color, string message)
3939

4040
static void Write (string message)
4141
{
42-
Console.Write (message);
42+
WriteStderr (message);
4343
}
4444

4545
static void Write (ConsoleColor color, string message)
4646
{
4747
ConsoleColor oldFG = Console.ForegroundColor;
4848
Console.ForegroundColor = color;
49-
Write (message);
49+
WriteStderr (message);
5050
Console.ForegroundColor = oldFG;
5151
}
5252

5353
static void WriteLine (string message)
5454
{
55-
Console.WriteLine (message);
55+
WriteLineStderr (message);
5656
}
5757

5858
static void WriteLine (ConsoleColor color, string message)
5959
{
6060
ConsoleColor oldFG = Console.ForegroundColor;
6161
Console.ForegroundColor = color;
62-
WriteLine (message);
62+
WriteLineStderr (message);
6363
Console.ForegroundColor = oldFG;
6464
}
6565

@@ -121,7 +121,7 @@ public static void Info (string tag, string message)
121121
Write (InfoColor, $"{tag}: ");
122122
}
123123

124-
WriteLine (InfoColor,message);
124+
WriteLineStderr (InfoColor,message);
125125
}
126126

127127
public static void Debug (string message = "")
@@ -144,7 +144,7 @@ public static void Debug (string tag, string message)
144144
Write (DebugColor, $"{tag}: ");
145145
}
146146

147-
WriteLine (message);
147+
WriteLineStderr (message);
148148
}
149149

150150
public static void Debug (string message, Exception ex)

tools/apput/src/Markdown/MarkdownDocument.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,27 @@ void AppendText (string text, MarkdownTextStyle style = MarkdownTextStyle.Plain,
8383
return;
8484
}
8585

86+
var textToAppend = new StringBuilder (text);
87+
if (style != MarkdownTextStyle.Plain) {
88+
if (style.HasFlag (MarkdownTextStyle.Monospace)) {
89+
textToAppend.Append ('`');
90+
textToAppend.Insert (0, '`');
91+
}
92+
93+
if (style.HasFlag (MarkdownTextStyle.Italic)) {
94+
textToAppend.Append ('_');
95+
textToAppend.Insert (0, '_');
96+
}
97+
98+
if (style.HasFlag (MarkdownTextStyle.Bold)) {
99+
textToAppend.Append ('*');
100+
textToAppend.Insert (0, '*');
101+
}
102+
}
103+
86104
// TODO: implement breaking the line at the last whitespace character before maximum line width.
87105
// Indent is included in calculations.
88-
doc.Append (text);
106+
doc.Append (textToAppend);
89107
}
90108

91109
public MarkdownDocument BeginList ()
@@ -122,6 +140,14 @@ public MarkdownDocument AddListItem (string? text = null, MarkdownTextStyle styl
122140
return this;
123141
}
124142

143+
public MarkdownDocument AddLabeledListItem (string label, string text, MarkdownTextStyle textStyle = MarkdownTextStyle.Plain, bool appendLine = true)
144+
{
145+
StartListItem ($"{label}:", MarkdownTextStyle.Bold);
146+
AppendText ($" {text}", textStyle, addIndent: false);
147+
EndListItem (appendLine);
148+
return this;
149+
}
150+
125151
public MarkdownDocument EndList ()
126152
{
127153
RestorePreviousIndent ();

tools/apput/src/Reporter.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using System.Reflection;
33

4+
using Microsoft.PowerShell.MarkdownRender;
5+
46
namespace ApplicationUtility;
57

68
class Reporter
79
{
8-
// TODO: add support for specifying to the renderer whether output goes to console
9-
// add support for optionally turning off color
10-
// add support for writing report to file
1110
public static void Report (IAspect aspect, bool plainTextRendering)
1211
{
1312
Type aspectType = aspect.GetType ();
@@ -36,12 +35,21 @@ public static void Report (IAspect aspect, bool plainTextRendering)
3635

3736
reporter.Report ();
3837
} finally {
39-
// MarkdownPresenter presenter = reportDoc.Render (toConsole: true, useColor: true, renderPlainText: plainTextRendering);
40-
// if (presenter.RendersToString) {
41-
// Console.WriteLine (presenter.AsString ());
42-
// }
38+
WriteReport (reportDoc, plainTextRendering);
39+
}
40+
}
41+
42+
static void WriteReport (MarkdownDocument reportDoc, bool plainTextRendering)
43+
{
44+
if (plainTextRendering || Console.IsOutputRedirected) {
4345
Console.WriteLine (reportDoc.Text);
46+
return;
4447
}
48+
49+
// TODO: this probably won't work with the old `cmd` terminal on Windows. Find out how to detect it.
50+
var options = new PSMarkdownOptionInfo ();
51+
var info = MarkdownConverter.Convert (reportDoc.Text, MarkdownConversionType.VT100, options);
52+
Console.WriteLine (info.VT100EncodedString);
4553
}
4654

4755
static IReporter? CreateSpecificReporter (Type aspectType, IAspect aspect, MarkdownDocument reportDoc)

tools/apput/src/Reporters/ApplicationPackageReporter.cs

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Xamarin.Android.Tools;
2-
31
namespace ApplicationUtility;
42

53
[AspectReporter (typeof (PackageAPK))]
@@ -20,108 +18,69 @@ public ApplicationPackageReporter (ApplicationPackage package, MarkdownDocument
2018

2119
protected override void DoReport ()
2220
{
23-
WriteAspectDesc (package.PackageFormat);
2421
AddAspectDesc (package.PackageFormat);
2522

26-
WriteSubsectionBanner ("Generic Android application information");
2723
AddSection ("Generic Android application information");
28-
29-
WriteNativeArch (package.Architectures);
3024
AddNativeArchDesc (package.Architectures);
31-
32-
WriteYesNo ("Valid Android package", package.ValidAndroidPackage);
3325
AddYesNo ("Valid Android package", package.ValidAndroidPackage);
34-
35-
WriteItem ("Package name", ValueOrNone (package.PackageName));
3626
AddLabeledItem ("Package name", ValueOrNone (package.PackageName));
37-
38-
WriteItem ("Main activity", ValueOrNone (package.MainActivity));
3927
AddLabeledItem ("Main activity", ValueOrNone (package.MainActivity));
40-
41-
WriteItem ("Minimum SDK version", ValueOrNone (package.MinSdkVersion));
4228
AddLabeledItem ("Minimum SDK version", ValueOrNone (package.MinSdkVersion));
43-
44-
WriteItem ("Target SDK version", ValueOrNone (package.TargetSdkVersion));
4529
AddLabeledItem ("Target SDK version", ValueOrNone (package.TargetSdkVersion));
46-
47-
WriteYesNo ("Signed", package.Signed);
4830
AddYesNo ("Signed", package.Signed);
49-
50-
WriteYesNo ("Debuggable", package.Debuggable);
5131
AddYesNo ("Debuggable", package.Debuggable).AddNewline ();
5232

5333
if (package.Permissions == null || package.Permissions.Count == 0) {
5434
AddText ("No permissions specified");
55-
WriteItem ("Permissions", "none");
5635
} else {
5736
AddText ($"Application requests the following {GetCountable (Countable.Permission, package.Permissions.Count)}:");
58-
WriteLine (LabelColor, "Permissions:");
5937

6038
ReportDoc.BeginList ();
6139
foreach (string permission in package.Permissions) {
6240
ReportDoc.AddListItem ($"{permission}", MarkdownTextStyle.Monospace);
63-
64-
Write (LabelColor, " * ");
65-
WriteLine (ValidValueColor, permission);
6641
}
6742
ReportDoc.EndList ();
6843
}
6944

7045
AddSection ("Shared libraries", 2);
71-
7246
if (package.SharedLibraries == null || package.SharedLibraries.Count == 0) {
7347
// Very unlikely...
74-
WriteItem ("Shared libraries", "none");
7548
AddText ("No shared libraries found in the package");
7649
} else {
7750
AddText ($"Application contains the following {GetCountable (Countable.SharedLibrary, package.SharedLibraries.Count)}:");
78-
WriteLine (LabelColor, "Shared libraries:");
7951

8052
ReportDoc.BeginList ();
8153
foreach (SharedLibrary lib in package.SharedLibraries) {
8254
ReportDoc.StartListItem ($"{lib.Name}", MarkdownTextStyle.Monospace);
55+
56+
// Markdown renderer has a bug where it won't render the first item of the sub-list
57+
// properly if the item line ends with a formatting character (or whitespace)
58+
AddText (": ", addIndent: false);
8359
ReportDoc.BeginList ()
84-
.AddListItem ($"Alignment: {lib.Alignment}")
85-
.AddListItem ($"Debug info: {YesNo (lib.HasDebugInfo)}")
86-
.AddListItem ($"Size: {lib.Size}", appendLine: false)
60+
.AddLabeledListItem ("Alignment", $"{lib.Alignment}")
61+
.AddLabeledListItem ("Debug info", $"{YesNo (lib.HasDebugInfo)}")
62+
.AddLabeledListItem ("Size", $"{lib.Size}", appendLine: false)
8763
.EndList ()
8864
.EndListItem ();
89-
90-
Write (LabelColor, " * ");
91-
WriteLine (ValidValueColor, $"{lib.Name}");
92-
WriteLine (LabelColor, $" * Alignment: {lib.Alignment}");
93-
WriteLine (LabelColor, $" * Debug info: {YesNo (lib.HasDebugInfo)}");
94-
WriteLine (LabelColor, $" * Size: {lib.Size}");
9565
}
9666
ReportDoc.AddNewline ();
9767
ReportDoc.EndList ();
9868
}
9969

100-
WriteSubsectionBanner (".NET for Android application information");
10170
AddSection (".NET for Android application information", 1);
102-
103-
WriteItem ("Runtime", package.Runtime.ToString ());
10471
AddLabeledItem ("Runtime", package.Runtime.ToString ());
10572

10673
AddSection ("Assembly stores", 2);
107-
10874
if (package.AssemblyStores == null || package.AssemblyStores.Count == 0) {
109-
WriteItem ("Assembly stores", "none");
11075
AddText ("No assembly stores found");
11176
} else {
11277
AddText ($"Application contains the following {GetCountable (Countable.AssemblyStore, package.AssemblyStores.Count)}:");
113-
WriteLine (LabelColor, "Assembly stores");
11478

11579
ReportDoc.BeginList ();
11680
foreach (AssemblyStore store in package.AssemblyStores) {
11781
ReportDoc.StartListItem ($"{store.Architecture}", MarkdownTextStyle.Monospace);
11882
AddListItemText ($" ({store.NumberOfAssemblies} {GetCountable (Countable.Assembly, store.NumberOfAssemblies)})");
11983
ReportDoc.EndListItem ();
120-
121-
var color = store.Architecture == AndroidTargetArch.None ? InvalidValueColor : ValidValueColor;
122-
123-
Write (LabelColor, " * ");
124-
WriteLine (color, $"{store.Architecture} ({store.NumberOfAssemblies} {GetCountable (Countable.Assembly, store.NumberOfAssemblies)})");
12584
}
12685
ReportDoc.EndList ();
12786
}

tools/apput/src/Reporters/BaseReporter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ protected MarkdownDocument AddLabeledItem (string label, string text, bool appen
6666
.AddText ($" {text}");
6767

6868
if (appendNewline) {
69-
return ReportDoc.AddNewline ();
69+
// in Markdown, a forced line break is two or more spaces
70+
return ReportDoc.AddText (" ").AddNewline ();
7071
}
7172

7273
return ReportDoc;
@@ -155,14 +156,14 @@ protected void WriteLabel (string label)
155156
Write (LabelColor, $"{label}: ");
156157
}
157158

158-
protected MarkdownDocument AddText (string text, MarkdownTextStyle style = MarkdownTextStyle.Plain)
159+
protected MarkdownDocument AddText (string text, MarkdownTextStyle style = MarkdownTextStyle.Plain, bool addIndent = true)
159160
{
160-
return ReportDoc.AddText (text, style);
161+
return ReportDoc.AddText (text, style, addIndent);
161162
}
162163

163164
public MarkdownDocument AddListItemText (string text, MarkdownTextStyle style = MarkdownTextStyle.Plain)
164165
{
165-
return ReportDoc.AddText (text, style, addIndent: false);
166+
return AddText (text, style, addIndent: false);
166167
}
167168

168169
protected void WriteItem (string label, string value)

0 commit comments

Comments
 (0)