Skip to content

Commit fffe9fc

Browse files
committed
Got powershell cmdlet working
1 parent 26dca83 commit fffe9fc

File tree

24 files changed

+551
-306
lines changed

24 files changed

+551
-306
lines changed

src/WinPrint.Console/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Program {
2323
private static ParserResult<Options> result;
2424

2525
static void Main(string[] args) {
26-
ServiceLocator.Current.TelemetryService.Start();
27-
ServiceLocator.Current.LogService.Start();
26+
ServiceLocator.Current.TelemetryService.Start(AppDomain.CurrentDomain.FriendlyName);
27+
ServiceLocator.Current.LogService.Start(AppDomain.CurrentDomain.FriendlyName);
2828

2929
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
3030
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
@@ -305,7 +305,7 @@ private void PropertyChangedEventHandler(object o, PropertyChangedEventArgs e) {
305305

306306
case "Reflowing":
307307
if (print.SheetViewModel.Reflowing)
308-
Log.Information("Formatting as {t}.", print.SheetViewModel.Type);
308+
Log.Information("Formatting as {t}.", print.SheetViewModel.ContentEngine.GetContentType());
309309
else if (ModelLocator.Current.Options.Verbose)
310310
Log.Information("Formating complete.");
311311
break;

src/WinPrint.Console/WinPrint.Console.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
<AssemblyName>winprint</AssemblyName>
1010
<StartupObject>WinPrint.Console.Program</StartupObject>
1111

12-
<Version>2.0.0.3019</Version>
12+
<Version>2.0.0.3499</Version>
1313
<Company>Kindel Systems</Company>
1414
<Product>winprint</Product>
1515
<Authors>Charlie Kindel</Authors>
1616
<Description>winprint Console App</Description>
1717
<Copyright>Copyright Kindel Systems, LLC</Copyright>
18-
<PackageReleaseNotes>No release notes.</PackageReleaseNotes>
1918
<PackageProjectUrl>https://github.com/tig/winprint</PackageProjectUrl>
2019
</PropertyGroup>
2120

src/WinPrint.Core/ContentTypeEngines/CodeCte.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ internal struct HtmlLine {
2525
/// </summary>
2626
// TOOD: Color code c# kewoards https://www.c-sharpcorner.com/UploadFile/kirtan007/syntax-highlighting-in-richtextbox-using-C-Sharp/
2727
public class CodeCte : ContentTypeEngineBase, IDisposable {
28+
private static readonly string _contentType = "text/sourcecode";
29+
/// <summary>
30+
/// ContentType identifier (shorthand for class name).
31+
/// </summary>
32+
public override string GetContentType() {
33+
return _contentType;
34+
}
35+
2836
public static CodeCte Create() {
37+
2938
var engine = new CodeCte();
3039
engine.CopyPropertiesFrom(ModelLocator.Current.Settings.TextContentTypeEngineSettings);
3140
return engine;
3241
}
3342

34-
public static new string ContentType = "Source code";
3543
public CodeCte() {
3644
}
3745

src/WinPrint.Core/ContentTypeEngines/ContentTypeEngineBase.cs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Diagnostics;
45
using System.Drawing;
56
using System.Drawing.Text;
67
using System.IO;
78
using System.Runtime.CompilerServices;
89
using System.Threading.Tasks;
10+
using Serilog;
911
using WinPrint.Core.Models;
1012
using WinPrint.Core.Services;
1113

@@ -33,7 +35,10 @@ public abstract class ContentTypeEngineBase : ModelBase, INotifyPropertyChanged
3335
/// <summary>
3436
/// ContentType identifier (shorthand for class name).
3537
/// </summary>
36-
public static string ContentType = "base";
38+
public virtual string GetContentType() {
39+
return _contentType;
40+
}
41+
private static readonly string _contentType = "base";
3742

3843
/// <summary>
3944
/// Calculated page size. Set by Sheet view model.
@@ -54,6 +59,15 @@ public abstract class ContentTypeEngineBase : ModelBase, INotifyPropertyChanged
5459
//}
5560

5661
internal string filePath = null;
62+
63+
/// <summary>
64+
/// The contents of the file to be printed.
65+
/// </summary>
66+
public string Document { get => document; set {
67+
//LogService.TraceMessage($"Document is {document.Length} chars.");
68+
SetField(ref document, value);
69+
}
70+
}
5771
internal string document = null;
5872

5973
internal StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic) {
@@ -67,23 +81,16 @@ public abstract class ContentTypeEngineBase : ModelBase, INotifyPropertyChanged
6781

6882

6983
/// <summary>
70-
/// Loads the file specified into memeory. (holds in document property).
84+
/// Loads the file specified into Document property.
7185
/// </summary>
7286
/// <param name="filePath"></param>
7387
/// <returns>True if file was read. False if the file was empty or failed to read.</returns>
7488
public async virtual Task<bool> LoadAsync(string filePath) {
7589
LogService.TraceMessage();
7690
this.filePath = filePath;
7791
using StreamReader streamToPrint = new StreamReader(filePath);
78-
//try {
79-
document = await streamToPrint.ReadToEndAsync();
80-
LogService.TraceMessage($"document is {document.Length} chars.");
81-
//}
82-
//catch (Exception e) {
83-
// LogService.TraceMessage($"Exception {e.Message}");
84-
// return false;
85-
//}
86-
return !String.IsNullOrEmpty(document);
92+
Document = await streamToPrint.ReadToEndAsync();
93+
return !String.IsNullOrEmpty(Document);
8794
}
8895

8996
/// <summary>
@@ -93,8 +100,8 @@ public async virtual Task<bool> LoadAsync(string filePath) {
93100
/// <returns>Number of sheets.</returns>
94101
public virtual async Task<int> RenderAsync(System.Drawing.Printing.PrinterResolution printerResolution, EventHandler<string> reflowProgress) {
95102
LogService.TraceMessage();
96-
if (document == null)
97-
throw new ArgumentNullException("document can't be null for Render");
103+
if (Document == null)
104+
throw new ArgumentNullException("Document can't be null for Render");
98105
return await Task.FromResult(0);
99106
}
100107

@@ -105,5 +112,59 @@ public virtual async Task<int> RenderAsync(System.Drawing.Printing.PrinterResolu
105112
/// <param name="pageNum">Page number to print</param>
106113
public abstract void PaintPage(Graphics g, int pageNum);
107114

115+
public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string filePath = null, string contentType = null) {
116+
ContentTypeEngineBase cte = null;
117+
118+
// If no contenttype was provied, but path was, determine from path.
119+
if (string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(filePath)) {
120+
var ext = Path.GetExtension(filePath).ToLower();
121+
// Use file assocations to figure it out
122+
if (!ModelLocator.Current.Associations.FilesAssociations.TryGetValue("*" + ext, out contentType))
123+
contentType = "text/plain"; // default
124+
}
125+
Debug.Assert(!string.IsNullOrEmpty(contentType));
126+
127+
switch (contentType) {
128+
case "text/html":
129+
cte = HtmlCte.Create();
130+
break;
131+
132+
case "text/plain":
133+
cte = TextCte.Create();
134+
break;
135+
136+
// TODO: Figure out if we really want to use the sourcecode CTE.
137+
case "sourcecode":
138+
cte = CodeCte.Create();
139+
((CodeCte)cte).Language = contentType;
140+
break;
141+
142+
default:
143+
// Not text or html. Is it a language?
144+
if (((List<Langauge>)ModelLocator.Current.Associations.Languages).Exists(lang => lang.Id == contentType)) {
145+
// It's a language. Verify node.js and Prism are installed
146+
if (await ServiceLocator.Current.NodeService.IsInstalled()) {
147+
// contentType == Language
148+
cte = PrismCte.Create();
149+
((PrismCte)cte).Language = contentType;
150+
}
151+
else {
152+
Log.Information("Node.js must be installed for Prism-based ({lang}) syntax highlighting. Using {def} instead.", contentType, "text/plain");
153+
contentType = "text/plain";
154+
cte = TextCte.Create();
155+
}
156+
}
157+
else {
158+
// No language mapping found, just use contentType as the language
159+
// TODO: Do more error checking here
160+
cte = PrismCte.Create();
161+
((PrismCte)cte).Language = contentType;
162+
}
163+
break;
164+
}
165+
166+
return cte;
167+
}
168+
108169
}
109170
}

src/WinPrint.Core/ContentTypeEngines/HtmlCte.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ namespace WinPrint.Core.ContentTypeEngines {
1616
/// Implements generic HTML file type support.
1717
/// </summary>
1818
public class HtmlCte : ContentTypeEngineBase, IDisposable {
19-
public static new string ContentType = "text/html";
19+
private static readonly string _contentType = "text/html";
20+
/// <summary>
21+
/// ContentType identifier (shorthand for class name).
22+
/// </summary>
23+
public override string GetContentType() {
24+
return _contentType;
25+
}
2026

2127
public static HtmlCte Create() {
2228
var content = new HtmlCte();

src/WinPrint.Core/ContentTypeEngines/PrismCte.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313

1414
namespace WinPrint.Core.ContentTypeEngines {
1515
public class PrismCte : HtmlCte {
16-
public static new string ContentType = "text/code";
17-
16+
private static readonly string _contentType = "text/code";
17+
/// <summary>
18+
/// ContentType identifier (shorthand for class name).
19+
/// </summary>
20+
public override string GetContentType() {
21+
return _contentType;
22+
}
1823
public static new PrismCte Create() {
1924
var content = new PrismCte();
2025
content.CopyPropertiesFrom(ModelLocator.Current.Settings.PrismContentTypeEngineSettings);

src/WinPrint.Core/ContentTypeEngines/TextCTE.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ internal struct Line {
2626
/// </summary>
2727
// TOOD: Color code c# kewoards https://www.c-sharpcorner.com/UploadFile/kirtan007/syntax-highlighting-in-richtextbox-using-C-Sharp/
2828
public class TextCte : ContentTypeEngineBase, IDisposable {
29+
private static readonly string _contentType = "text/plain";
30+
/// <summary>
31+
/// ContentType identifier (shorthand for class name).
32+
/// </summary>
33+
public override string GetContentType() {
34+
return _contentType;
35+
}
36+
2937
public static TextCte Create() {
3038
var engine = new TextCte();
3139
engine.CopyPropertiesFrom(ModelLocator.Current.Settings.TextContentTypeEngineSettings);
3240
return engine;
3341
}
3442

35-
public static new string ContentType = "text/plain";
3643
public TextCte() {
3744
// StringFormat to use throughout
3845

src/WinPrint.Core/Helpers/Macros.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ sealed internal class Macros {
1515
public string FilePath { get { return string.IsNullOrEmpty(svm.File) ? "" : Path.GetDirectoryName(FullyQualifiedPath); } }
1616
public string FullyQualifiedPath { get { return string.IsNullOrEmpty(svm.File) ? "" : Path.GetFullPath(svm.File); } }
1717
public static DateTime DatePrinted { get { return DateTime.Now; } }
18-
public DateTime DateRevised { get { return string.IsNullOrEmpty(svm.File) ? DateTime.Now : File.GetLastWriteTime(svm.File); } }
19-
public string FileType { get { return string.IsNullOrEmpty(svm.File) ? "" : svm.Type; } }
18+
public DateTime DateRevised { get { return string.IsNullOrEmpty(svm.File) ? DateTime.Now : File.GetLastWriteTime(svm.File); } }
19+
public string FileType { get { return svm.ContentEngine == null ? "" : svm.ContentEngine.GetContentType(); } }
2020

2121
public int Page { get; set; }
2222

src/WinPrint.Core/Print.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public async Task<int> CountSheets(int fromSheet = 1, int toSheet = 0) {
9292

9393
ServiceLocator.Current.TelemetryService.TrackEvent("Count Sheets",
9494
properties: new Dictionary<string, string> {
95-
["type"]= SheetViewModel.Type,
95+
["type"]= SheetViewModel.ContentEngine.GetContentType(),
9696
["printer"] = PrintDocument.PrinterSettings.PrinterName,
9797
["fromSheet"] = fromSheet.ToString(),
9898
["toSheet"] = toSheet.ToString(),
9999
},
100-
metrics: new Dictionary<string, double> {["sheetsPrinted"] = SheetViewModel.NumSheets});
100+
metrics: new Dictionary<string, double> {["sheetsPrinted"] = SheetViewModel.NumSheets});;
101101
return SheetViewModel.NumSheets;
102102
}
103103

@@ -114,7 +114,7 @@ public async Task<int> DoPrint() {
114114

115115
ServiceLocator.Current.TelemetryService.TrackEvent("Print Complete",
116116
properties: new Dictionary<string, string> {
117-
["type"] = SheetViewModel.Type,
117+
["type"] = SheetViewModel.ContentEngine.GetContentType(),
118118
["printer"] = PrintDocument.PrinterSettings.PrinterName,
119119
["fromSheet"] = PrintDocument.PrinterSettings.FromPage.ToString(),
120120
["toSheet"] = PrintDocument.PrinterSettings.ToPage.ToString(),

src/WinPrint.Core/Services/LogService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class LogService {
2020
public LoggingLevelSwitch ConsoleLevelSwitch { get; set; } = new LoggingLevelSwitch();
2121
public LoggingLevelSwitch DebugLevelSwitch { get; set; } = new LoggingLevelSwitch();
2222

23-
public void Start() {
23+
public void Start(string appName) {
2424
MasterLevelSwitch.MinimumLevel = LogEventLevel.Verbose;
2525
DebugLevelSwitch.MinimumLevel = LogEventLevel.Debug;
2626

@@ -43,7 +43,7 @@ public void Start() {
4343
////.WriteTo.ApplicationInsights(config, new CustomConverter(), restrictedToMinimumLevel: LogEventLevel.Information)
4444
.CreateLogger();
4545

46-
Log.Debug("--------- {app} {v} ---------", AppDomain.CurrentDomain.FriendlyName, productVersion);
46+
Log.Debug("--------- {app} {v} ---------", appName, productVersion);
4747
if (ServiceLocator.Current.TelemetryService.TelemetryEnabled) {
4848
string msg = string.IsNullOrEmpty(TelemetryService.Key) ? "However, telemetry key is missing so no telemetry will be tracked." : "";
4949
Log.Debug($"Telemetry is enabled. {msg}");

0 commit comments

Comments
 (0)