private string ApplyFormatting(string text, MessageFormatting formatting) { switch (formatting) { case MessageFormatting.Bold: return("**" + text + "**"); case MessageFormatting.BoldItalic: return("**_" + text + "_**"); case MessageFormatting.BoldItalicUnderlined: return("__**_" + text + "_**__"); case MessageFormatting.Italic: return("_" + text + "_"); case MessageFormatting.ItalicUnderlined: return("___" + text + "___"); case MessageFormatting.Underlined: return("__" + text + "__"); case MessageFormatting.Code: return("`" + text + "`"); case MessageFormatting.BlockCode: return("```" + text + "```"); default: return(text); } }
private string ApplyFormatting(string text, MessageFormatting formatting) { switch (formatting) { case MessageFormatting.BOLD: return("**" + text + "**"); case MessageFormatting.BOLD_ITALIC: return("**_" + text + "_**"); case MessageFormatting.BOLD_ITALIC_UNDERLINED: return("__**_" + text + "_**__"); case MessageFormatting.ITALIC: return("_" + text + "_"); case MessageFormatting.ITALIC_UNDERLINED: return("___" + text + "___"); case MessageFormatting.UNDERLINED: return("__" + text + "__"); case MessageFormatting.CODE: return("`" + text + "`"); case MessageFormatting.BLOCKCODE: return("```" + text + "```"); default: return(text); } }
private static double GetCompassChange(string scalarpart, string directionpart) { var eastwest = MessageFormatting.ParseOneLetterEnumByValue <EastWest>(directionpart); var scalar = double.Parse(scalarpart); return(EastWest.East == eastwest ? -scalar : scalar); }
public MessageBuilder AppendText(string text, MessageFormatting formatting = MessageFormatting.Plain, bool newLine = true, bool endWithSpace = false) { if (string.IsNullOrWhiteSpace(text)) { return(this); } text = ApplyFormatting(text, formatting); if (endWithSpace) { text += " "; } if (newLine) { _builder.AppendLine(text); } else { _builder.Append(text); } return(this); }
internal GGA(string talkerId, string[] parts) : base(talkerId) { try { Time = TimeSpan.ParseExact(parts[0], TIMESPAN_HHMMSSfff, DateTimeFormatInfo.InvariantInfo); } catch (FormatException) { Time = TimeSpan.ParseExact(parts[0], TIMESPAN_HHMMSS, DateTimeFormatInfo.InvariantInfo); } Position = new Position(parts[1], parts[2], parts[3], parts[4]); FixQuality = MessageFormatting.ParseOneLetterEnumByValue <FixQuality>(parts[5]); TrackedSatelliteCount = int.Parse(parts[6]); HorizontalDilution = float.Parse(parts[7]); Altitude = float.Parse(parts[8]); AltitudeUnit = MessageFormatting.ParseOneLetterEnumByValue <AltitudeUnit>(parts[9]); MeanSeaLevel = float.Parse(parts[10]); MeanSeaLevelUnit = MessageFormatting.ParseOneLetterEnumByValue <AltitudeUnit>(parts[11]); if (!string.IsNullOrWhiteSpace(parts[12])) { TimeSinceLastDGPSFix = TimeSpan.FromSeconds(int.Parse(parts[12])); } if (!string.IsNullOrWhiteSpace(parts[13])) { DGPSStationId = int.Parse(parts[13]); } }
/// <summary> /// Deserializes command /// </summary> /// <param name="talkedId"></param> /// <param name="parts"></param> internal RMC(string talkedId, string[] parts) : base(talkedId) { if (!string.IsNullOrWhiteSpace(parts[8])) { DateTime = System.DateTime.ParseExact(parts[8], DATETIME_DDMMYY, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); } if (!string.IsNullOrWhiteSpace(parts[8])) { DateTime time; /* * * if ( * !System.DateTime.TryParseExact(parts[0], HHMMSSfff, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out time)) * { * time = System.DateTime.ParseExact(parts[0], HHMMSS, DateTimeFormatInfo.InvariantInfo, * DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); * } */ try { time = System.DateTime.ParseExact(parts[0], DATETIME_HHMMSSfff, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); } catch (FormatException) { time = System.DateTime.ParseExact(parts[0], DATETIME_HHMMSS, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); } DateTime = DateTime?.Add(time.TimeOfDay) ?? time; } Status = MessageFormatting.ParseOneLetterEnumByValue <Flag>(parts[1]); Position = new Position(parts[2], parts[3], parts[4], parts[5]); SOG = float.Parse(parts[6]); TMG = !string.IsNullOrWhiteSpace(parts[7]) ? new TrueMessageCompassValue(double.Parse(parts[7])) : null; if (!string.IsNullOrWhiteSpace(parts[9])) { MagneticVariation = float.Parse(parts[9]); } if (!string.IsNullOrWhiteSpace(parts[10])) { MagneticVariationDirection = MessageFormatting.ParseOneLetterEnumByValue <EastWest>(parts[10]); } if (parts.Length > 11 && !string.IsNullOrWhiteSpace(parts[11])) { Mode = MessageFormatting.ParseOneLetterEnumByValue <FixMode>(parts[11]); } }
public void FormatWithTime() { //--Arrange MessageFormatting format = new MessageFormatting(); // Act string expectedString = $"[{DateTime.Now}]message #1 received"; //Assert; Assert.AreEqual(expectedString, MessageFormatting.FormatWithTime("message #1 received")); }
public void FormatWithLowerCase() { //--Arrange MessageFormatting format = new MessageFormatting(); // Act string expectedString = $"message #1 received"; //Assert; Assert.AreEqual(expectedString, MessageFormatting.FormatWithLowerCase("Message #1 received")); }
public void FormatWithUpperCase() { //--Arrange MessageFormatting format = new MessageFormatting(); // Act string expectedString = $"MESSAGE #1 RECEIVED"; //Assert; Assert.AreEqual(expectedString, MessageFormatting.FormatWithUpperCase("Message #1 received")); }
/// <summary> /// Deserializes command /// </summary> /// <param name="talkedId"></param> /// <param name="parts"></param> internal APB(string talkedId, string[] parts) : base(talkedId) { XTE = double.Parse(parts[2]); SteerTurn = MessageFormatting.ParseOneLetterEnumByValue <Turn>(parts[3]); XteUnits = MessageFormatting.ParseOneLetterEnumByValue <Units>(parts[4]); ArrivalCircular = MessageFormatting.ParseOneLetterEnumByValue <Flag>(parts[5]); ArrivalPerpendicular = MessageFormatting.ParseOneLetterEnumByValue <Flag>(parts[6]); BOD = MessageCompassValueFactory.FromMessageParts(parts[7], parts[8]); DestinationWayPointId = int.Parse(parts[9]); Bearing = MessageCompassValueFactory.FromMessageParts(parts[10], parts[11]); Heading = MessageCompassValueFactory.FromMessageParts(parts[12], parts[13]); }
private string ApplyFormatting(string text, MessageFormatting formatting) { return(formatting switch { MessageFormatting.Bold => ("**" + text + "**"), MessageFormatting.BoldItalic => ("**_" + text + "_**"), MessageFormatting.BoldItalicUnderlined => ("__**_" + text + "_**__"), MessageFormatting.Italic => ("_" + text + "_"), MessageFormatting.ItalicUnderlined => ("___" + text + "___"), MessageFormatting.Underlined => ("__" + text + "__"), MessageFormatting.Code => ("`" + text + "`"), MessageFormatting.BlockCode => ("```" + text + "```"), MessageFormatting.BoldUnderlined => ("**_" + text + "_**"), MessageFormatting.Plain => text, _ => text });
public MessageBuilder AppendText(string text, MessageFormatting formatting = MessageFormatting.PLAIN, bool newLine = true, bool endWithSpace = false) { if (string.IsNullOrWhiteSpace(text)) { return(this); } text = ApplyFormatting(text, formatting); if (endWithSpace) { text += " "; } message += text; if (newLine) { NewLine(); } return(this); }
internal GLL(string talkerId, string[] parts) : base(talkerId) { Position = new Position(parts[0], parts[1], parts[2], parts[3]); Time = TimeSpan.ParseExact(parts[4], TIMESPAN_HHMMSSfff, DateTimeFormatInfo.InvariantInfo); Status = MessageFormatting.ParseOneLetterEnumByValue <Flag>(parts[5]); }
/// <summary> /// Dopisz tekst (html i zwykły) do wiadomości. /// Domyślnie bez formatowania, kolor tła i tekstu czarny, czcionka MS Shell Dlg 2 o wielkości 9pt. /// </summary> /// <param name="text">Tekst.</param> /// <param name="formatting">Flagi formatowania.</param> /// <param name="redColor">Składowa czerwieni koloru tekstu.</param> /// <param name="greenColor">Składowa zieleni koloru tekstu.</param> /// <param name="blueColor">Składowa niebieska koloru tekstu.</param> /// <param name="fontFamily">Nazwa czcionki.</param> /// <param name="fontSize">Rozmiar czcionki, np. 9pt czyli 9 punktów.</param> public void AppendText(string text, MessageFormatting formatting = MessageFormatting.None, byte redColor = 0, byte greenColor = 0, byte blueColor = 0, string fontFamily = "MS Shell Dlg 2", string fontSize = "9pt") { //check color exists. if true then end <span> and set flag bool haveColor = false; if (redColor != 0 || greenColor != 0 || blueColor != 0) { haveColor = true; if (fontFamily != "MS Shell Dlg 2" || fontSize != "9pt") { if (!_htmlMessage.EndsWith("</span>")) { _htmlMessage += "</span>"; } } } #region Html //open html message if closed if (string.IsNullOrEmpty(_htmlMessage) || _htmlMessage.EndsWith("</span>")) { _htmlMessage += string.Format("<span style=\"color:#{0}{1}{2}; font-family:'{3}'; font-size:{4}; \">", redColor.ToString("X2"), greenColor.ToString("X2"), blueColor.ToString("X2"), fontFamily, fontSize); } //build tags StringBuilder builder = new StringBuilder(); if (formatting.HasFlag(MessageFormatting.Bold)) { builder.Append("<b>"); } if (formatting.HasFlag(MessageFormatting.Erasure)) { builder.Append("<s>"); } if (formatting.HasFlag(MessageFormatting.Italic)) { builder.Append("<i>"); } if (formatting.HasFlag(MessageFormatting.Subscript)) { builder.Append("<sub>"); } if (formatting.HasFlag(MessageFormatting.Superscript)) { builder.Append("<sup>"); } if (formatting.HasFlag(MessageFormatting.Underline)) { builder.Append("<u>"); } //add text builder.Append(text); //and close tags if (formatting.HasFlag(MessageFormatting.Underline)) { builder.Append("</u>"); } if (formatting.HasFlag(MessageFormatting.Superscript)) { builder.Append("</sup>"); } if (formatting.HasFlag(MessageFormatting.Subscript)) { builder.Append("</sub>"); } if (formatting.HasFlag(MessageFormatting.Italic)) { builder.Append("</i>"); } if (formatting.HasFlag(MessageFormatting.Erasure)) { builder.Append("</s>"); } if (formatting.HasFlag(MessageFormatting.Bold)) { builder.Append("</b>"); } if (formatting.HasFlag(MessageFormatting.NewLine)) { builder.Append("<br>"); } //add built message _htmlMessage += builder.ToString(); #endregion #region Plain //plain message for GG 7.x and oldest _plainMessage += text; if (formatting.HasFlag(MessageFormatting.NewLine)) { _plainMessage += Environment.NewLine; } if (formatting.HasFlag(MessageFormatting.Bold) || formatting.HasFlag(MessageFormatting.Italic) || formatting.HasFlag(MessageFormatting.Underline) || haveColor) { //attributes using (MemoryStream memStream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memStream)) { //position writer.Write((short)(_plainMessage.Length - text.Length)); //flags, bold etc. writer.Write((byte)((formatting.HasFlag(MessageFormatting.Bold) ? Container.GG_FONT_BOLD : 0) | (formatting.HasFlag(MessageFormatting.Italic) ? Container.GG_FONT_ITALIC : 0) | (formatting.HasFlag(MessageFormatting.Underline) ? Container.GG_FONT_UNDERLINE : 0) | (haveColor ? Container.GG_FONT_COLOR : 0))); //color if exists if (haveColor) { writer.Write(redColor); writer.Write(greenColor); writer.Write(blueColor); } } //go attributes to can :D byte[] newData = memStream.ToArray(); byte[] structHeader = new byte[3]; structHeader[0] = Container.GG_ATTRIBUTES_FLAG; byte[] structLength = BitConverter.GetBytes((short)newData.Length); Buffer.BlockCopy(structLength, 0, structHeader, 1, structLength.Length); if (_attributes == null) { _attributes = new byte[newData.Length + structHeader.Length]; } else { Array.Resize(ref _attributes, _attributes.Length + newData.Length + structHeader.Length); } //copy new attributes to global can ;P Buffer.BlockCopy(structHeader, 0, _attributes, _attributes.Length - newData.Length - structHeader.Length, structHeader.Length); Buffer.BlockCopy(newData, 0, _attributes, _attributes.Length - newData.Length, newData.Length); } } #endregion }