From e82da83a9f5c5a0a7eae90b43afc934a9082be83 Mon Sep 17 00:00:00 2001 From: jlewin Date: Tue, 2 Apr 2019 16:40:11 -0700 Subject: [PATCH] Add dedicated type for terminal lines --- .../TerminalWindow/TerminalLine.cs | 51 ++++++++++++++++++ .../TerminalWindow/TerminalLog.cs | 53 ++++++++++--------- .../TerminalWindow/TerminalWidget.cs | 17 +++--- .../TerminalWindow/TextScrollWidget.cs | 24 ++++----- .../SliceSettingsTests.cs | 2 +- 5 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 MatterControlLib/PrinterControls/TerminalWindow/TerminalLine.cs diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TerminalLine.cs b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLine.cs new file mode 100644 index 000000000..713d90f50 --- /dev/null +++ b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLine.cs @@ -0,0 +1,51 @@ +/* +Copyright (c) 2019, Lars Brubaker, John Lewin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +namespace MatterHackers.MatterControl +{ + public class TerminalLine + { + public TerminalLine(string line, MessageDirection direction) + { + this.Line = line; + this.Direction = direction; + } + + public string Line { get; } + + public MessageDirection Direction { get; } + + public enum MessageDirection + { + ToPrinter, + ToTerminal, + FromPrinter + } + } +} \ No newline at end of file diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs index f2b2db74e..906519783 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs @@ -29,7 +29,6 @@ either expressed or implied, of the FreeBSD Project. using System; using System.Collections.Generic; -using MatterHackers.Agg; using MatterHackers.Localizations; using MatterHackers.MatterControl.PrinterCommunication; @@ -59,13 +58,15 @@ namespace MatterHackers.MatterControl } } - public event EventHandler<(string line, bool output)> LineAdded; + public event EventHandler LineAdded; - public List<(string line, bool output)> PrinterLines { get; } = new List<(string line, bool output)>(); + public List PrinterLines { get; } = new List(); - private void OnLineAdded((string line, bool output) lineData) + private void OnLineAdded(TerminalLine terminalLine) { - LineAdded?.Invoke(this, lineData); + PrinterLines.Add(terminalLine); + + LineAdded?.Invoke(this, terminalLine); if (PrinterLines.Count > maxLinesToBuffer) { @@ -75,36 +76,35 @@ namespace MatterHackers.MatterControl private void Printer_LineReceived(object sender, string line) { - PrinterLines.Add((line, false)); - OnLineAdded((line, false)); + this.OnLineAdded( + new TerminalLine( + line, + TerminalLine.MessageDirection.FromPrinter)); } private void Printer_LineSent(object sender, string line) { - PrinterLines.Add((line, true)); - OnLineAdded((line, true)); + this.OnLineAdded( + new TerminalLine( + line, + TerminalLine.MessageDirection.ToPrinter)); } public void WriteLine(string line) { - this.WriteLine((line, true)); - } - - public void WriteLine((string line, bool output) lineData) - { - PrinterLines.Add(lineData); - OnLineAdded(lineData); + this.OnLineAdded( + new TerminalLine( + line, + TerminalLine.MessageDirection.ToTerminal)); } private void Instance_ConnectionFailed(object sender, EventArgs e) { - OnLineAdded((null, true)); - if (e is ConnectFailedEventArgs args) { string message; - switch(args.Reason) + switch (args.Reason) { case ConnectionFailure.AlreadyConnected: message = "You can only connect when not currently connected.".Localize(); @@ -118,17 +118,21 @@ namespace MatterHackers.MatterControl case ConnectionFailure.PortNotFound: message = "Port not found".Localize(); break; + case ConnectionFailure.PortUnavailable: + message = "Port not available".Localize(); + break; default: message = "Unknown Reason".Localize(); break; } - PrinterLines.Add(("Connection Failed".Localize() + ": " + message, true)); + WriteLine("Connection Failed".Localize() + ": " + message); } - StringEventArgs eventArgs = new StringEventArgs("Lost connection to printer."); - PrinterLines.Add((eventArgs.Data, true)); - OnLineAdded((eventArgs.Data, true)); + OnLineAdded( + new TerminalLine( + "Lost connection to printer.", + TerminalLine.MessageDirection.ToTerminal)); } public void Clear() @@ -138,7 +142,8 @@ namespace MatterHackers.MatterControl PrinterLines.Clear(); } - OnLineAdded((null, true)); + OnLineAdded( + new TerminalLine("", TerminalLine.MessageDirection.ToTerminal)); } public void Dispose() diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs b/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs index ac181b600..0efbaa02e 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs @@ -46,8 +46,6 @@ namespace MatterHackers.MatterControl private MHTextEditWidget manualCommandTextEdit; private TextScrollWidget textScrollWidget; private PrinterConfig printer; - private string writeFailedWaring = "WARNING: Write Failed!".Localize(); - private string cantAccessPath = "Can't access '{0}'.".Localize(); private List commandHistory = new List(); private int commandHistoryIndex = 0; @@ -110,8 +108,8 @@ namespace MatterHackers.MatterControl textScrollWidget.LineFilterFunction = lineData => { - var line = lineData.line; - var output = lineData.output; + var line = lineData.Line; + var output = lineData.Direction == TerminalLine.MessageDirection.ToPrinter; var outputLine = line; var lineWithoutChecksum = GCodeFile.GetLineWithoutChecksum(line); @@ -156,7 +154,7 @@ namespace MatterHackers.MatterControl if (UserSettings.Instance.Fields.GetBool(UserSettingsKey.TerminalShowInputOutputMarks, true)) { - if (lineData.output) + if (output) { outputLine = "->" + outputLine; } @@ -267,6 +265,7 @@ namespace MatterHackers.MatterControl if (!string.IsNullOrEmpty(saveParams.FileName)) { string filePathToSave = saveParams.FileName; + if (filePathToSave != null && filePathToSave != "") { try @@ -277,10 +276,10 @@ namespace MatterHackers.MatterControl { Debug.Print(ex.Message); - printer.Connection.TerminalLog.PrinterLines.Add(("", true)); - printer.Connection.TerminalLog.PrinterLines.Add((writeFailedWaring, true)); - printer.Connection.TerminalLog.PrinterLines.Add((cantAccessPath.FormatWith(filePathToSave), true)); - printer.Connection.TerminalLog.PrinterLines.Add(("", true)); + printer.Connection.TerminalLog.WriteLine(""); + printer.Connection.TerminalLog.WriteLine("WARNING: Write Failed!".Localize()); + printer.Connection.TerminalLog.WriteLine("Can't access".Localize() + " " + filePathToSave); + printer.Connection.TerminalLog.WriteLine(""); UiThread.RunOnIdle(() => { diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs b/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs index b035eed4a..7aa2554de 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs @@ -41,7 +41,7 @@ namespace MatterHackers.MatterControl { private object locker = new object(); - private List<(string line, bool output)> allSourceLines; + private List allSourceLines; private List visibleLines; private TypeFacePrinter typeFacePrinter = null; @@ -49,16 +49,16 @@ namespace MatterHackers.MatterControl private int forceStartLine = -1; - private Func<(string line, bool output), string> _lineFilterFunction; + private Func _lineFilterFunction; - public TextScrollWidget(PrinterConfig printer, List<(string line, bool output)> sourceLines) + public TextScrollWidget(PrinterConfig printer, List sourceLines) { this.printer = printer; printer.Connection.TerminalLog.LineAdded += TerminalLog_LineAdded; this.typeFacePrinter = new TypeFacePrinter("", new StyledTypeFace(ApplicationController.GetTypeFace(NamedTypeFace.Liberation_Mono), 12)); this.typeFacePrinter.DrawFromHintedCache = true; this.allSourceLines = sourceLines; - this.visibleLines = sourceLines.Select(ld => ld.line).ToList(); + this.visibleLines = sourceLines.Select(ld => ld.Line).ToList(); } public double Position0To1 @@ -96,7 +96,7 @@ namespace MatterHackers.MatterControl public Color TextColor { get; set; } = new Color(102, 102, 102); - public Func<(string line, bool output), string> LineFilterFunction + public Func LineFilterFunction { get => _lineFilterFunction; set @@ -106,23 +106,23 @@ namespace MatterHackers.MatterControl } } - private void ConditionalyAddToVisible((string line, bool output) lineData) + private void ConditionalyAddToVisible(TerminalLine lineData) { - var line = lineData.line; + var line = lineData.Line; if (LineFilterFunction != null) { line = LineFilterFunction(lineData); } - if(!string.IsNullOrEmpty(line)) + if (!string.IsNullOrEmpty(line)) { visibleLines.Add(line); } } - private void TerminalLog_LineAdded(object sender, (string line, bool output) lineData) + private void TerminalLog_LineAdded(object sender, TerminalLine lineData) { - if (lineData.line != null) + if (lineData.Line != null) { ConditionalyAddToVisible(lineData); } @@ -139,7 +139,7 @@ namespace MatterHackers.MatterControl lock (locker) { visibleLines = new List(); - (string, bool)[] allSourceLinesTemp = allSourceLines.ToArray(); + var allSourceLinesTemp = allSourceLines.ToArray(); foreach (var lineData in allSourceLinesTemp) { ConditionalyAddToVisible(lineData); @@ -150,7 +150,7 @@ namespace MatterHackers.MatterControl public void WriteToFile(string filePath) { // Make a copy so we don't have it change while writing. - string[] allSourceLinesTemp = allSourceLines.Select(ld => ld.line).ToArray(); + string[] allSourceLinesTemp = allSourceLines.Select(ld => ld.Line).ToArray(); System.IO.File.WriteAllLines(filePath, allSourceLinesTemp); } diff --git a/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs b/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs index d5e8afdb1..676ae4370 100644 --- a/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs +++ b/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs @@ -111,7 +111,7 @@ namespace MatterHackers.MatterControl.Tests.Automation Assert.AreEqual(printer.Connection.CommunicationState, PrinterCommunication.CommunicationStates.Connected); // Assert that two G28s were output to the terminal - int g28Count = printer.Connection.TerminalLog.PrinterLines.Where(lineData => lineData.line.Contains("G28")).Count(); + int g28Count = printer.Connection.TerminalLog.PrinterLines.Where(lineData => lineData.Line.Contains("G28")).Count(); Assert.AreEqual(2, g28Count, "The terminal log should contain one G28 from Start-GCode and one G28 from Cancel-GCode"); }